GUI (NEED INSTANT HELP...PLEASE)

displaying GUI for a vending machine

Page 1 of 1

2 Replies - 1776 Views - Last Post: 24 May 2008 - 10:38 PM Rate Topic: -----

#1 sktr4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-May 08

GUI (NEED INSTANT HELP...PLEASE)

Post icon  Posted 24 May 2008 - 09:38 AM

ive doing a homework which we need to provide buttons (GUI) for a vending machine.
the vending machine takes dollars, quaters, dimes, and nickels, and the vending machine has drinks (60 cents) and snacks (45 cents).

we done a code were it does work through "SCANNER", but now we have to create a GUI for it.

/**
 * VendingMachineCUI: A program that emulates a vending machine. It uses a 
 * VendingMachine object to represent a vending machine, and interacts with 
 * the user through the console.
 */ 

import java.util.*;		// for Scanner class



public class VendingMachineCUI
{
	public static void main (String[] args)
	{
		char	action;				// Store user's input characters
		String	strDrinkAvailable, 	// whether drink is sold out
				strSnackAvailable;	// whether snack is sold out
		Change	change;				// Used to keep a change
		
		// Create a Scanner object for input
		Scanner keyboard = new Scanner (System.in);
		
		// Create a vending machine object
		VendingMachine vm = new VendingMachine ();
		
		while (true)
		{
			if (vm.isItemAvailable (VendingMachine.ITEM_TYPE_DRINK))
				strDrinkAvailable = "";
			else
				strDrinkAvailable = " (sold out)";
			
			if (vm.isItemAvailable (VendingMachine.ITEM_TYPE_SNACK))
				strSnackAvailable = "";
			else
				strSnackAvailable = " (sold out)";
			
			// Display the menu
			System.out.println (
				"A - Drink" + strDrinkAvailable + "\n" + 
				"B - Snack" + strSnackAvailable + "\n\n" + 
				
				"D - Enter a dollar\n" + 
				"Q - Enter a quarter\n" + 
				"M - Enter a dime\n" + 
				"N - Enter a nickel\n\n" + 
				
				"R - Return change\n\n");
			
			// Display the amount entered in the machine
			System.out.println ("Amount available: " + vm.getAmountAvailable () + "\n\n");
			
			// Get user's action
			System.out.print ("Your action: ");
			action = keyboard.next ().charAt (0);
			action = Character.toUpperCase (action);
			
			// Process
			switch (action)
			{
			case 'A':
				if (! vm.isItemAvailable (VendingMachine.ITEM_TYPE_DRINK))
					System.out.println ("Sorry, it's sold out");
				else if (! vm.isMoneyEnough (VendingMachine.ITEM_TYPE_DRINK))
					System.out.println ("Sorry, not enough money for the purchase");
				else if (vm.buyItem (VendingMachine.ITEM_TYPE_DRINK))
					System.out.println ("Here is your drink");
				break;
			case 'B':
				if (! vm.isItemAvailable (VendingMachine.ITEM_TYPE_SNACK))
					System.out.println ("Sorry, it's sold out");
				else if (! vm.isMoneyEnough (VendingMachine.ITEM_TYPE_SNACK))
					System.out.println ("Sorry, not enough money for the purchase");
				else if (vm.buyItem (VendingMachine.ITEM_TYPE_SNACK))
					System.out.println ("Here is your snack");
				break;
			case 'D':
				vm.inputDollar ();
				break;
			case 'Q':
				vm.inputQuarter ();
				break;
			case 'M':
				vm.inputDime ();
				break;
			case 'N':
				vm.inputNickel ();
				break;
			case 'R':
				change = vm.makeChange ();
				
				// Display the coins to the user 
				System.out.println (
					"# of quarters: " + change.getQuarters () + "\n" + 
					"# of dimes: " + change.getDimes () + "\n" + 
					"# of nickles: " + change.getNickels () + "\n");
				
				break;
			
			default:
				System.out.println ("Sorry, " + action + " is an invalid option");
				break;		// simply do nothing for a invalid input	
			
			}	// end of switch
			
			System.out.println ("\n");		// get a vertical space
			
					
		} // end of while
	}
}



thats the scanner one, it does work (since i have other programs which displays the change, and everyything else).

but i dont know where to start...

ive done one program with GUI, and was still strugglin about it.

i know the basics, such as...

inputing

 
import java.awt.*; 		
import java.awt.event.*;


to activate the whole "event" thing through the eAction..

any help will be appreciateed...

thanks.

This post has been edited by sktr4life: 25 May 2008 - 12:41 AM


Is This A Good Question/Topic? 0
  • +

Replies To: GUI (NEED INSTANT HELP...PLEASE)

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: GUI (NEED INSTANT HELP...PLEASE)

Posted 24 May 2008 - 03:17 PM

Google java guy tutorial
or
Google java swing tutorial

or look a the posts in this forum you'll find a lot of examples
Was This Post Helpful? 0
  • +
  • -

#3 sktr4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-May 08

Re: GUI (NEED INSTANT HELP...PLEASE)

Posted 24 May 2008 - 10:38 PM

thanks...but its still strugglin me...

heres the other codes for the program...

/**
 * Change.java: Defines a class Change. A Change object represents change in
 * numbers of quarters, dimes, and nickels. 
 */

public class Change
{
	// private data members
	private int numQuarters, numDimes, numNickels;
	
	/**
	 * Initializes numbers of quarters, dimes and nickels to 0
	 */
	public Change ()
	{
		numQuarters = numDimes = numNickels = 0;
	}	
	
	/**
	 * Initializes numbers of quarters, dimes nickels according to the given values.
	 * 
	 * It assumes that inQuarters, inDimes and inNickels are all >= 0
	 */
	public Change (int inQuarters, int inDimes, int inNickels)
	{
		numQuarters = inQuarters;
		numDimes = inDimes;
		numNickels = inNickels;
	}
	
	/**
	 * Sets numQuarters, numDimes, and numNickels based on parameter amount, 
	 * which contains the amount of change to be made. 
	 *
	 * Sets all to 0 if amount is <= 0
	 */
	public Change (int amount)
	{
		if (amount <= 0)
		{
			numQuarters = numDimes = numNickels = 0;
			return;
		}
		
		// Calculate and set the changes based on amount
		numQuarters = amount / 25;
		amount = amount % 25;
		
		numDimes = amount / 10;
		amount = amount % 10;
		
		numNickels = amount / 5;
	}
	
	/**
	 * Returns number of quarters
	 */
	public int getQuarters ()
	{
		return numQuarters;
	}
	
	/**
	 * Returns number of dimes
	 */
	public int getDimes ()
	{
		return numDimes;
	}
	
	/**
	 * Returns number of nickels
	 */
	public int getNickels ()
	{
		return numNickels;
	} 
}


/**Ken Randle
 * This file contains the definition of VendingMachine class.
 */

 
public class VendingMachine // for ActionListener
{
		
	// Constants
	public static final int DEFAULT_NUM_DRINKS = 100;
	public static final int DEFAULT_NUM_SNACKS = 100;
	
	public static final int ITEM_TYPE_DRINK = 0;
	public static final int ITEM_TYPE_SNACK = 1;
	
	public static final int PRICE_OF_DRINK = 60;
	public static final int PRICE_OF_SNACK = 45;
	
	// Instance variables
	private int amountAvailable;	// amount of money for purchasing, in 
									// # of cents
	
	
	private int numDrinks;
	private int numSnacks;

	/**
	 * Default constructor: sets amountAvailable to 0
	 */
	public VendingMachine ()
	{
		amountAvailable = 0;
		
		
		numDrinks = DEFAULT_NUM_DRINKS;
		numSnacks = DEFAULT_NUM_SNACKS;
	}
	
	public VendingMachine (int inNumDrinks, int inNumSnacks)
	{
		
		 numDrinks = inNumDrinks;
		 numSnacks = inNumSnacks;
		
		amountAvailable = 0;
	}
	
	//Adds 100 cents
	public void inputDollar ()
	{
		amountAvailable += 100;
	}
	
	//adds 25 cents
	public void inputQuarter ()
	{
		amountAvailable += 25;
	}
	
	//adds 10 cents
	public void inputDime ()
	{
		amountAvailable += 10;
	}
		
	//Adds 5 cents to the available amount
	public void inputNickel ()
	{
		amountAvailable += 5;
	}
	
	//returns amount in cents
	public int getAmountAvailable()
	{
		return amountAvailable;
	}
	
	//Returns the number of Items in stock
	public int getNumberOfDrinks()
	{
		return numDrinks;	
	}
	
	public int getNumberOfSnacks()
	{
		return numSnacks;	
	}

	//Returns the number of items in stock
	public boolean isItemAvailable (int type)
	{
	
		
		if (type == ITEM_TYPE_DRINK)
		{
			if (numDrinks > 0)
				return true;
			else
				return false;
		}
		else if (type == ITEM_TYPE_SNACK)
		{
			if (numSnacks > 0)
				return true;
			else
				return false;
		}
		
	
	else
		return false;
	} 

	
	public boolean isMoneyEnough (int type) 
	{
		if (type == ITEM_TYPE_DRINK)
		{
			if (amountAvailable >= PRICE_OF_DRINK)
				return true;
			else
				return false;
		}
		else if (type == ITEM_TYPE_SNACK)
		{
			if (amountAvailable >= PRICE_OF_SNACK)
				return true;
			else
				return false;
		}
		else
			return false;
	}	

	public boolean buyItem (int type)
	{
	
		if (type == ITEM_TYPE_DRINK)
		{
			numDrinks --;
			amountAvailable = amountAvailable - PRICE_OF_DRINK;
			return true;
		}
		else if (type == ITEM_TYPE_SNACK)
		{
			numSnacks --;
			amountAvailable = amountAvailable - PRICE_OF_SNACK;
			return true;
		}
		else
			return false;
	}

	private void displayStatus()
	{
		System.out.println("The amount available: " + amountAvailable);
		System.out.println("The number of drinks available: " + numDrinks);
		System.out.println("The number of snacks available: " + numSnacks);
	}


	
	/**
	 * makeChange: calculate numbers of quarters, dimes and nickles to return
	 */
	public Change makeChange () 	// Hongyan; removed;
	{		
		// Make a Change object based on amountAvailable
		Change change = new Change (amountAvailable);
		
		// Reset amountAvailable to 0
		amountAvailable = 0;
		
		// Return the Change object
		return change;
	}

}



those two are the fixed ones...

this is what i have to fix... this is what i have started...

/**
 * VendingMachineCUI: A program that emulates a vending machine. It uses a 
 * VendingMachine object to represent a vending machine, and interacts with 
 * the user through the console.
 */ 

import javax.swing.*; 	
import java.awt.*; 		// for Container and Color
import java.awt.event.*;	// for events and listener classes



public class VendingMachineCUI 
{
	public static void main(String[] args) { 
		ButtonFrame frame = new ButtonFrame();  
		 // Set it so it teminates program when closed	
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Set the windows size and then show it.  
		frame.setSize (300,300);  
		frame.setVisible(true);  
	}
}
	
public class VendingMachineCUI extends JFrame implements ActionListener
{

		  // Window components, buttons
	  private JButton btdollar = new JButton("dollar");	
   	private JButton btquarter= new JButton("quarter");
   	private JButton btdime = new JButton("dime");
   	private JButton btnickel = new JButton("nickel");
   	private JButton btdrink = new JButton ("drink");
   	private JButton	btsnack = new JButton ("snack"); 
		  
	
	  // Define window size, etc
	  public static final int WIDTH = 300;
	  public static final int HEIGHT = 120;
	  public static final int LOCATION_X = 200;
	  public static final int LOCATION_Y = 200;
	  
	  
	  // Define the sizes and locations of components
	  public static final int START_X = 20;		// left margine 
	  public static final int START_Y = 10;		// top margine
	  public static final int SPACE_X = 20;		// horizontal space between components
	  public static final int SPACE_Y = 5;		// vertical space between components
	  public static final int LABEL_WIDTH = 80;
	  public static final int LABEL_HEIGHT = 20;	// height of all components
	  public static final int TEXT_WIDTH = 120;
	  public static final int BT_WIDTH = 80;
	  
  //constructor
  public VendingMachineCUI ()
  {
	  //set properties for windows...
	  setTitle ("Vending Machine");
	  setSize (WIDTH, HEIGHT);
	  setLocation (LOCATION_X, LOCATION_Y);
	  setResizable (false);
	  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  
	  //get window content pane
	  Container contentPane = getContantPane ();
	  contentPane.setLayout (null); 
		  
		  int x, y;
		  
		  //labels
		  x = START_X;
		  y = START_Y;
  }

	  
	
	
	
	/*public static void main (String[] args)
	{
		char	action;				// Store user's input characters
		String	strDrinkAvailable, 	// whether drink is sold out
				strSnackAvailable;	// whether snack is sold out
		Change	change;				// Used to keep a change
		
		// Create a Scanner object for input
		Scanner keyboard = new Scanner (System.in);
		
		// Create a vending machine object
		VendingMachine vm = new VendingMachine ();
		
		
		
		while (true)
		{
			if (vm.isItemAvailable (VendingMachine.ITEM_TYPE_DRINK))
				strDrinkAvailable = "";
			else
				strDrinkAvailable = " (sold out)";
			
			if (vm.isItemAvailable (VendingMachine.ITEM_TYPE_SNACK))
				strSnackAvailable = "";
			else
				strSnackAvailable = " (sold out)";
			
			// Display the menu
			System.out.println (
				"A - Drink" + strDrinkAvailable + "\n" + 
				"B - Snack" + strSnackAvailable + "\n\n" + 
				
				"D - Enter a dollar\n" + 
				"Q - Enter a quarter\n" + 
				"M - Enter a dime\n" + 
				"N - Enter a nickel\n\n" + 
				
				"R - Return change\n\n");
			
			// Display the amount entered in the machine
			System.out.println ("Amount available: " + vm.getAmountAvailable () + "\n\n");
			
			// Get user's action
			System.out.print ("Your action: ");
			action = keyboard.next ().charAt (0);
			action = Character.toUpperCase (action);
			
			// Process
			switch (action)
			{
			case 'A':
				if (! vm.isItemAvailable (VendingMachine.ITEM_TYPE_DRINK))
					System.out.println ("Sorry, it's sold out");
				else if (! vm.isMoneyEnough (VendingMachine.ITEM_TYPE_DRINK))
					System.out.println ("Sorry, not enough money for the purchase");
				else if (vm.buyItem (VendingMachine.ITEM_TYPE_DRINK))
					System.out.println ("Here is your drink");
				break;
			case 'B':
				if (! vm.isItemAvailable (VendingMachine.ITEM_TYPE_SNACK))
					System.out.println ("Sorry, it's sold out");
				else if (! vm.isMoneyEnough (VendingMachine.ITEM_TYPE_SNACK))
					System.out.println ("Sorry, not enough money for the purchase");
				else if (vm.buyItem (VendingMachine.ITEM_TYPE_SNACK))
					System.out.println ("Here is your snack");
				break;
			case 'D':
				vm.inputDollar ();
				break;
			case 'Q':
				vm.inputQuarter ();
				break;
			case 'M':
				vm.inputDime ();
				break;
			case 'N':
				vm.inputNickel ();
				break;
			case 'R':
				change = vm.makeChange ();
				
				// Display the coins to the user 
				System.out.println (
					"# of quarters: " + change.getQuarters () + "\n" + 
					"# of dimes: " + change.getDimes () + "\n" + 
					"# of nickles: " + change.getNickels () + "\n");
				
				break;
			
			default:
				System.out.println ("Sorry, " + action + " is an invalid option");
				break;		// simply do nothing for a invalid input	
			
			}	// end of switch
			
			System.out.println ("\n");		// get a vertical space
			
					
		} // end of while
	}
}*/



thanks...

This post has been edited by sktr4life: 24 May 2008 - 10:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1