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
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.

// 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;
    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;
    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){
    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!

5 Years
Discussion Span
Last Post by stultuske

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.