Here is the assignment and the code with it. The code compiles, but with errors.
ID: 638162 • Letter: H
Question
Here is the assignment and the code with it. The code compiles, but with errors. Its still able to run, but I would like to make it run perfectly. Also, please offer explanation of code commands.
Design an application to take and process a pizza order. The user should be able to make pizza order choices from listboxes, and the application should display the order price. One of the listboxes should allow a user to select pizza size under the following criteria: small with the base price of $7, medium with the base price of $9, and large with the base price of $11. The second listbox should allow a user to select an additional topping and add its price to the total price of the pizza. There is no additional price for cheese, but any other topping (of your choice to add to the listbox) adds $1 to the base price. Offer at least five different toppings of your choice. On a click of an
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JPizzaria extends JFrame implements ItemListener
{
FlowLayout flow = new FlowLayout();
JLabel companyName = new JLabel("Tower of Pizza Pizzaria");
JComboBox sizeBox = new JComboBox();
JLabel sizeList = new JLabel("Size List");
JComboBox toppingBox = new JComboBox();
JLabel toppingList = new JLabel("Topping List");
JTextField totPrice = new JTextField(10);
int totalPrice = 0;
int sizeNum, toppingNum;
int sPrice, tPrice, sumPrice;
int[] sizePrice = {0,7,9,11,14};
int[] toppingPrice = {0,0,1,1,1,1};
String output;
public JPizzaria()
{
super("Tower of Pizza Pizzaria");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.setLayout(flow);
sizeBox.addItem("None");
sizeBox.addItem("Small");
sizeBox.addItem("Medium");
sizeBox.addItem("Large");
sizeBox.addItem("Extra large");
toppingBox.addItem("None");
toppingBox.addItem("Cheese");
toppingBox.addItem("Sausage");
toppingBox.addItem("Pepperoni");
toppingBox.addItem("Green pepper");
toppingBox.addItem("Onion");
pane.add(sizeList);
pane.add(sizeBox);
sizeBox.addItemListener(this);
pane.add(toppingList);
pane.add(toppingBox);
toppingBox.addItemListener(this);
pane.add(companyName);
pane.add(totPrice);
setContentPane(pane);
}
public static void main(String[] arguments)
{
JPizzaria pframe = new JPizzaria();
pframe.setSize(200,200);
pframe.setVisible(true);
}
public void itemStateChanged(ItemEvent list)
{
Object source = list.getSource();
if (source == sizeBox)
{
sizeNum = sizeBox.getSelectedIndex();
sPrice = sizePrice[sizeNum];
sumPrice = sPrice + tPrice;
output = "Total Price $" + sumPrice;
totPrice.setText(output);
}
else if (source == toppingBox)
{
toppingNum = toppingBox.getSelectedIndex();
tPrice = toppingPrice[toppingNum];
sumPrice = sPrice + tPrice;
output = "Total Price $" + sumPrice;
totPrice.setText(output);
}
}
}