193k views
5 votes
The cash register program will have items. For each item, the name and cost are saved. For a transaction, it will accept any number of items (not known in advance) and any number of payments of different payment types (not known in advance). For each transaction, it calculates the total including a 7% sales tax and keeps prompting for a payment until the total has been paid. It also determines if any change is required. Thus, a transaction is responsible for calculating the amount of money that needs to be paid, accepting money, and generating a receipt. A receipt holds information about a transaction so that it can display how much each item cost, how the transaction was paid for, etc. (see example outputbelow).Payment type includes CASH, DEBIT_CARD, CREDIT_CARDandCHECK. Each payment consists of an amount and payment type

Hints
1. All fields should be private.
2. The PaymentType class should be an enum class with values: CASH, DEBIT_CARD, CREDIT_CARD, CHECK
3. Payment type information should not print the enum directly
4. Use final to store the transaction for each receipt.
5. Output:
A. The values for subtotal, tax, total, and payment information are all aligned. You can achieve this by using the tab character '\t' in your Strings. The item lines do not need to be aligned.
B. Do not worry about only using 2 decimal places. Let Java decide how many decimal places to use.
6. Create a main class to house the main method. In the main method, create some items, a transaction, and display the receipt.
A. Example outputs
Example: Pay everything with one payment
Transaction listing the items and asking for payment:
Item 1: apple: 0.5
Item 2: pear: 0.75
Item 3: pineapple: 0.75
Total: 2.14
Please enter payment type.
1. Cash
2. Debit card
3. Credit card
4. Check
2
Enter the amount to pay with this type.
2.14
Total after payment: 0.0
Receipt printed:
apple: 0.5
pear: 0.75
pineapple: 0.75
----------------------------------------------------------
Subtotal:
Tax:
Total:
Debit:
Change: 2.0 0.14 2.14 2.14 0.0
Example: Paying with multiple payments and payment type
Item 1: refrigerator: 800.71
Total: 856.7597000000001
Please enter payment type.
1. Cash
2. Debit card
3. Credit card
4. Check
1
Enter the amount to pay with this type.
400
Total after payment: 456.75970000000007
Please enter payment type.
1. Cash
2. Debit card
3. Credit card
4. Check
2
Enter the amount to pay with this type.
475
Total after payment: -18.240299999999934
refrigerator: 800.71
3
Subtotal: 800.71
Tax: 56.04970000000001
Total: 856.7597000000001
Cash: 400.0
Debit: 475.0
Change: 18.240299999999934
Example: Using the same payment type multiple times
Item 1: apple: 0.5
Item 2: pear: 0.75
Item 3: pineapple: 0.75
Total: 2.14
Please enter payment type.
1. Cash
2. Debit card
3. Credit card
4. Check
1
Enter the amount to pay with this type.
1.25
Total after payment: 0.8900000000000001
Please enter payment type.
1. Cash
2. Debit card
3. Credit card
4. Check
1
Enter the amount to pay with this type.
10.50
Total after payment: -9.61
apple: 0.5
pear: 0.75
pineapple: 0.75
Subtotal: 2.0
Tax : 0.14
Total: 2.14
Cash: 1.25
Cash: 110.5
Change: 9.61

User Phillc
by
4.8k points

1 Answer

1 vote

Answer:

Step-by-step explanation:

package sample;

import java.util.Scanner;

public class Main extends newOrder{

public static void main(String[] args) {

newOrder order = new newOrder();

order.newOrder("apple", 0.5);

order.newOrder("pear", 0.75);

order.newOrder("pineapple", 0.75);

double newTotal = order.getTotal();

order.printItems();

order.printTotal();

while (newTotal != 0) {

Scanner in = new Scanner(System.in);

System.out.println("Please enter payment type.\\" + "Cash \\Debit card \\Credit card \\Check");

String paymentType = in.nextLine();

System.out.println("Enter the amount to pay with this type: ");

double amount = in.nextDouble();

newTotal -= amount;

System.out.println("Total after payment: " + newTotal);

}

System.out.println("Receipt Printed:");

order.printItems();

order.printSubtotal();

System.out.println("Tax: 7%");

order.printTotal();

}

}

------------------------------------------------------------------------------------------------------------

package sample;

public enum PaymentType {

CASH, DEBIT_CARD, CREDIT_CARD, CHECK

}

-----------------------------------------------------------------------------------------------------------

package sample;

import java.util.HashMap;

class newOrder {

private String name;

private double cost;

private double total;

private double subtotal;

HashMap<String, Double> itemList = new HashMap<>();

public void newOrder(String name, double cost) {

this.name = name;

this.cost = cost;

this.total += cost;

}

public void printItems() {

for (String i: itemList.keySet()) {

System.out.println( i + ": \t" + itemList.get(i));

}

}

public double getTotal() {

return (total * 1.07);

}

public void printSubtotal() {

for (double i : itemList.values()) {

subtotal += i;

}

System.out.println("Subtotal: " + subtotal);

}

public void printTotal() {

double total = subtotal * 1.07;

System.out.println("Total: " + total);

}

}

User Diego Ramos
by
5.9k points