Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java arraylist invoice class need help

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    // 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!
    Last edited by pbrockway2; January 21st, 2013 at 03:32 PM. Reason: code tags added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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)

Similar Threads

  1. Making a invoice template in java
    By _lithium_ in forum Java Theory & Questions
    Replies: 14
    Last Post: July 30th, 2018, 02:46 PM
  2. ArrayList<Class> is returning null
    By havinFun in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 14th, 2012, 04:59 PM
  3. Using the ArrayList Class
    By m2msucks in forum Collections and Generics
    Replies: 9
    Last Post: November 21st, 2011, 02:22 PM
  4. How to use this 2d ArrayList class?
    By J05HYYY in forum Object Oriented Programming
    Replies: 14
    Last Post: January 19th, 2011, 01:48 PM
  5. [SOLVED] Passing arrayList inside class
    By KrisTheSavage in forum Collections and Generics
    Replies: 1
    Last Post: March 27th, 2010, 12:45 PM