Java arraylist invoice class need help
requirement:
Design, Implement and test the Invoice class
An Invoice class groups InvoiceItems into a single object. Each Invoice has a unique serial
number:the first Invoice has serial number 1, the second has serial number 2, etc. (Hint: implementing
this will require a static variable.) A newly created invoice will have no InvoiceItems. A public
method
public void add(InvoiceItem item) will add that item to the Invoice. Other public
method public double getTotal() will return the total of all the InvoiceItems.
Other methods to design and implement are:
• getNumItems(): returns the total number of items.
• InvoiceItem getItem(int i): return the i'th Invoice or throw an exception if i is illegal.
Code :
// flowing is my invioceitem class, works fine
public class InvoiceItem {
protected String partNUM;
protected String desc;
protected int quantity;
protected double price;
public InvoiceItem(String partNUM, String desc, int quantity, double price) {
this.partNUM = partNUM;
this.desc = desc;
this.quantity = quantity;
this.price = price;
if((quantity<0)||(price<0)){
throw new IllegalArgumentException("Wrong:quantity and price can't be negative");
}
}
public String getpartNUM(){
return partNUM;
}
public String getDesc(){
return desc;
}
public int getQuantity(){
return quantity;
}
public double getPrice(){
return price;
}
public void setQuantity(int quantity){
this.quantity = quantity;
if(quantity<0){
throw new IllegalArgumentException("Wrong: quantity can't be negative");
}
}
public double getTotal(){
return price * quantity;
}
public String toString(){
return partNUM+","+desc+","+quantity+","+price;
}
}
// this is my invioce class
import java.util.ArrayList;
public class Invoice {
private ArrayList<InvoiceItem> list = new ArrayList<InvoiceItem>();
static String item;
static int ID = 0;
int count=0;
public void InvoiceItem(String item, int ID){
this.item = item;
this.ID = ID;
}
public void add(InvoiceItem item){
list.add(item);
count++;
}
public int getNumItems(){
return count;
}
public double getTotal(){
return ;
}
public InvoiceItem getItem(int i){
return ;
}
i dont know how to do the getTotal and getItem method, can someone help me? thank you!
Re: Java arraylist invoice class need help
Hi wooof21, welcome to the forums! When you post code put [code] at the start and [/code] at the end: these tags will ensure that the forum software respects the formatting.
I'm not sure why you have a string item in the Invoice class. According to the instructions the Invoice class has an id and that's it. Of course to "combine" the items in an invoice there will be an List<InvoiceItem> as well, as you have. So I think you should remove item and change the constructor accordingly.
As far as getNumItems() and getItem(i) are concerned, these getter methods should simply look at the list and call a List method to get the information to return. That is, List (including ArrayList) has methods to say how many elements there are and to return an element at a specific index. Look them up in the API docs.
These two are the most straight forward, so I would start there. Implement these two and write some "driver" to assure yourself that they're doing what you intend. Post if you get stuck. (or if this doesn't make sense)