Project Description Write an application that calculates the cost of the meal or
ID: 3886084 • Letter: P
Question
Project Description
Write an application that calculates the cost of the meal order based on an adult meal and child meal. The user will input the name of the client, the number of adult meals and the number of child meals and the school code, which is four-digit code. The cost of the meal ordered is based on the adult meal costing $7.00 and child meal costing $3.50. Display the client’s name, number of adult meals, number of child meals, and calculated total charge.
Input
Collect the school code, number of adult meal(s) and number of child meal(s) using a Scanner object. You must prompt the user for the School code on the console, then prompt the user for the number of adult meal(s), an then prompt the user for the number of child meal(s) on the console. Then ask the user for their client’s name via a JOptionPane dialog. box.
Processing
Use appropriate Scanner method(s) so the input is assigned to appropriate variables. The School code needs not be calculated, so must be collected as text. The meals (adult and child) will be used in a calculation, and therefore must be assigned to an appropriate decimal number variable. You will also retrieve the name from the JOptionPane. You will calculate the charge of the meal by using the $7.00 for the adult meal and $4.00 for the child meal.
The adult meal and child meal must be stored as a constant.
Output
Display the client’s name, the school code, the number of adult meal(s) and child meal(s) and the total charge each in separate labels. Ensure that you have appropriate labeling information as the sample below shows. In order to display in labels you will have to create a JFrame, GUI application with JPanel and JLabels. You will be using 10 JLabel objects – five for the prompts and five the information
Name John Smith
School code 1234
Adult meal 2
Adult meal 1
Meal Cost 18.00
***(your decimal places may well be different, and that is fine for this assignment)
Explanation / Answer
import java.awt.Color;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class CalculateMeal {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String school_code="";
String client_name="";
int adult_meal_count=0;
int child_meal_count=0;
double adult_meal_cost=7.0;
double child_meal_cost=4.0;
double totalCost;
System.out.println("Enter School Code(4 letters/digits only) ");
school_code=sc.next();
System.out.println("Enter Adult count: ");
adult_meal_count=sc.nextInt();
System.out.println("Enter Child count: ");
child_meal_count=sc.nextInt();
client_name=JOptionPane.showInputDialog("Enter Client Name: ");
totalCost = (adult_meal_count*adult_meal_cost)+(child_meal_cost*child_meal_count);
JFrame jframe=new JFrame("Calculating Meal Cost");
JPanel panel=new JPanel();
panel.setBounds(10, 100, 100,50);
JLabel clientName=new JLabel("Name");
JLabel client=new JLabel(client_name);
clientName.setBounds(10, 100, 80, 30);
client.setBounds(100, 100, 80, 30);
JLabel schoolLabel=new JLabel("School Code");
JLabel schoolValue=new JLabel(school_code);
schoolLabel.setBounds(10, 100, 100, 80);
schoolValue.setBounds(100, 100, 100, 80);
JLabel adultMeal=new JLabel("Adult Meal");
JLabel adultMealValue=new JLabel(""+adult_meal_count);
adultMeal.setBounds(10, 100, 120, 120);
adultMealValue.setBounds(100, 100, 120, 120);
JLabel childMeal=new JLabel("Child Meal");
JLabel childMealValue=new JLabel(""+child_meal_count);
childMeal.setBounds(10, 100, 140, 170);
childMealValue.setBounds(100, 100, 140, 170);
JLabel MealCost=new JLabel("Meal Cost");
JLabel MealCostValue=new JLabel(""+totalCost);
MealCost.setBounds(10, 100, 160, 220);
MealCostValue.setBounds(100, 100, 160, 220);
panel.setLayout(null);
panel.add(client);
panel.add(clientName);
panel.add(schoolValue);
panel.add(schoolLabel);
panel.add(adultMeal);
panel.add(adultMealValue);
panel.add(childMeal);
panel.add(childMealValue);
panel.add(MealCost);
panel.add(MealCostValue);
jframe.add(panel);
jframe.setSize(400, 400);
jframe.setVisible(true);
}
}
here, i am taking child meal cost 4.0 . if you want ,you can use 3.50 also.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.