-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMember.java
More file actions
56 lines (48 loc) · 1.55 KB
/
Member.java
File metadata and controls
56 lines (48 loc) · 1.55 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
import java.util.ArrayList;
import java.util.List;
class Member {
private String memberId;
private String name;
private List<Book> borrowedBooks;
public Member(String memberId, String name) {
if (memberId.length() != 6) {
throw new IllegalArgumentException("Member ID must be exactly 6 characters long.");
}
this.memberId = memberId;
this.name = name;
this.borrowedBooks = new ArrayList<>();
}
public String getMemberId() {
return memberId;
}
public String getName() {
return name;
}
public void borrowBook(Book book) {
if (borrowedBooks.size() < 3 && book.isAvailable()) {
borrowedBooks.add(book);
book.borrow();
} else {
throw new IllegalStateException("Cannot borrow book: either limit reached or book not available.");
}
}
public void returnBook(Book book) {
if (borrowedBooks.contains(book)) {
borrowedBooks.remove(book);
book.returnBook();
} else {
throw new IllegalArgumentException("This book was not borrowed by the member.");
}
}
public void printMemberInfo() {
System.out.print("Member ID: " + memberId + ", Name: " + name + ", Borrowed Books: ");
if (borrowedBooks.isEmpty()) {
System.out.println("None");
} else {
for (Book book : borrowedBooks) {
System.out.print(book.getTitle() + " ");
}
System.out.println();
}
}
}