-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlingNenaStore.java
143 lines (120 loc) · 5.92 KB
/
AlingNenaStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Name: Jim Hendrix T. Bag-eo
Date of Completion: September 12, 2023
Class Code and Course Number: 9322B - CS112L
Algorithm:
Initializing:
1. Create a Scanner object ('kbd') to read user input.
2. Define arrays ('productList') and ('unitPriceList') to store products and their prices.
Input:
Prompt the user to input the following and loop until valid input is read:
1. Product
2. Quantity
3. Discount
4. Cash Tendered
Processes:
1. Identify the unitPrice after the Quantity is read.
2. Compute the Total Purchase Amount.
3. Compute the Total Discount.
4. Compute the Amount To Be Paid.
5. Compute the Change.
Output:
Display the following:
1. Total Purchase Amount
2. Total Discount
3. Amount To Be Paid
4. Change
*/
package exercises.prelims;
import java.util.Scanner;
import java.util.Random;
public class AlingNenaStore {
public static void main(String[] args) {
Random discountRandom = new Random();
Scanner kbd = new Scanner(System.in);
String[] productList = {"eggs", "milk", "sardines", "cooking oil", "vinegar", "garlic", "onion", "salt", "coffee", "corned beef", "pancit canton"}; // List of the products
double[] unitPriceList = {5.00, 18.75, 23.50, 10.0, 12.0, 4.5, 3.5, 10.0, 8.0, 19.50, 16.50}; // List of the prices of the products
String product = getProduct(kbd, productList);
int quantity = getQuantity(kbd);
double unitPrice = getUnitPrice(product, productList, unitPriceList);
double discount = getDiscount(discountRandom);
double cashTendered = getCashTendered(kbd);
double totalPurchaseAmount = computeTotalPurchaseAmount(quantity, unitPrice); //Computes for Total Purchase Amount
double totalDiscount = computeTotalDiscount(discount, totalPurchaseAmount); //Computes for Total Discount
double amountToBePaid = computeAmountToBePaid(totalPurchaseAmount, totalDiscount); //Computes for Amount To Be Paid
double change = computeChange(cashTendered, amountToBePaid); //Computes for Change
printReceipt(totalPurchaseAmount, totalDiscount, amountToBePaid, change); //Prints the Output
}
private static String getProduct(Scanner kbd, String[] productList) {
do {
System.out.print("Product: ");
String product = kbd.nextLine().toLowerCase().trim();
for (String i : productList) {
if (product.equals(i)) {
return product;
}
}
System.out.println("\nOur store doesn't sell " + product + " ");
} while (true); //Loop until the kbd reads a Product from the array
} // End of the getProduct method
private static int getQuantity(Scanner kbd) {
do {
System.out.print("Quantity: ");
if (kbd.hasNextInt()) {
int quantity = kbd.nextInt();
if (quantity > 0 && quantity <= 100) {
return quantity;
}
}
System.out.println("\nInvalid input. Please enter a valid quantity. ");
kbd.nextLine();
} while (true); //Loop until the kbd reads a Quantity 1-100
} // End of the getQuantity method
private static double getUnitPrice(String product, String[] productList, double[] unitPriceList) {
double unitPrice = 0;
for (int i = 0; i < productList.length; i++) { //Find the Unit Price of the product entered
if (product.equals(productList[i])) {
unitPrice = unitPriceList[i];
}
}
System.out.println("Unit Price: " + unitPrice);
return unitPrice;
} //End of the getUnitPrice Method
private static double getDiscount(Random discountRandom) {
int limit = 101;
double discount = discountRandom.nextInt(limit); //Generates random numbers for discount
System.out.print("Discount: " + discount + "\n");
return discount;
} // End of the getDiscountMethod
private static double getCashTendered(Scanner kbd) {
do {
System.out.print("Cash Tendered: ");
if (kbd.hasNextDouble()) {
double cashTendered = kbd.nextDouble();
if (cashTendered >= 0) {
return cashTendered;
}
}
System.out.println("\nInvalid input. Please enter a valid Cash Amount. ");
kbd.nextLine();
} while (true); //Loops until kbd reads a Valid Cash Amount
} // End of the getCashTendered method
private static double computeTotalPurchaseAmount(int quantity, double unitPrice) {
return quantity * unitPrice;
} // End of the computeTotalPurchaseAmount method
private static double computeTotalDiscount(double discount, double totalPurchaseAmount) {
return totalPurchaseAmount * (discount / 100);
} // End of the computeTotalDiscount method
private static double computeAmountToBePaid(double totalPurchaseAmount, double totalDiscount) {
return totalPurchaseAmount - totalDiscount;
} // End of the totalAmountToBePaid method
private static double computeChange(double cashTendered, double amountToBePaid) {
return cashTendered - amountToBePaid;
} // End of the computeChange method
private static void printReceipt(double totalPurchaseAmount, double totalDiscount, double amountToBePaid, double change) {
System.out.printf("%n%nTotal Purchase Amount: %.2f", totalPurchaseAmount);
System.out.printf("%nTotal Discount: %.2f", totalDiscount);
System.out.printf("%nAmount To Be Paid: %.2f", amountToBePaid);
System.out.printf("%nChange: %.2f", change);
} // End of the printReceiptMethod
} // End of class88