forked from exercism/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
60 lines (48 loc) · 1.48 KB
/
User.java
File metadata and controls
60 lines (48 loc) · 1.48 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
import static java.util.Collections.unmodifiableList;
import java.util.ArrayList;
import java.util.List;
/** POJO representing a User in the database. */
public class User {
private final String name;
private final List<Iou> owes;
private final List<Iou> owedBy;
private User(String name, List<Iou> owes, List<Iou> owedBy) {
this.name = name;
this.owes = new ArrayList<>(owes);
this.owedBy = new ArrayList<>(owedBy);
}
public String name() {
return name;
}
/** IOUs this user owes to other users. */
public List<Iou> owes() {
return unmodifiableList(owes);
}
/** IOUs other users owe to this user. */
public List<Iou> owedBy() {
return unmodifiableList(owedBy);
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String name;
private final List<Iou> owes = new ArrayList<>();
private final List<Iou> owedBy = new ArrayList<>();
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder owes(String name, double amount) {
owes.add(new Iou(name, amount));
return this;
}
public Builder owedBy(String name, double amount) {
owedBy.add(new Iou(name, amount));
return this;
}
public User build() {
return new User(name, owes, owedBy);
}
}
}