Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Define a class called PizzaOrder that represents a pizza order for a customer

ID: 3584746 • Letter: 1

Question

1. Define a class called PizzaOrder that represents a pizza order for a customer. This class has 4 private data members: size -- an integer representing the size of the pizza in inches. Valid values are 10, 15, 22. toppings -- a string representing a list of the toppings: "pepperoni,olives". Each item is separated by commas and there are no spaces. crust -- A string representing the type of crust. Valid values are "thin", "pan", and "handtossed". name -- A String representing the customer's name. Write a set/get method for each private variable -- for example, setSize(int s) sets the variable size to the value s. Likewise, getToppings( ) returns the value of toppings. 2. Write an application that uses your PizzaOrder class. First, your application will prompt the user for name, size of pizza, toppings, crust. Then create a PizzaOrder object and call the set methods on that object for each part of the order. After you have called the set methods, call the get methods of the object to retrieve and display the values of the object. Example: What size pizza do you want: 10 What toppings: pepperoni,onions,olives What kind of crust: pan What is your name: Allen Here is your order: Pan pizza 10 inch pepperoni,onions,olives For Allen

Explanation / Answer

// rate my answer first import java.awt.*; import java.awt.event.*; import java.applet.*; import java.text.*; import javax.swing.*; /** * This is the Pizza Ordering class */ public class pizz extends Applet implements ActionListener, KeyListener { // Constants for storing Prices private final double smallPizzaPrice = 9.00, regularPizzaPrice = 16.50, largePizzaPrice = 22.00, cheeseToppingPrice = 2.80, sausageToppingPrice = 2.80, mushroomToppingPrice = 2.30, oreganoToppingPrice = 3.00, pepperToppingPrice = 2.30, chickenToppingPrice = 3.00; //Graphical components Panel topPanel, centerPanel, centerQuantityPanel, centerSizePanel, centerToppingPanel, centerPricePanel, bottomPanel; Label titleLabel, quantityLabel, amountLabel, sizeLabel, toppingLabel, priceLabel, priceDisplayLabel; Button orderButton; TextField pizzaText, priceText; ButtonGroup sizeBGroup; JRadioButton smallJRadio, regularJRadio, largeJRadio; JCheckBox cheeseJCheck, sausageJCheck, mushroomJCheck, oreganoJCheck, pepperJCheck, chickenJCheck; /** * Initialize variable and components */ public void init() { // Setting applet setBackground(new Color(205,205,205)); setLayout(new BorderLayout(0,0)); // Initializing Panels topPanel = new Panel(); topPanel.setBackground(new Color(0,118,157)); topPanel.setLayout(new FlowLayout()); centerPanel = new Panel(); centerQuantityPanel = new Panel(); centerQuantityPanel.setLayout(new FlowLayout()); centerSizePanel = new Panel(); centerSizePanel.setLayout(new FlowLayout()); centerToppingPanel = new Panel(); centerToppingPanel.setLayout(new GridLayout(2,3,0,0)); centerPricePanel = new Panel(); centerPricePanel.setLayout(new FlowLayout()); bottomPanel = new Panel(); bottomPanel.setLayout(new FlowLayout()); // Initializing Labels titleLabel = new Label("CyberPizza.com"); titleLabel.setBackground(new Color(0,118,157)); titleLabel.setForeground(new Color(255,255,255)); titleLabel.setFont(new Font("Arial",Font.BOLD,50)); quantityLabel = new Label("Enter the number of pizza(s) you wish to order : "); quantityLabel.setForeground(new Color(0,0,205)); quantityLabel.setFont(new Font("Arial",Font.BOLD,14)); amountLabel = new Label("Amount: "); amountLabel.setForeground(new Color(0,0,0)); amountLabel.setFont(new Font("Arial",Font.BOLD,14)); sizeLabel = new Label("Select the size of the pizza(s) above to be made in :"); sizeLabel.setForeground(new Color(0,0,205)); sizeLabel.setFont(new Font("Arial",Font.BOLD,14)); toppingLabel = new Label("Select one or more choices of the toppings below : "); toppingLabel.setForeground(new Color(0,0,205)); toppingLabel.setFont(new Font("Arial",Font.BOLD,14)); priceLabel = new Label("Total price of your order : "); priceLabel.setForeground(new Color(200,0,0)); priceLabel.setFont(new Font("Arial",Font.BOLD,14)); priceDisplayLabel = new Label("$0.00 "); priceDisplayLabel.setForeground(new Color(235,0,0)); priceDisplayLabel.setFont(new Font("Arial",Font.BOLD,34)); // Initializing Textfields pizzaText = new TextField("0",1); pizzaText.setFont(new Font("Arial",Font.BOLD,14)); // Initializing JRadio buttons smallJRadio = new JRadioButton("Small", true); regularJRadio = new JRadioButton("Regular", false); largeJRadio = new JRadioButton("Large", false); // Grouping radio buttons sizeBGroup = new ButtonGroup(); sizeBGroup.add(smallJRadio); sizeBGroup.add(regularJRadio); sizeBGroup.add(largeJRadio); // Initializing JCheckboxes cheeseJCheck = new JCheckBox("Extra Cheese",false); sausageJCheck = new JCheckBox("Sausage Slices",false); mushroomJCheck = new JCheckBox("Button Mushroom",false); oreganoJCheck = new JCheckBox("Oregano",false); pepperJCheck = new JCheckBox("Green Pepper",false); chickenJCheck = new JCheckBox("Chicken Nugget",false); // Initializing Buttons orderButton = new Button("Order Now"); orderButton.setFont(new Font("Arial",Font.BOLD,16)); // Registering listeners to components smallJRadio.addActionListener(this); regularJRadio.addActionListener(this); largeJRadio.addActionListener(this); cheeseJCheck.addActionListener(this); sausageJCheck.addActionListener(this); mushroomJCheck.addActionListener(this); oreganoJCheck.addActionListener(this); pepperJCheck.addActionListener(this); chickenJCheck.addActionListener(this); pizzaText.addKeyListener(this); orderButton.addActionListener(this); } /** * Start of applet */ public void start() { // Top Panel topPanel.add(titleLabel); // Nested Center Panels centerQuantityPanel.add(amountLabel); centerQuantityPanel.add(pizzaText); centerSizePanel.add(smallJRadio); centerSizePanel.add(regularJRadio); centerSizePanel.add(largeJRadio); centerToppingPanel.add(cheeseJCheck); centerToppingPanel.add(sausageJCheck); centerToppingPanel.add(mushroomJCheck); centerToppingPanel.add(oreganoJCheck); centerToppingPanel.add(pepperJCheck); centerToppingPanel.add(chickenJCheck); centerPricePanel.add(priceLabel); centerPricePanel.add(priceDisplayLabel); // Center Panel centerPanel.add(quantityLabel); centerPanel.add(centerQuantityPanel); centerPanel.add(sizeLabel); centerPanel.add(centerSizePanel); centerPanel.add(toppingLabel); centerPanel.add(centerToppingPanel); centerPanel.add(centerPricePanel); // Bottom Panel bottomPanel.add(orderButton); // Main Applet Window add(topPanel, BorderLayout.NORTH); add(centerPanel, BorderLayout.CENTER); add(bottomPanel, BorderLayout.SOUTH); pizzaText.selectAll(); } // Unused interface methods public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { } /** * Perform validation check on user text input */ public void keyReleased(KeyEvent e) { // Trap all non-valid numbers try { Integer.parseInt(pizzaText.getText()); } catch (NumberFormatException fe) { pizzaText.setText("0"); } refreshPrice(); } /** * Traps user input */ public void actionPerformed(ActionEvent e) { // If order now button is pressed if (e.getSource() == orderButton) { JOptionPane.showMessageDialog(this, "Thank you for your " + priceDisplayLabel.getText() + " payment." + " Your pizza will be delivered to you in 3 months !" + " Why dont you order another pizza while waiting ?", "Orders Confirmed", JOptionPane.INFORMATION_MESSAGE); } refreshPrice(); } /** * Method to calculate and refresh the price display */ private void refreshPrice() { // Local variables used to accumulate total price double price = 0; int pizzaAmount = Integer.parseInt(pizzaText.getText()); // Initializing Number Format NumberFormat numberForm = NumberFormat.getNumberInstance(); DecimalFormat moneyForm = (DecimalFormat)numberForm; moneyForm.applyPattern("0.00"); // Pizza size prices if (smallJRadio.isSelected()) { price+= smallPizzaPrice * pizzaAmount; } if (regularJRadio.isSelected()) { price+= regularPizzaPrice * pizzaAmount; } if (largeJRadio.isSelected()) { price+= largePizzaPrice * pizzaAmount; } // Pizza topping prices if (cheeseJCheck.isSelected()) { price+= cheeseToppingPrice * pizzaAmount; } if (sausageJCheck.isSelected()) { price+= sausageToppingPrice * pizzaAmount; } if (mushroomJCheck.isSelected()) { price+= mushroomToppingPrice * pizzaAmount; } if (oreganoJCheck.isSelected()) { price+= oreganoToppingPrice * pizzaAmount; } if (pepperJCheck.isSelected()) { price+= pepperToppingPrice * pizzaAmount; } if (chickenJCheck.isSelected()) { price+= chickenToppingPrice * pizzaAmount; } priceDisplayLabel.setText("$"+moneyForm.format(price)); } }