Results 1 to 8 of 8
- 02-15-2012, 06:00 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Please Help out a bit with Java Program
This is my assignment guys: Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
Modify the output to display this additional feature you have chosen and the restocking fee.
My problem is I cannot get my code to display the restocking fee or the manufacturer properly. Can any one see something I am missing and explain in laymens terms please. I got it to display one manufacturer for notepads but it shows on all the others also and if I try to put more than one manufacturer it gives error.
This is what I have:
Java Code:// InventoryProgram.java // // This program calculates inventory value package inventory.project; import java.util.Scanner; /** * * @author Curtis Criff */ public class InventoryProject { // main method begins program execution public static void main(String args[] ) { // create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); // display a welcome message to the InventoryProgramPart3 user System.out.println( "Welcome to the Inventory Program!" ); // office supplies supplies[] supplies = new supplies[100]; // an array of 100 supplies manufacturer[] manufacturer = new manufacturer[100]; supplies notepads = new supplies( 04, "notepads", 60, 2.75 ); supplies pencils = new supplies( 05, "pencils", 75, 1.25 ); supplies folders = new supplies( 02, "folders", 30, 4.75 ); supplies envelopes = new supplies( 01, "envelopes", 15, 5.25 ); supplies markers = new supplies( 03, "markers", 45, 3.50 ); // display the inventories one at a time envelopes.showInventory(); folders.showInventory(); markers.showInventory(); notepads.showInventory(); pencils.showInventory(); // sort supplies by name for ( int i = 0; i < args.length; i++ ) System.out.println( args[i] + ", " ); double array[] = { 78.75, 142.50, 157.50, 165.00, 93.75 }; double total = 0; // add each element's value to total for ( int counter = 0; counter < array.length; counter++) total += array[ counter ]; System.out.printf( "\nTotal inventory value is: $%.2f\n", total ); System.out.println( "\nThank you for using the Inventory Program!\n" ); } // end method main } // end class InventoryProgram // Office Supplies class supplies { public int suppliesNumber; public String suppliesName = new String(); public int suppliesUnits; public double suppliesPrice; // set supplies number public void setSuppliesNumber( int number ) { this.suppliesNumber = number; } // end method set supplies number // return supplies number public int getSuppliesNumber() { return suppliesNumber; } // end method get supplies number // set supplies name public void setSuppliesName( String name ) { this.suppliesName = name; } // end method set supplies name // return supplies name public String getSuppliesName() { return suppliesName; } // end method get supplies name // set supplies in stock public void setSuppliesUnits( int units ) { this.suppliesUnits = units; } // end method set supplies units // return supplies units public int getSuppliesUnits() { return suppliesUnits; } // end method get supplies units // set supplies price public void setSuppliesPrice( double price ) { this.suppliesPrice = price; } // end method set supplies price // return supplies price public double getSuppliesPrice() { return suppliesPrice; } // end method get supplies price // calculate supplies inventory value public double getValue() { return suppliesUnits * suppliesPrice; } // end method supplies inventory value // four-argument constructor supplies( int number, String name, int units, double price ) { suppliesNumber = number; suppliesName = name; suppliesUnits = units; suppliesPrice = price; } // end four-argument constructor // display inventory public void showInventory() { System.out.println(); // outputs blank line System.out.println( "Product Number: "+suppliesNumber ); System.out.println( "Product Name: "+suppliesName ); System.out.println( "Units in Stock: "+suppliesUnits ); System.out.printf( "Unit Price: $%.2f", suppliesPrice ); manufacturer supplies = new manufacturer ( 04, "notepads", 60, 2.75, "Ampad" ); System.out.println( "\nManufacturer: "+supplies.getSuppliesManufacturer() ); // value() method and display the value System.out.printf( "\nInventory value of "+suppliesName+ " is = $%.2f\n", getValue() ); } // end display inventory } // end class supplies class manufacturer extends supplies { // holds the supplies manufacturer private String suppliesManufacturer; // five-argument constructor manufacturer( int number, String name, int units, double price, String manufacturer ) { super( number, name, units, price ); suppliesManufacturer = manufacturer; } // end five-argument constructor // set supplies manufacturer public void setSuppliesManufacturer( String manufacturer ) { this.suppliesManufacturer = manufacturer; } // end method set supplies manufacturer // return supplies manufacturer public String getSuppliesManufacturer() { return suppliesManufacturer; } // end method get supplies manufacturer // add 5% restocking fee @Override public double getValue() { return super.getValue() * 1.05; } // end method return supplies manufacturer // calculate restocking fee public double getRestockingFee() { return super.getValue() * .05; } //end method calculate restocking fee //return String representation of suppliesManufacturer @Override public String toString() { String formatString = "Manufacturer: %s"; formatString += "Restocking Fee: $%.2f"; formatString = String.format( formatString, suppliesManufacturer, super.getValue() * 0.05 ); return( formatString + super.toString() ); } // end toString() // display inventory @Override public void showInventory() { super.showInventory(); System.out.println( toString() ); System.out.println( "\nManufacturer: "+getSuppliesManufacturer() ); // Display value plus restocking fee System.out.printf( "\nInventory value of "+suppliesName+ " is = $%.2f\n", getRestockingFee() ); } // end method display inventory } // end class manufacturer
- 02-15-2012, 12:58 PM #2
Re: Please Help out a bit with Java Program
Please copy and pasted here the full text of the error message.it gives error.
- 02-15-2012, 08:39 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Please Help out a bit with Java Program
There is no error code. The code compile and runs but does not do what it is suppose to do. It does not show Restocking Fee and Ampad shows on every Products Manufacturer list instead of just Notepads.
This is the output from NetBeans 7.1
Welcome to the Inventory Program!
Product Number: 1
Product Name: envelopes
Units in Stock: 15
Unit Price: $5.25
Manufacturer: Ampad
Inventory value of envelopes is = $78.75
Product Number: 2
Product Name: folders
Units in Stock: 30
Unit Price: $4.75
Manufacturer: Ampad
Inventory value of folders is = $142.50
Product Number: 3
Product Name: markers
Units in Stock: 45
Unit Price: $3.50
Manufacturer: Ampad
Inventory value of markers is = $157.50
Product Number: 4
Product Name: notepads
Units in Stock: 60
Unit Price: $2.75
Manufacturer: Ampad
Inventory value of notepads is = $165.00
Product Number: 5
Product Name: pencils
Units in Stock: 75
Unit Price: $1.25
Manufacturer: Ampad
Inventory value of pencils is = $93.75
Total inventory value is: $637.50
Thank you for using the Inventory Program!
- 02-15-2012, 08:49 PM #4
Re: Please Help out a bit with Java Program
Where in your program is the Restocking Fee printed out?It does not show Restocking Fee
Where does the program execute the code that prints out the Restocking fee?
If the code does not execute the printing out of the data, then it won't be printed.
Play computer with your program and set through the code to see where it goes and why it is not printing out what you want.
Also add some println statements to show execution flow and the values of variables as the code executes so you can see what the program is doing.
- 02-15-2012, 09:05 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Please Help out a bit with Java Program
You have to forgive me as I am new to Java programming.
The restocking fee should be under the manufacturer
[code]
// Display value plus restocking fee
2 System.out.printf( "\nInventory value of "+suppliesName+ " is = $%.2f\n",
3 getRestockingFee() );[code]
I think this is where I ask the program to print out the restocking fee
I do not exactly know where to put the println statements as I am fairly new to this and have not did any debugging as of yet.
Thanks for the help.
- 02-15-2012, 09:13 PM #6
Re: Please Help out a bit with Java Program
Your code does not follow java coding conventions and that makes it harder to read. Class names should start with Uppercase letters and variables with lowercase letters.
Where do you set the manufacturer for each Supplies object? I don't see where the Supplies class saves the manufacturer.
If you call the Supplies class's showInventory() method there is no manufacturer saved in the class to show!!!
There is a suppliesManufacturer variable in the Manufacturer class.
You may need to redesign how you are using these classes.
- 02-17-2012, 05:16 AM #7
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Please Help out a bit with Java Program
Thanks for the help so far.
I have gotten past my first post and now need a little constructive help with my GUI:
I know where the errors are at which consist of lines 266 - 300 and line 411.
If anyone can point me in the right direction, that would be great
Here is the code
Java Code:// InventoryProgram.java // // This program calculates inventory values package inventory.project; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.NumberFormat; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; import javax.swing.*; /** * * @author Curtis Criff */ public class InventoryProject { // main method begins program execution public static void main(String args[] ) { // create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); // display a welcome message to the InventoryProgramPart3 user System.out.println( "Welcome to the Inventory Program!" ); // office supplies supplies[] supplies = new supplies[100]; // an array of 100 supplies manufacturer [] manufacturer = new manufacturer [100]; manufacturer notepads = new manufacturer ( 04, "notepads", 60, 2.75, "Rapid" ); manufacturer pencils = new manufacturer( 05, "pencils", 75, 1.25, "Ampad" ); manufacturer folders = new manufacturer( 02, "folders", 30, 4.75, "Rapid" ); manufacturer envelopes = new manufacturer( 01, "envelopes", 15, 5.25, "Rapid" ); manufacturer markers = new manufacturer( 03, "markers", 45, 3.50, "Ampad" ); // display the inventories one at a time envelopes.showInventory(); folders.showInventory(); markers.showInventory(); notepads.showInventory(); pencils.showInventory(); // sort supplies by name for ( int i = 0; i < args.length; i++ ) System.out.println( args[i] + ", " ); double array[] = { 78.75, 142.50, 157.50, 165.00, 93.75 }; double total = 0; // add each element's value to total for ( int counter = 0; counter < array.length; counter++) total += array[ counter ]; System.out.printf( "\nTotal inventory value is: $%.2f\n", total ); System.out.println( "\nThank you for using the Inventory Program!\n" ); } // end method main } // end class InventoryProject class supplies { public int suppliesNumber; public String suppliesName = new String(); public int suppliesUnits; public double suppliesPrice; // set supplies number public void setSuppliesNumber( int number ) { this.suppliesNumber = number; } // end method set supplies number // return supplies number public int getSuppliesNumber() { return suppliesNumber; } // end method get supplies number // set supplies name public void setSuppliesName( String name ) { this.suppliesName = name; } // end method set supplies name // return supplies name public String getSuppliesName() { return suppliesName; } // end method get supplies name // set supplies in stock public void setSuppliesUnits( int units ) { this.suppliesUnits = units; } // end method set supplies units // return supplies units public int getSuppliesUnits() { return suppliesUnits; } // end method get supplies units // set supplies price public void setSuppliesPrice( double price ) { this.suppliesPrice = price; } // end method set supplies price // return supplies price public double getSuppliesPrice() { return suppliesPrice; } // end method get supplies price // calculate supplies inventory value public double getValue() { return suppliesUnits * suppliesPrice; } // end method supplies inventory value // four-argument constructor supplies( int number, String name, int units, double price ) { suppliesNumber = number; suppliesName = name; suppliesUnits = units; suppliesPrice = price; } // end four-argument constructor // display inventory public void showInventory() { System.out.println(); // outputs blank line System.out.println( "Product Number: "+suppliesNumber ); System.out.println( "Product Name: "+suppliesName ); System.out.println( "Units in Stock: "+suppliesUnits ); System.out.printf( "Unit Price: $%.2f", suppliesPrice ); System.out.println(); // outputs blank line // value() method and display the value System.out.printf( "\nInventory value of "+suppliesName+ " is = $%.2f\n", getValue() ); } // end display inventory } // end class supplies class manufacturer extends supplies { // holds the supplies manufacturer private String suppliesManufacturer = "manufacturer"; // five-argument constructor public manufacturer( int number, String name, int units, double price, String manufacturer ) { super( number, name, units, price ); this.suppliesManufacturer = manufacturer; } // end five-argument constructor // set supplies manufacturer public void setSuppliesManufacturer( String manufacturer ) { suppliesManufacturer = manufacturer; } // end method set supplies manufacturer // return supplies manufacturer public String getSuppliesManufacturer() { return suppliesManufacturer; } // end method get supplies manufacturer // add 5% restocking fee @Override public double getValue() { return suppliesUnits * suppliesPrice; } // end method return supplies manufacturer // calculate restocking fee public double getRestockingFee() { return 0.05 * suppliesUnits * suppliesPrice; } //end method calculate restocking fee //return String representation of suppliesManufacturer @Override public String toString() { String formatString = "\nManufacturer: %s"; formatString += "\nRestocking Fee: $%.2f"; formatString = String.format( formatString, suppliesManufacturer, super.getValue() * 0.05 ); return( formatString ); } // end toString() // display inventory @Override public void showInventory() { super.showInventory(); System.out.println( toString() ); } // end method display inventory } // end class manufacturer // GUI class Manufacturer extends JFrame { NumberFormat nf = NumberFormat.getCurrencyInstance(); private JPanel gridPanel = new JPanel(); private JPanel panel = new JPanel(); JButton nextButton; JTextField suppliesNumberField; JTextField suppliesNameField; JTextField suppliesUnitsField; JTextField suppliesPriceField; JTextField ValueofunitsField; JTextField suppliesManufacturerField; JTextField RestockingFeeField; JTextField TotalinventoryvalueField; JLabel lblsuppliesNumber; JLabel lblsuppliesName; JLabel lblsuppliesUnits; JLabel lblsuppliesPrice; JLabel lblValueofunits; JLabel lblsuppliesManufacturer; JLabel lblRestockingFee; JLabel lblTotalinventoryvalue; manufacturer envelopes; manufacturer folders; manufacturer markers; manufacturer notepads; manufacturer pencils; public static final int MAX_manufacturer = 100; // set maximum size for Supplies Array manufacturer[] manufacturer = new manufacturer[MAX_manufacturer]; // create Supplies Array object static int ArrayIndex = 0; // array index public Manufacturer() // constructor { envelopes = new (); envelopes.setsuppliesNumber("01"); envelopes.setsuppliesName("envelopes"); envelopes.suppliesUnits(15); envelopes.setsuppliesPrice(5.25); envelopes.setsuppliesManufacturer("Rapid"); folders = new (); folders.setsuppliesNumber("02"); folders.setsuppliesName("folders"); folders.suppliesUnits(30); folders.setsuppliesPrice(4.75); folders.setsuppliesManufacturer("Rapid"); markers = new (); markers.setsuppliesNumber("03"); markers.setsuppliesName("markers"); markers.suppliesUnits(45); markers.setsuppliesPrice(3.50); markers.setsuppliesManufacturer("Ampad");; notepads = new (); notepads.setsuppliesNumber("04"); notepads.setsuppliesName("notepads"); notepads.suppliesUnits(60); notepads.setsuppliesPrice(2.75); notepads.setsuppliesManufacturer("Rapid"); pencils = new (); pencils.setsuppliesNumber("05"); pencils.setsuppliesName("pencils"); pencils.suppliesUnits(75); pencils.setsuppliesPrice(1.25); pencils.setsuppliesManufacturer("Ampad"); manufacturer[0] = envelopes; manufacturer[1] = folders; manufacturer[2] = markers; manufacturer[3] = notepads; manufacturer[4] = pencils; gridPanel.setLayout(new BorderLayout()); // create a border layout gridPanel.add(this.createLabelPanel(), BorderLayout.WEST); // add label panel gridPanel.add(this.createTextPanel(), BorderLayout.CENTER); // add field panel gridPanel.add(this.createButtonPanel(), BorderLayout.SOUTH); // add button panel add(gridPanel); } private JPanel createButtonPanel() { ActionListener btnListen = new ButtonListener(); nextButton = new JButton("Next Product"); nextButton.setActionCommand("Next"); nextButton.addActionListener(btnListen); JPanel panel = new JPanel(); panel.add(nextButton); return panel; } private JPanel createLabelPanel() { lblsuppliesNumber = new JLabel("Product Number: "); lblsuppliesName = new JLabel("Product Name: "); lblsuppliesUnits = new JLabel("Units in Stock: "); lblsuppliesPrice = new JLabel("Unit Price: "); lblValueofunits = new JLabel("Value Of Units In Stock: "); lblsuppliesManufacturer = new JLabel("Manufacturer: "); lblRestockingFee = new JLabel("Restocking Fee: "); lblTotalinventoryvalue = new JLabel("Total Inventory Value: "); panel = new JPanel(); panel.setLayout(new GridLayout(8, 1, 5 , 5)); panel.add(lblsuppliesNumber); panel.add(lblsuppliesName); panel.add(lblsuppliesUnits); panel.add(lblsuppliesPrice); panel.add(lblValueofunits); panel.add(lblsuppliesManufacturer); panel.add(lblRestockingFee); panel.add(lblTotalinventoryvalue); return panel;// return the panel } private JPanel createTextPanel() { suppliesNumberField = new JTextField(); suppliesNumberField.setEditable(false); suppliesNameField = new JTextField(); suppliesNameField.setEditable(false); suppliesPriceField = new JTextField(); suppliesPriceField.setEditable(false); suppliesUnitsField = new JTextField(); suppliesUnitsField.setEditable(false); ValueofunitsField = new JTextField(); ValueofunitsField.setEditable(false); suppliesManufacturerField = new JTextField(); suppliesManufacturerField.setEditable(false); RestockingFeeField = new JTextField(); RestockingFeeField.setEditable(false); TotalinventoryvalueField = new JTextField(); TotalinventoryvalueField.setEditable(false); panel = new JPanel(); panel.setLayout(new GridLayout(8, 1 , 5 , 5)); panel.add(suppliesNumberField); panel.add(suppliesNameField); panel.add(suppliesPriceField); panel.add(suppliesUnitsField); panel.add(ValueofunitsField); panel.add(suppliesManufacturerField); panel.add(RestockingFeeField); panel.add(TotalinventoryvalueField); return panel; // return the panel } // end createTextPanel method // nested ButtonListener class class ButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Arrays.sort(manufacturer, new suppliesComparator(); if (e.getActionCommand().equals("Next")) if (ArrayIndex < manufacturer.length) { suppliesNumberField.setText(nf.format(manufacturer[ArrayIndex].getSuppliesNumber())); suppliesNameField.setText(manufacturer[ArrayIndex].getSuppliesName()); suppliesPriceField.setText(nf.format(manufacturer[ArrayIndex].getSuppliesPrice())); suppliesUnitsField.setText(String.valueOf(manufacturer[ArrayIndex].getSuppliesUnits())); ValueofunitsField.setText(nf.format(manufacturer[ArrayIndex].getValue())); suppliesManufacturerField.setText(String.valueOf(manufacturer[ArrayIndex].getSuppliesManufacturer())); RestockingFeeField.setText(nf.format(manufacturer[ArrayIndex].getRestockingFee())); TotalinventoryvalueField.setText(nf.format(manufacturer[ArrayIndex].getValue())); ArrayIndex = ArrayIndex + 1; } // end if } // end actionPerformed } // end class ButtonListener } // end FrameHelperClass class suppliesComparator<T> implements Comparator<T> { @Override public int compare (Object suppliesOne , Object suppliesTwo) { String SuppliesName1 = ((supplies)suppliesOne).getSuppliesName().toUpperCase(); String SuppliesName2 = ((supplies)suppliesTwo).getSuppliesName().toUpperCase(); if (!(SuppliesName1.equals(SuppliesName2))) return SuppliesName1.compareTo(SuppliesName2); else return SuppliesName2.compareTo(SuppliesName1); } }
- 02-17-2012, 12:55 PM #8
Similar Threads
-
Call one Java Program from another Java Program
By rajpalparyani in forum New To JavaReplies: 3Last Post: 02-14-2011, 04:13 AM -
Is There A Way To Call Another Java Program Within A Java Program
By SwissR in forum New To JavaReplies: 4Last Post: 07-30-2010, 12:25 PM -
execute java program within java program
By popey in forum New To JavaReplies: 2Last Post: 10-22-2009, 05:32 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks