-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomer.java
192 lines (140 loc) · 4.68 KB
/
Customer.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
188
189
190
191
192
import java.util.ArrayList;
public class Customer extends User {
private String phone;
private double fund;
private ArrayList<Content> contents;
//introduce a boolean variable to check if the customer is premium
private boolean premium;
public Customer(String id, String name, String phone, double fund) {
super(id, name);
this.phone = phone;
this.fund = fund;
contents = new ArrayList<Content>();
premium = false;
}
public Customer(String id, String name, String phone) {
this(id, name, phone, 50);
}
public String getPhone() {
return phone;
}
public double getFund() {
return fund;
}
//setter for premium
public boolean isPremium() {
return premium;
}
public ArrayList<Content> getContents() {
return contents;
}
public void addContent(Content content) {
contents.add(content);
}
//deduct amount from fund
public boolean cost(double amount) {
if(fund >= amount) {
fund -= amount;
return true;
}
else {
System.out.println(getId() + " " + getName() + " not enough found.");
return false;
}
}
public boolean download (Content content) {
//use nested if-else to check fund and premium
if (premium) {
//price for premium customer is 20% off
//round to two decimal places for discounted price
double discountPrice = Math.round(content.getPrice() * 0.8 * 100.0) / 100.0;
if (discountPrice <= fund) {
cost(discountPrice);
content.addDownload();
addContent(content);
System.out.println("Premium customer " + getId() + " " + getName() + " downloaded " + content.getId() + " " + content.getName() + " for discount price $" + discountPrice + ".");
return true;
}
else {
System.out.println("Premium customer " + getId() + " " + getName() + " download " + content.getId() + " " + content.getName() + " fail.");
return false;
}
}
//full price for non premium customer
else {
if (content.getPrice() <= fund) {
cost(content.getPrice());
content.addDownload();
addContent(content);
System.out.println("Customer " + getId() + " " + getName() + " downloaded " + content.getId() + " " + content.getName() + " for $" + content.getPrice() + ".");
return true;
}
else {
System.out.println("Customer " + getId() + " " + getName() + " download " + content.getId() + " " + content.getName() + " fail.");
return false;
}
}
}
//method overloading for bulk download
public boolean download (Content[] list) {
double tol_price = 0;
//calculate a sum price of contents in the list
for(Content content : list) {
tol_price += content.getPrice();
}
//use nested if-else to check fund and premium
if (premium) {
//price for premium customer is 20% off
//round to two decimal places for discounted price
double discont_tol_price = Math.round(tol_price * 0.8* 100.0) / 100.0;
if (discont_tol_price <= fund) {
cost(discont_tol_price);
for(Content content : list) {
content.addDownload();
addContent(content);
}
System.out.println("Premium customer " + getId() + " " + getName() + " bulk downloaded for total discount price $" + discont_tol_price + ".");
return true;
}
else {
System.out.println("Premium customer " + getId() + " " + getName() + " bulk download fail.");
return false;
}
}
//full price for non premium customer
else {
if (tol_price <= fund) {
cost(tol_price);
for(Content content : list) {
content.addDownload();
addContent(content);
}
System.out.println("Customer " + getId() + " " + getName() + " bulk downloaded for total price $" + tol_price + ".");
return true;
}
else {
System.out.println("Customer " + getId() + " " + getName() + " bulk download fail.");
return false;
}
}
}
//use for loop and ArrayList to print all contents downloaded by this customer
public void showDownloads() {
System.out.println("Contents downloaded by " + getId() + " " + getName() + ": ");
for(Content content : contents) {
System.out.println(content.getId() + " " + content.getName());
}
}
//deduct 100 from fund and change premium to true
public boolean becomePremium() {
if(cost(100)) {
premium = true;
System.out.println(getId() + " " + getName() + " upgraded to premium.");
return true;
}
else {
System.out.println(getId() + " " + getName() + " upgraded fail.");
return false;
}
}
}