-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.java
More file actions
81 lines (71 loc) · 1.93 KB
/
Book.java
File metadata and controls
81 lines (71 loc) · 1.93 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
package book;
public class Book {
private String title = "";
private String author = "";
private boolean borrowed;
public Book (String title, String author, boolean status) { // constructor for book class
setTitle(title);
setAuthor(author);
setBorrowed(status);
}
public Book (String title, String author) { // second constructor
setTitle(title);
setAuthor(author);
}
public Book (boolean status) {
setBorrowed(status);
}
public Book () {}
// package private methods
void setTitle (String a) { // set title
this.title = a;
}
void setAuthor (String a) { // set author
this.author = a;
}
void setBorrowed (boolean a) { // set status
this.borrowed = a;
}
//public methods
//accessors
public String getTitle() { // get title
return title;
}
public String getAuthor() { // get author
return author;
}
public boolean isBorrowed() { // get status
return borrowed;
}
/* returns the title of the book if
it is available and sets the state of the book to borrowed.
Returns the empty string if the the book is not available .
*/
public String borrow () {
if(!borrowed){
borrowed = true;
return title;
} else {
return "";
}
}
/* returns true if the book state is borrowed
and sets the state of the book to available.
Returns false if the the book was already available .
*/
public boolean giveBack () {
if(borrowed){
borrowed = false;
return true;
} else {
return false;
}
}
/* returns a string consisting of the title of the book
followed by a colon and a space followed by the name
of the author.
*/
public String toString() {
return title+" by "+author;
}
}