Skip to content

Instantly share code, notes, and snippets.

@markheramis
Last active November 2, 2024 10:14
Show Gist options
  • Select an option

  • Save markheramis/4506d38a7f79fc76fc9b921fb64fbb46 to your computer and use it in GitHub Desktop.

Select an option

Save markheramis/4506d38a7f79fc76fc9b921fb64fbb46 to your computer and use it in GitHub Desktop.
Change Calculator

Change Calculator

Description

Change.java is a simple Java program designed to assist cashiers by calculating the total cost of an item, including sales tax, and determining the change due to the customer. It provides a summary of all relevant figures, ensuring a quick and accurate checkout process.

Features

  • Prompts the user to enter the cost of an item.
  • Applies a 6% sales tax to the item cost.
  • Calculates the total cost, including tax.
  • Prompts for the amount tendered by the customer.
  • Outputs a summary showing:
    • Original cost of the item
    • Sales tax amount (6%)
    • Total cost with tax
    • Amount tendered by the customer
    • Change due to the customer

Usage

  1. Compile the program:
javac Change.java
  1. Run the program:
java Change
  1. Follow the prompts:
  • Enter the cost of the item (e.g., 10.00).
  • Enter the amount tendered by the customer (e.g., 20.00).
  1. View the output summary, which should look similar to:
Enter the cost of the item: 10.00
Enter the amount tendered by the customer: 20.00

--- Summary ---
Item Cost: $10.00
Sales Tax (6%): $0.60
Total with Tax: $10.60
Amount Tendered: $20.00
Change Due: $9.40

Example Output

Enter the cost of the item: 15.00
Enter the amount tendered by the customer: 20.00

--- Summary ---
Item Cost: $15.00
Sales Tax (6%): $0.90
Total with Tax: $15.90
Amount Tendered: $20.00
Change Due: $4.10

Notes

  • Ensure that the amount tendered by the customer is equal to or greater than the total cost. Otherwise, the change due will be a negative value, indicating an insufficient payment.
  • Modify the taxRate variable in the code if a different sales tax rate is required.
import java.util.Scanner;
/**
*
* @author Mark
*/
public class Change {
private final double tax = 0.06;
private double cost;
private double payment;
public double getCost() {
return this.cost;
}
public double getTax() {
return this.tax;
}
public double getPayment() {
return this.payment;
}
public void setCost(double cost) {
this.cost = cost;
}
public void setPayment(double payment) {
this.payment = payment;
}
public static void main (String [] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the cost of the item: ");
double cost = scan.nextDouble();
System.out.print("Enter the amount tendered by the customer: ");
double payment = scan.nextDouble();
Change change = new Change();
change.setCost(cost);
change.setPayment(payment);
System.out.println("--- Summary ---");
System.out.println("Item Cost: " + change.getCost());
// Convert double value (0.06) to its integer equivallent (6)
// so we can display it as "6%"
int taxPercent = (int) (change.getTax() * 100);
double taxedAmount = change.getTax() * change.getCost();
System.out.println("Sales Tax (" + taxPercent + "%): " + taxAmount);
// Calcualte total with tax by adding cost and taxedAmount
double totalWithTax = cost + taxedAmount;
System.out.println("Total with Tax: " + totalWithTax);
System.out.println("Amount Tendered: " + payment);
// Calculate Change Due by subtracting payment with totalWithTax
double changeDue = payment - totalWithTax;
System.out.println("Change Due: " + changeDue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment