-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomer.java
More file actions
107 lines (83 loc) · 3.56 KB
/
Customer.java
File metadata and controls
107 lines (83 loc) · 3.56 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
package me.smartstore.customer;
import me.smartstore.customer.exception.InvalidCustomerIdException;
import me.smartstore.customer.exception.InvalidCustomerNameException;
import me.smartstore.group.Group;
public class Customer {
public static final String ID_FORMAT =
"ID Format: 4~16 letters consisting of alphabets, digits, underscore(_)";
public static final String NAME_FORMAT =
"Name Format: [1,4] space separated chuck(s) of alphabets of [2,32] lengths.";
private static final String ID_PATTERN = "^[0-9a-zA-Z_]{4,16}$";
private static final String NAME_CHUCK = "[a-z]{2,32}";
private static final String NAME_PATTERN = '^' + NAME_CHUCK + "( " + NAME_CHUCK + "){0,3}$";
private static final int DEFAULT_SPENT_HOURS = 0;
private static final int DEFAULT_TOTAL_PAID_AMOUNT = 0;
private static final Group DEFAULT_GROUP = Group.NONE;
private String id;
private String name;
private Integer spentHours;
private Integer totalPaidAmount;
private Group group;
public Customer(String id, String name) {
this(id, name, DEFAULT_SPENT_HOURS, DEFAULT_TOTAL_PAID_AMOUNT, DEFAULT_GROUP);
}
public Customer(String id, String name, Integer spentHours, Integer totalPaidAmount, Group group) {
this.id = id;
this.name = name;
this.spentHours = spentHours;
this.totalPaidAmount = totalPaidAmount;
this.group = group;
}
public Customer(Customer e) { copy(e); }
public void copy(Customer e) {
id = e.id;
name = e.name;
spentHours = e.spentHours;
totalPaidAmount = e.totalPaidAmount;
group = e.group;
}
public String getId() { return id; }
public void setId(String id, boolean checked) throws InvalidCustomerIdException {
if (!checked)
checkIfIdIsValid(id);
this.id = id;
}
public static void checkIfIdIsValid(String id) throws InvalidCustomerIdException {
if (!isValidId(id))
throw new InvalidCustomerIdException("Invalid ID input.\n");
}
private static boolean isValidId(String id) {
throwIfNull(id, "ID");
return id.matches(ID_PATTERN);
}
public String getName() { return name; }
public void setName(String name) throws InvalidCustomerNameException {
checkIfNameIsValid(name);
this.name = name;
}
public static void checkIfNameIsValid(String name) throws InvalidCustomerNameException {
if (!isValidName(name))
throw new InvalidCustomerNameException("Invalid Name input.\n");
}
private static boolean isValidName(String name) {
throwIfNull(name, "Name");
return name.matches(NAME_PATTERN);
}
private static void throwIfNull(Object o, String title) {
if (o == null)
throw new IllegalArgumentException(title + " cannot be null.\n");
}
public Integer getSpentHours() { return spentHours; }
public void setSpentHours(Integer spentHours) { this.spentHours = spentHours; }
public Integer getTotalPaidAmount() { return totalPaidAmount; }
public void setTotalPaidAmount(Integer totalPaidAmount) { this.totalPaidAmount = totalPaidAmount; }
public Group getGroup() { return group; }
public void updateGroup() {
group = Group.getGroupByParameter(spentHours, totalPaidAmount);
}
@Override
public String toString() {
return String.format("Customer{id='%s', name='%s', spentHours=%d, totalAmountPaid=%d, group=%s}",
id, name, spentHours, totalPaidAmount, group.name());
}
}