Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
188 lines (164 loc) · 7.46 KB
/
Copy pathMain.java
File metadata and controls
188 lines (164 loc) · 7.46 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
LibrarySystem library = new LibrarySystem();
library.loadData(); // Load data from files
Scanner scanner = new Scanner(System.in);
User currentUser = null;
System.out.println("Welcome to Library Management System");
// Login process
while (currentUser == null) {
System.out.print("Username: ");
String username = scanner.nextLine();
System.out.print("Password: ");
String password = scanner.nextLine();
currentUser = library.authenticateUser(username, password);
if (currentUser == null) {
System.out.println("Invalid credentials. Please try again.");
}
}
System.out.println("Login successful! Welcome, " + currentUser.getName() + " (" + currentUser.getRole() + ")");
boolean running = true;
while (running) {
displayMenuForRole(currentUser.getRole());
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(scanner.nextLine());
switch (currentUser.getRole()) {
case "Admin":
running = handleAdminOptions(choice, library, scanner, currentUser);
break;
case "Librarian":
running = handleLibrarianOptions(choice, library, scanner, currentUser);
break;
case "Reader":
running = handleReaderOptions(choice, library, scanner, currentUser);
break;
}
}
library.saveData(); // Save data to files
System.out.println("Thank you for using Library Management System.");
scanner.close();
}
private static void displayMenuForRole(String role) {
System.out.println("\n===== " + role + " Menu =====");
switch (role) {
case "Admin":
System.out.println("1. Add a user");
System.out.println("2. Remove a user");
System.out.println("3. List all users");
System.out.println("4. Exit");
break;
case "Librarian":
System.out.println("1. Add a book");
System.out.println("2. Remove a book");
System.out.println("3. List all books");
System.out.println("4. Exit");
break;
case "Reader":
System.out.println("1. Borrow a book");
System.out.println("2. Return a book");
System.out.println("3. View available books");
System.out.println("4. Exit");
break;
}
}
private static boolean handleAdminOptions(int choice, LibrarySystem library, Scanner scanner, User currentUser) {
Admin admin = (Admin) currentUser;
switch (choice) {
case 1: // Add a user
System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter role (Admin/Librarian/Reader): ");
String role = scanner.nextLine();
admin.addUser(library, username, password, name, role);
break;
case 2: // Remove a user
System.out.println("Current users:");
admin.listUsers(library);
System.out.print("Enter username to remove: ");
String userToRemove = scanner.nextLine();
admin.removeUser(library, userToRemove);
break;
case 3: // List all users
admin.listUsers(library);
break;
case 4: // Exit
return false;
default:
System.out.println("Invalid option. Please try again.");
}
return true;
}
private static boolean handleLibrarianOptions(int choice, LibrarySystem library, Scanner scanner, User currentUser) {
Librarian librarian = (Librarian) currentUser;
switch (choice) {
case 1: // Add a book
System.out.print("Enter title: ");
String title = scanner.nextLine();
System.out.print("Enter author: ");
String author = scanner.nextLine();
System.out.print("Enter genre: ");
String genre = scanner.nextLine();
System.out.print("Enter ISBN: ");
String isbn = scanner.nextLine();
System.out.print("Enter book type (PrintedBook/Ebook): ");
String bookType = scanner.nextLine();
if (bookType.equalsIgnoreCase("PrintedBook")) {
System.out.print("Enter number of pages: ");
int pages = Integer.parseInt(scanner.nextLine());
librarian.addPrintedBook(library, title, author, genre, isbn, pages);
} else if (bookType.equalsIgnoreCase("Ebook")) {
System.out.print("Enter file format: ");
String fileFormat = scanner.nextLine();
librarian.addEbook(library, title, author, genre, isbn, fileFormat);
}
break;
case 2: // Remove a book
System.out.println("Current books:");
librarian.listBooks(library);
System.out.print("Enter ISBN to remove: ");
String isbnToRemove = scanner.nextLine();
librarian.removeBook(library, isbnToRemove);
break;
case 3: // List all books
librarian.listBooks(library);
break;
case 4: // Exit
return false;
default:
System.out.println("Invalid option. Please try again.");
}
return true;
}
private static boolean handleReaderOptions(int choice, LibrarySystem library, Scanner scanner, User currentUser) {
Reader reader = (Reader) currentUser;
switch (choice) {
case 1: // Borrow a book
System.out.println("Available books:");
reader.listAvailableBooks(library);
System.out.print("Enter ISBN to borrow: ");
String isbnToBorrow = scanner.nextLine();
reader.borrowBook(library, isbnToBorrow);
break;
case 2: // Return a book
System.out.println("Your borrowed books:");
reader.listBorrowedBooks(library);
System.out.print("Enter ISBN to return: ");
String isbnToReturn = scanner.nextLine();
reader.returnBook(library, isbnToReturn);
break;
case 3: // View available books
reader.listAvailableBooks(library);
break;
case 4: // Exit
return false;
default:
System.out.println("Invalid option. Please try again.");
}
return true;
}
}