Design an application for a pizzeria. The user makes pizza order choices from li
ID: 3681866 • Letter: D
Question
Design an application for a pizzeria. The user makes pizza order choices from list
boxes, and the application displays the price. The user can choose a pizza size of small
($7), medium ($9), large ($11), or extra large ($14), and one of any number of
toppings. There is no additional charge for cheese, but any other topping adds $1 to
the base price. Offer at least five different topping choices. Save the file as JPizza.java.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JPizza extends JFrame implements ItemListener{
int totalCost = 0;
int pizzaSize;
int toppings;
int pizzaSizePrice;
int toppingsTotal;
int totalPrice;
int[] sizeCosts = {0, 7, 9, 11, 14};
int[] toppingsSelected = {0, 0, 1, 1, 1, 1, 1, 1, 1};
String ordered;
JComboBox pizzaSizes = new JComboBox();
JComboBox toppingsChoice = new JComboBox();
JLabel sizeOfPizzaLabel = new JLabel ("Select pizza size");
JLabel toppingsLabel = new JLabel ("Select topping");
JLabel selectedItems = new JLabel();
FlowLayout layout = new FlowLayout();
public JPizza(){
super("Pizza order form.");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(layout);
pizzaSizes.addItem(" ");
pizzaSizes.addItem("Small");
pizzaSizes.addItem("Medium");
pizzaSizes.addItem("large");
pizzaSizes.addItem("Extra Large");
toppingsChoice.addItem(" ");
toppingsChoice.addItem("Cheese");
toppingsChoice.addItem("Pepperoni");
toppingsChoice.addItem("Sausage");
toppingsChoice.addItem("Hamburger");
toppingsChoice.addItem("Green Peppers");
toppingsChoice.addItem("Onions");
toppingsChoice.addItem("Mushrooms");
toppingsChoice.addItem("Black Olives");
pizzaSizes.addItemListener(this);
toppingsChoice.addItemListener(this);
panel.add(sizeOfPizzaLabel);
panel.add(pizzaSizes);
panel.add(toppingsLabel);
panel.add(toppingsChoice);
panel.add(selectedItems);
setContentPane(panel);
}
public void itemStateSelected (ItemEvent e){
Object source = e.getSource();
if(source == pizzaSizes){
pizzaSize = pizzaSizes.getSelectedIndex();
pizzaSizePrice = sizeCosts[pizzaSize];
totalPrice = pizzaSizePrice + toppingsTotal;
ordered = "Total price of your pizza $" + totalPrice;
selectedItems.setText(ordered);
}
else if(source == toppingsChoice){
toppings = toppingsChoice.getSelectedIndex();
toppingsTotal = toppingsSelected[toppings];
totalPrice = pizzaSizePrice + toppingsTotal;
ordered = "Total price of your pizza $" + totalPrice;
selectedItems.setText(ordered);
}
}
public static void main(String[] args){
JPizza frame = new JPizza();
frame.setSize(250, 200);
frame.setVisible(true);
}
@Override
public void itemStateChanged(ItemEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
at JPizza.itemStateChanged(JPizza.java:115)
at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1223)
at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1271)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1330)
at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:576)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:622)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:852)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:501)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Explanation / Answer
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.JComboBox;
public class JPizza extends JFrame implements ActionListener {
int TotalPrice;
// Here I will create JComboBoxes
JComboBox<String> pizza = new JComboBox<String>();
JComboBox<String> toping = new JComboBox<String>();
Font headlineFont = new Font("Arial", Font.BOLD,36);
JLabel TowerOfPizza = new JLabel("Tower of Pizza");
JLabel SizeList = new JLabel("Size List ");
JLabel Toping = new JLabel("Topping List ");
JTextField totalPrice = new JTextField(15);
public JPizza() {
super("Tower of Pizza");
// sets the layout of the form
setLayout(new FlowLayout());
//setLayout(new BorderLayout());
//setLayout(new CardLayout());
// adds items to pizza
pizza.addItem("None");
pizza.addItem("Small");
pizza.addItem("Medium");
pizza.addItem("Large");
pizza.addItem("Extra large");
// adds items to toping list
toping.addItem("None");
toping.addItem("Cheese");
toping.addItem("Sausage");
toping.addItem("Lettus");
toping.addItem("carrots");
toping.addItem("Meet lovers");
// add labels
add(TowerOfPizza);
//This will set the fon't of the TowerOfPizza
TowerOfPizza.setFont(headlineFont);
add(SizeList);
add(pizza);
add(Toping);
add(toping);
add(totalPrice);
// Closes the program upon exit
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Finally in the constructor I registered a class as a
// listener for events generated by each of the three JComboBoxes
pizza.setSelectedIndex(0);
toping.setSelectedIndex(0);
totalPrice.setEditable(false);
pizza.addActionListener(this);
toping.addActionListener(this);
// prices.addItemListener(this);
}
// Begin the itemStateChanged() or actionPerformed() method that executes
// when the user selects
// one of the elements
// in the JComboBox. Use the appropriate
// method to determine which specific member is the source of the current
// ItemEvent and whether the event was generated.
public void actionPerformed(ActionEvent e) {
int positionOfSelected = (pizza).getSelectedIndex();
int topingPositionSelected = toping.getSelectedIndex();
// Switch statement for positionOfSelected
switch (positionOfSelected) {
case 0:
TotalPrice = 0;
totalPrice.setText("The Price is $" + TotalPrice);
break;
case 1:
TotalPrice = 7;
totalPrice.setText("The Price is $" + TotalPrice);
break;
case 2:
TotalPrice = 9;
totalPrice.setText("The Price is $" + TotalPrice);
break;
case 3:
TotalPrice = 11;
totalPrice.setText("The Price is $" + TotalPrice);
break;
case 4:
TotalPrice = 14;
totalPrice.setText("The Price is $" + TotalPrice);
break;
default:
totalPrice.setText("The Price is $" + TotalPrice);
break;
}
// If statement that adds a dollar to the total price for a topping
if (topingPositionSelected == 2 || topingPositionSelected == 3
|| topingPositionSelected == 4) {
TotalPrice++;
totalPrice.setText("The Price is $" + TotalPrice);
}
else
totalPrice.setText("The Price is $" + TotalPrice);
}
// Main Method
public static void main(String[] args) {
JPizza aFrame = new JPizza();
final int WIDTH = 300;
final int HEIGHT = 200;
aFrame.setSize(WIDTH, HEIGHT);
aFrame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.