create a cash register program using jave programming, that includes three class
ID: 3569026 • Letter: C
Question
create a cash register program using jave programming, that includes three classes, cash register, transaction, and an item class. Use GUI app if needed.
Create a program that simulates a standard Cash Register that meets the following requirements:
1) Stores separate sales tax rates for food and non-food items. The rate for food items will be 2% and the rate for non-food items will be 4%.
2) The program will prompt the user to select the tax type of the item (food or non-food) and then to 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 price for each item, the subtotal, calculate the two separate sales taxes and display each, and then display a 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.
Hints
Create an Item class that is made up from an Item Type (food or non-food) and and Item Price. Then create an array of Item objects. Item Type could be represented by a Boolean variable called something like
Explanation / Answer
Java Program for cash Register program :
import java.util.Scanner;
public class cashRegister
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
final double foodTax = .2;
final double nonFoodTax = .4;
final int TOTALLIMIT = 100;
registerObjects [] array = new registerObjects[TOTALLIMIT];
String y,f,n = null;
int i = 0;
do
{
registerObjects groceries = new registerObjects();
System.out.println("Is this a food item(f) or a non=food item(n): ");
f = input.next();
if(f.toLowerCase().charAt(0) == 'f')
{
do
{
System.out.println("Enter the price of the first item: ");
double food = input.nextDouble();
groceries.setFood(food);
array[i++] = groceries;
System.out.println("Do you wish to add more items (y/n): ");
y = input.next();
}while(y.toLowerCase().charAt(0) == 'y')
else
{
do
{
System.out.println("Enter the price of the first item: ");
double nonFood = input.nextDouble();
groceries.setNonFood(nonFood);
array[i++] = groceries;
System.out.println("Do you wish to add more items (y/n): ");
y = input.next();
}while(y.toLowerCase().charAt(0) == 'y');
}
}
System.out.println("Your food items are " + groceries);
input.close();
}
private static void calculate(registerObjects [] array, Scanner input)
{
}
}
public class registerObjects
{
double food;
double nonFood;
public double getFood()
{
return food;
}
public void setFood(double food)
{
this.food = food;
}
public double getNonFood()
{
return nonFood;
}
public void setNonFood(double nonFood)
{
this.nonFood = nonFood;
}
public void display()
{
System.out.println("Your food items: " + food);
System.out.println("Your non-food items: " + nonFood);
}
}
Program to Display the amount of change owed the customer including a breaking down the change owed in dollars, quarters, dimes, nickels and pennies :
package cashregister;
/**
* A cash register totals up sales and computes change due.
*/
public class CashRegister
{
/** value of a quarter */
public static final double QUARTER_VALUE = 0.25;
/** value of a dime */
public static final double DIME_VALUE = 0.1;
/** value of a nickel */
public static final double NICKEL_VALUE = 0.05;
/** value of a penny */
public static final double PENNY_VALUE = 0.01;
/** the total amount of the purchase */
private double purchase;
/** the amount of change due to the customer */
private double changeDue;
/**
* Constructs a cash register with no money in it.
*/
public CashRegister()
{
purchase = 0;
changeDue = 0;
}
/**
* Records the purchase price of an item.
* @param amount the price of the purchased item
*/
public void recordPurchase(double amount)
{
purchase += amount;
}
/**
* Enters the payment received from the customer and
* calculates the change due.
* @param dollars the number of dollars in the payment
* @param quarters the number of quarters in the payment
* @param dimes the number of dimes in the payment
* @param nickels the number of nickels in the payment
* @param pennies the number of pennies in the payment
*/
public void enterPayment(int dollars, int quarters,
int dimes, int nickels, int pennies)
{
double payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE
+ nickels * NICKEL_VALUE + pennies * PENNY_VALUE;
changeDue = payment - purchase;
purchase = 0;
}
/**
* Returns the number of dollars to give as change and subtracts this from the amount of change due.
* @return number of dollars to give
*/
public int giveDollars()
{
int dollars = (int) changeDue;
changeDue -= dollars;
return dollars;
}
/**
* Returns the number of quarters to give as change and subtracts this from the amount of change due.
@return number of quarters to give
*/
public int giveQuarters()
{
int quarters = (int) (changeDue / QUARTER_VALUE);
changeDue -= quarters * QUARTER_VALUE;
return quarters;
}
/**
* Returns the number of dimes to give as change and subtracts this from the amount of change due.
* @return number of dimes to give
*/
public int giveDimes()
{
int dimes = (int) (changeDue / DIME_VALUE);
changeDue -= dimes * DIME_VALUE;
return dimes;
}
/**
* Returns the number of nickels to give as change and subtracts this from the amount of change due.
* @return number of nickels to give
*/
public int giveNickels()
{
int nickels = (int) (changeDue / NICKEL_VALUE);
changeDue -= nickels * NICKEL_VALUE;
return nickels;
}
/**
* Returns the number of pennies to give as change and subtracts this from the amount of change due. After calling this then the amount of change due should be zero.
* @return number of pennies to give
*/
public int givePennies()
{
int pennies = (int) (changeDue / PENNY_VALUE);
changeDue -= pennies * PENNY_VALUE;
return pennies;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.