-
Notifications
You must be signed in to change notification settings - Fork 0
/
Book.java
160 lines (155 loc) · 4.05 KB
/
Book.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
public class Book {
private String isbn;
private String name;
private String author;
private String publisher;
private String page;
private int return_day;
private int return_year;
private String who;
private boolean lend;
//constructor
public Book(String isbn, String name, String author, String publisher, String page, boolean lend, int return_year, int return_day, String who) {
setIsbn(isbn);
setName(name);
setAuthor(author);
setPublisher(publisher);
setLend(lend);
setPage(page);
setReturn_day(return_day);
setReturn_year(return_year);
setWho(who);
}
/*public Book(String isbn, String name, String author, String publisher, String page, boolean lend) {
setIsbn(isbn);
setName(name);
setAuthor(author);
setPublisher(publisher);
setLend(lend);
setPage(page);
setReturn_day(0);
setReturn_year(0);
setWho(null);
}*/
//service methods
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public boolean getLend() {
return lend;
}
public void setLend(boolean lend) {
this.lend = lend;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page=page;
}
public int getReturn_day() {
return return_day;
}
public void setReturn_day(int return_day) {
this.return_day = return_day;
}
public int getReturn_year() {
return return_year;
}
public void setReturn_year(int return_year) {
this.return_year = return_year;
}
public String getWho() {
return who;
}
public void setWho(String who) {
this.who = who;
}
//functional methods
//Count date difference
public int day_difference(int day_now, int day_return, int year_now, int year_return){
if(year_now == year_return){
return day_return-day_now;
}
else{
int lap = 0;
for(int i = year_now ; i < year_return ; i++)
{
if((i%4==0 && i%100!=0) || i%400==0) //leap year
{
lap += 366;
}
else //not leap year
{
lap += 365;
}
}
return lap+day_return-day_now;
}
}
public static String date(int dayofyear, int year){
if((year%4==0 && year%100!=0) || year%400==0){
int m[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(int i=0;i<12;i++){
if(dayofyear<=m[i]){
return year+"/"+(i+1)+"/"+dayofyear;
}
dayofyear-=m[i];
}
}
else //not leap year
{
int m[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(int i=0;i<12;i++){
if(dayofyear<=m[i]){
return year+"/"+(i+1)+"/"+dayofyear;
}
dayofyear-=m[i];
}
}
return "";
}
//Output
public String toString(){
String s="書名: ";
s += getName();
s += "\n作者: ";
s += getAuthor();
s += "\nIsbn: ";
s += getIsbn();
s += "\n出版商: ";
s += getPublisher();
s += "\n頁數: ";
s += getPage();
s += "\n租借狀態: ";
if(getLend()){
s += "已借出";
}
else{
s += "未借出";
}
s += "\n";
return s;
}
}