Results 1 to 5 of 5
  1. #1
    F4iChick02 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default Displaying a price with a checkbox

    I want to assign my small,medium,large, and extra large a price along with the extras like the onions, etc. It comes up with incompatible types when I run it because I am trying to assign it a number. I am just wondering how I can make this work with it being a checkbox and a radio button. Here is my code. Thanks




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

    public class Pizza extends JFrame implements ActionListener
    {


    JRadioButton small, medium, large, extraLarge;
    JCheckBox onions, olives, pickles, bacon, chicken, cheese;
    JButton order, clear, exit;
    JPanel westPanel, eastPanel, midPanel, lowerPanel;

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

    small = new JRadioButton("Small");
    medium = new JRadioButton("Medium");
    large = new JRadioButton("Large");
    extraLarge = new JRadioButton("Extra Large");
    westPanel = new JPanel(new GridLayout(5,1,10,10));
    westPanel.add(small);
    westPanel.add(medium);
    westPanel.add(large);
    westPanel.add(extraLarge);
    con.add(westPanel, BorderLayout.WEST);

    onions = new JCheckBox("Onions");
    olives = new JCheckBox("Olives");
    pickles = new JCheckBox("Pickles");
    bacon = new JCheckBox("Bacon");
    chicken = new JCheckBox("Chicken");
    cheese = new JCheckBox("Cheese");
    midPanel = new JPanel(new GridLayout(5,1,10,10));
    midPanel.add(onions);
    midPanel.add(olives);
    midPanel.add(pickles);
    midPanel.add(bacon);
    midPanel.add(chicken);
    con.add(midPanel, BorderLayout.CENTER);

    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("Allison's Pizza Palace");
    heading.setFont(new Font("Helvetica", Font.BOLD | Font.BOLD, 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);
    extraLarge.setSelected(false);
    onions.setSelected(false);
    pickles.setSelected(false);
    bacon.setSelected(false);
    chicken.setSelected(false);
    cheese.setSelected(false);
    }

    private void placeOrder()
    {
    String size;
    if (small.isSelected())
    size = "Small $7.00 ";
    else if (medium.isSelected())
    size = "Medium $9.00 ";
    else if(large.isSelected())
    size = "Large 11.00 ";
    else if(extraLarge.isSelected())
    size = "Extra Large $14.00";

    String extras;
    if (onions.isSelected())
    extras = "Onions $1.00 ";
    else if (olives.isSelected())
    extras = "Olives $1.00 ";
    else if (pickles.isSelected())
    extras = "Pickles 1.00 ";
    else if (bacon.isSelected())
    extras = "Bacon $1.00 ";
    else if(chicken.isSelected())
    extras = "Chicken $1.00 ";
    else if(cheese.isSelected())
    extras = "Cheese is Free!";

    String totalPrice;
    if(small.isSelected())
    totalPrice = 7;
    else if(medium.isSelected())
    totalPrice = 9;
    else if(large.isSelected())
    totalPrice = 11;
    else if(extraLarge.isSelected())
    totalPrice = 14;




    System.out.println(size + extras + totalPrice);
    clearUI();
    }

    public static void main(String[] args)
    {
    Pizza myPizza = new Pizza();
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The way I'd think about this is that you want to closely tie together two different bits of data -- a String and a number -- and this holds true whether we're talking size or ingredient, right? So what I'd do would be to create a separate class, you can call it Item, and have this class hold a String and a number. If you want to be a purist, you could even have two classes, a PizzaSize class and a PizzaTopping class, but holding a String and a number, and these could both derive from Item.

    I'd then create a List or Array of PizzaSize objects and PizzaTopping objects and use this list to create the JRadioButtons and the JCheckBoxes, probably with a String obtained by either getName() or a toString() override.

    Then when selections are made, you could iterate through the selections, and compare them with the names / strings of the items in the lists, then get the number from the selected objects and add them up.

    YMMV of course. HTH.

    Oh, by the way, don't forget to put your JRadioButton objects all in the same ButtonGroup object so that you'll only be able to choose one size at a time.

  3. #3
    F4iChick02 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default reply

    So basically there is really not a good way of setting the pizza values like with an if statement other than an array?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Oh, I'm not saying my way is the only way, but I know that it works well for me. Again, your mileage may vary.

  5. #5
    F4iChick02 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default List

    So if I do create that list which I just looked up because I don't know too much about it, how would I call the members in the list that would get me the price. To make the list it goes something with list.add(xyz") right?

Similar Threads

  1. [SOLVED] Checkbox new to Java
    By s34nn4 in forum Java Applets
    Replies: 11
    Last Post: 04-12-2009, 06:21 AM
  2. How to construct checkbox in rtf document using iText?
    By soumyanil in forum Advanced Java
    Replies: 0
    Last Post: 07-28-2008, 12:05 PM
  3. How to use ItemListener for CheckBox class
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 09:18 PM
  4. Cheapest Domain price:.CN $0.14&.COM $6.62(www.todaynic.com)
    By Bettypeng in forum Reviews / Advertising
    Replies: 0
    Last Post: 03-27-2008, 10:48 AM
  5. checkbox
    By Alan in forum AWT / Swing
    Replies: 3
    Last Post: 05-18-2007, 11:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •