Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.
Create a method to calculate the value of the entire inventory.
Create another method to sort the array items by the name of the product.
Post as an attachment in java format.
I am getting error that identifier is needed at lines 87 and 90, I marked them in red... also not sure if the Class inventory is correct so it will sort... please help!! Thanks so much!!
Code:public class InventoryProgramPart2 { public static void main(String args []) { CD cd; cd = new CD(1, "Elton John", 9, 15.00); System.out.println(cd); cd = new CD(2, "Beyonce", 2, 13.99); System.out.println(cd); cd = new CD(3, "George Straight", 5, 14.00); System.out.println(cd); cd = new CD(4, "Tim McGraw", 4, 12.99); System.out.println(cd); } // end main } // end class InventoryProgramPart2 class CD { private int cdItem; private String cdArtist; private int cdStock; private double cdPrice; public CD(int item, String artist, int stock, double price) { cdItem = item; cdArtist = artist; cdStock = stock; cdPrice = price; } //end constructor //set CD Item public void setCdItem(int item) { cdItem = item; } //end method set Cd Item //return CD Item public int getCdItem() { return cdItem; } //end method get Cd Item //set CD Artist public void setCdArtist(String artist) { cdArtist = artist; } //end method set Cd Artist //return Cd Artist public String getCdArtist() { return cdArtist; } //end method get Cd Artist public void setCdStock(int stock) { cdStock = stock; } //end method set Cd Stock //return cd Stock public int getCdStock() { return cdStock; } //end method get Cd Stock public void setCdPrice(double price) { cdPrice = price; } //end method set Cd Price //return Cd Price public double getCdPrice() { return cdPrice; } //end method get Cd Price //calculate inventory value public double value() { return cdPrice * cdStock; } // end method inventory value public String toString() { return String.format("item=%3d, artist=%-20s, units=%d, price=%.2f, value=%.2f", cdItem, cdArtist, cdStock, cdPrice, value()); } } //end class CD class Inventory { CD[] = new CD[100]; // Add to inventory public static void addToInventory(CD) { } // Get inventory value public double getInventoryValue() { Inventory myInventory = new Inventory (); myInventory.addToInventory(new CD(5, "Poison", 3, 11.99)); // Print Total Inventory System.out.println("Total inventory value is: " + myInventory.getInventoryValue()); return myInventory.getInventoryValue(); } }
Moved to correct forum.
You haven't given a name to the array.Code:CD[] = new CD[100];
Change to:
The term between CD[] and = is the name of the array. That is why you forgot.Code:CD[] arOCD = new CD[100];
thank you so much, but now it is telling me identifier is expected on line 91 which I highlighted in red. ??? I'm not sure how to fix that.. any help would be greatly appreciated.
Code:public class InventoryProgramPart2 { public static void main(String args []) { CD cd; cd = new CD(1, "Elton John", 9, 15.00); System.out.println(cd); cd = new CD(2, "Beyonce", 2, 13.99); System.out.println(cd); cd = new CD(3, "George Straight", 5, 14.00); System.out.println(cd); cd = new CD(4, "Tim McGraw", 4, 12.99); System.out.println(cd); } // end main } // end class InventoryProgramPart2 class CD { private int cdItem; private String cdArtist; private int cdStock; private double cdPrice; public CD(int item, String artist, int stock, double price) { cdItem = item; cdArtist = artist; cdStock = stock; cdPrice = price; } //end constructor //set CD Item public void setCdItem(int item) { cdItem = item; } //end method set Cd Item //return CD Item public int getCdItem() { return cdItem; } //end method get Cd Item //set CD Artist public void setCdArtist(String artist) { cdArtist = artist; } //end method set Cd Artist //return Cd Artist public String getCdArtist() { return cdArtist; } //end method get Cd Artist public void setCdStock(int stock) { cdStock = stock; } //end method set Cd Stock //return cd Stock public int getCdStock() { return cdStock; } //end method get Cd Stock public void setCdPrice(double price) { cdPrice = price; } //end method set Cd Price //return Cd Price public double getCdPrice() { return cdPrice; } //end method get Cd Price //calculate inventory value public double value() { return cdPrice * cdStock; } // end method inventory value public String toString() { return String.format("item=%3d, artist=%-20s, units=%d, price=%.2f, value=%.2f", cdItem, cdArtist, cdStock, cdPrice, value()); } } //end class CD class Inventory{ CD[]arOCD = new CD[50]; // Add to inventory public void addToInventory(CD) { } // Get inventory value public double getInventoryValue() { Inventory myInventory = new Inventory(); myInventory.addToInventory(new CD(5, "Poison", 3, 11.99)); // Print total value of inventory System.out.println("Total value of inventory is: " + myInventory.getInventoryValue()); return myInventory.getInventoryValue();} }
In the parenthesize you have to give a name to the parameter.Code:public void addToInventory(CD)
When you declare a variable you have to give it a type and a name.Code:public void addToInventory(CD cd1) { }
Wow that worked, thank you again! Only problem now is that it didn't add in the new CD (Poison) and it didn't display the total Inventory for all the CD's (but there are no errors)?? I really appreciate your help, I have 3 more weeks of this class and it has become so difficult.
Code:public class InventoryProgramPart2 { public static void main(String args []) { CD cd; cd = new CD(1, "Elton John", 9, 15.00); System.out.println(cd); cd = new CD(2, "Beyonce", 2, 13.99); System.out.println(cd); cd = new CD(3, "George Straight", 5, 14.00); System.out.println(cd); cd = new CD(4, "Tim McGraw", 4, 12.99); System.out.println(cd); } // end main } // end class InventoryProgramPart2 class CD { private int cdItem; private String cdArtist; private int cdStock; private double cdPrice; public CD(int item, String artist, int stock, double price) { cdItem = item; cdArtist = artist; cdStock = stock; cdPrice = price; } //end constructor //set CD Item public void setCdItem(int item) { cdItem = item; } //end method set Cd Item //return CD Item public int getCdItem() { return cdItem; } //end method get Cd Item //set CD Artist public void setCdArtist(String artist) { cdArtist = artist; } //end method set Cd Artist //return Cd Artist public String getCdArtist() { return cdArtist; } //end method get Cd Artist public void setCdStock(int stock) { cdStock = stock; } //end method set Cd Stock //return cd Stock public int getCdStock() { return cdStock; } //end method get Cd Stock public void setCdPrice(double price) { cdPrice = price; } //end method set Cd Price //return Cd Price public double getCdPrice() { return cdPrice; } //end method get Cd Price //calculate inventory value public double value() { return cdPrice * cdStock; } // end method inventory value public String toString() { return String.format("item=%3d, artist=%-20s, units=%d, price=%.2f, value=%.2f", cdItem, cdArtist, cdStock, cdPrice, value()); } } //end class CD class Inventory{ CD[]arOCD = new CD[50]; // Add to inventory public void addToInventory(CD cd5) { } // Get inventory value public double getInventoryValue() { Inventory myInventory = new Inventory(); myInventory.addToInventory(new CD(5, "Poison", 3, 11.99)); // Print total value of inventory System.out.println("Total value of inventory is: " + myInventory.getInventoryValue()); return myInventory.getInventoryValue();} }
Your addToInventory method doesn't do anything. Also you aren't actually creating an Inventory object and galling getInventoryValue.
You need to make an inventory object and code the addToInventory method to add them to the array.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks