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!

for getItem, get the item at the index of i, so, the index in list.
getTotal should be returntype int, and return the value of count.
but I think you're mistaken with what is and should be static. count should be static, item and ID shouldn't be.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.