Java Write a graphical application that produces a restaurant bill. YOU WILL NOT
ID: 3686181 • Letter: J
Question
Java
Write a graphical application that produces a restaurant bill. YOU WILL NOT BE GRADED ON LAYOUT/AESTHETICS. SOLEY ON FUNCTIONALITY AND ADHERANCE TO REQUIREMENTS.
Provide buttons for ten popular dishes or drink items. You decide on the items and their prices. (5 points)
Provide text fields for entering less popular items and prices (item separate field from its price, of course). (5 points)
In a text area, show the bill, including tax and a suggested tip after the user clicks a finish/calculate bill button. (5 points)
please make ONE action listener that can respond to all 10 menu item buttons.
don't use if statement
Explanation / Answer
package chegg;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.print.attribute.standard.MediaSize.Other;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Restaurant extends JFrame implements ActionListener {
private JButton[] menuItems;
private JTextField menuItem, menuItemPrice, totalBill;
private JButton calculateBill;
private JLabel item, price, totalPrice, tip, tipAmount;
private JTextArea showBill;
private double bill;
public Restaurant() {
initialize();
}
private void initialize() {
bill = 0;
menuItems = new JButton[10];
menuItems[0] = new JButton("Fries - $10.00");
menuItems[1] = new JButton("Sandwich - $15.00");
menuItems[2] = new JButton("Coke - $5.00");
menuItems[3] = new JButton("Nachos - $20.00");
menuItems[4] = new JButton("Sizzler - $30.00");
menuItems[5] = new JButton("Chicken wings - $10.00");
menuItems[6] = new JButton("Burger - $15.00");
menuItems[7] = new JButton("Beer - $12.00");
menuItems[8] = new JButton("Mojito - $10.00");
menuItems[9] = new JButton("Pizza - $20.00");
setLayout(new GridLayout(10, 2));
for (JButton button: menuItems) {
button.addActionListener(this);
add(button);
}
item = new JLabel("Other item");
add(item);
price = new JLabel("Price");
add(price);
menuItem = new JTextField();
add(menuItem);
menuItemPrice = new JTextField();
add(menuItemPrice);
totalPrice = new JLabel("Total bill = ");
add(totalPrice);
totalBill = new JTextField();
add(totalBill);
tip = new JLabel("Suggested tip");
add(tip);
tipAmount = new JLabel();
add(tipAmount);
calculateBill = new JButton("Calculate bill");
add(calculateBill);
calculateBill.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
switch(e.getActionCommand()) {
case "Calculate bill":
String s = menuItemPrice.getText();
if (s.length() > 0) {
double additionalCost = Double.parseDouble(s);
bill += additionalCost;
}
totalBill.setText(bill + "");
tipAmount.setText(bill*0.2 + "");
break;
case "Fries - $10.00":
bill += 10;
break;
case "Sandwich - $15.00":
bill += 15;
break;
case "Coke - $5.00":
bill += 5;
break;
case "Nachos - $20.00":
bill += 20;
break;
case "Sizzler - $30.00":
bill += 30;
break;
case "Chicken wings - $10.00":
bill += 10;
break;
case "Burger - $15.00":
bill += 15;
break;
case "Beer - $12.00":
bill += 12;
break;
case "Mojito - $10.00":
bill += 10;
break;
case "Pizza - $20.00":
bill += 20;
break;
}
}
public static void main(String[] args) {
Restaurant frame = new Restaurant();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(1000,1000);
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.