Create a programin which the user can order a pizza for pickup. Your program sho
ID: 653813 • Letter: C
Question
Create a programin which the user can order a pizza for pickup. Your program should have a graphical user interface that contains a combo box for the pizza size(10,12,14,or 16inch). It can contain radio button so ra combo box for the crust type (hand-tossed, deep-dish, thin crust). You should have check boxes for toppings (at least five toppings must be offered). The user should be able to enter his / her name in a text field. The user will click on a button to process the order or click on a cancel button to reset the GUI to the default starting values. You can determine layout, colors, fonts and look and feel of your GUI. The program should calculate the total price of the pizza and output a confirmation of the order. The output should contain all the information that the user entered for the order including a final price of the order including 5.5%salestax. The listener for the submit order button should create aninstance of a Pizza Order object based on the Pizza Qrder UML. The Pizza Cfrder constructor will initialize the size, crust Type , and name variables. It will also create an instance o f the toppings Atfay J Lisi Toppings will be added to the Analyst through the addTppping method. Use the toString0 method of Pizza Order describe the order to be shown in the GUI. Use this method to get the text to output on the GUI in a J Text Area. 10inchpizza-$10.79 12inchpizza-S12.79 14 inch pizza-$13.79 16inchpizza~$15.79 Toppings cost $1.25 each Java doc & Code Style 20pts Appeal of GUI 10pts Pizza Order UML 20pts Cancel Order 20pts Correct Output for Order 30pts (size, crust, toppings, name, tax, and final price) This is an individual assignment all work must be your own. You may consult the Instructor, SIs,or fellow students for general conceptual help and assistance debugging. They can help vout hrough 1 or2 bugs, but they should not debug your program for more than 5-10 minutes. You may not copy code from other students (either in electronic copy or "over-the-shoulder" copy) Do not "team-code" this assignment, i.e. it is best to be doing this assignment by yourself or in the lab. You may use your textbook or D2L course content to help with your program. Finally, if you are unsure about any other methodology, PLEASE JUST ASK the Instructor.Explanation / Answer
package com.tutai;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class PizzaOrder {
private int size;
private String crustType;
private ArrayList <String> toppings;
private String name;
PizzaOrder(int size,String crustType,String name){
this.size=size;
this.crustType=crustType;
this.name=name;
toppings=new ArrayList<String>();
}
double calculatePrice(){
double price=0;
double saleTax=0;
switch(size){
case 10:
price=10.79;
break;
case 12:
price=12.79;
break;
case 14:
price=13.79;
break;
case 16:
price=15.79;
break;
}
int countOfToppings=toppings.size();
price+=1.25*countOfToppings;
return price;
}
public String toString(){
String orderDetails="";
double saleTax=0;
double price=0;
orderDetails="Size: "+size+" ";
orderDetails+="Crust Type: "+crustType+" ";
orderDetails+="Toppings: ";
for(int i=0;i<toppings.size();i++){
orderDetails+=toppings.get(i)+" ";
}
orderDetails+="Name: "+name+" ";
price=calculatePrice();
saleTax=(5.5*price)/100;
orderDetails+="Sale Tax: "+saleTax+" ";
price+=saleTax;
orderDetails+="Total Price: "+price;
return orderDetails;
}
void generateGUI(){
JFrame frame = new JFrame("Pizza Order"); //declare a frame with title
frame.setSize(325, 225); //set the size of frame
frame.setLocation(100, 100); //set the location of frame on screen
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set default close operation for frame
JPanel panel1 = new JPanel(); //declare a panel object
Color lightYellowColor=new Color(255,255,224); //declare a color object
panel1.setBackground(lightYellowColor); //set background color of panel
JLabel label1 = new JLabel("Pizza Size"); //declare label object
Color navyColor=new Color(0,0,128); //declare color object
label1.setForeground(navyColor); //set font color of label
String[] comboTypes = { "10", "12", "14", "16" };
// Create the combo box, and set 2nd item as Default
JComboBox comboTypesList = new JComboBox(comboTypes);
comboTypesList.setSelectedIndex(0);
comboTypesList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox jcmbType = (JComboBox) e.getSource();
size =Integer.parseInt((String)jcmbType.getSelectedItem());
System.out.println(size);
}
});
JLabel label2 = new JLabel("Crust Type"); //declare label object
label2.setForeground(navyColor); //set font color of label
JRadioButton crustType1 = new JRadioButton("hand-tossed", true);
JRadioButton crustType2 = new JRadioButton("deep-dish");
JRadioButton crustType3 = new JRadioButton("thin crust");
crustType1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
crustType=e.getStateChange()==1?"hand-tossed":"";
System.out.println(crustType);
}
});
crustType2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
crustType=e.getStateChange()==1?"deep-dish":"";
System.out.println(crustType);
}
});
crustType3.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
crustType=e.getStateChange()==1?"thin crust":"";
System.out.println(crustType);
}
});
JCheckBox chk1 = new JCheckBox("Cheese");
JCheckBox chk2 = new JCheckBox("Egg");
JCheckBox chk3 = new JCheckBox("Mayoneese");
JCheckBox chk4 = new JCheckBox("Paneer");
JCheckBox chk5 = new JCheckBox("Mushroom");
chk1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==1){
addTopping("Cheese");
}
}
});
chk2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==1){
addTopping("Egg");
}
}
});
chk3.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==1){
addTopping("Mayoneese");
}
}
});
chk4.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==1){
addTopping("Paneer");
} }
});
chk5.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==1){
addTopping("Mushroom");
}
}
});
JLabel label3 = new JLabel("Toppings");
label3.setForeground(navyColor);
JLabel label4 = new JLabel("Customer Name");
label4.setForeground(navyColor);
JTextField tName=new JTextField(20);
JTextArea orderDetails=new JTextArea(10,30);
orderDetails.setEditable(false);
JLabel label5=new JLabel("Order Details");
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name = tName.getText();
PizzaOrder p1=new PizzaOrder(size,crustType,name);
orderDetails.setText(p1.toString());
}
});
JButton cancelButton = new JButton("CANCEL");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
comboTypesList.setSelectedIndex(0);
crustType1.setSelected(true);
crustType2.setSelected(false);
crustType3.setSelected(false);
chk1.setSelected(false);
chk2.setSelected(false);
chk3.setSelected(false);
chk4.setSelected(false);
chk5.setSelected(false);
tName.setText("");
orderDetails.setText("");
}
});
panel1.add(label1); //add label to panel
panel1.add(comboTypesList); //add button to panel
ButtonGroup group = new ButtonGroup();
group.add(crustType1);
group.add(crustType2);
group.add(crustType3);
panel1.add(label2);
panel1.add(crustType1); //add text to panel
panel1.add(crustType2);
panel1.add(crustType3);
panel1.add(label3);
panel1.add(chk1);
panel1.add(chk2);
panel1.add(chk3);
panel1.add(chk4);
panel1.add(chk5);
panel1.add(label4);
panel1.add(tName);
panel1.add(okButton);
panel1.add(cancelButton);
panel1.add(label5);
panel1.add(orderDetails);
frame.add(panel1,BorderLayout.PAGE_START); //add panel to start of frame
frame.pack(); //size the frame so that all its contents are at or above their preferred sizes
frame.setVisible(true); //set frame to visible
}
void addTopping(String topping){
toppings.add(topping);
}
public static void main(String[] args){
PizzaOrder p=new PizzaOrder(10,"hand-tossed","");
p.generateGUI();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.