Results 1 to 20 of 27
Thread: Inventory Part 4 assistance
- 11-03-2010, 08:44 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
Inventory Part 4 assistance
OK, I am now coding part 4 of my inventory program.
This is what I have so far:
I am getting one error. It says: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
The error line shows up at the curly bracket after the Comparator method.<anonymous inventoryp4.Inventory$1> is not abstract and does not override abstract method compare(java.lang.Object,java.lang.Object) in java.util.ComparatorLast edited by ladykrimson; 11-03-2010 at 08:49 PM.
- 11-03-2010, 09:18 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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.
-
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:
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:public int compare(Object o1, Object 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.Java Code:new Comparator<BlueraDVD> () { public int compare( BlurayDVD o1, BlurayDVD o2) { //......Last edited by Fubarable; 11-03-2010 at 09:35 PM.
- 11-03-2010, 10:21 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
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.
- 11-03-2010, 10:30 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.)
- 11-03-2010, 10:33 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
- 11-03-2010, 10:50 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 11-04-2010, 12:45 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
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."
- 11-04-2010, 01:06 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 11-04-2010, 04:02 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
- 11-04-2010, 06:45 PM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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?
- 11-04-2010, 11:02 PM #12
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
OK, this is the code I have:
I need to access the "getTotal" method, but when I try to do it, I get errors. Also, the "sort" code isn't working.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
- 11-05-2010, 12:47 PM #13
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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);
- 11-05-2010, 02:52 PM #14
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
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."
- 11-05-2010, 02:59 PM #15
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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.
- 11-05-2010, 03:08 PM #16
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
- 11-05-2010, 03:11 PM #17
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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));
- 11-05-2010, 03:22 PM #18
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
- 11-05-2010, 03:28 PM #19
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
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...
- 11-05-2010, 03:51 PM #20
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Similar Threads
-
Help with inventory program part 5 PLEASE!
By Exether in forum New To JavaReplies: 2Last Post: 08-09-2010, 06:25 AM -
Inventory Program Part 2 of 6
By tlouvierre in forum New To JavaReplies: 2Last Post: 05-28-2009, 01:30 AM -
Inventory Program Part 3 ~ please help!
By marMcD in forum New To JavaReplies: 13Last Post: 02-25-2009, 05:57 AM -
Java Inventory Program Part 3
By ljk8950 in forum New To JavaReplies: 18Last Post: 07-28-2008, 05:47 AM -
Inventory part 2 help please
By badness in forum New To JavaReplies: 1Last Post: 12-12-2007, 07:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks