-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProject2.java
187 lines (159 loc) · 6.38 KB
/
Project2.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Assignment 2, implemenet a simple cash register program.
*
* @author Liam Considine
* 5/20/2015
*/
import java.text.NumberFormat;
import java.util.Scanner;
public class CashRegister
{
// instance variables defining cash register class variables
double currentAmountDue;
double totalDailySales;
String storeName;
final double salesTax = 0.06;
NumberFormat fmt = NumberFormat.getCurrencyInstance();
/**
* Constructor for objects of class CashRegister with store name ony
*/
public CashRegister(String pName)
{
currentAmountDue = 0.0;
totalDailySales = 0.0;
storeName = pName;
NumberFormat fmt = NumberFormat.getCurrencyInstance();
System.out.println("Welcome to " + storeName);
}
/**
* Constructor for objects of class CashRegister with all parameters defined
*/
public CashRegister(double currentAmountDue, double totalDailySales, String storeName)
{
this.currentAmountDue = currentAmountDue;
this.totalDailySales = totalDailySales;
this.storeName = storeName;
NumberFormat fmt = NumberFormat.getCurrencyInstance();
System.out.println("Welcome to " + storeName);
}
public double getTotalSales()
{
// Total Sales Getter
return totalDailySales;
}
public double getAmountDue()
{
// Current Amount Due Getter
return currentAmountDue;
}
public void scanPrice(double price)
{
// Scan Item if less than or equal to 0.0 scan error
if (price <= 0.0) {
System.out.println("There has been a scanner error... Scan returned: " + price);
}
// If not less than or equal to 0.0 add to current amount due and show current item price and cart total price
else {
currentAmountDue = currentAmountDue + price;
System.out.println("The price of that item is: " + fmt.format(price) + " current cart cost: " + fmt.format(currentAmountDue));
}
}
public void completeSale()
{
// Close out current order show sales tax add sales tax to currentAmountDue add currentAmountDue to totalDailySales print total amount due
System.out.println("Order entered");
double tax = salesTax * currentAmountDue;
System.out.println("Sales Tax: " + fmt.format(tax));
currentAmountDue = currentAmountDue + tax;
totalDailySales = totalDailySales + currentAmountDue;
System.out.println("Total amount due: " + fmt.format(currentAmountDue));
}
public void cancelSale()
{
// Cancel out current cart/order show operator amount due reset
currentAmountDue = 0.0;
System.out.println("Sale canceled, amount due set to: " + fmt.format(currentAmountDue));
}
public void makePayment(double amount)
{
// If payment is less than 0 payment can't be negative
if (amount <= 0){
System.out.println("Payment cannot be Zero or negative.");
System.out.println("Payment due: " + currentAmountDue);
}
// If amount paid is less than current due show remaining charges
else if (amount < currentAmountDue) {
currentAmountDue = currentAmountDue - amount;
System.out.println("Payment: " + fmt.format(amount));
System.out.println("Customer still owes: " + fmt.format(currentAmountDue));
}
// If amount paid is exact
else if (amount == currentAmountDue){
System.out.println("Payment: " + fmt.format(amount));
System.out.println("Great work on that exact change! Thank you. Have a nice day!");
currentAmountDue = 0.0;
}
// If the payment is greater than the cost
else if (amount > currentAmountDue) {
double change = amount - currentAmountDue;
currentAmountDue = 0.0;
System.out.println("Payment: " + fmt.format(amount));
System.out.println("Change due to customer: " + fmt.format(change));
}
}
public void showSalesReport()
{
// print the totalDailySales in a nice string for SalesReport
System.out.println(storeName + "'s Total Daily Sales = " + fmt.format(totalDailySales));
}
public void clearAllSales()
{
// Asks the user for input, clear currentAmountDue and totalDailySales if they say "y"
Scanner sc = new Scanner(System.in);
System.out.println("Do you want to clear all sales? Y/N");
String answer = sc.next();
if (answer.equals("y")){
totalDailySales = 0.0;
currentAmountDue = 0.0;
System.out.println("All sales cleared");
}
else{
System.out.println("No changes to Daily Sales made");
}
}
public static void main(String [] args)
{
CashRegister uMart = new CashRegister("uMart");
//Test scanPrice, completeSale, getAmountDue, getTotalSales, makePayment(under), makePayment(exact case), clearAllSales(y), showSalesReport
uMart.scanPrice(1.50);
uMart.scanPrice(3.00);
uMart.scanPrice(4.50);
uMart.completeSale();
System.out.println("Test getAmountDue: " + uMart.getAmountDue());
uMart.makePayment(9.00);
uMart.makePayment(0.54);
System.out.println("Test getTotalSales: " + uMart.getTotalSales());
uMart.clearAllSales();
uMart.showSalesReport();
//Test scanPrice, completeSale, makePayment(negative), makePayment(zero), makePayment(overpay), clearAllSales(n)
uMart.scanPrice(10.50);
uMart.scanPrice(3.00);
uMart.scanPrice(4.50);
uMart.completeSale();
uMart.makePayment(0.00);
uMart.makePayment(-1.00);
uMart.makePayment(9.54);
uMart.makePayment(10.00);
uMart.clearAllSales();
//Test second instance of CashRegister class, scanPrice(zero), scanPrice(negative)
CashRegister quickieMart = new CashRegister("quickieMart");
quickieMart.scanPrice(1.50);
quickieMart.scanPrice(36.00);
quickieMart.cancelSale();
quickieMart.scanPrice(50.00);
quickieMart.scanPrice(-1.00);
quickieMart.scanPrice(0.00);
quickieMart.completeSale();
quickieMart.makePayment(100.0);
}
}