Question: package lab12; import java.awt.*; import java.awt.event.*; import javax.swing.*; //Sammy Student public class CoffeeMachine extends JFrame implements ActionListener { private static final long serialVersionUID = 1L;
| package lab12; import java.awt.*; import java.awt.event.*; import javax.swing.*; //Sammy Student public class CoffeeMachine extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; JLabel l1, l2, l3, l4, l5, l6; JButton b1, b2; JTextField t1, t2, t3; JCheckBox cream, raw, espresso; private JRadioButton small; private JRadioButton large; private ButtonGroup group; CoffeeMachine() { l1 = new JLabel(" Customer Name"); l2 = new JLabel(" amount to pay"); l3 = new JLabel(" "); l4 = new JLabel(" "); l5 = new JLabel(" "); l6 = new JLabel(" "); b1 = new JButton("COMPUTE"); b2 = new JButton("EXIT"); t1 = new JTextField(10); t2 = new JTextField(10); small = new JRadioButton("small", true); large = new JRadioButton("large", false); group = new ButtonGroup(); group.add(small); group.add(large); cream = new JCheckBox("cream", false); raw = new JCheckBox("raw sugar", false); espresso = new JCheckBox("espresso shot", false); add(l1); add(t1); add(small); add(cream); add(large); add(raw); add(l3); add(espresso); add(l2); add(t2); add(l5); add(l6); add(b1); add(b2); b1.addActionListener(this); b2.addActionListener(e -> System.exit(0)); setSize(500,300); setLayout(new GridLayout(7,2)); setTitle("Coffee Machine"); } public void actionPerformed(ActionEvent ae) { float price = 0; String a = "", message = ""; if (cream.isSelected() == true) { // perform a task ... } if (raw.isSelected() == true) { // perform a task ... } if (espresso.isSelected() == true) { // perform a task ... } if (small.isSelected() == true) { System.out.println("small"); // perform a task ... } if (large.isSelected() == true) { System.out.println("large"); // perform a task ... } if(ae.getSource() == b1) { a = t1.getText(); message = "your order: " + a + " $" + price; t2.setText("thank you: " + a ); } JOptionPane.showMessageDialog(null, "Summary: " + message, "Order Summary", JOptionPane.PLAIN_MESSAGE); } public static void main(String args[]) { CoffeeMachine a = new CoffeeMachine(); a.setVisible(true); a.setLocation(200,200); } } |
Your completed program ( after modification ) will perform, at the minimum, each of the following tasks
allow the user to enter their name in a text field element
allow the user to select a coffee / beverage size, whose prices are given as:
( small : $ 1.25 ; medium : $ 1.75 ; large : $ 2.30 )
allow the user to select none, one or more of: cream, raw sugar, espresso shot ( espresso shot price : $ 0.75 )
when the [ COMPUTE ] button is clicked a text field will display a message to thank the program user and a message box display summary of the users coffee order.
when the [ EXIT ] button is clicked the application will close
Modifications
1. With your initial starter program tested and functioning properly, you will now alter the program such that when the user clicks the [ COMPUTE ] button The amount to pay will appear in the output text field and an order summary will appear in the message box.
2. You will now expand your programs layout by including a new row of GUI elements. To accomplish this, perform these tasks: Add a new radio button to the Radio Button Group. This button will allow the program user to select a medium coffee beverage. The users may now be able to select from either a small, medium or large cup of coffee. In the same GUI row, add a label that will be used as a spacer element. Set the text property of the label to an empty string.
3. Add some exception handling to your code. Error trap any numeric characters input into textfield t1 by the user and allow for reinput if that is the case. Also add in an animated gif at the bottom area of your app, perhaps a cup of freshly brewed steaming coffee! Save your program and perform a trial run of it. Test your program with data similar to that shown below.
4. Add a JMenuBar() to your application. Your GUI is to appear as shown in this screen snapshot segment, which shows two JMenu() objects namely File and Help. The File menu will allow you to exit the running application and the Help menu will show information About the programmer and the program.
Here are some code statements that you can implement to construct the JMenuBar() . Place these statements in an appropriate area of your class file.
// create the menu bar
JMenuBar menuBar = new JMenuBar();
add(menuBar);
Build the menu with these menu items:
// add the File menu
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
// add a File menu item
JMenuItem mntmExit = new JMenuItem("Exit");
mnFile.add(mntmExit);
// add the Help menu
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
// add a Help menu item
JMenuItem mntmAbout = new JMenuItem("About");
mnHelp.add(mntmAbout);
// display the menu bar on the frame
setJMenuBar(menuBar);
Add now the listeners which will allow menu items to interact with the program.
// the Exit menu item listener
mntmExit.addActionListener(e -> System.exit(0));
// the Help menu item listener
mntmAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{ JOptionPane.showMessageDialog(null, "Your Message", "About", JOptionPane.PLAIN_MESSAGE); } });
For your [ Help ] -> [ About ] menu item write a multi - line message which lists you as the programmer and also includes some information about this application.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
