-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathReceiptCalculator.java
More file actions
65 lines (53 loc) · 2.16 KB
/
ReceiptCalculator.java
File metadata and controls
65 lines (53 loc) · 2.16 KB
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
package org.codedifferently;
import java.util.Random; //imports the Random class
public class ReceiptCalculator {
Random random = new Random(); //Creates a Random object named "random"
double discount;
//Returns a random value ranging from 0 to 100
public int generateItemPrice() {
return random.nextInt(0, 101);
}
//Returns the raw total of the bill before tax and discounts are applied
public double calcBillSubtotal(double firstItemPrice, double secondItemPrice, double thirdItemPrice) {
return firstItemPrice + secondItemPrice + thirdItemPrice;
}
//Takes in the bill amount and returns the tax rate amount
public double calcTaxAmount(double billAmt) {
return Math.round(billAmt * (random.nextDouble(0.0, 8.25) / 100));
}
//Checks if the user enters a valid coupon code
public boolean isValidCoupon(String couponCode) {
switch (couponCode) {
case "ABC123", "123ABC", "CODE_DIFFERENTLY":
return true;
default:
return false;
}
}
//Returns the discount that the user receives based on the coupon code they enter in
public double getDiscountAmt(String couponCode) {
switch (couponCode) {
case "ABC123":
return discount = 10.00;
case "123ABC":
return discount = 20.00;
case "CODE_DIFFERENTLY":
return discount = 50.00;
default:
return 0.0;
}
}
//Returns the final bill amount including tax and any discounts applied
public double calcFinalTotal(String couponCode, double firstItemPrice, double secondItemPrice, double thirdItemPrice, double budget, double tax) {
double subTotal = calcBillSubtotal(firstItemPrice, secondItemPrice, thirdItemPrice);
double finalTotal;
if (isValidCoupon(couponCode)) {
finalTotal = Math.round((subTotal + tax) - discount);
if (finalTotal < 0) {
return 0;
}
}
finalTotal = Math.round((subTotal + tax) - discount);
return finalTotal;
}
} //ends ReceiptCalculator class