-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrarySystem.java
More file actions
157 lines (141 loc) · 4.65 KB
/
LibrarySystem.java
File metadata and controls
157 lines (141 loc) · 4.65 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
import java.util.*;
public class LibrarySystem {
private List<Book> books = new ArrayList<>();
private List<User> users = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
private int bookId = 1, userId = 1;
public void start() {
System.out.println("Welcome to the Library Management System");
while (true) {
System.out.println("\n1. Admin Login\n2. User Login\n3. Exit");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1: adminMenu(); break;
case 2: userMenu(); break;
case 3: return;
default: System.out.println("Invalid option.");
}
}
}
private void adminMenu() {
System.out.print("Enter Admin Password: ");
String pass = scanner.nextLine();
if (!pass.equals("admin123")) {
System.out.println("Incorrect password!");
return;
}
while (true) {
System.out.println("\n--- Admin Menu ---");
System.out.println("1. Add Book\n2. View Books\n3. Add User\n4. View Users\n5. Back");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1: addBook(); break;
case 2: viewBooks(); break;
case 3: addUser(); break;
case 4: viewUsers(); break;
case 5: return;
}
}
}
private void addBook() {
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter author name: ");
String author = scanner.nextLine();
books.add(new Book(bookId++, title, author));
System.out.println("Book added.");
}
private void viewBooks() {
for (Book b : books) {
System.out.println(b);
}
}
private void addUser() {
System.out.print("Enter user name: ");
String name = scanner.nextLine();
users.add(new User(userId++, name));
System.out.println("User added.");
}
private void viewUsers() {
for (User u : users) {
System.out.println(u.getId() + " | " + u.getName());
}
}
private void userMenu() {
System.out.print("Enter your User ID: ");
int id = scanner.nextInt();
scanner.nextLine();
User user = findUserById(id);
if (user == null) {
System.out.println("User not found!");
return;
}
while (true) {
System.out.println("\n--- User Menu ---");
System.out.println("1. View Available Books\n2. Issue Book\n3. Return Book\n4. View Issued Book\n5. Back");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1: viewAvailableBooks(); break;
case 2: issueBook(user); break;
case 3: returnBook(user); break;
case 4: viewIssuedBook(user); break;
case 5: return;
}
}
}
private void viewAvailableBooks() {
for (Book b : books) {
if (!b.isIssued()) {
System.out.println(b);
}
}
}
private void issueBook(User user) {
if (user.getIssuedBook() != null) {
System.out.println("You already issued a book.");
return;
}
viewAvailableBooks();
System.out.print("Enter Book ID to issue: ");
int id = scanner.nextInt();
scanner.nextLine();
Book book = findBookById(id);
if (book != null && !book.isIssued()) {
user.issueBook(book);
System.out.println("Book issued successfully.");
} else {
System.out.println("Book not available.");
}
}
private void returnBook(User user) {
if (user.getIssuedBook() == null) {
System.out.println("No book to return.");
return;
}
user.returnBook();
System.out.println("Book returned.");
}
private void viewIssuedBook(User user) {
Book book = user.getIssuedBook();
if (book != null) {
System.out.println("Issued Book: " + book);
} else {
System.out.println("No book issued.");
}
}
private User findUserById(int id) {
for (User u : users) {
if (u.getId() == id) return u;
}
return null;
}
private Book findBookById(int id) {
for (Book b : books) {
if (b.getId() == id) return b;
}
return null;
}
}