Create a user friendly interface to order a pizza. Use appropriate controls (rad
ID: 3831002 • Letter: C
Question
Create a user friendly interface to order a pizza. Use appropriate controls (radio buttons, list boxes, check boxes) to obtain the type of pizza (e.g. small, medium, large) and the toppings. Calculate the cost of the pizza based upon the size, number of toppings and delivery charge. Display a summary of the order in a text area along with the total cost. Provide buttons which places the order and clears the order.
Allow for multiple pizzas to orders
Submit as .jar file.
"Please use Eclipse to write this program"
Explanation / Answer
PROGRAM CODE:
package pizza;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class PizzaOrderingApp {
static String pizzas[] = {"Select a Pizza", "Chicken Supreme", "Chicken Exotica", "BBQ", "Italiana"};
static double basePizzaRate[] = {0, 10, 20, 15, 12};
static String toppings[] = {"Select a topping", "Pineapple", "Jalapeno", "Onion", "Olives", "Capsicum"};
static double toppingsRate[] = {0, 1.5, 2, 1.9, 2.5, 2};
static String sizes[] = {"Small", "Medium", "Large"};
static double sizesRate[] = {4, 5, 6};
static double totalAmount = 0;
public static void main(String args[])
{
JFrame frame = new JFrame();
JPanel pizzaSelectionPanel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
JComboBox<String> pizzaSelections = new JComboBox<>(pizzas);
pizzaSelectionPanel.add(pizzaSelections, constraints);
JButton add = new JButton("Add");
constraints.gridx = 1;
pizzaSelectionPanel.add(add, constraints);
constraints.fill = GridBagConstraints.VERTICAL;
constraints.gridx = 0;
constraints.gridy = 1;
pizzaSelectionPanel.add(new JLabel("Your Selection: "), constraints);
JPanel selectedPanel = new JPanel(new GridLayout(4, 2));
selectedPanel.setVisible(true);
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton order = new JButton("Order");
JButton clear = new JButton("Clear");
JTextArea area = new JTextArea();
buttonPanel.add(order);
buttonPanel.add(clear);
order.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
area.setText("Total Amount: " + totalAmount);
}
});
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
totalAmount = 0;
selectedPanel.removeAll();
selectedPanel.repaint();
}
});
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectedPanel.add(new JLabel(pizzaSelections.getSelectedItem().toString()));
totalAmount += basePizzaRate[pizzaSelections.getSelectedIndex()];
JButton customize = new JButton("Customize");
customize.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFrame customizerFrame = new JFrame("Customize " + pizzaSelections.getSelectedItem().toString());
JComboBox<String> toppingsBox = new JComboBox<String>(toppings);
JComboBox<String> sizesBox = new JComboBox<>(sizes);
JButton done = new JButton("Done");
customizerFrame.getContentPane().setLayout(new GridLayout(3, 2, 10, 10));
customizerFrame.getContentPane().add(new JLabel("Select your topping: "));
customizerFrame.getContentPane().add(toppingsBox);
customizerFrame.getContentPane().add(new JLabel("Select the type: "));
customizerFrame.getContentPane().add(sizesBox);
customizerFrame.getContentPane().add(done);
done.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
totalAmount += toppingsRate[toppingsBox.getSelectedIndex()];
totalAmount += sizesRate[sizesBox.getSelectedIndex()];
}
});
customizerFrame.setSize(300, 300);
customizerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
customizerFrame.setVisible(true);
}
});
selectedPanel.add(customize);
selectedPanel.repaint();
frame.getContentPane().repaint();
}
});
frame.getContentPane().setLayout(new GridLayout(4, 1));
frame.getContentPane().add(pizzaSelectionPanel);
frame.getContentPane().add(selectedPanel);
frame.getContentPane().add(buttonPanel);
frame.getContentPane().add(area);
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.