candymachine in java

can anyone help me with this cause there are in this program. . .

Page 1 of 1

1 Replies - 1296 Views - Last Post: 29 September 2010 - 03:50 AM Rate Topic: -----

#1 sukarno1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-September 10

candymachine in java

Posted 29 September 2010 - 01:03 AM

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

public class Sukarno_chap8candy extends JFrame
{
	private static final int WIDTH = 300;
	private static final int HEIGHT = 300;
	
	//STEP 1 VARIABLES
	 private CashRegister cashRegister = new CashRegister();
	 private Dispenser candy = new Dispenser(100, 50);
	 private Dispenser chips = new Dispenser(100, 65);
	 private Dispenser gum = new Dispenser(75, 45);
	 private Dispenser cookies = new Dispenser(100, 85);
	
	private JLabel headingMainL;
	private JLabel selectionL;
	
	private JButton exitB, candyB, chipsB, gumB, cookiesB;
	private ButtonHandler pbHandler;
	
	public Sukarno_chap8candy()
	{
		setTitle("Candy Machine");//SET THE WINDOW TITLE
		setSize(WIDTH, HEIGHT);//SET THE WINDOW SIZE
		
		Container pane = getContentPane(); //GET THE CONTAINER
		pane.setLayout(new GridLayout(7, 1)); //SET THE PANE LAYOUT
		
		pbHandler = new ButtonHandler();// INSTANTIATE THE LISTENER
										// OBJECT
		headingMainL = new JLabel ("WELCOM TO SHELLY S CANDY SHOP",
		SwingConstants.CENTER); //INSTATIATE
								//THE FIRST LABEL
		selectionL = new JLabel("To Make a Selection, " + "Click on the Product Button",
		SwingConstants.CENTER); // INSTANTIATE
								//THE SECOND LABEL
								
		pane.add(headingMainL); // add the first label to the pane
		pane.add(selectionL); // add the second label to the pane
		
		candyB = new JButton("Candy"); // instantiate the candy
										// button
		
		candyB.addActionListener(pbHandler); // register the listener to candy button
		
		chipsB = new JButton ("Chips"); // instatiate the chips button
		chipsB.addActionListener(pbHandler);// register the listener to the chips button
		
		gumB = new JButton ("Gum"); // instatiate the gum button
		gumB.addActionListener(pbHandler);// register the listener to the gum button
		
		cookiesB = new JButton ("Cookies"); // instatiate the cookies button
		cookiesB.addActionListener(pbHandler);// register the listener to the cookies button
		
		
		exitB = new JButton ("Exit"); // instatiate the exit button
		exitB.addActionListener(pbHandler);// register the listener to the exit button
		
		pane.add(candyB); // add the candy button to the pane
		pane.add(chipsB); // add the chips button to the pane
		pane.add(gumB); // add the gum button to the pane
		pane.add(cookiesB); // add the cookies button to the pane
		pane.add(exitB); // add the exit button to the pane
				
		setVisible(true); // show the window and its contents
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
	} //end constructor
	
	//class to hanlde the button events
	private class ButtonHandler implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			if(e.getActionCommand().equals("Exit"))
			System.exit(0);
			else if (e.getActionCommand().equals("Candy"))
			sellProduct(candy, "Candy");
			else if (e.getActionCommand().equals("Chips"))
			sellProduct(chips, "Chips");
			else if (e.getActionCommand().equals("Gum"))
				sellProduct(gum, "Gum");
			else if (e.getActionCommand().equals("Cookies"))
				sellProduct(cookies, "Cookies");
				
		}
	}
	
	//Method to sell a product
	private void sellProduct(Dispenser product, String productName)
	{
		int coinsInserted = 0;
		int price, coinsRequired;
		String str;
		
		if(product.getCount() > 0)
		{
			price = product.getProductCost();
			coinsRequired = price - coinsInserted;
			
			while (coinsRequired > 0)
			{
				str = JOptionPane.showInputDialog("To buy"
				+ productName
				+ "please insert"
				+ coinsRequired + " cents " );
				
				coinsInserted = coinsInserted + Integer.parseInt(str);
				
				coinsRequired = price - coinsInserted;
				
			}
			
			cashRegister.acceptAmount(coinsInserted);
			product.makeSale();
			
			JOptionPane.showMessageDialog(null,"Please pick up your "
			+ productName + " and enjoy",
			"Thank you, come again!",
			JOptionPane.PLAIN_MESSAGE);
			
		
	}
	else
	
		JOptionPane.showMessageDialog(null,"Sorry"
			+ productName 
			+ "is sold out\n"
			+ "Make another selection",
			"Thank you, come again!",
			JOptionPane.PLAIN_MESSAGE);
			
			
			
		}//end sellProduct
		public static void main(String [] args)
		
		{
			Sukarno_chap8candy candyShop = new Sukarno_chap8candy();
			
		}
	}


This post has been edited by Dogstopper: 29 September 2010 - 03:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: candymachine in java

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: candymachine in java

Posted 29 September 2010 - 03:50 AM

:code:

What's wrong with your program that you don't expect? What is happening that you don't understand. Nobody is going to dive through a wall of code and not know what's wrong with it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1