-
Notifications
You must be signed in to change notification settings - Fork 4
/
Book.java
65 lines (51 loc) · 1.84 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
package com.example.myFirstSpring.model;
//https://projectlombok.org/features/all
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.lang.reflect.Field;
import java.util.Arrays;
@Data // generates getters, setters, equals, hashCode, and toString methods
@NoArgsConstructor // generates a no-args constructor
@AllArgsConstructor // generates a constructor with all arguments
public class Book {
private String bookId;
private String title;
private String author;
private int pages;
private int publishedYear;
private String isbn;
public void update(Book other) {
Class<?> clazz = getClass();
Field[] fields = clazz.getDeclaredFields();
Arrays.stream(fields)
.filter(field -> {
try {
Object value = field.get(other);
return value != null &&
(!(value instanceof Number) || ((Number) value).intValue() != 0) &&
(!(value instanceof Boolean) || (Boolean) value);
} catch (IllegalAccessException e) {
return false;
}
})
.forEach(field -> {
try {
field.set(this, field.get(other));
} catch (IllegalAccessException e) {
// Handle exception
}
});
}
@Override
public String toString() {
return "Book{" +
"bookId=" + bookId +
", title='" + title + '\'' +
", authorName=" + author +
", pages=" + pages +
", publishedYear=" + publishedYear +
", isbn='" + isbn + '\'' +
'}';
}
}