Repository navigation
Expand file tree
/
Copy pathRun.java
More file actions
112 lines (104 loc) · 4.32 KB
/
Copy pathRun.java
File metadata and controls
112 lines (104 loc) · 4.32 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
package bookdb;
import java.util.ArrayList;
import java.util.Scanner;
public class Run {
static String instructions = "[1] List all books\n"
+ "[2] List all books by given author\n"
+ "[3] Search for books by title\n"
+ "[4] List all books of a given genre\n"
+ "[5] List all the genres\n"
+ "[6] List all physical books\n"
+ "[7] List all digital books\n"
+ "[0] Exit program\n";
public static void main(String[] args) {
BookDB bdb = new BookDB();
for (Book b : Books.myBooks) {
bdb.add(b);
}
char option;
// Print first set of instructions
System.out.println("Welcome to BookDB!\nPlease enter one of the options below.\n");
System.out.println(instructions);
Scanner input = new Scanner(System.in);
option = input.nextLine().charAt(0);
// Program loop
while (option != '0') {
System.out.println();
if (option == '1') {
ArrayList<Book> books = bdb.getAllBooks();
if (books.isEmpty()) {
System.out.println("No books in DB.");
} else {
bdb.printBooks(books);
}
} else if (option == '2') {
System.out.print("Enter the name of the author: ");
String author = input.nextLine();
System.out.println();
ArrayList<Book> books = bdb.getBooksByAuthor(author);
if (books.isEmpty()) {
System.out.println("No books in DB matching author \"" + author + "\".\n");
} else {
bdb.printBooks(books);
}
} else if (option == '3') {
System.out.print("Enter the title to search for: ");
String title = input.nextLine();
System.out.println();
ArrayList<Book> books = bdb.getBooksByTitle(title);
if (books.isEmpty()) {
System.out.println("No books in DB matching title \"" + title + "\".\n");
} else {
bdb.printBooks(books);
}
} else if (option == '4') {
System.out.print("Enter the genre: ");
String genre = input.nextLine();
System.out.println();
ArrayList<Book> books = bdb.getBooksByGenre(genre);
if (books.isEmpty()) {
System.out.println("No books in DB of genre \"" + genre + "\".\n");
} else {
bdb.printBooks(books);
}
} else if (option == '5') {
ArrayList<String> genres = bdb.getAllGenres();
if (genres.isEmpty()) {
System.out.println("No genres found in DB.\n");
} else {
for (String g : genres) {
System.out.print(g + " ");
}
System.out.println("\n");
}
} else if (option == '6') {
ArrayList<Book> books = bdb.getPhysicalBooks();
if (books.isEmpty()) {
System.out.println("No physical books found in DB.\n");
} else {
for (Book b : books) {
System.out.println(b.getTitle());
}
System.out.println();
}
} else if (option == '7') {
ArrayList<Book> books = bdb.getDigitalBooks();
if (books.isEmpty()) {
System.out.println("No digital books found in DB.\n");
} else {
for (Book b : books) {
System.out.println(b.getTitle());
}
System.out.println();
}
} else if (option == '0') {
break;
} else {
System.out.println("\nInvalid option.\n");
}
System.out.println(instructions);
option = input.nextLine().charAt(0);
}
System.out.println("See you later!");
}
}