-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatron.java
More file actions
44 lines (37 loc) · 1.56 KB
/
Copy pathPatron.java
File metadata and controls
44 lines (37 loc) · 1.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
import java.util.ArrayList;
import java.util.List;
public class Patron {
private Integer memberId;
private String name;
private String email;
private List<Integer> issuedBooks;
public Patron(Integer memberId, String name, String email) {
this.memberId = memberId;
this.name = name == null ? "" : name.trim();
this.email = email == null ? "" : email.trim();
this.issuedBooks = new ArrayList<>();
}
public Integer getMemberId() { return memberId; }
public String getName() { return name; }
public String getEmail() { return email; }
public List<Integer> getIssuedBooks() { return new ArrayList<>(issuedBooks); }
public void addIssuedBook(int bookId) { issuedBooks.add(bookId); }
public boolean returnIssuedBook(int bookId) { return issuedBooks.remove(Integer.valueOf(bookId)); }
public String toCSV() {
StringBuilder sb = new StringBuilder();
sb.append(memberId).append(",").append(escape(name)).append(",").append(escape(email)).append(",");
if (!issuedBooks.isEmpty()) {
for (int i = 0; i < issuedBooks.size(); i++) {
if (i > 0) sb.append(";");
sb.append(issuedBooks.get(i));
}
}
return sb.toString();
}
private String escape(String s) { return s.replace(",", " "); }
@Override
public String toString() {
return String.format("MemberID:%d | Name:%s | Email:%s | Issued:%s",
memberId, name, email, issuedBooks.toString());
}
}