-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathReceipt.java
More file actions
94 lines (79 loc) · 3.76 KB
/
Receipt.java
File metadata and controls
94 lines (79 loc) · 3.76 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
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
package org.codedifferently;
import java.util.Random; //Imports the random class
public class Receipt {
Random random = new Random(); //Creates a Random object named "random"
ReceiptCalculator calculator = new ReceiptCalculator(); //Creates a ReceiptCalculator object named "calculator"
//Returns a random number ranging from 1000 to 9999
public int generateVistId(){
return random.nextInt(1000,10_000);
}
//Returns the receipt code consisting of the user's name and a random number ranging from 1000 to 9999
public String generateReceiptCode(String customerName) {
customerName = customerName.trim().toLowerCase();
int index = customerName.indexOf(" ");
String firstName;
String lastName;
if (index == -1) {
firstName = customerName;
lastName = "";
} else {
firstName = customerName.substring(0, index);
lastName = customerName.substring(index + 1);
}
if (firstName.length() > 3) {
firstName = firstName.substring(0, 3);
}
if (lastName.length() > 3) {
lastName = lastName.substring(0, 3);
}
return firstName + lastName + random.nextInt(1000, 10_000);
}
//Prints out a Welcome message for the receipt based on the value returned from the random number generator
public void generateWelcomeMessage(){
int value = random.nextInt(1, 7);
if (value % 2 != 0) {
System.out.println("Welcome to Walmart");
} else {
System.out.println("Welcome to Target");
}
}
//Prints out a closing message for the receipt based on the value returned from the random number generator
public void generateReceiptTagline() {
int value = random.nextInt(1, 7);
if (value % 2 == 0) {
System.out.println("Have a WONDERFUL day!");
} else {
System.out.println("Have a FANTASTIC day!");
}
}
//Prints out all the receipt information based on the values entered in by the user
public void displayReceipt(String customerName, double customerBudget, String couponCode){
double item1 = calculator.generateItemPrice();
double item2 = calculator.generateItemPrice();
double item3 = calculator.generateItemPrice();
double subTotal = calculator.calcBillSubtotal(item1, item2, item3);
double tax = calculator.calcTaxAmount(subTotal);
double discount = calculator.getDiscountAmt(couponCode);
double finalTotal = calculator.calcFinalTotal(couponCode, item1, item2, item3, customerBudget, tax);
generateWelcomeMessage();
System.out.println("---------------------------------");
System.out.println("Customer Name: " + customerName);
System.out.println("Your budget is $" + customerBudget);
System.out.println("Visit ID: " + generateVistId());
System.out.println("---------------------------------");
System.out.println("Receipt Code " + generateReceiptCode(customerName));
System.out.println("Your Item prices are: $" + item1 + ", $" + item2 + ", $" + item3);
System.out.println("Subtotal: $" + subTotal);
System.out.println("Tax: $" + tax);
System.out.println("You have a $" + discount + " discount off your bill");
System.out.println("---------------------------------");
System.out.println("Your final total is $" + finalTotal);
if (customerBudget >= finalTotal){
System.out.println("You have $" + (customerBudget - finalTotal) + " left in your budget" );
}
else{
System.out.println("You are $" + (finalTotal - customerBudget) + " dollars short");
}
generateReceiptTagline();
}
} //ends Receipt class