-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathReceipt.java
More file actions
59 lines (49 loc) · 1.6 KB
/
Receipt.java
File metadata and controls
59 lines (49 loc) · 1.6 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
package org.codedifferently;
import java.util.Random;
public class Receipt {
double item1;
double item2;
double item3;
double subtotal;
double discountedTotal;
double taxAmount;
double finalTotal;
private final double TAX_RATE = 0.08; // 8% tax
// Generate 3 separate random item prices
public void generateItemPrices() {
Random random = new Random();
item1 = 10 + (90 * random.nextDouble());
item2 = 10 + (90 * random.nextDouble());
item3 = 10 + (90 * random.nextDouble());
}
// Calculate subtotal
public void calculateSubtotal() {
subtotal = item1 + item2 + item3;
}
// Apply coupon
public void applyCoupon(String couponCode) {
if (couponCode.equalsIgnoreCase("SAVE50")) {
discountedTotal = subtotal * 0.5;
} else {
discountedTotal = subtotal;
}
}
// Calculate tax
public void calculateTax() {
taxAmount = discountedTotal * TAX_RATE;
}
// Calculate final total
public void calculateFinalTotal() {
finalTotal = discountedTotal + taxAmount;
}
// Print receipt details in a simple way
public void printReceiptDetails() {
System.out.println("Item 1 Price: $" + item1);
System.out.println("Item 2 Price: $" + item2);
System.out.println("Item 3 Price: $" + item3);
System.out.println("Subtotal: $" + subtotal);
System.out.println("Discounted Total: $" + discountedTotal);
System.out.println("Tax: $" + taxAmount);
System.out.println("Final Total: $" + finalTotal);
}
}