This is my assignment. I've done it 36 hours straight and I can't think properly so my program looks like kindergarden kid's program.

Basically, we have to do a GUI for Pizza ordering. Everything works except for the amount. The output for amount is "0.00".

I have to send it now to my lecturer but I just want to know where I am wrong. My lecturer won't tell me so I need help from you guys to tell me where I did wrong. Thank you.

Earlier, I have problems with the arrays too but I solved it the long way. Hope you don't mind.

Thank you in advanced!

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

public class PizzaLand extends JFrame
{
    private JLabel name = new JLabel("Customer Name:");
    private JLabel email = new JLabel("Email Address:");
    private JLabel telNo = new JLabel("Telephone No:");
    private JLabel pizza = new JLabel("Pizza Type:");
    private JLabel crust = new JLabel("Crust Type:");
    private JLabel size = new JLabel("Size:");
    private JLabel info = new JLabel("Pizza info:");

    private JTextField tName = new JTextField();
    private JTextField tEmail = new JTextField();
    private JTextField tTelNo = new JTextField();

    private String[] pizzaType = {"Cheese & Tomatoes", "Vegetarian", "Anchovies", "Superb meat lover", "Juicy chicken", "All cheezey", "Seafood heaven", "Sambal berapi"};
    private JList lPizza = new JList(pizzaType);
    private JScrollPane scrollPizza = new JScrollPane(lPizza);

    private String[] crustType = {"Super thin", "Thin & crispy", "Hand tossed", "Deep pan", "Cheesy edge"};
    //private String comboSize;
   // private double pizzaPrice;
    private String[] comboSize = {"Personal 6 inch", "Regular 9 inch", "Large 12 inch", "Extra large 15 inch"};
    //private double[] pizzaPrice = {10.00, 20.00, 25.00, 30.00};
    private JComboBox cCrust = new JComboBox(crustType);
    private JComboBox cSize = new JComboBox(comboSize);

    private JRadioButton delivery = new JRadioButton("Delivery", true);
    private JRadioButton pickUp = new JRadioButton("Pick up", true);

    private JButton submit = new JButton("Submit");
    private JButton clear = new JButton("Clear");

    private JTextArea aInfo = new JTextArea(1, 2);

    private JPanel p1 = new JPanel();
    private JPanel p2 = new JPanel();

    private double amount;
    //int[] pizzaItem;
    String selected = "";

    public PizzaLand()
    {
        lPizza.setVisibleRowCount(4);
        lPizza.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        Container pane = getContentPane();
        pane.setLayout(new GridLayout(2, 1, 5, 5));

        p1.setLayout(new GridLayout(4, 4));
        p1.add(name);
        p1.add(tName);
        p1.add(email);
        p1.add(tEmail);
        p1.add(telNo);
        p1.add(tTelNo);
        p1.add(pizza);
        p1.add(scrollPizza);
        p1.add(crust);
        p1.add(cCrust);
        p1.add(size);
        p1.add(cSize);
        p1.add(delivery);
        p1.add(pickUp);
        p1.add(submit);
        p1.add(clear);

        p2.setLayout(new BorderLayout());
        p2.add(info, "West");
        p2.add(aInfo, "Center");

        pane.add(p1);
        pane.add(p2);

        action listener = new action();
        delivery.addActionListener(listener);
        pickUp.addActionListener(listener);
        submit.addActionListener(listener);
        clear.addActionListener(listener);

        item listener1 = new item();
        lPizza.addListSelectionListener(listener1);
    }

        class action implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                Object choice = e.getSource();
                //Object radioB = e.getSource();

                if (choice == submit)
                {
                    String item1 = tName.getText();
                    String item2 = tEmail.getText();
                    String item3 = tTelNo.getText();

                    int item4 = cCrust.getSelectedIndex();
                    int item5 = cSize.getSelectedIndex();

                    if (choice == delivery)
                    {
                        if (cSize.getSelectedItem().equals("Personal 6 inch"))
                        {
                            amount = 10+5;
                        }
                        else if (cSize.getSelectedItem().equals("Regular 9 inch"))
                        {
                            amount = 20+5;
                        }
                        else if (cSize.getSelectedItem().equals("Large 12 inch"))
                        {
                            amount = 25+5;
                        }
                        else if (cSize.getSelectedItem().equals("Extra large 15 inch"))
                        {
                            amount = 30+5;
                        }
                    }

                    else if (choice == pickUp)
                    {
                        if (cSize.getSelectedItem().equals("Personal 6 inch"))
                        {
                            amount = 10;
                        }
                        else if (cSize.getSelectedItem().equals("Regular 9 inch"))
                        {
                            amount = 20;
                        }
                        else if (cSize.getSelectedItem().equals("Large 12 inch"))
                        {
                            amount = 25;
                        }
                        else if (cSize.getSelectedItem().equals("Extra large 15 inch"))
                        {
                            amount = 30;
                        }
                    }

                    aInfo.setText(item1 + "\n" + item2 + "\n" + item3 + "\n" + selected + "\n" + crustType[item4] + "\n" + comboSize[item5] + "\nTotal price: RM" + String.format("%.2f", amount));
                }

                else if (choice == clear)
                {
                    aInfo.setText("");
                }
            }
        }

        class item implements ListSelectionListener
        {
            public void valueChanged(ListSelectionEvent e)
            {
                String str = "";
                int selectedIndex[] = lPizza.getSelectedIndices();
                for (int i=0; i<selectedIndex.length; i++)
                {
                    int index = selectedIndex[i];
                    str += pizzaType[index];
                }
                selected = str;
            }

         }

public static void main (String []args)
{
   PizzaLand frame = new PizzaLand();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setTitle("Order your pizza from PizzaLand. Satisfaction guaranteed!");
   frame.setSize(650, 650);
   frame.setVisible(true);

}
}

Recommended Answers

All 9 Replies

On lines 107 and 127, you are trying to check which radio button was selected. However, you used the choice object. You set this object to be assigned the source of the event (submit or clear button).

To check which radio button (pickUp or delivery) was selected, simply replace those two lines with the following:

if (delivery.isSelected()) {

and

if (pickUp.isSelected()) {

respectively.

commented: Really helped! +0

Oh. Thank you!

My problem is solved! Thank you so much!
1

Delivery and pickup should be mutually exclusive. At the moment you can select both at the same time.

you should work on gui more and the size of buttons (submit,clear) is so much big its looking odd and the option should be delvery and dine in

Starstreak - Yup. I don't know how to disable it.. Do you know how?

Techno123 - They are big because I use GridLayout for p1 so they came out same sizes as evrything.. The assignment asked us to make those options. It is odd..

If you want only one RadioButton to be pressed at any one time, add them to a ButtonGroup.

Ohh. Thank you soo much!! You guys are awesome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.