Skip to content

Commit fc7a869

Browse files
committed
refactor: rename, extract, move some classes
1. rename `classification` -> `summary` 2. extract `Summary` 3. move `Order` and the static constant Comparators of `Customer` 4. `CustomerRepository` extends `List<Customer>` 5. fix some typos
1 parent 96a0a0e commit fc7a869

39 files changed

+330
-270
lines changed

src/me/smartstore/SmartStoreApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public SmartStoreApp test() {
3131
String id = String.format("%s_%02d", group.name(), j);
3232
repository.setTempId(id);
3333

34-
String name = String.format(Character.toString('a'+j).repeat(4));
34+
String name = (char) ('a'+j) + "zzz";
3535
repository.setTempName(name);
3636

3737
Integer spentHours = (int) (Math.random() * 10) + param[0];

src/me/smartstore/customer/Customer.java

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
package me.smartstore.customer;
22

3+
import me.smartstore.customer.exception.InvalidCustomerIdException;
4+
import me.smartstore.customer.exception.InvalidCustomerNameException;
35
import me.smartstore.group.Group;
4-
import java.util.Comparator;
56

67
public class Customer {
78

8-
public static final Comparator<Customer> ORDER_NAME_ASC = Comparator.comparing(e -> e.name);
9-
public static final Comparator<Customer> ORDER_NAME_DEC = (e1, e2) -> e2.name.compareTo(e1.name);
10-
public static final Comparator<Customer> ORDER_SPENT_HOURS_ASC = Comparator.comparing(e -> e.spentHours);
11-
public static final Comparator<Customer> ORDER_SPENT_HOURS_DEC = (e1, e2) -> e2.spentHours.compareTo(e1.spentHours);
12-
public static final Comparator<Customer> ORDER_TOTAL_PAID_AMOUNT_ASC = Comparator.comparing(e -> e.totalPaidAmount);
13-
public static final Comparator<Customer> ORDER_TOTAL_PAID_AMOUNT_DEC = (e1, e2) -> e2.totalPaidAmount.compareTo(e1.totalPaidAmount);
149
public static final String ID_FORMAT =
1510
"ID Format: 4~16 letters consisting of alphabets, digits, underscore(_)";
1611
public static final String NAME_FORMAT =
@@ -22,7 +17,6 @@ public class Customer {
2217
private static final int DEFAULT_TOTAL_PAID_AMOUNT = 0;
2318
private static final Group DEFAULT_GROUP = Group.NONE;
2419

25-
2620
private String id;
2721
private String name;
2822
private Integer spentHours;
@@ -41,28 +35,24 @@ public Customer(String id, String name, Integer spentHours, Integer totalPaidAmo
4135
this.group = group;
4236
}
4337

44-
public Customer(Customer e) {
45-
copy(e);
38+
public Customer(Customer e) { copy(e); }
39+
40+
public void copy(Customer e) {
41+
id = e.id;
42+
name = e.name;
43+
spentHours = e.spentHours;
44+
totalPaidAmount = e.totalPaidAmount;
45+
group = e.group;
4646
}
4747

4848
public String getId() { return id; }
4949

50-
public void setId(String id) throws InvalidCustomerIdException {
50+
public void setId(String id, boolean checked) throws InvalidCustomerIdException {
51+
if (!checked)
52+
checkIfIdIsValid(id);
5153
this.id = id;
5254
}
5355

54-
public void setName(String name) throws InvalidCustomerNameException {
55-
this.name = name;
56-
}
57-
58-
public void setSpentHours(Integer spentHours) {
59-
this.spentHours = spentHours;
60-
}
61-
62-
public void setTotalPaidAmount(Integer totalPaidAmount) {
63-
this.totalPaidAmount = totalPaidAmount;
64-
}
65-
6656
public static void checkIfIdIsValid(String id) throws InvalidCustomerIdException {
6757
if (!isValidId(id))
6858
throw new InvalidCustomerIdException("Invalid ID input.\n");
@@ -73,6 +63,13 @@ private static boolean isValidId(String id) {
7363
return id.matches(ID_PATTERN);
7464
}
7565

66+
public String getName() { return name; }
67+
68+
public void setName(String name) throws InvalidCustomerNameException {
69+
checkIfNameIsValid(name);
70+
this.name = name;
71+
}
72+
7673
public static void checkIfNameIsValid(String name) throws InvalidCustomerNameException {
7774
if (!isValidName(name))
7875
throw new InvalidCustomerNameException("Invalid Name input.\n");
@@ -88,18 +85,18 @@ private static void throwIfNull(Object o, String title) {
8885
throw new IllegalArgumentException(title + " cannot be null.\n");
8986
}
9087

91-
public void copy(Customer e) {
92-
id = e.id;
93-
name = e.name;
94-
spentHours = e.spentHours;
95-
totalPaidAmount = e.totalPaidAmount;
96-
group = e.group;
97-
}
88+
public Integer getSpentHours() { return spentHours; }
89+
90+
public void setSpentHours(Integer spentHours) { this.spentHours = spentHours; }
91+
92+
public Integer getTotalPaidAmount() { return totalPaidAmount; }
93+
94+
public void setTotalPaidAmount(Integer totalPaidAmount) { this.totalPaidAmount = totalPaidAmount; }
9895

9996
public Group getGroup() { return group; }
10097

10198
public void updateGroup() {
102-
group = Group.calculate(spentHours, totalPaidAmount);
99+
group = Group.getGroupByParameter(spentHours, totalPaidAmount);
103100
}
104101

105102
@Override
Lines changed: 26 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,49 @@
11
package me.smartstore.customer;
22

3+
import me.smartstore.customer.exception.DuplicateCustomerIdException;
4+
import me.smartstore.customer.exception.InvalidCustomerIdException;
5+
import me.smartstore.customer.exception.InvalidCustomerNameException;
36
import me.smartstore.group.Group;
47

5-
import java.util.Comparator;
6-
7-
public class CustomerRepository {
8+
public class CustomerRepository extends List<Customer> {
89

910
private static class InstanceHolder {
1011
private static final CustomerRepository INSTANCE = new CustomerRepository();
1112
}
1213
public static CustomerRepository getInstance() { return InstanceHolder.INSTANCE; }
1314
private CustomerRepository() {}
1415

15-
private final List<Customer> customerList = new List<>();
16-
private static Customer tempCustomer;
17-
private static int tempIdx;
18-
19-
public void checkIfCanAddMore() throws MaxCapacityReachedException {
20-
customerList.checkIfReachedMaxCapacity();
21-
}
16+
private Customer tempCustomer;
17+
private int tempIdx;
2218

23-
public void resetTempCustomer() {
24-
tempCustomer = new Customer(null, null);
25-
}
19+
public void resetTempCustomer() { tempCustomer = new Customer(null, null); }
2620

2721
public void checkIfHasNoDuplicate(String id) throws DuplicateCustomerIdException {
2822
if (hasCustomerWithId(id))
2923
throw new DuplicateCustomerIdException("The customer already exists which has same id.");
3024
}
3125

3226
private boolean hasCustomerWithId(String id) {
33-
return findIdxById(id) >= 0;
34-
}
35-
36-
private int findIdxById(String id) {
3727
assert id != null;
38-
39-
int size = customerList.size();
40-
for (int i = 0; i < size; ++i)
41-
if (id.equals(customerList.get(i).getId()))
42-
return i;
43-
return -1;
28+
for (Customer customer : this)
29+
if (id.equals(customer.getId()))
30+
return true;
31+
return false;
4432
}
4533

4634
public void setTempId(String id) throws InvalidCustomerIdException, DuplicateCustomerIdException {
4735
Customer.checkIfIdIsValid(id);
4836
checkIfHasNoDuplicate(id);
49-
tempCustomer.setId(id);
37+
tempCustomer.setId(id, true);
5038
}
5139

5240
public void setTempName(String name) throws InvalidCustomerNameException {
53-
Customer.checkIfNameIsValid(name);
5441
tempCustomer.setName(name);
5542
}
5643

57-
public void setTempSpentHours(Integer spentHours) {
58-
tempCustomer.setSpentHours(spentHours);
59-
}
44+
public void setTempSpentHours(Integer spentHours) { tempCustomer.setSpentHours(spentHours); }
6045

61-
public void setTempTotalPaidAmount(Integer totalPaidAmount) {
62-
tempCustomer.setTotalPaidAmount(totalPaidAmount);
63-
}
46+
public void setTempTotalPaidAmount(Integer totalPaidAmount) { tempCustomer.setTotalPaidAmount(totalPaidAmount); }
6447

6548
public String getTempInfo() {
6649
updateGroupOfTempCustomer();
@@ -70,115 +53,48 @@ public String getTempInfo() {
7053
public void addTempIntoRepository() {
7154
// app logic에 따라 다음 두 구문이 true 임을 보장합니다.
7255
assert tempCustomer != null;
73-
assert !customerList.isReachedMaxCapacity();
56+
assert !isReachedMaxCapacity();
7457

75-
customerList.add(tempCustomer);
58+
add(tempCustomer);
7659
tempCustomer = null;
7760
}
7861

79-
public boolean isTempIdNull() {
80-
return tempCustomer.getId() == null;
81-
}
82-
83-
public int size() {
84-
return customerList.size();
85-
}
62+
public boolean isTempIdNull() {return tempCustomer.getId() == null; }
8663

8764
public String deleteAndGetInfoOf(int num) {
8865
int idx = num - 1;
89-
return customerList.remove(idx).toString();
66+
return remove(idx).toString();
9067
}
9168

9269
public String setTempAndGetInfoOf(int num) {
9370
int idx = num - 1;
94-
Customer updatingCustomer = customerList.get(idx);
71+
Customer updatingCustomer = get(idx);
9572
tempCustomer = new Customer(updatingCustomer);
9673
tempIdx = idx;
9774
return updatingCustomer.toString();
9875
}
9976

100-
public void updateTempInRepository() {
101-
customerList.get(tempIdx).copy(tempCustomer);
102-
}
77+
public void updateTempInRepository() { get(tempIdx).copy(tempCustomer); }
10378

10479
public String getUpdateBeforeAndAfterInfo() {
105-
Customer before = customerList.get(tempIdx);
80+
Customer before = get(tempIdx);
10681
Customer after = tempCustomer;
10782
updateGroupOfTempCustomer();
10883
return String.format("\nNo. %2d\n", tempIdx) +
10984
"Before: " + before + '\n'+
11085
"After : " + after;
11186
}
11287

113-
public void updateGroupOfTempCustomer() {
114-
tempCustomer.updateGroup();
115-
}
116-
117-
public String getSummary() {
118-
Group[] groups = Group.values();
119-
int len = groups.length;
120-
StringBuilder[] stringBuilders = new StringBuilder[len];
121-
int[] cnt = new int[len];
122-
for (int i = 0; i < len; ++i) {
123-
StringBuilder sb = new StringBuilder();
124-
sb.append('\n').append("==============================")
125-
.append(groups[i])
126-
.append("==============================").append('\n');
127-
stringBuilders[i] = sb;
128-
}
129-
130-
int size = customerList.size();
131-
for (int i = 0; i < size; ++i) {
132-
Customer customer = customerList.get(i);
133-
Group group = customer.getGroup();
134-
for (int j = 0; j < len; ++j) {
135-
if (group == groups[j]) {
136-
stringBuilders[j].append(customer).append('\n');
137-
cnt[j]++;
138-
break;
139-
}
140-
}
141-
}
142-
143-
StringBuilder sb = new StringBuilder();
144-
for (int i = 0; i < len; ++i) {
145-
sb.append(stringBuilders[i]);
146-
if (cnt[i] == 0)
147-
sb.append("No Customers.").append('\n');
148-
}
149-
return sb.append('\n').toString();
150-
}
151-
152-
public String getSummaryByName(Order order) {
153-
Comparator<Customer> cmp = order == Order.ASCENDING ? Customer.ORDER_NAME_ASC
154-
: Customer.ORDER_NAME_DEC;
155-
customerList.sort(cmp);
156-
return getSummary();
157-
}
158-
159-
public String getSummaryBySpentHours(Order order) {
160-
Comparator<Customer> cmp = order == Order.ASCENDING ? Customer.ORDER_SPENT_HOURS_ASC
161-
: Customer.ORDER_SPENT_HOURS_DEC;
162-
customerList.sort(cmp);
163-
return getSummary();
164-
}
165-
166-
public String getSummaryByTotalPaidAmount(Order order) {
167-
Comparator<Customer> cmp = order == Order.ASCENDING ? Customer.ORDER_TOTAL_PAID_AMOUNT_ASC
168-
: Customer.ORDER_TOTAL_PAID_AMOUNT_DEC;
169-
customerList.sort(cmp);
170-
return getSummary();
171-
}
88+
public void updateGroupOfTempCustomer() { tempCustomer.updateGroup(); }
17289

17390
public void updateGroupIn(Group group) {
17491
Group[] groups = Group.values();
17592
int idx = 0;
17693
while (groups[idx] != group)
17794
idx++;
95+
assert idx > 0;
17896
Group rightLowGroup = groups[idx - 1];
179-
int size = customerList.size();
180-
for (int i = 0; i < size; ++i) {
181-
Customer customer = customerList.get(i);
97+
for (Customer customer : this) {
18298
Group g = customer.getGroup();
18399
if (g == group || g == rightLowGroup)
184100
customer.updateGroup();
@@ -187,7 +103,7 @@ public void updateGroupIn(Group group) {
187103

188104
@Override
189105
public String toString() {
190-
if (customerList.isEmpty()) return "No Customers." + " Please input one first.\n";
191-
return customerList.toString();
106+
if (isEmpty()) return "No Customers." + " Please input one first.\n";
107+
return super.toString();
192108
}
193109
}

0 commit comments

Comments
 (0)