0

I was create a book inventory program. I has two classes one is the main class, and the other one is the constructor class name Item. On the main class, i has create a array (Item[] book = new Item[100]) to store my input. And in my Item class, i want to create a function below

public boolean addItem(Item[] iArray, String itemCode){

    boolean c = false;


    for(int i=0; i<iArray.length; i++){
        if(iArray[i].getItemCode().equals(itemCode)){
            c = true;
        }
        else{
            c = false;

        }
    }

    return c;
}

how to i make that Item[] iArray sync with the book array in main class?


public class Item {


private String itemCode;
private String description;
private int quantity;
private double costprice;
private double sellprice;
private String status = "Available";
private boolean check;
private double  discount;


public Item(){
    this("A000","default",0,0.00,0.00,0.25,"Available");
}

//construtor with parameter
public Item(String itemCode, String description, int quantity, double costprice, double sellprice, double discount, String status){
    this.setItemCode(itemCode);
    this.setDescription(description);
    this.setQuantity(quantity);
    this.setCostprice(costprice);
    this.setSellprice(sellprice);
    this.setStatus(status);
    this.setDiscount(discount);
}


//setter and getter methods
public void setItemCode(String itemCode){
    this.itemCode = itemCode;
}

public String getItemCode(){
    return this.itemCode;
}

public void setDescription(String description){
    this.description = description;
}

public String getDescription(){
    return this.description;
}

public void setQuantity(int quantity){
    this.quantity = quantity;
}

public int getQuantity(){
    return this.quantity;
}

public void setCostprice(double costprice){
    this.costprice = costprice;
}

public double getCostprice(){
    return this.costprice;
}

public void setSellprice(double sellprice){
    this.sellprice = sellprice;
}

public double getSellprice(){
    return this.sellprice;
}

public void setStatus(String status){

    this.status = status;
}

public String getStatus(){
    return this.status;
}

public void setDiscount(double discount){
    this.discount = discount;
}

public double getDiscount(){
    return this.discount;
}

public void setCheck(boolean check){
    this.check = check;
}

public boolean getCheck(){
    return this.check;
}

public boolean addItem(Item[] iArray, String itemCode){

    boolean c = false;


    for(int i=0; i<iArray.length; i++){
        if(iArray[i].getItemCode().equals(itemCode)){
            c = true;
        }
        else{
            c = false;
            numberofobject++;
        }
    }

    return c;
}


    public void displaymenu(){
    System.out.println("Menu");
    System.out.println("1. Add New Item");
    System.out.println("2. Search");
    System.out.println("3. Edit Details");
    System.out.println("4. Edit Quantity");
    System.out.println("5. Stop Sell");
    System.out.println("6. List");
    System.out.println("7. Exit");
}*/



public String toString(){
    String msg = "";
    msg = this.getItemCode()+"\t\t\t\t"+this.getDescription()+"\t\t\t\t"+this.getQuantity()+"\t\t\t\t"+this.getCostprice()+"\t\t\t\t"+this.getSellprice()+"\t\t\t\t"+this.getDiscount()+"\t\t\t\t"+this.getStatus();
    return msg;
}

this is my Item class.


import java.util.*;
public class Driver {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int choice,quantity,NOI=0;
    double cprice,sprice,discount;
    String itc,name,status = "Available";
    boolean check = true;

    Item[] book = new Item[100];



    Scanner s1 = new Scanner(System.in);
    do{
        Item display = new Item();


        display.displaymenu();
        System.out.print("Please Select Menu: ");
        choice = s1.nextInt();

        if(choice==1){

            do{


            System.out.print("Please Enter the Item Code: ");
            itc =  s1.next();

                //for(int i=0; i<book.length; i++){
                //book[i].addItem(book, itc);

                if(display.addItem(book, itc)==true){
                    System.out.println("the book item code already exist."+NOI);
                    check = false;
                }
                else
                {
                    check = true;
                } //This is the question where i faced. 
                //}

            }while(check==false);

                   System.out.print("Please Enter the Description: ");
            name = s1.next();
            System.out.print("Please Enter the Quantity: ");
            quantity = s1.nextInt();
            System.out.print("Please Enter the Cost Price: ");
            cprice = s1.nextDouble();
            System.out.print("Please Enter the Sell Price: ");
            sprice = s1.nextDouble();
            System.out.print("Please Enter the Discount: ");
            discount = s1.nextDouble();*/

            book[NOI] =  new Item(itc,name,quantity,cprice,sprice,discount,status);
            NOI++;

        }

when i add the second item, there was a error (Exception in thread "main" java.lang.NullPointerException),

how to solve it?

5
  • 3
    The question is not pretty clear, try improving it. Commented Jun 9, 2013 at 15:40
  • The method is the same as the following one line of code: return iArray[iArray.length - 1].getItemCode().equals(itemCode); You are only returning if the last itemcode equals the given itemcode. Commented Jun 9, 2013 at 15:43
  • your function returns only the last test case Commented Jun 9, 2013 at 15:46
  • for(int y = 0; y < 100; ++y) { iArray[y]= new Item(); } Is it being created? Commented Jun 9, 2013 at 15:47
  • Why is your method called addItem when it doesn't add an item...? Commented Jun 9, 2013 at 15:50

2 Answers 2

1

Your method does not do what you want, because even if you find the item code, the loop continues. You probably want something like this instead:

public boolean addItem(Item[] iArray, String itemCode){
    for (Item item : iArray) {
        if (item.getItemCode().equals(itemCode)) {
            return true;
        }
    }
    return false;
}

Note that the method you posted seems oddly named, because it does not add anything anywhere.

You might also consider using a List<Item> (ArrayList, etc.) instead of an Item[].

Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure I understand what you are looking for so if my answer is irrelevant just comment it and I will delete.

I assume you are trying to store information : add new item with its code to an array. But I'm not sure if you're:

  • trying to insure the uniqueness of your item in the array before inserting it:

    maybe you can use a set of codes, it will simplify your problem, just check with .contains() and then add it or not

  • trying to add it to the list and if it already exist perform something (incrementation of the number of book for the code?)

    maybe you can use a HashMap with code as key and book as item.

In your current state, your method addItem does not add anything, just return if your last book in the array matches your code...

2 Comments

yeah, actually this addItem is not for adding Item. it was looking up if existing item code match my new input item code, it will return false, so it will loop back again want u to input the new item code. but the problem is it will not loop back and has the error i show just now. i dont know what is the problem for it, but it can compile can run. the error come out when i input the second item code.
ok, then you should post your stack trace and the line where the NPE appears

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.