Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default Inventory Part 4 assistance

    OK, I am now coding part 4 of my inventory program.

    This is what I have so far:

    Java Code:
    package inventoryp4;
    
    import java.util.*;
    import java.awt.GridLayout;
    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JList;
    import javax.swing.JLabel;
    
    
    public class Inventory
    {
    
        public static void main(String args [])
        {
        	BlurayDVD[] blu = new BlurayDVD[6];
    
        	blu[0] = new BlurayDVD("Independence Day", 6, 5.23, 1, "Standard DVD");
            System.out.println(blu);
    
            blu[1] = new BlurayDVD("X-Men", 8, 4.73, 2, "Standard DVD");
            System.out.println(blu);
    
            blu[2] = new BlurayDVD("Jurassic Park", 3, 6.01, 3, "Standard DVD");
            System.out.println(blu);
    
            blu[3] = new BlurayDVD ("My Cousin Vinny", 4, 3.15, 4, "Standard DVD");
            System.out.println(blu);
    
            blu[4] = new BlurayDVD ("The Mist", 3, 6.58, 5, "BluRay DVD");
            System.out.println(blu);
    
            blu[5] = new BlurayDVD ("Independence DaySE", 4, 7.81, 6, "BluRay DVD");
            System.out.println(blu);
    
    	double total = 0;
    
    	ArrayList<BlurayDVD> bluList = new ArrayList<BlurayDVD>();
            
             BlurayDVD b0 = new BlurayDVD ("The Mist", 3, 6.58, 5, "Standard DVD");
             BlurayDVD b1 = new BlurayDVD ("Independence Day", 4, 7.81, 6, "BluRay DVD");
             BlurayDVD b2 = new BlurayDVD ("X-Men", 8, 4.73, 2,"BluRay DVD");
             BlurayDVD b3 = new BlurayDVD ("Jurassic Park", 3, 6.01, 3,"BluRay DVD");
             BlurayDVD b4 = new BlurayDVD ("My Cousin Vinny", 4, 3.15, 4, "Standard DVD");
             BlurayDVD b5 = new BlurayDVD("Independence DaySE", 4, 7.81, 6, "BluRay DVD");
    
              bluList.add(b0);
              bluList.add(b1);
              bluList.add(b2);
              bluList.add(b3);
              bluList.add(b4);
              bluList.add(b5);
    
                  Collections.sort(bluList, new Comparator()
                  [COLOR="Red"][B][SIZE="5"]{[/SIZE][/B][/COLOR]
    
                        public int compare( BlurayDVD o1, BlurayDVD o2)
                        {
                            BlurayDVD p1 = (BlurayDVD) o1;
                            BlurayDVD p2 = (BlurayDVD) o2;
                            return p1.getDvdTitle().compareToIgnoreCase(p2.getDvdTitle());
                        }
    
                });
    
                for (int i = 0; i < blu.length; i++)
                {
                    System.out.printf(" %d\t%18s\t%d\t $%.2f\t$%.2f\t$%.2f\t%s", blu[i].dvdItem, blu[i].getDvdTitle(),  blu[i].getDvdStock(), blu[i].getDvdPrice(),  blu[i].value(),  blu[i].getRestockFee(), blu[i].getCategory());
                    System.out.println(); // prints a blank line
                }
    
            System.out.println(); // prints a blank line
            System.out.printf("The total value of the entire inventory is $%.2f", total);
            }
    } // end class Inventory
    
    class DVD
    {
        String dvdTitle;
        int dvdStock;
        double dvdPrice;
        int dvdItem;
        double total;
        double titleValue;
        double totalValue;
        String dvdCategory;
    
    
        public DVD(String title, int stock, double price, int item, String category)
        {
    
            dvdTitle = title;
            dvdStock = stock;
            dvdPrice = price;
            dvdItem  = item;
            dvdCategory = category;
        } //end five-argument constructor
    
    	// set DVD title
        public void setDvdTitle(String title)
        {
            dvdTitle = title;
        } //end method  setDvdTitle
    
        //return DVD title
        public String getDvdTitle()
        {
            return dvdTitle;
        } //end method getDvdTitle
    
        //set number if DVDs in stock
        public void setDvdStock(int stock)
        {
            dvdStock = stock;
        } //end method setDvdStock
    
        //retrieve the number of DVD's in stock
        public int getDvdStock()
        {
            return dvdStock;
        } //end method getDvdStock
    
        // set the price of the title
        public void setDvdPrice(double price)
        {
            dvdPrice = price;
        } //end method setDvdPrice
    
        //retrieve the price of this title
        public double getDvdPrice()
        {
            return dvdPrice;
        } //end method getDvdPrice
    
        // set the DVD item number
        public void setDvdItem(int item)
        {
            dvdItem = item;
        } //end method setDvdItem
    
        //retrieve the DVD item number
        public int getDvdItem()
        {
            return dvdItem;
        } //end  method getDvdItem
    
        //	calculate inventory value for each title and cumulatively calculate the total value of the entire inventory
        public double value()
        {
            titleValue = dvdPrice * dvdStock;
            totalValue = titleValue + totalValue;
            return titleValue;
    
        } //end method value
    
       public double getTotal(DVD[] dvd)
        {
             for (int i = 0; i < dvd.length; i++)
    	{
    		totalValue += dvd[i].value();
    	}
    	return totalValue;
        } // end method totalValue
    
    
    } //end class DVD
    
    // subclass BluRayDVD
    class BlurayDVD extends DVD
    {
    	// adds one field
    	double restockFee;
    
    	// constructor for BlurayDVD
    	public BlurayDVD(String title, int stock, double price, int item, String category)
    	{
    		// add these from superclass DVD
    		super(title, stock, price, item, category);
    	}
    
    	// method to set the category
    	public void setCategory(String category)
    	{
    		dvdCategory = category;
    	} // end method setCategory
    
    	// method to retrieve the category
    	public String getCategory()
    	{
    		return dvdCategory;
    	} // end method getCategory
    
        //	calculate inventory value for each title and cumulatively calculate the total value of the entire inventory
        @Override
        public double value()
        {
        	double initialTitleValue;
    
            initialTitleValue = (dvdPrice * dvdStock); // initial value
            restockFee = (initialTitleValue * .05); // calculate restocking fee
            titleValue = initialTitleValue + restockFee; // add restock fee to the initial value
            totalValue = titleValue + totalValue; // add to the cumulative value
            return titleValue;
        } // end method value
    
        // retrieve the restocking fee
        public double getRestockFee()
        {
        	return restockFee;
        } // end method getRestockFee
    
    } // end subclass BlurayDVD
    I am getting one error. It says:

    <anonymous inventoryp4.Inventory$1> is not abstract and does not override abstract method compare(java.lang.Object,java.lang.Object) in java.util.Comparator
    The error line shows up at the curly bracket after the Comparator method.
    Last edited by ladykrimson; 11-03-2010 at 09:49 PM.

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Try changing the parameter types to both be type Object. Then do a check inside the method to ensure that both parameters are an instanceof your BluerayDVD class and if so do your implementation of the compare, otherwise return the super call.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The problem is that your inner class that implements Comparator implements a non-generic Comparator, and it is expecting the class to hold a method of signature:

    Java Code:
    public int compare(Object o1, Object o2) {
    And one solution as has been suggested above, is to change your compare method's signature to reflect this. I think that perhaps a better solution is to use a generic Comparator:
    Java Code:
    new Comparator<BlueraDVD> () {
       public int compare( BlurayDVD o1, BlurayDVD o2) {
                //......
    as this will allow you to use the compare method that you currently have and will do your type checking at compile time -- a good thing.
    Last edited by Fubarable; 11-03-2010 at 10:35 PM.

  4. #4
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Thanks so much. That worked beautifully.

    My teacher told me that there must be a method in the DVD class that totals the value of the entire inventory, but I am uncertain how to call that method.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    there must be a method in the DVD class that totals the value of the entire inventory, but I am uncertain how to call that method

    There is a DVD method value() that returns the value for a given DVD. If you want the value for the entire inventory that would done in the Inventory class. Possibly in the main() method.

    At the moment you have a for loop which goes through all the dvds in the inventory and prints stuff about them. You could use this loop to accumulate (add up) the values of each of the dvds. (Then print the total value of the inventory after that for loop has finished which you are actually doing. The problem is that don't add the individual values to total inside the loop.)

  6. #6
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    There is a DVD method value() that returns the value for a given DVD. If you want the value for the entire inventory that would done in the Inventory class. Possibly in the main() method.

    At the moment you have a for loop which goes through all the dvds in the inventory and prints stuff about them. You could use this loop to accumulate (add up) the values of each of the dvds. (Then print the total value of the inventory after that for loop has finished which you are actually doing. The problem is that don't add the individual values to total inside the loop.)
    I would love to do that, but the teacher has informed me that it must be a separate method.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    If I were you I would check and confirm that the DVD class would have a method returning the value of the entire inventory. Consider an actual (physical) dvd: peer at it and try to figure out the value of the entire collection of which it is part. The simple fact is that the entire inventory value is not a property of an individual dvd.

    You could have such a method within the inventory class. Basically it would be a static method that is passed the blu array. It would have its own for loop similar to the one you already have in the main() method. Within this loop it would add up the separate dvd values and return the total which would then be assigned to total within the main() method.

    That such a method would be static is simply because Inventory is written that way rather than being a "proper" class - ie having a constructor and where the dvd array is a private instance variable. Since the method outlined above is static you could actually place it within the DVD class! That would be possible but it wouldn't be sane for the reasons given in the first paragraph of this post.

  8. #8
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    What you are saying makes sense, but here was the instruction:

    "create a separate function, in your DVD class, that accepts your DVD array of objects as a parameter, and then iterate through it in order to calculate the total inventory values."

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Ok, then that's what you've got to do!

    Did the middle paragraph of my last post make sense? If so make such a method inside your DVD class and see what happens when you call it to set the value of total.

  10. #10
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Ok, then that's what you've got to do!

    Did the middle paragraph of my last post make sense? If so make such a method inside your DVD class and see what happens when you call it to set the value of total.
    So I should make the method, getTotal, a static method within the DVD class?

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    So I should make the method, getTotal, a static method within the DVD class?

    I hve been talking about it as static because nothing in the method seems to relate to the particular DVD instance. Try it and see.

    You could make it an ordinary method. Try that as well. How would you call the method? and would you always be able to call it that way?

  12. #12
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    OK, this is the code I have:

    Java Code:
    public class Inventory
    {
    
        public static void main(String args [])
        {
        	BlurayDVD[] blu = new BlurayDVD[6];
    
        	blu[0] = new BlurayDVD("Independence Day", 6, 5.23, 1, "Standard DVD");
            System.out.println(blu);
    
            blu[1] = new BlurayDVD("X-Men", 8, 4.73, 2, "Standard DVD");
            System.out.println(blu);
    
            blu[2] = new BlurayDVD("Jurassic Park", 3, 6.01, 3, "Standard DVD");
            System.out.println(blu);
    
            blu[3] = new BlurayDVD ("My Cousin Vinny", 4, 3.15, 4, "Standard DVD");
            System.out.println(blu);
    
            blu[4] = new BlurayDVD ("The Mist", 3, 6.58, 5, "BluRay DVD");
            System.out.println(blu);
    
            blu[5] = new BlurayDVD ("Independence DaySE", 4, 7.81, 6, "BluRay DVD");
            System.out.println(blu);
    
            double total = 0;
    
    	ArrayList<BlurayDVD> bluList = new ArrayList<BlurayDVD>();
            
             BlurayDVD b0 = new BlurayDVD ("The Mist", 3, 6.58, 5, "Standard DVD");
             BlurayDVD b1 = new BlurayDVD ("Independence Day", 4, 7.81, 6, "BluRay DVD");
             BlurayDVD b2 = new BlurayDVD ("X-Men", 8, 4.73, 2,"BluRay DVD");
             BlurayDVD b3 = new BlurayDVD ("Jurassic Park", 3, 6.01, 3,"BluRay DVD");
             BlurayDVD b4 = new BlurayDVD ("My Cousin Vinny", 4, 3.15, 4, "Standard DVD");
             BlurayDVD b5 = new BlurayDVD("Independence DaySE", 4, 7.81, 6, "BluRay DVD");
    
              bluList.add(b0);
              bluList.add(b1);
              bluList.add(b2);
              bluList.add(b3);
              bluList.add(b4);
              bluList.add(b5);
    
                  Collections.sort(bluList, new Comparator<BlurayDVD>()
                  {
                        public int compare(BlurayDVD o1, BlurayDVD o2)
                        {
                            BlurayDVD p1 = (BlurayDVD) o1;
                            BlurayDVD p2 = (BlurayDVD) o2;
                            return p1.getDvdTitle().compareToIgnoreCase(p2.getDvdTitle());
                        }
    
                });
                
                for (int i = 0; i < blu.length; i++)
                {
                    System.out.printf(" %d\t%18s\t%d\t $%.2f\t$%.2f\t$%.2f\t%s", blu[i].dvdItem, blu[i].getDvdTitle(),  blu[i].getDvdStock(), blu[i].getDvdPrice(),  blu[i].value(),  blu[i].getRestockFee(), blu[i].getCategory());
                    System.out.println(); // prints a blank line
    
                }
    
            System.out.println(); // prints a blank line
            System.out.printf("The total value of the entire inventory is $%.2f", total);
            System.out.println(); // prints a blank line
            }
    } // end class Inventory
    
    class DVD
    {
        String dvdTitle;
        int dvdStock;
        double dvdPrice;
        int dvdItem;
        double titleValue;
        double totalValue;
        String dvdCategory;
    
    
        public DVD(String title, int stock, double price, int item, String category)
        {
    
            dvdTitle = title;
            dvdStock = stock;
            dvdPrice = price;
            dvdItem  = item;
            dvdCategory = category;
        } //end five-argument constructor
    
    	// set DVD title
        public void setDvdTitle(String title)
        {
            dvdTitle = title;
        } //end method  setDvdTitle
    
        //return DVD title
        public String getDvdTitle()
        {
            return dvdTitle;
        } //end method getDvdTitle
    
        //set number if DVDs in stock
        public void setDvdStock(int stock)
        {
            dvdStock = stock;
        } //end method setDvdStock
    
        //retrieve the number of DVD's in stock
        public int getDvdStock()
        {
            return dvdStock;
        } //end method getDvdStock
    
        // set the price of the title
        public void setDvdPrice(double price)
        {
            dvdPrice = price;
        } //end method setDvdPrice
    
        //retrieve the price of this title
        public double getDvdPrice()
        {
            return dvdPrice;
        } //end method getDvdPrice
    
        // set the DVD item number
        public void setDvdItem(int item)
        {
            dvdItem = item;
        } //end method setDvdItem
    
        //retrieve the DVD item number
        public int getDvdItem()
        {
            return dvdItem;
        } //end  method getDvdItem
    
        //	calculate inventory value for each title and cumulatively calculate the total value of the entire inventory
        public double value()
        {
            titleValue = dvdPrice * dvdStock;
            totalValue = titleValue + totalValue;
            return titleValue;
    
        } //end method value
    
       public  double getTotal(BlurayDVD[] blu)
        {
             for (int i = 0; i < blu.length; i++)
    	{
    		totalValue += blu[i].value();
    	}
    	return totalValue;
        } // end method getTotal
    
    
    } //end class DVD
    
    // subclass BluRayDVD
    class BlurayDVD extends DVD
    {
    	// adds one field
    	double restockFee;
    
    	// constructor for BlurayDVD
    	public BlurayDVD(String title, int stock, double price, int item, String category)
    	{
    		// add these from superclass DVD
    		super(title, stock, price, item, category);
    	}
    
    	// method to set the category
    	public void setCategory(String category)
    	{
    		dvdCategory = category;
    	} // end method setCategory
    
    	// method to retrieve the category
    	public String getCategory()
    	{
    		return dvdCategory;
    	} // end method getCategory
    
        //	calculate inventory value for each title and cumulatively calculate the total value of the entire inventory
        @Override
        public double value()
        {
        	double initialTitleValue;
    
            initialTitleValue = (dvdPrice * dvdStock); // initial value
            restockFee = (initialTitleValue * .05); // calculate restocking fee
            titleValue = initialTitleValue + restockFee; // add restock fee to the initial value
            totalValue = titleValue + totalValue; // add to the cumulative value
            return titleValue;
        } // end method value
    
        // retrieve the restocking fee
        public double getRestockFee()
        {
        	return restockFee;
        } // end method getRestockFee
    
    } // end subclass BlurayDVD
    I need to access the "getTotal" method, but when I try to do it, I get errors. Also, the "sort" code isn't working.

  13. #13
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    The variable totalValue is not really something each dvd will know about, as pbrockway2 stated.

    Instead of keeping the total cost as a variable inside the DVD class, just try creating a static method that accepts the List/Array of DVD objects as a parameter and returns the total value. Then just call it using...
    Java Code:
         double totalValue = DVD.getTotalValue(bluList);

  14. #14
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    the teacher has specifically instructed that the function be in the DVD class.

    "create a separate function, in your DVD class, that accepts your DVD array of objects as a parameter, and then iterate through it in order to calculate the total inventory values."

  15. #15
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Then do not hold the total as a variable and just calculate it internally inside the method. It doesn't have to be static, though it makes absolutely no sense for it not to be.

  16. #16
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    Then do not hold the total as a variable and just calculate it internally inside the method. It doesn't have to be static, though it makes absolutely no sense for it not to be.
    I agree. I am having a problem calling the method, though. It tells me that I cannot reference a non-static method with a static context using

    Java Code:
     System.out.printf("The total value of the entire inventory is $%.2f", DVD.getTotal(blu));

  17. #17
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    You'll need to call it just like you'd call any other method on an Object. Since its not static, you'll need to use an instance of DVD.

    Try
    Java Code:
         System.out.printf("The total value of the entire inventory is $%.2f", b1.getTotal(blu));

  18. #18
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by stormywaters View Post
    you'll need to call it just like you'd call any other method on an object. Since its not static, you'll need to use an instance of dvd.

    Try
    Java Code:
         system.out.printf("the total value of the entire inventory is $%.2f", b1.gettotal(blu));
    omg!!! Thank you so much! It worked!!!!

  19. #19
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Now that I have made all the corrections, I have to add a gui. I am using netbeans right now, and have managed to create the gui window into which the lines will display. Now I have to migrate the code into these windows.

    Do I migrate just the system.out code? There can't be two static main methods in the code, correct? *sigh* I wish they had given me a better school book for this...

  20. #20
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    You can have as many static main methods as you want, but only one can be executed when you run the program.

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with inventory program part 5 PLEASE!
    By Exether in forum New To Java
    Replies: 2
    Last Post: 08-09-2010, 07:25 AM
  2. Inventory Program Part 2 of 6
    By tlouvierre in forum New To Java
    Replies: 2
    Last Post: 05-28-2009, 02:30 AM
  3. Inventory Program Part 3 ~ please help!
    By marMcD in forum New To Java
    Replies: 13
    Last Post: 02-25-2009, 06:57 AM
  4. Java Inventory Program Part 3
    By ljk8950 in forum New To Java
    Replies: 18
    Last Post: 07-28-2008, 06:47 AM
  5. Inventory part 2 help please
    By badness in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 08:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •