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

PIZZA ORDER FRAME PROGRAM Write a frame with components for an order form for a

ID: 3688186 • Letter: P

Question

PIZZA ORDER FRAME PROGRAM

Write a frame with components for an order form for a pizza (see image of frame). The frame should be able to run itself and include an appropriate title bar. All your frame components will have an appropriate pizza colored background and foreground. The purpose of this exercise is to show layouts and panels with different kinds of events. You will use a border layout with panels to divide the frame into 3 sections - top, middle, and bottom. All of the sections will use the panels with a grid layout for the multiple components.

The top section will have the centered title telling the name of the company (in a larger and different style typeface) and underneath it separate centered instructions to choose your pizza size and toppings.

The middle section will have four radio buttons in a button group: small - $7, medium - $9, large - $11, and extra large - $13. The right side of the middle section will also have a list object for the available toppings - Eggplant, Green Peppers, Hot Peppers, Pepperoni, Sausage, Mushrooms, and Anchovies (of which they can pick as many as they want from 0 - 7). Each topping will add $1 to the total price. Before they pick any pizza size or toppings, there should be no size or toppings selected.

The bottom section will have first a label saying APrice of your pizza is $0.00", and then to the right of the price label will be a checkbox promoting a $5.00 side order of hot wings. The price of the pizza will be updated as they pick different sizes or toppings, also in a larger point size and different typeface with the price formatted in Currency.

Use the setSize() for the frame, but be careful of the sizing, components tend to fill up unused areas in a border layout. For the list, make sure you can see all items but don't let there be any extra space, just resize the frame so everything fits and there is no extra space and don=t let the user change the frame=s size on you.

As soon as the user clicks on a new size, topping, or the wings check box, update the price immediately. Let the user make changes to the form as often as they want, seeing the updated price immediately. Have a price variable for the size price (which could include the extra wings price), a price variable for the topping price, and a total price. Only calculate and update what they change (ex. if they add a topping, the size does not have to be recalculated).

When they pick a size radio button, check to see which one they picked and change the size price (you will be an ItemListener). Then check to see if the wings check box is checked off, if so then add the $5.00 to the size price. Add to this size price the topping price, format the final price to Currency and update the price label. When they pick a topping or toppings (hold control key down for multiple toppings), calculate the topping price (add $1.00 for each topping they picked) (you will be a ListSelectionListener). Add to this the size price, format the final price to Currency, and update the price label.

1st Run:

Large Pizza with Eggplant and Hot Peppers, with a side order of wings.

2nd Run:

Medium Pizza with Green Peppers, Pepperoni, and Mushrooms, without a side order of wings.

Save the frame class code with documentation - full 4-5 line paragraph describing the frame and at least 5 documentation lines throughout the code. Run with each of the two runs, each time saving a picture of the actual frame results (they can be saved together in the same file as a .jpg). You can copy them with <alt><ptrscr> (just active frame) and paste both pictures into Paint for saving as a .jpg file. Submit both files (.java java code and .jpg output image of the two frame images).

Explanation / Answer

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.DecimalFormat;

public class PizzaApp extends JFrame implements ActionListener {

private final int WIDTH = 250;

private final int HEIGHT = 300;

private JLabel lblPizza;

private JComboBox<String> cboPizza;

private JLabel lblSize;

private JComboBox<String> cboSize;

private JLabel lblWithCheese;

private JCheckBox chkWithCheese;

private JLabel lblTopping;

private JList<String> lstToppings;

private JButton btnCheckOut;

private JLabel lblTotal;

private JTextField txtTotal;

private PizzaService service = new PizzaService();

public static void main(String[] args) {

   SwingUtilities.invokeLater(new Runnable() {

     @Override

     public void run() {

       new PizzaApp();

     }

   });

}

public PizzaApp() {

   initialiseComponent();

}

private void initialiseComponent() {

   JPanel pane = new JPanel();

   pane.setLayout(new GridBagLayout());

   GridBagConstraints gbc = new GridBagConstraints();

   gbc.insets = new Insets(4, 4, 4, 4);

   gbc.gridx = 0;

   gbc.gridy = 0;

   gbc.anchor = GridBagConstraints.WEST;

   lblPizza = new JLabel("Pizza:");

   pane.add(lblPizza, gbc);

   gbc.gridx = 1;

   cboPizza = new JComboBox<String>(service.getAllPizzas());

   pane.add(cboPizza, gbc);

   gbc.gridx = 0;

   gbc.gridy = 1;

   lblSize = new JLabel("Size:");

   pane.add(lblSize, gbc);

   gbc.gridx = 1;

   cboSize = new JComboBox<String>(service.getAllSizes());

   pane.add(cboSize, gbc);

   gbc.gridx = 0;

   gbc.gridy = 2;

   lblWithCheese = new JLabel("Cheese?");

   pane.add(lblWithCheese, gbc);

   gbc.gridx = 1;

   chkWithCheese = new JCheckBox();

   pane.add(chkWithCheese, gbc);

   gbc.gridx = 0;

   gbc.gridy = 3;

   lblTopping = new JLabel("Toppings:");

   pane.add(lblTopping, gbc);

   gbc.gridx = 1;

   lstToppings = new JList<String>(service.getAllToppings());

   lstToppings.setSelectedIndex(0);

   pane.add(lstToppings, gbc);

   gbc.gridy = 4;

   btnCheckOut = new JButton("Place Order");

   btnCheckOut.addActionListener(this);

   pane.add(btnCheckOut, gbc);

   gbc.gridx = 0;

   gbc.gridy = 5;

   lblTotal = new JLabel("Total");

   pane.add(lblTotal, gbc);

   gbc.gridx = 1;

   txtTotal = new JTextField(6);

   txtTotal.setText("0.00");

   txtTotal.setEditable(false);

   pane.add(txtTotal, gbc);

   getContentPane().add(pane);

   setTitle("Pizza Selector");

   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   setSize(WIDTH, HEIGHT);

   setVisible(true);

}

@Override

public void actionPerformed(ActionEvent e) {

   if (btnCheckOut == e.getSource()) {

     calculateOrder();

   }

}

private void calculateOrder() {

   int numberOfToppingsSelected = lstToppings.getSelectedIndices().length;

   String size = (String) cboSize.getSelectedItem();

   DecimalFormat df = new DecimalFormat("#,##0.00");

   txtTotal.setText(df.format(service.getOrderPrice(size, numberOfToppingsSelected)));

}

}

class PizzaService {

public String[] getAllPizzas() {

   String[] pizzas = {"Pepperoni", "Mushrooms", "greenPeppers, "eggplant

", "Black Olives"};

   return pizzas;

}

public String[] getAllSizes() {

   String[] sizes = {"Small", "Medium", "Large", "X Large"};

   return sizes;

}

public String[] getAllToppings() {

   String[] toppings = {"Toppings 1", "Toppings 2", "Toppings 3", "Toppings 4", "Toppings 5"};

   return toppings;

}

private double getPizzaBasePrice() {

   return 5.0;

}

private double getPriceBySize(String size) {

   double result = 0;

   if (size.equals("Medium")) {

     result = 1.0;

   } else if (size.equals("Large")) {

     result = 2.0;

   } else if (size.equals("X Large")) {

     result = 3.0;

   }

   return result;

}

private double getPriceByNumberOfToppingsSelected(int numberOfToppings) {

   return Math.max((numberOfToppings - 1), 0);

}

public double getOrderPrice(String size, int numberOfToppings) {

   double result = getPizzaBasePrice();

   result += getPriceBySize(size);

   result += getPriceByNumberOfToppingsSelected(numberOfToppings);

   return result;

}

}