-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.java
More file actions
193 lines (147 loc) · 4.63 KB
/
Copy pathlibrary.java
File metadata and controls
193 lines (147 loc) · 4.63 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
import java.util.ArrayList;
import java.util.Scanner;
class Book {
private int id;
private String title;
private String author;
private boolean issued;
public Book(int id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
this.issued = false;
}
public int getId() {
return id;
}
public boolean isIssued() {
return issued;
}
public void issueBook() {
issued = true;
}
public void returnBook() {
issued = false;
}
public void display() {
System.out.println("--------------------------------");
System.out.println("Book ID : " + id);
System.out.println("Title : " + title);
System.out.println("Author : " + author);
System.out.println("Status : " +
(issued ? "Issued" : "Available"));
System.out.println("--------------------------------");
}
}
public class LibraryManagementSystem {
static Scanner scanner = new Scanner(System.in);
static ArrayList<Book> books = new ArrayList<>();
static void addBook() {
System.out.print("\nEnter Book ID: ");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter Book Title: ");
String title = scanner.nextLine();
System.out.print("Enter Author Name: ");
String author = scanner.nextLine();
books.add(new Book(id, title, author));
System.out.println("\nBook added successfully!");
}
static Book findBook(int id) {
for (Book book : books) {
if (book.getId() == id) {
return book;
}
}
return null;
}
static void displayBooks() {
if (books.isEmpty()) {
System.out.println("\nNo books available.");
return;
}
System.out.println("\n========== LIBRARY BOOKS ==========");
for (Book book : books) {
book.display();
}
}
static void issueBook() {
System.out.print("\nEnter Book ID: ");
int id = scanner.nextInt();
Book book = findBook(id);
if (book == null) {
System.out.println("Book not found.");
return;
}
if (book.isIssued()) {
System.out.println("Book is already issued.");
return;
}
book.issueBook();
System.out.println("Book issued successfully.");
}
static void returnBook() {
System.out.print("\nEnter Book ID: ");
int id = scanner.nextInt();
Book book = findBook(id);
if (book == null) {
System.out.println("Book not found.");
return;
}
if (!book.isIssued()) {
System.out.println("Book is already available.");
return;
}
book.returnBook();
System.out.println("Book returned successfully.");
}
static void searchBook() {
System.out.print("\nEnter Book ID: ");
int id = scanner.nextInt();
Book book = findBook(id);
if (book != null) {
System.out.println("\nBook Found:");
book.display();
} else {
System.out.println("Book not found.");
}
}
public static void main(String[] args) {
int choice;
while (true) {
System.out.println("\n\n========== LIBRARY MANAGEMENT ==========");
System.out.println("1. Add Book");
System.out.println("2. Display Books");
System.out.println("3. Search Book");
System.out.println("4. Issue Book");
System.out.println("5. Return Book");
System.out.println("6. Exit");
System.out.println("========================================");
System.out.print("Enter Choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
addBook();
break;
case 2:
displayBooks();
break;
case 3:
searchBook();
break;
case 4:
issueBook();
break;
case 5:
returnBook();
break;
case 6:
System.out.println("\nThank you for using the system!");
scanner.close();
return;
default:
System.out.println("\nInvalid choice!");
}
}
}
}