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

Write a class called IceCreamStand that allows the user to \"purchase\" ice crea

ID: 3535680 • Letter: W

Question

Write a class called IceCreamStand that allows the user to "purchase" ice cream. On the top of the window there should be three buttons and a combo box. The buttons should be labeled "Small":, "Medium", and "Large". The combo box should include five different ice cream flavors. Below each button there should be a label that displays the price of that particular size of ice cream. When the user clicks a button, the bill should be displayed in the middle of the window as a TextArea. The bill should include the size, flavor, and price of the ice cream and should grow as the user orders more ice cream. Also, the bill should display the total cost of all the ice creams on the bill. On the bottom of the window, there should be a "New Order" button that resets the bill so that it is blank

Explanation / Answer

import java.awt.EventQueue;


import javax.swing.AbstractAction;

import javax.swing.JFrame;

import javax.swing.GroupLayout;

import javax.swing.GroupLayout.Alignment;

import javax.swing.JPanel;

import javax.swing.LayoutStyle.ComponentPlacement;

import javax.swing.JLabel;

import javax.swing.JComboBox;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.Font;


import javax.swing.JTextArea;



public class IceCreamStand {


private JFrame frmPurchaseIceCream;

private JTextArea textAreaBill = new JTextArea();

private JComboBox comboBoxFlavor = null;

private String billItemsDescription = "";

private int totalBillPrice = 0;


/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

IceCreamStand window = new IceCreamStand();

window.frmPurchaseIceCream.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}


/**

* Create the application.

*/

public IceCreamStand() {

initialize();

}


/**

* Initialize the contents of the frame.

*/

private void initialize() {

frmPurchaseIceCream = new JFrame();

frmPurchaseIceCream.setTitle("Purchase Ice Cream");

frmPurchaseIceCream.setSize(700, 500);

frmPurchaseIceCream.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frmPurchaseIceCream.setLocationRelativeTo(null);

frmPurchaseIceCream.setResizable(false);

JPanel sizePanel = new JPanel();

JPanel panel = new JPanel();

JLabel labelFlavor = new JLabel("Select Flavor");

JPanel panel_1 = new JPanel();

String[] flavorStrings = { "Flavor 1", "Flavor 2", "Flavor 3", "Flavor 4", "Flavor 5"};

comboBoxFlavor = new JComboBox(flavorStrings);

GroupLayout gl_panel_1 = new GroupLayout(panel_1);

gl_panel_1.setHorizontalGroup(

gl_panel_1.createParallelGroup(Alignment.LEADING)

.addGroup(gl_panel_1.createSequentialGroup()

.addGap(33)

.addComponent(comboBoxFlavor, GroupLayout.PREFERRED_SIZE, 132, GroupLayout.PREFERRED_SIZE)

.addContainerGap(160, Short.MAX_VALUE))

);

gl_panel_1.setVerticalGroup(

gl_panel_1.createParallelGroup(Alignment.TRAILING)

.addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()

.addContainerGap()

.addComponent(comboBoxFlavor, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)

.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

panel_1.setLayout(gl_panel_1);

GroupLayout gl_panel = new GroupLayout(panel);

gl_panel.setHorizontalGroup(

gl_panel.createParallelGroup(Alignment.LEADING)

.addGroup(gl_panel.createSequentialGroup()

.addGap(17)

.addComponent(labelFlavor, GroupLayout.PREFERRED_SIZE, 173, GroupLayout.PREFERRED_SIZE)

.addGap(18)

.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 325, GroupLayout.PREFERRED_SIZE)

.addContainerGap(119, Short.MAX_VALUE))

);

gl_panel.setVerticalGroup(

gl_panel.createParallelGroup(Alignment.TRAILING)

.addGroup(gl_panel.createSequentialGroup()

.addComponent(labelFlavor, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)

.addContainerGap())

.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()

.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)

.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

panel.setLayout(gl_panel);

JPanel panel_2 = new JPanel();

JLabel lblBill = new JLabel("Bill");

lblBill.setFont(new Font("Tahoma", Font.BOLD, 15));

//JTextArea textAreaBill = new JTextArea();

JButton btnNewOrder = new JButton("New Order");

GroupLayout gl_panel_2 = new GroupLayout(panel_2);

gl_panel_2.setHorizontalGroup(

gl_panel_2.createParallelGroup(Alignment.LEADING)

.addGroup(gl_panel_2.createSequentialGroup()

.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)

.addGroup(gl_panel_2.createSequentialGroup()

.addGap(256)

.addComponent(lblBill, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE))

.addGroup(gl_panel_2.createSequentialGroup()

.addGap(44)

.addComponent(textAreaBill, GroupLayout.PREFERRED_SIZE, 560, GroupLayout.PREFERRED_SIZE))

.addGroup(gl_panel_2.createSequentialGroup()

.addGap(257)

.addComponent(btnNewOrder)))

.addContainerGap(48, Short.MAX_VALUE))

);

gl_panel_2.setVerticalGroup(

gl_panel_2.createParallelGroup(Alignment.TRAILING)

.addGroup(Alignment.LEADING, gl_panel_2.createSequentialGroup()

.addComponent(lblBill, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)

.addPreferredGap(ComponentPlacement.RELATED)

.addComponent(textAreaBill, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE)

.addPreferredGap(ComponentPlacement.RELATED)

.addComponent(btnNewOrder)

.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

panel_2.setLayout(gl_panel_2);

GroupLayout groupLayout = new GroupLayout(frmPurchaseIceCream.getContentPane());

groupLayout.setHorizontalGroup(

groupLayout.createParallelGroup(Alignment.TRAILING)

.addGroup(groupLayout.createSequentialGroup()

.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)

.addGroup(groupLayout.createSequentialGroup()

.addContainerGap()

.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 652, GroupLayout.PREFERRED_SIZE))

.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)

.addGroup(groupLayout.createSequentialGroup()

.addGap(32)

.addComponent(sizePanel, GroupLayout.DEFAULT_SIZE, 652, Short.MAX_VALUE))

.addGroup(groupLayout.createSequentialGroup()

.addContainerGap(32, Short.MAX_VALUE)

.addComponent(panel, GroupLayout.PREFERRED_SIZE, 652, GroupLayout.PREFERRED_SIZE))))

.addContainerGap())

);

groupLayout.setVerticalGroup(

groupLayout.createParallelGroup(Alignment.LEADING)

.addGroup(groupLayout.createSequentialGroup()

.addContainerGap()

.addComponent(sizePanel, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)

.addPreferredGap(ComponentPlacement.UNRELATED)

.addComponent(panel, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)

.addGap(18)

.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 250, GroupLayout.PREFERRED_SIZE)

.addContainerGap(63, Short.MAX_VALUE))

);

JPanel rdbtnpanel = new JPanel();

JButton btnSmall = new JButton("Small");

JButton btnMedium = new JButton("Medium");

btnMedium.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

}

});

JButton btnLarge = new JButton("Large");

btnSmall.addActionListener(new ActionAddSelectedIcream("Small",5));

btnMedium.addActionListener(new ActionAddSelectedIcream("Medium",10));

btnLarge.addActionListener(new ActionAddSelectedIcream("Large",15));

JLabel lblNewLabel = new JLabel("$5");

lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));

JLabel label = new JLabel("$10");

label.setFont(new Font("Tahoma", Font.BOLD, 15));

JLabel label_1 = new JLabel("$15");

label_1.setFont(new Font("Tahoma", Font.BOLD, 15));

GroupLayout gl_rdbtnpanel = new GroupLayout(rdbtnpanel);

gl_rdbtnpanel.setHorizontalGroup(

gl_rdbtnpanel.createParallelGroup(Alignment.LEADING)

.addGroup(gl_rdbtnpanel.createSequentialGroup()

.addGap(28)

.addGroup(gl_rdbtnpanel.createParallelGroup(Alignment.LEADING)

.addGroup(gl_rdbtnpanel.createSequentialGroup()

.addGap(10)

.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)

.addGap(40)

.addComponent(label, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)

.addGap(42)

.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))

.addGroup(gl_rdbtnpanel.createSequentialGroup()

.addComponent(btnSmall)

.addPreferredGap(ComponentPlacement.RELATED)

.addComponent(btnMedium)

.addPreferredGap(ComponentPlacement.RELATED)

.addComponent(btnLarge, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)))

.addContainerGap(90, Short.MAX_VALUE))

);

gl_rdbtnpanel.setVerticalGroup(

gl_rdbtnpanel.createParallelGroup(Alignment.LEADING)

.addGroup(gl_rdbtnpanel.createSequentialGroup()

.addContainerGap()

.addGroup(gl_rdbtnpanel.createParallelGroup(Alignment.BASELINE)

.addComponent(btnSmall)

.addComponent(btnMedium)

.addComponent(btnLarge))

.addPreferredGap(ComponentPlacement.RELATED)

.addGroup(gl_rdbtnpanel.createParallelGroup(Alignment.LEADING)

.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)

.addComponent(label, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)

.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE))

.addContainerGap())

);

rdbtnpanel.setLayout(gl_rdbtnpanel);

JLabel lblSelectSize = new JLabel("Select Size");

GroupLayout gl_sizePanel = new GroupLayout(sizePanel);

gl_sizePanel.setHorizontalGroup(

gl_sizePanel.createParallelGroup(Alignment.LEADING)

.addGroup(gl_sizePanel.createSequentialGroup()

.addContainerGap()

.addComponent(lblSelectSize, GroupLayout.PREFERRED_SIZE, 173, GroupLayout.PREFERRED_SIZE)

.addGap(25)

.addComponent(rdbtnpanel, GroupLayout.PREFERRED_SIZE, 325, GroupLayout.PREFERRED_SIZE)

.addContainerGap(119, Short.MAX_VALUE))

);

gl_sizePanel.setVerticalGroup(

gl_sizePanel.createParallelGroup(Alignment.TRAILING)

.addComponent(rdbtnpanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)

.addGroup(Alignment.LEADING, gl_sizePanel.createSequentialGroup()

.addComponent(lblSelectSize, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)

.addContainerGap())

);

sizePanel.setLayout(gl_sizePanel);

frmPurchaseIceCream.getContentPane().setLayout(groupLayout);

btnNewOrder.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

IceCreamStand.this.comboBoxFlavor.setSelectedIndex(1);

IceCreamStand.this.billItemsDescription = "";

IceCreamStand.this.totalBillPrice = 0;

IceCreamStand.this.textAreaBill.setText("");

}

});

}

private class ActionAddSelectedIcream extends AbstractAction {

private String size;

private int price;

public ActionAddSelectedIcream(String size, int price) {

this.size = size;

this.price = price;

}

public void actionPerformed(ActionEvent e) {

String selectedFlavor = (String) IceCreamStand.this.comboBoxFlavor.getSelectedItem();

String textToAdd = " Size: "+this.size+" Flavor: "+selectedFlavor+" Price: "+this.price;

IceCreamStand.this.billItemsDescription += textToAdd;

totalBillPrice += this.price;

IceCreamStand.this.textAreaBill.setText(IceCreamStand.this.billItemsDescription+" Total Price: "+totalBillPrice);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote