Results 1 to 2 of 2
  1. #1
    Raven9 is offline Member
    Join Date
    Apr 2011
    Rep Power

    Default Modify the Inventory Program to use a GUI.

    I am totally lost how to go about modifying the Inventory Program to use a GUI. I am already a day late and really need some advice.The GUI should display all of the items in the inventory and include 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 GUI should display the value of the entire inventory, the additional attribute, and the restocking fee. All dollar values should be displayed as currency (i.e. $D,DDD.CC). There should not be any output in the DOS console.

    Java Code:
    //W6 CheckPoint:
    //Inventory Program Part 3 Week 6 class Television modified with additional class and restocking fee
    import statements
    import java.text.NumberFormat;
    import java.util.Locale;
    import java.util.Arrays;
      class Television { //class name and attributes
        //declare class variables
     	private String itemNumber; //item # of product
     	private String televisionName; //product name
     	public int unitsinStock; //# of units in stock
     	public double unitPrice; //Price per unit
      	NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
     	//class constructor
     	public Television(String itemNumber, String televisionName, int unitsinStock, double price) {
     		this.itemNumber = itemNumber;
     		this.televisionName = televisionName;
     		this.unitsinStock = unitsinStock;
     		this.unitPrice = price;
     } //end constructor
      //define set and get methods for class variables
     //getter and setter methods for Television
     //item number
        public String getItemNumber() { //getter for item number
     		return itemNumber;
     	} //end getter item number
      	public void setItemNumber (String itemNumber) { //setter for item number
     		this.itemNumber = itemNumber;
     	} //end setter item number
     	//television name
        public String getTelevisionName() { //getter for product name
    	 		return televisionName;
     	} //end getter television name
     	public void setTelevisionName (String product) { //setter for product name
     		this.televisionName = televisionName;
     	} //end setter television name
         //available units
        public double getUnitsInStock() { //getter for units in stock
    	  		return unitsinStock;
    	} //end getter units in stock
     	public void setUnitsInStock (double units) { //setter for units in stock
     		this.unitsinStock = unitsinStock;
     	} //end setter units in stock
        public double getUnitPrice() { //getter for unit price
    	 		return unitPrice;
     	} //end getter for unit price
     	public void setUnitPrice (double price) { //setter for unit price
     		this.unitPrice = unitPrice;
     	} //end setter unit price
         //calculate the total inventory by returning the product of available units and price
    	public double calculateInventory(){
    		return unitPrice * unitsinStock;
        //toString method that outputs the class variables
    	public String toString () {
    			return "Television Name:" + "\t" + televisionName + "\n" +
    				   "Item Number:" + "\t" + itemNumber + "\n" +
    			       "UnitsInStock:" + "\t \t" + unitsinStock + "\n" +
    			       "UnitPrice:" + "\t \t" + nf.format(unitPrice) + "\n" +
    			       "Item Total:" + "\t" + nf.format(calculateInventory());
    // The is-a test the inheritance relationship between Samsung and Television
    class Televisions extends Television{
    	//class variables
    	private String serialNumber;
    	private double restockFee;
    	private static final double RESTOCK_FEE_PERCENTAGE = .05;
    	//class constructor
    	public Televisions(String itemNumber, String televisionName, int unitsinStock, double price, String serialNumber){
    		super (itemNumber, televisionName, unitsinStock, price);
    		this.serialNumber = serialNumber;
    		//calculate restock fee as 5% of the inventory total
    		restockFee = super.calculateInventory() * RESTOCK_FEE_PERCENTAGE;
    	//declare get and set methods
    	//serial number
    	public String getSerialNumber(){
    		return serialNumber;
    	public void setSerialNumber(String serialNumber){
    		this.serialNumber = serialNumber;
    	//restockFee does not have a set method, to ensure that it is only set through the HP constructor
    	public double getRestockFee(){
    		return restockFee;
    	//add restock fee and serial number to the output
    	public String toString () {
    				return super.toString() + "\n" +
    				       "Serial Number: " + "\t" + serialNumber + "\n" +
    				       "Restock Fee:" + "\t" + nf.format(getRestockFee());
    public class InventoryPart3 {
        public static Television[] sortArray(Television[] televisions){
            // First Step
            String[] names = new String[televisions.length];
           //Second Step
           Television[] tempProduct = new Television [televisions.length];
          //Third Step
          for (int i = 0; i < televisions.length; i++){
    	  		 names[i] = televisions[i].getTelevisionName();
          //Fourth Step
         //Fifth Step
        	for (int i = 0; i < televisions.length; i++){ //Product Array
    			     for (int j = 0; j < names.length; j++){ //Names Array
    			          if (televisions[i].getTelevisionName().equalsIgnoreCase(names[j])) {
    			                     tempProduct[j] = televisions[i];
    			return tempProduct;
     	public static double calculateInventoryTotal(Television[] televisions){
    		double total = 0;
    		for (int i = 0; i < televisions.length; i++){
    			total += televisions[i].calculateInventory();
    		return total;
        public static void main (String[] args) {
            NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
           //create several items for the inventory of type Television and LG
           Television[] televisions = new Television[5];
           Television samsung = new Television ("003", "Samsung UN46D6400",9,1599.99);
           Television vizio = new Television ("005", "Vizio XVT553SV",6,1299.00);
           Television panasonic = new Television ("002", "Panasonic Viera TC-P50VT25",2,2079.99);
           Television sony = new Television ("004", "Sony Bravia KDL-55EX720",8, 1889.99);
           Television lg = new Television ("001", "LG Infinia 47LX9500",2,2099.00);
    //add all of the items to the inventory
    		televisions[2] = samsung;
    		televisions[4] = vizio;
    		televisions[1] = panasonic;
    		televisions[3] = sony;
    		televisions[0] = lg;
            //sort items
    		televisions = sortArray(televisions);
    		//calculate inventory total
    		double inventoryTotal = calculateInventoryTotal(televisions);
    		//output inventory
    		System.out.println("Inventory Items: " + "\n");
    		for (int i = 0; i < televisions.length; i++){
    		//output inventory total
    		System.out.println("Inventory Total: " + nf.format(inventoryTotal));

    GIU I coded

    Java Code:
      import java.awt.*;  
      import javax.swing.*;  
      import java.awt.event.*;  
      public class RectangleProgram extends JFrame  
          private static final int WIDTH = 400;  
          private static final int HEIGHT = 300;  
          private JLabel lengthL, widthL, areaL;  
          private JTextField lengthTF, widthTF, areaTF;  
          private JButton calculateB, exitB;  
          //Button handlers:  
         private CalculateButtonHandler cbHandler;  
         private ExitButtonHandler ebHandler;  
          public RectangleProgram()  
              lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);  
              widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT);  
              areaL = new JLabel("Area: ", SwingConstants.RIGHT);  
              lengthTF = new JTextField(10);  
              widthTF = new JTextField(10);  
              areaTF = new JTextField(10);  
              //SPecify handlers for each button and add (register) ActionListeners to each button.  
              calculateB = new JButton("Calculate");  
              cbHandler = new CalculateButtonHandler();  
              exitB = new JButton("Exit");  
              ebHandler = new ExitButtonHandler();  
              setTitle("Sample Title: Area of a Rectangle");  
              Container pane = getContentPane();  
              pane.setLayout(new GridLayout(4, 2));  
              //Add things to the pane in the order you want them to appear (left to right, top to bottom)  
              setSize(WIDTH, HEIGHT);  
          private class CalculateButtonHandler implements ActionListener  
             public void actionPerformed(ActionEvent e)  
                  double width, length, area;  
                  length = Double.parseDouble(lengthTF.getText()); //We use the getText & setText methods to manipulate the data entered into those fields.  
                  width = Double.parseDouble(widthTF.getText());  
                  area = length * width;  
                  areaTF.setText("" + area);  
         public class ExitButtonHandler implements ActionListener  
             public void actionPerformed(ActionEvent e)  
          public static void main(String[] args)  
              RectangleProgram rectObj = new RectangleProgram();  
    Last edited by Raven9; 04-26-2011 at 05:51 AM. Reason: Needed to include GUI I have so far not sure how to add to my code.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Rep Power

Similar Threads

  1. My Inventory Program
    By ladykrimson in forum New To Java
    Replies: 28
    Last Post: 11-01-2010, 09:06 PM
  2. Help with Inventory Program, Please
    By NewKidjava in forum New To Java
    Replies: 4
    Last Post: 08-21-2009, 11:09 AM
  3. Inventory Program Part 2 of 6
    By tlouvierre in forum New To Java
    Replies: 2
    Last Post: 05-28-2009, 01:30 AM
  4. Inventory Program
    By tlouvierre in forum New To Java
    Replies: 5
    Last Post: 05-17-2009, 05:09 AM
  5. Inventory program
    By Nexcompac in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 05:51 PM

Tags for this Thread

Posting Permissions

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