Inventory Program Part 4

~~** Using the GUI Swing **~~

Page 1 of 1

3 Replies - 2411 Views - Last Post: 27 May 2009 - 02:35 PM Rate Topic: -----

#1 inmate188826  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 18-May 09

Inventory Program Part 4

Post icon  Posted 26 May 2009 - 08:22 PM

I am having a lot of trouble with the next part of my program. This is the code that I have so far and I am supposed to modify this 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.

So here is what I have started. I have no clue what I am doing, and some help getting started would be exceptionally great! The program runs and compiles... I even have a single window that pops up. All my output must go from the console to a windows GUI.

import java.util.Arrays;


// Currently the main class is used to obtain data through code.
// The Public Class Inventory3 works with Game Class to modify Inventory3
// information and  output it accordingly.

public class Inventory4 {

	public static void main(String[] args) {
		
	   
		Game myGame;			
		Inventory inventory = new Inventory();
		GUI gui = new GUI(inventory);
		// New Game Records

		myGame = new Game(27, "NFL: Madden '09", 10, 69.43f);
		inventory.add(myGame);

		myGame = new NewGame(57, "Far Cry", 2, 8.50f, "FPS");	
		inventory.add(myGame);						
				
		myGame = new Game(58, "Far Cry 2", 4, 7.34f);	
		inventory.add(myGame);						
				
		myGame = new Game(39, "NHL 2K9", 8, 59.34f);
		inventory.add(myGame);

		myGame = new NewGame(30, "Lord of the Rings: Conquest", 1, 50.34f, "RPG");
		inventory.add(myGame);

		myGame = new NewGame(41, "Tiger Woods '08", 4, 49.99f, "Sports");
		inventory.add(myGame);

		myGame = new NewGame(32, "Blazing Angels: Squadrons of WWII", 5, 40.34f, "RPG");
		inventory.add(myGame);

		myGame = new NewGame(23, "SmackDown VS. Raw: 2009", 7, 59.44f, "Sports");
		inventory.add(myGame);
		
		//Call Inventory Display Method
		inventory.display();

		System.out.println("End of Inventory3\n");
	} // End Method Main

} // End Public Class Inventory2



// Class Inventory is used to set the inventory as an object into an array. It is used to display
// the contents of each array, as well as sort them alphabetically, in ascending order.
// This class is also responsible for calculating the total amount of inventory.

class Inventory {
	private Game[] games; // the array of games	
	private int nGames;   // number of games in the array

	//Inventory Constructor
	Inventory() {
		games = new Game[25]; // Provide space in memory for games
		nGames = 0;
	}
	
	public int nGames() { return nGames; }


	 //Add Game Method
	public void add(Game game) {
		games[nGames] = game; // insert the game in the array
		nGames = ++nGames;
		sort();			   // sorts the array
	}
	

	// calculates the total amount of inventory.
	public float total() { 
		float tot = 0;
		for (int i = 0; i < nGames; i++)
			tot += games[i].totalPrice();
		return tot;
	}

	// Method to sort games with three part argument
	private void sort() { 
		Arrays.sort(games, 0, nGames);
	}

   


	// Method to display the array information.
	public void display() { 
		System.out.println(
			"Tim's Game Collection Contains " + nGames + " Games\n");
		for (int i = 0; i < nGames; i++)
			System.out.println(games[i]);
		System.out.printf("Total value of the collection is $%,.2f\n\n", total());


	} // end display

} // end class Inventory





class Game implements Comparable {

	private int itemNumber;		private String prodName;
	private int inStock;
	private float itemPrice;
	   
	   //Default Game Constructor
	   Game() {
		itemNumber = inStock = 0;
		prodName   = "";
		itemPrice  = 0;
	}

	// Game constructor
	Game(int itemNumber, String prodName, int inStock, float itemPrice) {
		this.itemNumber = itemNumber;
		this.prodName = prodName;
		this.inStock = inStock;
		this.itemPrice = itemPrice;
	} // end Constructor

	//Set Product Name
	public String getProdName() {
		return prodName;
	} // End Prodname
	
	//Set Item Price
	public float getItemPrice(){
		return itemPrice;
	}
	
	//Set Stock amount
	public int getInStock() {
		return inStock;
	}
  
	// calculate total price method
	public float totalPrice() {
		return inStock * itemPrice;
	} // end totalPrice

	// Set Game String output
	public String toString() {
		return String.format(
				   "	Item Number = %03d	 Description = %-12s\n"
				   + "Number in Stock = %3d	 Unit Price = $%.2f"	 
				   + "	 Total Price = $%.2f\n",
				   itemNumber,
				   prodName,
				   inStock,
				   itemPrice,
				   totalPrice());
	} // end method toString
	
	//Makes product name comparable for sorting
	public int compareTo(Object obj) {
		Game gs = (Game) obj;
		return prodName.compareTo(gs.prodName);
	} // End Compare to

} // End Game Class


//Class NewGame extends its parent class of Games 
//so that an additional feature of each game may be added
//and a restocking fee is calculated and applied to the total price.

class NewGame extends Game {

	private String genre;
			
	//Default contructor
	NewGame() {
		genre = "";   //empty type
	} // End

	//Constructor with 5 arguments
	NewGame(int itemNumber, String prodName, int inStock,
			float itemPrice, String genre) {
		super(itemNumber, prodName, inStock, itemPrice);
		this.genre = genre;
	} // End Constructor

	//Set Genre
	public String getGenre() {
		return genre;
	} // End Genre

	//calculate restock fee
	private float restockFee() {
		return 0.05f * super.getItemPrice();
	}
	//Calculate new total price 
	public float totalPrice() {
		return getInStock() * restockFee() + super.totalPrice();
	}

	// Set Game String output
	public String toString() {
		return String.format("%s Restocking Fee = $%5.2f  Genre = %s\n", 
								super.toString(), restockFee(), genre);
	} // end method toString

} // End Game




import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
	  

class GUI extends JFrame {
 
 Inventory inventory;
 
 GUI(Inventory inventory){
	  
	  JFrame frame = new JFrame("Inventory 4 Program");

	JLabel label = new JLabel("Hello", JLabel.CENTER);
	

	Container contentPane = frame.getContentPane();
	
	contentPane.add(label);
	
	frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

	frame.setSize(300,300);

	frame.setVisible(true);
	
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Inventory Program Part 4

#2 ralph_komer  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 71
  • Joined: 12-May 09

Re: Inventory Program Part 4

Posted 27 May 2009 - 06:30 AM

All you need to do is to create a Frame and pack all the components with desired values in that frame. The best way to go around it is to separate the design in three different tiers:

Presentation Layer
Application Layer
Database/Backend Layer

In the presentation layer, design the GUI using swing and invoke the methods in other classes which will act as the application layer and which in turn will return values to the front end or application layer.

Application layer in turn will communicate to the database for any data processing.

I hope this is what you are looking for. Let me know if you need assistance with your code issues too.
Was This Post Helpful? 0
  • +
  • -

#3 inmate188826  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 18-May 09

Re: Inventory Program Part 4

Posted 27 May 2009 - 02:01 PM

View Postralph_komer, on 27 May, 2009 - 05:30 AM, said:

All you need to do is to create a Frame and pack all the components with desired values in that frame. The best way to go around it is to separate the design in three different tiers:

Presentation Layer
Application Layer
Database/Backend Layer

In the presentation layer, design the GUI using swing and invoke the methods in other classes which will act as the application layer and which in turn will return values to the front end or application layer.

Application layer in turn will communicate to the database for any data processing.

I hope this is what you are looking for. Let me know if you need assistance with your code issues too.



Ralph, Thanks for the help... I got all that figured out already, but I am stuck on my setText() methods... I am trying to set text from my Game Class and some are int, some are strings, floats... but I can only get the strings to work.... Here is a copy of the new GUI Class I have. Another problem I will have is getting my next button to function properly... Other than the setText using floats and int, and the next button, I will be in great shape!! Any advice for me?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



class GUI extends JFrame {

	private JPanel jpOuterPanel; 	// for outermost frame

	private JLabel jlWelcome1, jlWelcome2, jlNGames, jlProdName,
			jlProdNumber, jlStock, jlGenre, jlRestockFee,jlTotalPrice, jlUnitPrice;

	private JTextField jtProdName, jtProdNumber, jtStock, jtGenre,
			jtRestockFee, jtTotalPrice, jtUnitPrice;

	private Inventory inventory;
	private int gameIndex;				///TIM

	GUI(Inventory inventory) {
		super("Tim's Inventory 4 Program");
		this.inventory = inventory;
		gameIndex = 0;					///TIM

		// Set up the outer panel, into which all else will be put
		JPanel jp;

		jpOuterPanel = new JPanel();
		jpOuterPanel.setLayout(new BoxLayout(jpOuterPanel, BoxLayout.Y_AXIS));

		// Set up the "Welcome" display line
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.CENTER));
		jlWelcome1 = new JLabel("Welcome to the Video Game Inventory Program");

		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.CENTER));
		jlWelcome2 = new JLabel("Press Next To View New Product, or Quit to Exit");
		jp.add(jlWelcome1);
		jp.add(jlWelcome2);
		jpOuterPanel.add(jp);

		// Set up the "nGames" display line
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.CENTER));
		String sGames = String.format("My Current Inventory Has %s Games",
									  inventory.getNGames());

		jlNGames = new JLabel(sGames);
		jp.add(jlNGames);
		jpOuterPanel.add(jp);

		// Set up product Number Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlProdNumber = new JLabel("Product Number: ");
		jtProdNumber = new JTextField(2);
		jtProdNumber.setEditable(false);
		jp.add(jlProdNumber);
		jp.add(jtProdNumber);
		jpOuterPanel.add(jp);

		// Set up Product Name Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlProdName = new JLabel("Product Name:  ");
		jtProdName = new JTextField(30);				///TIM
		jtProdName.setEditable(false);					///TIM
		jp.add(jlProdName);
		jp.add(jtProdName);								///TIM
		jpOuterPanel.add(jp);

		// Set Stock Amount Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlStock = new JLabel("Amount in Stock:");
		jp.add(jlStock);
		jpOuterPanel.add(jp);

		// Set Item Price
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlUnitPrice = new JLabel("Unit Price:");
		jp.add(jlUnitPrice);
		jpOuterPanel.add(jp);

		// Set Genre Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlGenre = new JLabel("Genre:  ");
		jp.add(jlGenre);
		jpOuterPanel.add(jp);

		// Set Restock Fee Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlRestockFee = new JLabel("Restocking Fee: ");
		jp.add(jlRestockFee);
		jpOuterPanel.add(jp);

		// Set Total Price Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlTotalPrice = new JLabel("My Inventory's Total Value:  ");
		jp.add(jlTotalPrice);
		jpOuterPanel.add(jp);

		// Set up the Quit and Next buttons
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEADING));
		JButton jbQuit = new JButton("  Quit  ");

		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.TRAILING));
		JButton jbNext = new JButton("  Next  ");

		jbQuit.addActionListener(new QuitButtonHandler());
		jp.add(jbQuit);
		jpOuterPanel.add(jp);
		jp.add(jbNext);
		jpOuterPanel.add(jp);
		
		updateFields();					
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500, 500); // set frame location and size

		// Set up the outer panel; turn it on
		setContentPane(jpOuterPanel);
		setResizable(true);
		setVisible(true);
	}


	private void updateFields() {
		Game game = inventory.getGame(gameIndex);
		jtProdName.setText(game.getProdName());
		jtProdNumber.setText(game.getItemNumber());
	}
	
	class NextButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {}
	} // end EnterKeyHandler class

	class QuitButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			dispose();
			System.exit(0);
		}
	} // end GUI constructor

} // end QuitBottonHandler class

Was This Post Helpful? 0
  • +
  • -

#4 inmate188826  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 18-May 09

Re: Inventory Program Part 4

Posted 27 May 2009 - 02:35 PM

View Postinmate188826, on 27 May, 2009 - 01:01 PM, said:

View Postralph_komer, on 27 May, 2009 - 05:30 AM, said:

All you need to do is to create a Frame and pack all the components with desired values in that frame. The best way to go around it is to separate the design in three different tiers:

Presentation Layer
Application Layer
Database/Backend Layer

In the presentation layer, design the GUI using swing and invoke the methods in other classes which will act as the application layer and which in turn will return values to the front end or application layer.

Application layer in turn will communicate to the database for any data processing.

I hope this is what you are looking for. Let me know if you need assistance with your code issues too.



Ralph, Thanks for the help... I got all that figured out already, but I am stuck on my setText() methods... I am trying to set text from my Game Class and some are int, some are strings, floats... but I can only get the strings to work.... Here is a copy of the new GUI Class I have. Another problem I will have is getting my next button to function properly... Other than the setText using floats and int, and the next button, I will be in great shape!! Any advice for me?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



class GUI extends JFrame {

	private JPanel jpOuterPanel; 	// for outermost frame

	private JLabel jlWelcome1, jlWelcome2, jlNGames, jlProdName,
			jlProdNumber, jlStock, jlGenre, jlRestockFee,jlTotalPrice, jlUnitPrice;

	private JTextField jtProdName, jtProdNumber, jtStock, jtGenre,
			jtRestockFee, jtTotalPrice, jtUnitPrice;

	private Inventory inventory;
	private int gameIndex;				///TIM

	GUI(Inventory inventory) {
		super("Tim's Inventory 4 Program");
		this.inventory = inventory;
		gameIndex = 0;					///TIM

		// Set up the outer panel, into which all else will be put
		JPanel jp;

		jpOuterPanel = new JPanel();
		jpOuterPanel.setLayout(new BoxLayout(jpOuterPanel, BoxLayout.Y_AXIS));

		// Set up the "Welcome" display line
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.CENTER));
		jlWelcome1 = new JLabel("Welcome to the Video Game Inventory Program");

		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.CENTER));
		jlWelcome2 = new JLabel("Press Next To View New Product, or Quit to Exit");
		jp.add(jlWelcome1);
		jp.add(jlWelcome2);
		jpOuterPanel.add(jp);

		// Set up the "nGames" display line
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.CENTER));
		String sGames = String.format("My Current Inventory Has %s Games",
									  inventory.getNGames());

		jlNGames = new JLabel(sGames);
		jp.add(jlNGames);
		jpOuterPanel.add(jp);

		// Set up product Number Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlProdNumber = new JLabel("Product Number: ");
		jtProdNumber = new JTextField(2);
		jtProdNumber.setEditable(false);
		jp.add(jlProdNumber);
		jp.add(jtProdNumber);
		jpOuterPanel.add(jp);

		// Set up Product Name Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlProdName = new JLabel("Product Name:  ");
		jtProdName = new JTextField(30);				///TIM
		jtProdName.setEditable(false);					///TIM
		jp.add(jlProdName);
		jp.add(jtProdName);								///TIM
		jpOuterPanel.add(jp);

		// Set Stock Amount Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlStock = new JLabel("Amount in Stock:");
		jp.add(jlStock);
		jpOuterPanel.add(jp);

		// Set Item Price
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlUnitPrice = new JLabel("Unit Price:");
		jp.add(jlUnitPrice);
		jpOuterPanel.add(jp);

		// Set Genre Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlGenre = new JLabel("Genre:  ");
		jp.add(jlGenre);
		jpOuterPanel.add(jp);

		// Set Restock Fee Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlRestockFee = new JLabel("Restocking Fee: ");
		jp.add(jlRestockFee);
		jpOuterPanel.add(jp);

		// Set Total Price Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlTotalPrice = new JLabel("My Inventory's Total Value:  ");
		jp.add(jlTotalPrice);
		jpOuterPanel.add(jp);

		// Set up the Quit and Next buttons
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEADING));
		JButton jbQuit = new JButton("  Quit  ");

		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.TRAILING));
		JButton jbNext = new JButton("  Next  ");

		jbQuit.addActionListener(new QuitButtonHandler());
		jp.add(jbQuit);
		jpOuterPanel.add(jp);
		jp.add(jbNext);
		jpOuterPanel.add(jp);
		
		updateFields();					
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500, 500); // set frame location and size

		// Set up the outer panel; turn it on
		setContentPane(jpOuterPanel);
		setResizable(true);
		setVisible(true);
	}


	private void updateFields() {
		Game game = inventory.getGame(gameIndex);
		jtProdName.setText(game.getProdName());
		jtProdNumber.setText(game.getItemNumber());
	}
	
	class NextButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {}
	} // end EnterKeyHandler class

	class QuitButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			dispose();
			System.exit(0);
		}
	} // end GUI constructor

} // end QuitBottonHandler class




Actually, Ralph, I just need help with the NEXT button and grabbing methods from my NewGame Class.. I have the rest figured out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1