1

am doing bookstore system. the user will have to chose whether to add, delete and so on. if he chose to add then he will write all the book attributes which later will be saved in the items array "items array is an array object" this is my code

public class userChoices {

    Items[] items = new Items[200];
    AddItem add = new AddItem();
    Scanner s = new Scanner(System.in);

    public void add(){
        boolean invalidInput;
        int q = -1;
        do {        
            try {        
                invalidInput = false;
        System.out.println("How many book(s) you want to add?");
            q = s.nextInt();
            for(int i = 0; i < q; i++){
                add.getCode();
                add.getQuantity();
                add.getCostPrice();
                add.getDescription();
                add.getDiscount();
                add.getSellingPrice();
                add.getStatus();
                for(int r = 0; r < items.length; r++){
                    if(items[r] != null){
                        items[r] = new Items(add.getCode(), add.getDescription(), add.getQuantity(),
                                add.getCostPrice(), add.getSellingPrice(), add.getStatus(), add.getDiscount());
                        continue;
                    }
                }
            }

            } catch (InputMismatchException e) {
        System.out.println("Please enter a valid integer.");
                s.next();
        invalidInput = true;  // This is what will get the program to loop back
            }
    } while (invalidInput);
    }

this is my Items class
    public class Items {
    private int code, quantity;
    private String description;
    private double costPrice, sellingPrice;
    String status, discount;

    public Items(){
        this.code = 1111;
        this.quantity = 1;
        this.description = "Action";
        this.costPrice = 12.00;
        this.sellingPrice = 16.00;
        this.discount = "5%";
        this.status = "Unvailable";

    }

    public Items(int code, String description, int quantity,
            double costPrice, double sellingPrice, String status, String discount){
        this.code = code;
        this.description = description;
        this.quantity = quantity;
        this.costPrice = costPrice;
        this.sellingPrice = sellingPrice;
        this.status = status;
        this.discount = discount;
    }

    public void setCode(int code){
        this.code = code;
    }

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

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

    public void setcostPrice(double costPrice){
        this.costPrice = costPrice;
    }

    public void setsellingPrice(double sellingPrice){
        this.sellingPrice = sellingPrice;
    }

    public void setStatus(String status){
        this.status = status;
    }

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

    public int getCode(int code){
        this.code = code;
        return this.code;
    }

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

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

    public double getcostPrice(double costPrice){
        this.costPrice = costPrice;
        return this.costPrice;
    }

    public double getsellingPrice(double sellingPrice){
        this.sellingPrice = sellingPrice;
        return this.sellingPrice;
    }

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

    public String getDiscount(String discount){
        this.discount = discount;
        return this.discount;
    }

    public String toString(){
        return ("code : " + this.code + "\nQuantity : " + this.quantity +
                "\nDescription : " + this.description + "\nCost price : " + this.costPrice
                + "\nSelling price : " + this.sellingPrice + "\nstatus : " + this.status
                + "\ndiscount : " + this.discount);
    }


}

when i print items[0] after i add there, it shows me "null"

2
  • Please post only the relevant code Commented Oct 12, 2014 at 11:16
  • 1
    if(items[r] != null) you're overwriting exsiting entries and no book will be added, if the array is empty. It should be if(items[r] == null) Commented Oct 12, 2014 at 11:18

2 Answers 2

2

In addition to the if(items[r] != null){ pointed out by others, I believe the continue statement in this code is wrong:

for(int r = 0; r < items.length; r++){
    if(items[r] != null){
        items[r] = new Items(add.getCode(), add.getDescription(), add.getQuantity(),
                add.getCostPrice(), add.getSellingPrice(), add.getStatus(), add.getDiscount());
        continue;
    }
}

This statement will continue on the next iteration of this:

for(int r = 0; r < items.length; r++){

while it seems to me you want it to continue on the next iteration of

for(int i = 0; i < q; i++){

So you should use break; instead of continue;


Getters

public double getcostPrice(double costPrice){
    this.costPrice = costPrice;
    return this.costPrice;
}

This "getter" is essentially a setter that returns the value you just set, that is not what a getter should do.

public double getcostPrice(){
    return this.costPrice;
}

That is how a getter should work.

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

Comments

1

Your mistake is in this line:

                    if(items[r] != null){

When you create the array, since it is an array of object references, all its elements are null. So this condition causes it to never insert an item into the array.

You shouldn't have a loop there anyway. You are supposed to keep the index of the next empty place in the array and use it to put the item in the array, and then increment (add one to) it.

5 Comments

Why should he track of the last inserted index? if(items[r] == null) works fine.
@Tom, the loop there is superfluous. There is no nead to run, for every item, from the beginning of the array until you find a null. You already know where the next null is - the array element after the one you have used in the previous round.
There is no need to use an array in the first place :D. A Collection would be much better. You could recommend this in your answer. (The problem with tracking the next index might be, that this index is already filled by another method)
@Tom I don't think he got as far as collection in his studies.
Well, this is a good point to introduce them :D. But nevermind, his question is answered.

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.