0

I am in my final weeks of my first Java Class. I have the next part of my program due over the weekend and I am stuck. The assignment is as follows:

Modify the Inventory Program to use a GUI. The GUI 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 GUI should display the value of the entire inventory, the additional attribute, and the restocking fee.

I have my code written and it compiles and opens a window, but I can not figure out how to call the information that needs to be displayed. The information that needs to be displayed is what is gets displayed in a command window using System.out.println. Any help would be greatly appreciated.

import java.util.Arrays; 
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JPanel;
class Inventory3 extends JFrame // declares public class inventory 

{ 	

public static void main(String args[]) // starts the program	

  { 
    		
    ExtendDVD[] ExtendDVD = new ExtendDVD[3]; //creates instance of ExtendDVD		
    

		ExtendDVD[0] = new ExtendDVD(1, "Knocked Up", 2, 17.99, "129 Min");// declares a new item number, dvd title, quantity, and price

		ExtendDVD[1] = new ExtendDVD(2, "Napolean Dynamite", 4, 14.99, "82 Min");

		ExtendDVD[2] = new ExtendDVD(3, "Happy Gilmore", 6, 9.99, "92 Min");

               
              
             

		Inventory3 x = new Inventory3();// declare second instance of inventory to sort
		x.SortDVD(ExtendDVD);

                for(int i = 0; i < 3; i++)// loop to call array list and set dvd = i 
              
               {
               
               System.out.println();  
              
               System.out.println("The item number is " + ExtendDVD[i].getItemNumber()); // print item number              
               System.out.println("Product Title is " + ExtendDVD[i].getDvdTitle()); //print dvd title
               System.out.println("The number of units in stock is " + ExtendDVD[i].getOnHand()); //print on hand quantity
               System.out.println("The price of each DVD is $" + ExtendDVD[i].getDvdCost()); //print cost
               System.out.println("The value of this Item is $" + ExtendDVD[i].value());  // print value of all on hand

               System.out.println("The Duration of the Movie is  " + ExtendDVD[i].getTime());//print duration of movie
               System.out.printf( "The Restocking fee is $%.2f\n " , ExtendDVD[i].fee());// print restocking fee
                            
               }           
        
                
               
              
 
               
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Inventory Info");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String text = "getItemNumber";
    JTextArea textAreal = new JTextArea(text, 5, 10);
    textAreal.setPreferredSize(new Dimension(500, 500));
    

    
    JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    textAreal.setLineWrap(true);
    
    frame.add(textAreal);
    
    frame.pack();
    frame.setVisible(true);
             
              
double total = 0;// initiate total inventory as double
             
               for( int counter = 0; counter < ExtendDVD.length; counter++ )// calculate total inventory 
               total += ExtendDVD[ counter ].value();
               System.out.println(); 
               System.out.println( "The Value of All On Hand Inventory is $" + total );//print the total value of the inventory
             
                             
             
 
	}// end main

	public void SortDVD(DVD[] theDVD)// method to sort array dvd 

               {
		for (int index = 0; index < theDVD.length - 1; index++) 
                       
                    {
	             String s1 = theDVD[index].getDvdTitle(); // set title to sort 
		     String s2 = theDVD[index + 1].getDvdTitle();// set title to compare in sort
			if (comparewords(s1, s2)) // compares titles to sort
                       
                    {
		     DVD temp = theDVD[index];// creates temporary instance of dvd
		     theDVD[index] = theDVD[index + 1];
		     theDVD[index + 1] = temp;
		     index = -1;
		    }
		}
	}

	private boolean comparewords(String s1, String s2)// creates boolean and compares the two strings 
            {
	     boolean islarger = false; //continues to sort until true

	     for (int index = 0; index < s1.length(); index++) //loop to determine title length
                     
                    {
		     if (index < s2.length()) 
                       
                    {
		     if (s1.toLowerCase().charAt(index) > s2.toLowerCase().charAt(index)) 
                    {
	             islarger = true;//sort by longer name
	             break;
	            }
			if (s1.toLowerCase().charAt(index) < s2.toLowerCase().charAt(index)) 
                        {
					
		        break;// stop
			}
			}
                        else 
                        { 
		        return true; 
			}
		     }

		return islarger;// return larger moves title higher in the list
	}
        
       
} // end class Inventory2
class DVD // declares class inventory
{
 
        int itemNumber;// declares item number as in
	String dvdTitle;// declares dvd title as string
	int onHand; // declares on hand as int
	double dvdCost;// declares cost as double
       
	public DVD(int stockNumber, String title, int inStock, double price, String studio) // constructor
	{
		itemNumber = stockNumber;// intitiates stock number, title, instock, and
		// price
		dvdTitle = title;
		onHand = inStock;
		dvdCost = price;

	} 

	// set DVD StockNumber //sets stock number
	public void setItemNumber(int stockNumber)

	{
		itemNumber = stockNumber;

	}

	public int getItemNumber() // class item number

	{
		return itemNumber; // returns item number
	} // end method get Dvd Item

	public void setDvdTitle(String title) // set DVD Title

	{
		dvdTitle = title;
	}

	public String getDvdTitle() // gets and returns DVD Title

	{

		return dvdTitle;
	}

	public void setOnHand(int inStock) // Set on hand

	{
		onHand = inStock;
	}

	public int getOnHand() // gets and returns on hand

	{
		return onHand;
	} //

	public void setDvdCost(double price) // sets dvd cost

	{
		dvdCost = price;
	}

	public double getDvdCost() // gets and returns DVD cost

	{
		return dvdCost;
	}

	public double value() // calculates the value of stock

	{
		return dvdCost * onHand;
	}
public String toString() // sets answer to string and outputs itme number, title, on hand, price, and stock value

    {
        
        return String.format("Item Number=%3d  DVD Title=%-10s   On Hand=%d    Price=%.2f   value=%.2f",
                             itemNumber, dvdTitle, onHand, dvdCost, value());
    }

        


} // end class DVD
public class ExtendDVD extends DVD //declares class ExtendDVD that extends class DVD
{
 
 

    private String time = "";//declares string time

    ExtendDVD( int item, String name, int units, double price, String length )//constructor for ExtendDVD
    
     {
       super( item, name, units, price, length);// Call to Super Class DVD
       this.time = length;
     }

     public String getTime() // call to get and return string time
       {
        return time;
       }
     
     public void setTime( String time )// set time
       {
        this.time = time;
       }

     public double fee() //calculate restocking fee
       {
        return 0.05 * getDvdCost();
       }

     
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by peter_budo
0

For quick fix you can do few changes as follows

import java.util.Arrays; 
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JPanel;
class Inventory3 extends JFrame // declares public class inventory 

{ 	

public static void main(String args[]) // starts the program	

  { 
    		
    ExtendDVD[] ExtendDVD = new ExtendDVD[3]; //creates instance of ExtendDVD		
    

		ExtendDVD[0] = new ExtendDVD(1, "Knocked Up", 2, 17.99, "129 Min");// declares a new item number, dvd title, quantity, and price

		ExtendDVD[1] = new ExtendDVD(2, "Napolean Dynamite", 4, 14.99, "82 Min");

		ExtendDVD[2] = new ExtendDVD(3, "Happy Gilmore", 6, 9.99, "92 Min");

               
              
             

		Inventory3 x = new Inventory3();// declare second instance of inventory to sort
		x.SortDVD(ExtendDVD);        
 
               
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Inventory Info");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //String text = "getItemNumber";
    JTextArea textAreal = new JTextArea();
    for(int i = 0; i < 3; i++)// loop to call array list and set dvd = i 
              
               { 
              
               textAreal.append("The item number is " + ExtendDVD[i].getItemNumber()+"\n"); // print item number              
               textAreal.append("Product Title is " + ExtendDVD[i].getDvdTitle()+"\n"); //print dvd title
               textAreal.append("The number of units in stock is " + ExtendDVD[i].getOnHand()+"\n"); //print on hand quantity
               textAreal.append("The price of each DVD is $" + ExtendDVD[i].getDvdCost()+"\n"); //print cost
               textAreal.append("The value of this Item is $" + ExtendDVD[i].value()+"\n");  // print value of all on hand

               textAreal.append("The Duration of the Movie is  " + ExtendDVD[i].getTime()+"\n");//print duration of movie
               textAreal.append( "The Restocking fee is $ " + ExtendDVD[i].fee()+"\n\n");// print restocking fee
                            
               }
               
    double total = 0;// initiate total inventory as double
             
    for( int counter = 0; counter < ExtendDVD.length; counter++ )// calculate total inventory 
          total += ExtendDVD[ counter ].value();
     
    textAreal.append( "\n\nThe Value of All On Hand Inventory is $" + total );//print the total value of the inventory
    
    textAreal.setPreferredSize(new Dimension(500, 500));
    

    
    JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    textAreal.setLineWrap(true);
    
    frame.add(textAreal);
    
    frame.pack();
    frame.setVisible(true);      
              
             
 
	}// end main

	public void SortDVD(DVD[] theDVD)// method to sort array dvd 

               {
		for (int index = 0; index < theDVD.length - 1; index++) 
                       
                    {
	             String s1 = theDVD[index].getDvdTitle(); // set title to sort 
		     String s2 = theDVD[index + 1].getDvdTitle();// set title to compare in sort
			if (comparewords(s1, s2)) // compares titles to sort
                       
                    {
		     DVD temp = theDVD[index];// creates temporary instance of dvd
		     theDVD[index] = theDVD[index + 1];
		     theDVD[index + 1] = temp;
		     index = -1;
		    }
		}
	}

	private boolean comparewords(String s1, String s2)// creates boolean and compares the two strings 
            {
	     boolean islarger = false; //continues to sort until true

	     for (int index = 0; index < s1.length(); index++) //loop to determine title length
                     
                    {
		     if (index < s2.length()) 
                       
                    {
		     if (s1.toLowerCase().charAt(index) > s2.toLowerCase().charAt(index)) 
                    {
	             islarger = true;//sort by longer name
	             break;
	            }
			if (s1.toLowerCase().charAt(index) < s2.toLowerCase().charAt(index)) 
                        {
					
		        break;// stop
			}
			}
                        else 
                        { 
		        return true; 
			}
		     }

		return islarger;// return larger moves title higher in the list
	}
        
       
} // end class Inventory2

However if you looking for more "sophisticated" solution, then you should create a form consisting of labels(JLabel) and text fields (JTetxField) where you would display only one DVD item. Then provide 2 buttons for navigation Next & Previous to move back and forward in your DVD array and provide some buttons to trigger an action of displaying the value of the entire inventory, the additional attribute, and the restocking fee.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.