forked from code-differently/code-differently-24-q1
/
Patron.java
91 lines (80 loc) · 2.39 KB
/
Patron.java
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
package com.codedifferently.lesson10.library;
import com.codedifferently.lesson10.library.exceptions.LibraryNotSetException;
import com.codedifferently.lesson10.library.exceptions.WrongLibraryException;
import java.util.Objects;
import java.util.Set;
/** Represents a patron of a library. */
public class Patron {
private Library library;
private String name;
private String email;
/**
* Create a new patron with the given name and email.
*
* @param name The name of the patron.
* @param email The email of the patron.
*/
public Patron(String name, String email) {
this.name = name;
this.email = email;
}
/**
* Get the library that the patron is in.
*
* @param library The library that the patron is in.
* @throws WrongLibraryException If the patron is not in the library.
*/
public void setLibrary(Library library) throws WrongLibraryException {
if (library != null && !library.hasPatron(this)) {
throw new WrongLibraryException(
"Patron " + this.getId() + " is not in library " + library.getId());
}
this.library = library;
}
/** Get the name of the patron. */
public String getName() {
return this.name;
}
/** Get the email of the patron. */
public String getId() {
return this.email;
}
/**
* Gets the books currently checked out to the patron.
*
* @return The books currently checked out to the patron.
* @throws LibraryNotSetException If the library is not set for the patron.
*/
public Set<Book> getCheckedOutBooks() throws LibraryNotSetException {
if (this.library == null) {
throw new LibraryNotSetException("Library not set for patron " + this.getId());
}
return this.library.getCheckedOutByPatron(this);
}
<<<<<<< HEAD
/**
* Get the library associated with the patron.
*
* @return The library associated with the patron.
*/
public Library getLibrary() {
return this.library;
}
=======
>>>>>>> 22ce3dee9fbec444ab04ff845cb6c7fdd2f19932
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Patron)) return false;
Patron patron = (Patron) o;
return Objects.equals(getId(), patron.getId());
}
@Override
public int hashCode() {
return Objects.hash(getId());
}
@Override
public String toString() {
return "Patron{" + "id='" + getId() + '\'' + ", name='" + getName() + '\'' + '}';
}
}