-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary.java
132 lines (101 loc) · 2.86 KB
/
Library.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author kdost
*/
public class Book {
private String title;
private String publisher;
private int year;
public Book(String title, String publisher, int year) {
this.title = title;
this.publisher = publisher;
this.year = year;
}
public String title() {
return this.title;
}
public String publisher() {
return this.publisher;
}
public int year() {
return this.year;
}
public String toString() {
return this.title + ", " + this.publisher + ", " + this.year;
}
}
import java.util.ArrayList;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author kdost
*/
public class Library {
private ArrayList<Book> bookList;
public Library() {
this.bookList = new ArrayList<Book>();
}
public void addBook(Book newBook) {
this.bookList.add(newBook);
}
public void printBooks() {
for (Book bk : this.bookList) {
System.out.println(bk);
}
}
public ArrayList<Book> searchByTitle(String title) {
ArrayList<Book> found = new ArrayList<Book>();
for (Book tl : this.bookList) {
//if (tl.title().contains(title)) {
if (StringUtils.included(tl.title(), title)) {
found.add(tl);
}
}
return found;
}
public ArrayList<Book> searchByPublisher(String publisher) {
ArrayList<Book> found = new ArrayList<Book>();
for (Book tl : this.bookList) {
//if (tl.publisher().contains(publisher)) {
if (StringUtils.included(tl.publisher(), publisher)) {
found.add(tl);
}
}
return found;
}
public ArrayList<Book> searchByYear(int year) {
ArrayList<Book> found = new ArrayList<Book>();
for (Book tl : this.bookList) {
if (tl.year() == year) {
found.add(tl);
}
}
return found;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author kdost
*/
public class StringUtils {
public static boolean included(String word, String searched) {
if (word.toUpperCase().trim().contains(searched.trim().toUpperCase())) {
return true;
}
return false;
}
}