Create an interface for a program that helps a group of people to calculate how much each of them owes for a restaurant meal that looks like the one below. The interface does not need to do anything in response to user clicks. Total: Tip Percentage: Number of People: Check Divider Each person pays: Calculate
Create an interface for a program that helps a group of people to calculate how much each of them owes for a restaurant meal that looks like the one below. The interface does not need to do anything in response to user clicks. Total: Tip Percentage: Number of People: Check Divider Each person pays: Calculate
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
code needs to be written in java? view below
Expert Solution
Step 1
import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;
public class RestaurantBillCalculator implements Runnable {
private JLabel totalLabel, tipLabel, numPeopleLabel, eachPaysLabel;
private JTextField totalField, tipField, numPeopleField, eachPaysField;
private JButton calculateButton;
public void run() {
// create the components
totalLabel = new JLabel("Total:");
tipLabel = new JLabel("Tip Percentage:");
numPeopleLabel = new JLabel("Number of People:");
eachPaysLabel = new JLabel("Each person pays:");
totalField = new JTextField(10);
tipField = new JTextField(10);
numPeopleField = new JTextField(10);
eachPaysField = new JTextField(10);
eachPaysField.setEditable(false);
calculateButton = new JButton("Calculate");
// create the panel
JPanel panel = new JPanel(new GridLayout(5, 1));
panel.add(totalLabel);
panel.add(totalField);
panel.add(tipLabel);
panel.add(tipField);
panel.add(numPeopleLabel);
panel.add(numPeopleField);
panel.add(eachPaysLabel);
panel.add(eachPaysField);
// create the frame and add the panel
JFrame frame = new JFrame("Restaurant Bill Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
// add the button listener
calculateButton.addActionListener(e -> calculate());
// add the button to the panel
panel.add(calculateButton);
}
private void calculate() {
// get the inputs
double total = Double.parseDouble(totalField.getText());
double tipPercentage = Double.parseDouble(tipField.getText());
int numPeople = Integer.parseInt(numPeopleField.getText());
// calculate the result
double tipAmount = total * tipPercentage / 100;
double totalWithTip = total + tipAmount;
double eachPays = totalWithTip / numPeople;
// set the result field
DecimalFormat df = new DecimalFormat("0.00");
eachPaysField.setText(df.format(eachPays));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new RestaurantBillCalculator());
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education