Next Button/Getting a method

Inventory 4 ~*Next button/Restock fee Help*~

Page 1 of 1

1 Replies - 593 Views - Last Post: 29 May 2009 - 01:54 AM Rate Topic: -----

#1 inmate188826  Icon User is offline

  • New D.I.C Head

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

Next Button/Getting a method

Post icon  Posted 28 May 2009 - 10:28 PM

I need help with two things. First, my next button needs to cycle through the inventory of the array, and start back to the beginning when it reaches the end. It does this, but I have to click the button twice once i'm at the end. Another problem I am having is getting my restockFee() method from New Game class to display in my GUI. I know it goes in my if...else statement, but I cannot get it to work no matter how I try!! Someone save me please!!

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();

		// New Game Records

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

		myGame = new NewGame(57, "Far Cry", 2, 18.50f, "FPS");	
		inventory.add(myGame);						
				
		myGame = new Game(58, "Far Cry 2", 4, 17.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();
		GUI gui = new GUI(inventory);

		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 getNGames() { 
		return nGames; 
	}
	
	public Game getGame(int i) {
		return games[i];
	}

	// 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 class Inventory



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

//Class GUI displays the information from the Inventory, Game and New Game
//classes into a graphical interface with working buttons. 
class GUI extends JFrame {

	private JPanel jpOuterPanel; 	// for outermost frame

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

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

	private Inventory inventory;
	private int gameIndex;				

	//GUI Constructor
	GUI(Inventory inventory) {
		super("Tim's Inventory 4 Program");
		this.inventory = inventory;
		gameIndex = 0;					
		// 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");
		 
		 // Welcome2 display line
		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());

		 //Add games
		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(3);
		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);				
		jtProdName.setEditable(false);					
		jp.add(jlProdName);
		jp.add(jtProdName);						
		jpOuterPanel.add(jp);

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

		// Set Item Price
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlUnitPrice = new JLabel("Unit Price:  ");
		jtUnitPrice = new JTextField(6);
		jtUnitPrice.setEditable(false);
		jp.add(jlUnitPrice);
		jp.add(jtUnitPrice);
		jpOuterPanel.add(jp);
		
		// Set Total Game Price
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlTotalPrice = new JLabel("Total Price: ");
		jtTotalPrice = new JTextField(6);
		jtTotalPrice.setEditable(false);
		jp.add(jlTotalPrice);
		jp.add(jtTotalPrice);
		jpOuterPanel.add(jp);

		// Set Genre Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlGenre = new JLabel("Genre: ");
		jtGenre = new JTextField(7);			///TIM
		jtGenre.setEditable(false);				///TIM
		jp.add(jlGenre);
		jp.add(jtGenre);						
		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 Inventory Value Label
		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		jlInventoryValue = new JLabel("My Inventory's Total Value:  ");
		jtInventoryValue = new JTextField(7);
		jtInventoryValue.setEditable(false);
		jp.add(jlInventoryValue);
		jp.add(jtInventoryValue);
		jpOuterPanel.add(jp);

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

		jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.TRAILING));
		JButton jbQuit = new JButton("  Quit  ");

		
		jbNext.addActionListener(new NextButtonHandler());		
		jbQuit.addActionListener(new QuitButtonHandler());

		jp.add(jbNext);
		jpOuterPanel.add(jp);
		jp.add(jbQuit);
		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);
	}

   //Update the display information fields when next button is pressed
	private void updateFields() {
		Game game = inventory.getGame(gameIndex);
		jtProdName.setText(game.getProdName());
		jtProdNumber.setText("" + game.getItemNumber());
		jtStock.setText("" + game.getInStock());
		jtUnitPrice.setText(String.format("$%.2f", game.getItemPrice()));
	   jtTotalPrice.setText(String.format("$%.2f", game.totalPrice()));
		
		//Game instances of NewGame
		if (game instanceof NewGame) {			
			NewGame ng = (NewGame) game;
			jtGenre.setText(ng.getGenre());
			jtTotalPrice.setText(String.format("$%.2f", ng.totalPrice()));
		
		}//end if
		 else {
			jtGenre.setText("");
			jtTotalPrice.setText(String.format("$%.2f", game.totalPrice()));  
			
					}//end else
			//Set Inventory Value display
			jtInventoryValue.setText(String.format("$%,.2f", inventory.total()));

	}//end Updatefields
	
	//This inner class instructs the GUIs next button to scroll through
	//elements of the array.
	class NextButtonHandler implements ActionListener {

	   public void actionPerformed(ActionEvent e) {
			if(gameIndex == inventory.getNGames())
				   gameIndex = 0;
			else ++gameIndex;		
			updateFields();		
		}//End NextButtonHandler method
			   }//end nextbuttonhandler class
			   
	//Inner class defines the actions of the quit button.
	class QuitButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			dispose();
			System.exit(0);
		}//end QuitButtonHandler Method
	} // end QuitButtonHandler class

} // end GUI class



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
	
	public int getItemNumber() {
		return itemNumber;
	   }
	
	// 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();
	}
	
	public float getRestockFee(){
		return restockFee(); }

	// 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



Is This A Good Question/Topic? 0
  • +

Replies To: Next Button/Getting a method

#2 Hurshai  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 14-September 08

Re: Next Button/Getting a method

Posted 29 May 2009 - 01:54 AM

On my first glance of your code your comments are screwed. I think you have getters and setters confused. Also the Game constructor:

itemNumber = inStock = 0


?

Sorry, I'm just having a rough time following your code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1