Create a program that simulates a standard Cash Register tha
Create a program that simulates a standard Cash Register that means the following requirements:
1. Stores separate sales tax rates for food and non-food item. The rate for the food items will be 3.5% and non-food items will be 7%.
2. The program will prompt user to select the tax type for the items and enter the price of the items.
3. The program will ask the user after each item whether the user wants to enter another item. If the user selects yes then the program should repeat prompting the user for item type and price. If the user selects no then the program will stop prompting for item entry.
4. The program will then display the subtotal, calculate the two separate sales tax rates and then display total.
5. The program will then prompt the user to enter the amount tendered by the customer and then display the amount of change owed the customer including a breaking down the change owed in dollars, quarters, dimes, nickels and pennies.
(there was a similar question already answered but the solution does not work)
Solution
/**
* The java program CashRegister that prompts user to enter type of food
* (f or n). Then prompt for name of food and cost of food and then
* reprompt until user enters n to stop .Then prints
* subtotal, tax for food and tax for non food and
* total value and then prompt for pay amount and then
* subtract totalamount from pay and print change to console.
* */
//CashRegister.java
import java.util.ArrayList;
import java.util.Scanner;
public class CashRegister
{
public static void main(String[] args)
{
//Create a Scanner class object
Scanner scanner = new Scanner(System.in);
//Tax for food , 3.5/100=0.035
final double foodTax = .035;
//Tax for non food , 7/100=0.07
final double nonFoodTax = .07;
//Create an instance of Array list of type Item
ArrayList<Item> foodItems=new ArrayList<Item>();
String foodName;
String userChoice=\"y\";
Item item=null;
do
{
System.out.println(\"Is this a food item(f) or a non=food item(n): \");
char type = scanner.nextLine().charAt(0);
System.out.println(\"Enter food name of Item: \");
foodName = scanner.nextLine();
System.out.println(\"Enter price of Item: \");
double cost = Double.parseDouble(scanner.nextLine());
if(type == \'f\')
//Create an object of food type ,f
item=new Item(foodName, cost, foodTax,\'f\');
else
//Create an object of food type ,n
item=new Item(foodName, cost, nonFoodTax,\'n\');
foodItems.add(item);
System.out.println(\"Do you wish to add more items (y/n): \");
userChoice = scanner.nextLine();
}while(userChoice.toLowerCase().charAt(0) == \'y\');
System.out.println(\"Items purchased\");
//print food items to console
for (Item itm : foodItems) {
System.out.println(itm);
}
double totalCost=0;
for (Item itm : foodItems) {
totalCost+=itm.getCost();
}
System.out.printf(\"Sub-Total : %5.2f\ \",totalCost);
double taxForFood=calculateFoodTax(foodItems);
System.out.printf(\"Food tax : %5.2f\ \",taxForFood);
double taxForNonFood=calculateNonFoodTax(foodItems);
System.out.printf(\"Non-Food tax : %5.2f\ \",taxForNonFood);
//calcualte total cost using tax for food and non food itesm
totalCost+=taxForFood+taxForNonFood;
System.out.printf(\"Total cost : %5.2f\ \",totalCost);
System.out.println(\"Enter your pay amount :\");
double pay=scanner.nextDouble();
double change=(pay-totalCost);
System.out.printf(\"Change :%5.2f\ \",change);
}
//Returns the nonfood items tax cost of items in the food items list
private static double calculateNonFoodTax(ArrayList<Item> foodItems) {
double foodtax=0;
for (Item itm : foodItems) {
if(itm.getType()==\'n\')
foodtax+=itm.getTax();
}
return foodtax;
}
//Returns the food items tax cost of items in the food items list
private static double calculateFoodTax(ArrayList<Item> foodItems) {
double nonfoodtax=0;
for (Item itm : foodItems) {
if(itm.getType()==\'f\')
nonfoodtax+=itm.getTax();
}
return nonfoodtax;
}
}//end of the class
----------------------------------------------------------------------------------------------------------------
/**
* The Item class contains instance variables
* to store, name,cost,tax and type
* */
//Item.java
public class Item {
private String foodName;
private double cost;
private double tax;
private char type;
public Item(String foodName, double cost,
double tax,char type) {
this.foodName=foodName;
this.cost=cost;
this.tax=tax;
this.type=type;
}
public char getType(){
return type;
}
public String getFoodName(){
return foodName;
}
public double getCost(){
return cost;
}
public double getTax(){
return cost*tax;
}
@Override
public String toString() {
return String.format(\"Food Name : %10s Cost : %5.2f\", foodName,cost);
}
}
---------------------------------------------------------------------------------
Sample Output:
Is this a food item(f) or a non=food item(n):
f
Enter food name of Item:
Pizza
Enter price of Item:
100
Do you wish to add more items (y/n):
y
Is this a food item(f) or a non=food item(n):
n
Enter food name of Item:
Drinks
Enter price of Item:
200
Do you wish to add more items (y/n):
n
Items purchased
Food Name : Pizza Cost : 100.00
Food Name : Drinks Cost : 200.00
Sub-Total : 300.00
Food tax : 3.50
Non-Food tax : 14.00
Total cost : 317.50
Enter your pay amount :
400
Change :82.50



