-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
251 lines (219 loc) · 7.12 KB
/
Copy pathDriver.java
File metadata and controls
251 lines (219 loc) · 7.12 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import java.util.Comparator;
import java.util.Scanner;
public class Driver {
public static ArrayList<Book> library;
public static void main(String[] args) {
initLibrary(20);
menu();
}
public static void initLibrary(int numBooks) {
library = new ArrayList<>();
String subject = "";
// https://stackoverflow.com/questions/22186778/using-math-round-to-round-to-one-decimal-place
double scale = (int) Math.pow(10, 1);
// we'll use the ArrayList for publication years so we can randomly pick a year
// and remove it so we don't use it again
ArrayList<Integer> publicationYears = new ArrayList<>();
for (int i = 1980; i < 2020; i++) {
publicationYears.add(i);
}
int programmingCount, dataStructCount, algorithmsCount, opSysCount, gamingCount;
programmingCount = dataStructCount = algorithmsCount = opSysCount = gamingCount = 0;
for (int i = 0; i < numBooks; i++) {
boolean subjectAssigned = false;
while (subjectAssigned == false) {
int x = (int) (Math.random() * 5) + 1;
if (x == 1) {
if (programmingCount >= 5)
continue;
subject = "Programming";
programmingCount++;
} else if (x == 2) {
if (dataStructCount >= 5)
continue;
subject = "Data Structures";
dataStructCount++;
} else if (x == 3) {
if (algorithmsCount >= 5)
continue;
subject = "Algorithms";
algorithmsCount++;
} else if (x == 4) {
if (opSysCount >= 5)
continue;
subject = "Operating Systems";
opSysCount++;
} else if (x == 5) {
if (gamingCount >= 5)
continue;
subject = "Gaming";
gamingCount++;
}
subjectAssigned = true;
}
// get random publication year
int index = (int) (Math.random() * publicationYears.getSize());
int publicationYear = publicationYears.get(index);
publicationYears.remove(index);
// https://stackoverflow.com/questions/22186778/using-math-round-to-round-to-one-decimal-place
Double value = (double) (Math.random() * 10.0 + 0.1);
Double rating = (double) (Math.round(value * scale) / scale);
library.add(
new Book(
"Book" + (i + 1),
publicationYear,
((int) (Math.random() * 950) + 50),
subject,
rating));
}
}
public static ArrayList<Book> sort(Comparator<Book> comparator) {
return null;
} // credit to my dad
public static void sortByPageLength() {
for (int j = 0; j < library.getSize() - 1; j++) {
int minIndex = j;
for (int k = j + 1; k < library.getSize(); k++) {
if (library.get(k).numPages < library.get(minIndex).numPages) {
minIndex = k;
}
}
Book temp = library.get(j);
library.set(j, library.get(minIndex));
library.set(minIndex, temp);
}
} // credit to CSawesome(modified after)
public static void sortbyReviewRatings() {
for (int j = 0; j < library.getSize() - 1; j++) {
int minIndex = j;
for (int k = j + 1; k < library.getSize(); k++) {
if (library.get(k).rating < library.get(minIndex).rating) {
minIndex = k;
}
}
Book temp = library.get(j);
library.set(j, library.get(minIndex));
library.set(minIndex, temp);
}
}// credit to CSawesome(modified after)
public static void sortByPublicationYear() {
for (int j = 0; j < library.getSize() - 1; j++) {
int minIndex = j;
for (int k = j + 1; k < library.getSize(); k++) {
if (library.get(k).publicationYear < library.get(minIndex).publicationYear) {
minIndex = k;
}
}
Book temp = library.get(j);
library.set(j, library.get(minIndex));
library.set(minIndex, temp);
}
} // credit to CSawesome(modified after)
public enum SearchType {
NAME,
SUBJECT
} // credit to my dad
public static ArrayList<Book> search(ArrayList<Book> library, SearchType searchType, String searchParam) {
ArrayList<Book> searchResults = new ArrayList<>();
switch (searchType) {
case SUBJECT:
for (int i = 0; i < library.getSize(); i++) {
Book book = library.get(i);
if (book.subject.equalsIgnoreCase(searchParam)) {
searchResults.add(book);
}
}
break;
case NAME:
// search by name
for (int i = 0; i < library.getSize(); i++) {
Book book = library.get(i);
if (book.title.equalsIgnoreCase(searchParam)) {
searchResults.add(book);
}
}
break;
}
return searchResults;
} // credit to my dad(modified)
public static void printBooks(ArrayList<Book> books) {
for (int i = 0; i < books.getSize(); i++) {
System.out.println(books.get(i));
}
}
public static void menu() {
Scanner in = new Scanner(System.in);
String name;
ArrayList<Book> books;
System.out.println("Please select one of the following options:");
System.out.println("1. List all books");
System.out
.println("2. Display the books sorted according to year of publication, starting with the oldest one");
System.out.println("3. Sort the books according to length in pages, starting with the shortest");
System.out.println("4. Sort the books according to review ratings, starting with the highest rating");
System.out.println("5. Ask user for a subject, and display all the books belonging to that specific subject");
System.out.println("6. Search for a specific book by name, and display all the details if the book exists");
System.out.println("7. Add a book to the list of books (ask the user for all the details)");
System.out.println("8. Exit");
String x = in.nextLine();
switch (x) {
case "1":
printBooks(library);
menu();
case "2":
sortByPublicationYear();
printBooks(library);
menu();
case "3":
sortByPageLength();
printBooks(library);
menu();
case "4":
sortbyReviewRatings();
printBooks(library);
menu();
case "5":
System.out.println("What subject would you like to search?");
name = in.next();
books = search(library, SearchType.SUBJECT, name);
if (books.getSize() == 0) {
System.out.println("No books found");
} else {
printBooks(books);
}
menu();
case "6":
System.out.println("What book would you like to search?");
name = in.next();
books = search(library, SearchType.NAME, name);
if (books.getSize() == 0) {
System.out.println("No books found");
} else {
printBooks(books);
}
menu();
case "7":
System.out.println("Please enter the following details");
System.out.println("Publication year:");
String py = in.next();
int publish = Integer.parseInt(py);
System.out.println("Pages:");
String pn = in.next();
int pages = Integer.parseInt(pn);
System.out.println("Subject:");
String subject = in.next();
System.out.println("Rating:");
String br = in.next();
Double rating = Double.parseDouble(br);
library.add(new Book("Book 21", publish, pages, subject, rating));
menu();
case "8":
System.out.println("Goodbye...");
System.exit(0);
default:
System.out.println("Invalid choice, please try again.");
menu();
}
in.close();
}
}