-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryConsole.java
More file actions
102 lines (90 loc) · 5.02 KB
/
Copy pathLibraryConsole.java
File metadata and controls
102 lines (90 loc) · 5.02 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
import java.util.List;
import java.util.Scanner;
public class LibraryConsole {
public static void main(String[] args) {
Archive archive = new Archive("catalogue.txt", "registry.txt");
Scanner sc = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("\n=== City Library Digital Management System ===");
System.out.println("1) Add Book");
System.out.println("2) Add Member");
System.out.println("3) Issue Book");
System.out.println("4) Return Book");
System.out.println("5) Search Books");
System.out.println("6) Sort Books");
System.out.println("7) View Categories");
System.out.println("8) Save & Exit");
System.out.print("Enter choice: ");
String choice = sc.nextLine().trim();
try {
switch (choice) {
case "1":
System.out.print("Title: "); String title = sc.nextLine().trim();
System.out.print("Author: "); String author = sc.nextLine().trim();
System.out.print("Category: "); String cat = sc.nextLine().trim();
int bid = archive.addBook(title, author, cat);
System.out.println("Book added successfully with ID: " + bid);
break;
case "2":
System.out.print("Member Name: "); String name = sc.nextLine().trim();
System.out.print("Email: "); String email = sc.nextLine().trim();
int mid = archive.addMember(name, email);
System.out.println("Member added successfully with ID: " + mid);
break;
case "3":
System.out.print("Book ID to issue: "); int issueBid = Integer.parseInt(sc.nextLine().trim());
System.out.print("Member ID: "); int issueMid = Integer.parseInt(sc.nextLine().trim());
boolean issued = archive.issueBook(issueBid, issueMid);
System.out.println(issued ? "Book issued." : "Book is already issued.");
break;
case "4":
System.out.print("Book ID to return: "); int retBid = Integer.parseInt(sc.nextLine().trim());
System.out.print("Member ID: "); int retMid = Integer.parseInt(sc.nextLine().trim());
boolean returned = archive.returnBook(retBid, retMid);
System.out.println(returned ? "Book returned." : "This member did not have that book.");
break;
case "5":
System.out.println("Search by: 1) Title 2) Author 3) Category");
String sOpt = sc.nextLine().trim();
System.out.print("Enter search text: ");
String q = sc.nextLine().trim();
List<Tome> results;
if ("1".equals(sOpt)) results = archive.searchBooksByTitle(q);
else if ("2".equals(sOpt)) results = archive.searchBooksByAuthor(q);
else results = archive.searchBooksByCategory(q);
if (results.isEmpty()) System.out.println("No books found.");
else results.forEach(System.out::println);
break;
case "6":
System.out.println("Sort by: 1) Title 2) Author 3) Category");
String so = sc.nextLine().trim();
List<Tome> sorted;
if ("2".equals(so)) sorted = archive.sortBooksByAuthor();
else if ("3".equals(so)) sorted = archive.sortBooksByCategory();
else sorted = archive.sortBooksByTitle();
sorted.forEach(System.out::println);
break;
case "7":
System.out.println("Categories:");
archive.getCategories().forEach(System.out::println);
break;
case "8":
archive.saveAll();
System.out.println("Saved. Exiting.");
running = false;
break;
default:
System.out.println("Invalid option.");
}
} catch (NumberFormatException nfe) {
System.out.println("Invalid number format.");
} catch (IllegalArgumentException iae) {
System.out.println("Input error: " + iae.getMessage());
} catch (Exception ex) {
System.out.println("Unexpected error: " + ex.getMessage());
}
}
sc.close();
}
}