3 Replies - 607 Views - Last Post: 18 December 2008 - 09:13 PM Rate Topic: -----

#1 aleechia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-December 08

Code not working specifically the pricing

Posted 18 December 2008 - 08:52 PM

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

public class JPizza extends JFrame implements ActionListener
{
	JRadioButton Small, Medium, Large, XLarge;
	JCheckBox cheese, pepperoni, sausage, ham, mushroom, pineapple;
	JButton order, clear, exit;
	JPanel westPanel, eastPanel, lowerPanel;

	public JPizza()
	{
		super("Order Entry System");
		Container con = getContentPane();
		con.setLayout(new BorderLayout());

		Small = new JRadioButton("Small");
		Medium = new JRadioButton("Medium");
		Large = new JRadioButton("Large");
		XLarge = new JRadioButton("X-Large");
		westPanel = new JPanel(new GridLayout(5,1,10,10));
		westPanel.add(Small);
		westPanel.add(Medium);
		westPanel.add(Large);
		westPanel.add(XLarge);
		con.add(westPanel, BorderLayout.WEST);


		cheese = new JCheckBox("cheese");
		pepperoni = new JCheckBox("pepperoni");
		sausage = new JCheckBox("sausage");
		ham = new JCheckBox("ham");
		mushroom = new JCheckBox("mushroom");
		pineapple = new JCheckBox("pineapple");
		eastPanel = new JPanel(new GridLayout(5,1,10,10));
		eastPanel.add(cheese);
		eastPanel.add(pepperoni);
		eastPanel.add(sausage);
		eastPanel.add(ham);
		eastPanel.add(mushroom);
		eastPanel.add(pineapple);
		con.add(eastPanel, BorderLayout.EAST);

		order = new JButton("Place Order");
		clear = new JButton("Clear Choices");
		exit = new JButton("Exit System");
		lowerPanel = new JPanel(new GridLayout(1, 3, 5, 5));
		lowerPanel.add(order);
		lowerPanel.add(clear);
		lowerPanel.add(exit);
		con.add(lowerPanel, BorderLayout.SOUTH);

		JLabel heading = new JLabel("Delicious"); 
		heading.setFont(new Font("Helvetica", Font.BOLD | Font.ITALIC, 28));
		con.add(heading, BorderLayout.NORTH);

		order.addActionListener(this);
		clear.addActionListener(this);
		exit.addActionListener(this);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(360, 250);
		setLocation(400, 250);
		setVisible(true);
		clearUI();

	}


	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if (source == order)
			placeOrder();
		else if (source == clear)
			clearUI();
		else if (source == exit)
			System.exit(0);
	}


	private void clearUI()
	{
		Small.setSelected(false);
		Medium.setSelected(false);
		Large.setSelected(false);
		XLarge.setSelected(false);
		cheese.setSelected(false);
		pepperoni.setSelected(false);
		sausage.setSelected(false);
		ham.setSelected(false);
		mushroom.setSelected(false);
		pineapple.setSelected(false);

	}
	private void placeOrder()
	{

		int PiePrice;
		int ToppingPrice;
		int TotalPrice;


		String size;
			if (Small.isSelected())
			{
				size = "Small ";
				PiePrice = 7;
			}
			else if (Medium.isSelected())
			{
				size = "Medium ";
				PiePrice = 9;
			}
			else if (Large.isSelected())
			{

				size = "Large ";
				PiePrice = 11;
			}
			else
			{
				size = "X-Large";
				PiePrice = 14;
			}

		String Topping = "with";
			if (cheese.isSelected())
			{
				Topping += " Cheese";
				ToppingPrice = 0;
			}
			if (pepperoni.isSelected())
			{
				Topping += " Pepperoni";
				ToppingPrice  = 1;
			}
			if (sausage.isSelected())
				Topping += " suasage";
				{ToppingPrice = 1;
			}
			if (ham.isSelected())
			{
				Topping += " ham";
				ToppingPrice = 1;
			}
			if (mushroom.isSelected())
			{
				Topping += " Mashroom";
				ToppingPrice = 1;
			}
			if (pineapple.isSelected())
			{
				Topping += " Pineapple";
				ToppingPrice = 1;
			}

			TotalPrice = PiePrice + ToppingPrice;

			System.out.println(size + Topping + " Total Price is: $" + TotalPrice);
			clearUI();
	}
	public static void main(String[] args)
	{
			JPizza mypie = new JPizza();
	}

}



*Edited to add the [ code] tags. Please :code:

This post has been edited by pbl: 18 December 2008 - 09:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Code not working specifically the pricing

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Code not working specifically the pricing

Posted 18 December 2008 - 08:56 PM

What problems are you having?
-->Compile errors?
-->Runtime errors?
-->Logic errors?
-->etc.

Also :code:
Was This Post Helpful? 0
  • +
  • -

#3 KeyboardKowboy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 142
  • Joined: 15-December 08

Re: Code not working specifically the pricing

Posted 18 December 2008 - 09:10 PM

If you're having problems with pricing because of adding topping prices... the problem is that you are continually setting ToppingPrice = 1. You should be setting ToppingPrice += 1, to take the current value of ToppingPrice and adding 1 to it for each added item. You are already applying this method with the ToppingPrice string... When you take the sum of PiePrice and ToppingPrice, your total should then be correct.

Also, your code was difficult to read... please use the code tags in your future posts.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Code not working specifically the pricing

Posted 18 December 2008 - 09:13 PM

View PostBigAnt, on 18 Dec, 2008 - 07:56 PM, said:

What problems are you having?
-->Compile errors?
-->Runtime errors?
-->Logic errors?
-->etc.

Also :code:


Admins when will we have a tag code like [ rules][ /rules] for that type of reply ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1