-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasyRentals.java
More file actions
195 lines (161 loc) · 5.17 KB
/
EasyRentals.java
File metadata and controls
195 lines (161 loc) · 5.17 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
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
193
194
195
package Customer;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Customer {
private int id;
private String name;
private LocalDate dob;
private String custType; // p-> Privileged, R-> Regular
private String gender;
private String contactNo;
private String emailId;
private LocalDate registrationDate;
private String country;
private String zone;
public Customer(int id, String name, LocalDate dob, String custType, String gender, String contactNo, String emailId, LocalDate registrationDate, String country, String zone) {
this.id = id;
this.name = name;
this.dob = dob;
this.custType = custType;
this.gender = gender;
this.contactNo = contactNo;
this.emailId = emailId;
this.registrationDate = registrationDate;
this.country = country;
this.zone = zone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate getDob() {
return dob;
}
public void setDob(LocalDate dob) {
this.dob = dob;
}
public String getCustType() {
return custType.equals("P") ? "ER Privileged" : "ER Regular";
}
public void setCustType(String custType) {
this.custType = custType;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public String getEmailId() {
return emailId != null ? emailId : "Email ID not provided";
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public LocalDate getRegistrationDate() {
return registrationDate;
}
public void setRegistrationDate(LocalDate registrationDate) {
this.registrationDate = registrationDate;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getZone() {
return zone;
}
public void setZone(String zone) {
this.zone = zone;
}
public String getFormattedRegistrationDate() {
if (zone.equals("USA")) {
return registrationDate.format(DateTimeFormatter.ofPattern("MM/dd/yyyy"));
} else if (zone.equals("Europe")) {
return registrationDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
} else {
return registrationDate.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
}
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", dob=" + dob +
", custType='" + custType + '\'' +
", gender='" + gender + '\'' +
", contactNo='" + contactNo + '\'' +
", emailId='" + emailId + '\'' +
", registrationDate=" + getFormattedRegistrationDate() +
", country='" + country + '\'' +
", zone='" + zone + '\'' +
'}';
}
//Point-1 print customer data
public static void printAllCustomers(List<Customer> customers) {
customers.stream()
.sorted(Comparator.comparing(Customer::getDob)).forEach(System.out::println);
}
//Point-2 Discount
public static void printDiscountedCustomers(List<Customer> customers) {
customers.stream()
.filter(c -> c.getRegistrationDate().isBefore(LocalDate.now().minusYears(5)))
.forEach(c -> {
double discount = 0.1 * calculateRent(c);
System.out.printf("%s gets a discount of %.2f%n", c.getName(), discount);
});
}
//Point-3(1)
public static void printRegularCustomersFromChinaAboveAge(List<Customer> customers, int age) {
customers.stream()
.filter(c -> c.getCustType().equals("R"))
.filter(c -> c.getCountry().equals("China"))
.filter(c -> c.getDob().isBefore(LocalDate.now().minusYears(age)))
.forEach(System.out::println);
}
//Point-3(2)
public static long countCustomersAboveAge(List<Customer> customers, int age) {
return customers.stream()
.filter(c -> c.getDob().isBefore(LocalDate.now().minusYears(age)))
.count();
}
//Point-3(3)
public static void printPrivilegedCustomersFromUSA(List<Customer> customers) {
customers.stream()
.filter(c -> c.getCustType().equals("P"))
.filter(c -> c.getCountry().equals("USA"))
.forEach(System.out::println);
}
//Pint-4 ER
public static void addCustomer(List<Customer> customers, Customer customer) {
customers.add(customer);
if (customer.getCustType().equals("P")) {
customer.setEmailId("ER" + customer.getEmailId());
}
}
private static int calculateRent(Customer customer) {
// some calculation based on customer data
return 100;
}
}