I have a class called receipt
and one of the attributes is an array item[] items;
and I have a method addItem (string name , int quantity , double price)
. My question is how can I add these parameters to the array items[]
? And how do I check if the quantity is more than 0?
Whats the code? Is my question clear?
Hers is my code:
public boolean addItem(String name, int quantity,double price){
if(quantity <= 0){
System.out.println("Item wasnt added");
return false;}
else{
item [nItem++];//here is the problem
}
}
You could use an item object as parameter rather than transmitting each individual properties.
If you use an array, you should have a variable which stores the index of the last element added in the array.
Item could have a constructor and required getters :
How to use :