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"
if(items[r] != null)you're overwriting exsiting entries and no book will be added, if the array is empty. It should beif(items[r] == null)