In: Computer Science
JAVA Point of Sale System
The McDowell Restaurant chain has asked you to write a menu program
for their new Fast-food
service machines. Your program already prints the following menu
like this:
**********************************************************************
McDowell’s Restaurant
**********************************************************************
Make your selection from the menu below:
1. Regular Hamburger $1.50
2. Regular Cheeseburger $1.75
3. Fish Sandwich $2.50
4. Half-pounder with cheese $2.75
5. French Fries $0.99
6. Large Soft Drink $1.25
***********************************************************************
Select 1, 2, 3, 4, 5, or 6 ----- >
Your program must now read the customer’s selection and compute the
total price of their
purchase, including 6.5% sales tax.
Input
The first line of input represents N, the number of test cases. The
additional lines consists of a
sequence of integers scoped between 1 to 6. Each number should
indicate a selection from the
above menu to be purchased.
Output
The program should print the sum of the orders plus tax as “Please
pay $<dollars>”, where
<dollar> is the total amount of the purchase then conclude
with “Thank you for eating at
McDowell’s”.
Sample Input Sample Output
1
1 4 4 5 3 1 Please pay $12.77
Thank you for eating at McDowell’s!
/******************************
* McDowellRestaurantChain.java
*
******************************/
import java.text.DecimalFormat;
import java.util.Scanner;
//The class: McDowellRestaurantChain
public class McDowellRestaurantChain {
//menu and price are taken in a constant static array
of equal length
//where ith element in MENU array represent the
(i+1)th menu name and
//ith element in PRICE array represents the
corresponding menu's price
private static final String[] MENU = {"Regular
Hamburger","Regular Cheeseburger","Fish Sandwich",
"Half-pounder
with cheese","French Fries","Large Soft Drink"};
private static final double[] PRICE =
{1.50,1.75,2.50,2.75,0.99,1.25};
private static final double SALES_TAX = 0.065; //6.5%
sales tax as constant
//the main method
public static void main(String[] args){
printMenu(); //prints menu
first
Scanner sc = new
Scanner(System.in); //create the keyboard scanner
try{
int
numOfTestCases = Integer.parseInt(sc.nextLine()); //get the number
of test cases
//prompt for
user entry for <numOfTestCases> times to compute total for
each test case
for(int i = 1; i
<= numOfTestCases ; i++){
//get the current line entries
//expecting the entries as space separated
integer
String currentEntry = sc.nextLine();
String[] data = currentEntry.split(" "); //split
it by space
int[] selections = new int[data.length];//create
integer array of splitted data array length
//conver string[] into int[]
for(int p = 0 ; p < data.length; p++){
selections[p] =
Integer.parseInt(data[p]);
}
printTotalPay(selections); //print the total pay
using current tst case's selection
System.out.println("Thank you for eating at
McDowell’s!");
}
}catch(NumberFormatException
nfe){
System.out.println("Invalid input"); //if any issue with string to
integer conversion
}
sc.close();//close scanner
}
/**
* print the menu
*/
private static void printMenu(){
DecimalFormat df = new
DecimalFormat("#.00");//this will be used to format the price in 2
decimal digit
System.out.println("**********************************************************************");
System.out.println("McDowell’s
Restaurant");
System.out.println("**********************************************************************");
System.out.println("Make your
selection from the menu below:");
for(int i = 0; i < MENU.length;
i++){
System.out.println((i+1)+". "+MENU[i]+"
"+"$"+df.format(PRICE[i]));
}
System.out.println("**********************************************************************");
System.out.println("Select 1, 2, 3,
4, 5, or 6 ----- >");
}
/**
* calculate and print total pay including sales
tax
* using the selections array
* @param selections
*/
private static void printTotalPay(int[]
selections){
double total = 0;
DecimalFormat df = new
DecimalFormat("#.00"); //this will be used to format the output in
2 decimal digit
for(int currentSelection:
selections){
total = total +
PRICE[currentSelection -1];//get the price from PRICE array
}
double finalTotal = total + (total
* SALES_TAX);
System.out.println("Please pay
$"+df.format(finalTotal));
}
}
======================================
OUTPUT
======================================
RUN1
**********************************************************************
McDowell’s Restaurant
**********************************************************************
Make your selection from the menu below:
1. Regular Hamburger $1.50
2. Regular Cheeseburger $1.75
3. Fish Sandwich $2.50
4. Half-pounder with cheese $2.75
5. French Fries $.99
6. Large Soft Drink $1.25
**********************************************************************
Select 1, 2, 3, 4, 5, or 6 ----- >
1
1 4 4 5 3 2
Please pay $13.04
Thank you for eating at McDowell’s!
RUN2
**********************************************************************
McDowell’s Restaurant
**********************************************************************
Make your selection from the menu below:
1. Regular Hamburger $1.50
2. Regular Cheeseburger $1.75
3. Fish Sandwich $2.50
4. Half-pounder with cheese $2.75
5. French Fries $.99
6. Large Soft Drink $1.25
**********************************************************************
Select 1, 2, 3, 4, 5, or 6 ----- >
3
1 4 4 5 3 1 2
Please pay $14.63
Thank you for eating at McDowell’s!
2 4 5 1 2 3 4 5 1 2
Please pay $19.41
Thank you for eating at McDowell’s!
2 2 2 3 1 3 4
Please pay $15.44
Thank you for eating at McDowell’s!