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 11 of 11

Thread: Java abstract/implements

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java abstract/implements

    takeInventory suppose to keep count of the items that passes through and print their quantity. The result I'm getting is
    "guar: Quantity = 2, Total cost = 1110000.0
    Neon: Quantity = 3, Total cost = 34875.32
    Jigsaw: Quantity = 1, Total cost = 298.36
    Jaguar: Quantity = 2, Total cost = 220000.0
    Neon: Quantity = 3, Total cost = 35375.32
    Neon: Quantity = 3, Total cost = 35750.64
    RAM: Quantity = 1, Total cost = 71400.0
    CircularSaw: Quantity = 2, Total cost = 350.0
    CircularSaw: Quantity = 2, Total cost = 300.0
    "
    How can I prevent it from printing the same thing over and over? Thanks
    public class InventoryDemo
    {
        public static void takeInventory(List<Product> item)
        {
     
            double cost = 0 ;
            for(int x = 0 ; x < item.size();x++)
            {
                int quant = 0;
                for(int i = 0 ; i<item.size();i++)
                {
                    if(item.get(x).getName().equalsIgnoreCase(item.get(i).getName()))
                    {    
                        quant ++;
                        cost = item.get(x).getCost() + item.get(i).getCost();
                    }
                }
                System.out.println(item.get(x).getName()+ ": Quantity = " + quant + ", Total cost = "+ cost );
            }
        }
        public static void main(String[]args)
        {
            List<Product> product = new ArrayList<Product>();
     
            product.add(new Vehicle("Jaguar",1000000.00));
            product.add(new Vehicle("Neon",17000.00));
            product.add(new Tool("Jigsaw",149.18));
            product.add(new Vehicle("Jaguar",110000.00));
            product.add(new Vehicle("Neon",17500.00));
            product.add(new Vehicle("Neon",17875.32));
            product.add(new Vehicle("RAM",35700.00));
            product.add(new Tool("CircularSaw",200.00));
            product.add(new Tool("CircularSaw",150.00));
     
            takeInventory(product);
        }
    }


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Java abstract/implements

    This looks like the perfect opportunity for a Map. Maps relate one object to another. For instance you could use a HashMap<String,Product> to relate the name of the car to the car object. Read up on Maps and see if you think it would fit. With a map you can just call map.get("Jaguar") and it will give you your Jaguar vehicle object. Then yes you would want to have an inventory value in each of your products to keep track of how many you have.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java abstract/implements

    Chris, thanks for the reply! Can you give me help on my updated code above? Thanks

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Java abstract/implements

    Well i'm not going to code it for you but i'll help you through it.

    Start by adding the inventory amount as an attribute in your Product class and give it some getters and setters so you can update the value.

    After that, determine you want to find the item. If you want to hunt it down with loops that's fine or you can look into something more sophisticated like a map. Although i wouldnt recommend using a map if you are doing this for a class and the class hasnt gone over them. You have to gauge if your teacher will applaud the advanced tactics or get upset that you are going beyond their teaching.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java abstract/implements

    Oh, I don't mean to code it for me. I updated the code with the loop going through and keeping the quantity above, but it printing all the items. IE if there 2 item A, it would print item A two time , instead of item A quantity 2 .

  6. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Java abstract/implements

    Your System.out.println is in the loop. Is this intended?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  7. #7
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java abstract/implements

    Is there another way to print out the results? That the only wait I could of think, as it complete the inner loop, it print.

  8. #8
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Java abstract/implements

    Aha, i see. It is in the list twice. I guess the problem here is that you have two different Jaguar objects which makes sense. What would really help me out would be to show me what you would expect to have to output given the input you have provided.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  9. #9
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java abstract/implements

    Jaguar: Quantity = 2, Total cost = 2100000.0
    Neon: Quantity = 3, Total cost = 52375.32
    Jigsaw: Quantity = 1, Total cost = 149.18
    RAM: Quantity = 1, Total cost = 35700.0
    CircularSaw: Quantity = 2, Total cost = 350.0
    ** Don't worry about the cost, I'm just trying to get the quantity to work right now.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,040
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Java abstract/implements

    Another way to use a Map would be with the product name as the key and an object that contains the count and the cost as the value.
    Last edited by Norm; March 28th, 2013 at 11:08 AM. Reason: Changed value to be an object
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Java abstract/implements

    I agree norm, a map would help but you would need two of them. One for quantity and one for total cost. And again i warn of using things your teacher doesnt allow. Many assignments are done to teach you a certain thing about programming that using an advanced tactic could render useless. Some teachers mark off for that. Just use caution.

    I would recommend, for a more basic solution, sort your list so the items are grouped together and you can just go through the list and total things up. Then when you see the item name change you can output the values and go onto the next item.

    --- Update ---

    Another way to do this would be to keep a list of "processed items". Then you could check, is my item in the processed list and if it is you can skip it. Some of these options are pretty obnoxious, you'll have to decide what works with where you are at in your progression of learning java.

    Something else i thought about. Are you supposed to be checking for a possibility that you have a vehicle and a tool that share the same name? A big part of my job is knowing how to break code and this is the first thing i would try.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. implementing implements & extends
    By nischalinn in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2013, 09:00 PM
  2. GUI error: is not abstract and does not override abstract method
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2011, 01:26 PM
  3. subclass that implements
    By speedycerv in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2011, 07:35 AM
  4. Implements
    By p_dibb in forum Java Theory & Questions
    Replies: 2
    Last Post: March 16th, 2011, 08:04 AM
  5. [SOLVED] implements MouseListener doesn't work why
    By voltaire in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2010, 04:30 PM