forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrarySystem.java
More file actions
92 lines (78 loc) · 2.68 KB
/
LibrarySystem.java
File metadata and controls
92 lines (78 loc) · 2.68 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
import java.util.*;
class Book {
int id;
String title;
String author;
Book(int id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
}
public String toString() {
return "ID: " + id + ", Title: " + title + ", Author: " + author;
}
}
public class LibrarySystem {
static List<Book> library = new ArrayList<>();
static Scanner sc = new Scanner(System.in);
public static void addBook() {
System.out.print("Enter Book ID: ");
int id = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter Title: ");
String title = sc.nextLine();
System.out.print("Enter Author: ");
String author = sc.nextLine();
library.add(new Book(id, title, author));
System.out.println("Book added successfully!");
}
public static void viewBooks() {
if (library.isEmpty()) {
System.out.println("Library is empty.");
return;
}
System.out.println("\n--- Book List ---");
for (Book b : library) {
System.out.println(b);
}
}
public static void searchBook() {
System.out.print("Enter title to search: ");
sc.nextLine(); // consume newline
String keyword = sc.nextLine().toLowerCase();
boolean found = false;
for (Book b : library) {
if (b.title.toLowerCase().contains(keyword)) {
System.out.println("Found: " + b);
found = true;
}
}
if (!found) System.out.println("No book found with that title.");
}
public static void removeBook() {
System.out.print("Enter Book ID to remove: ");
int id = sc.nextInt();
boolean removed = library.removeIf(b -> b.id == id);
if (removed)
System.out.println("Book removed successfully.");
else
System.out.println("Book not found.");
}
public static void main(String[] args) {
int choice;
do {
System.out.println("\n---Library Menu--");
System.out.println("1. Add Book\n2. View Books\n3. Search Book by Title\n4. Remove Book by ID\n5. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1: addBook(); break;
case 2: viewBooks(); break;
case 3: searchBook(); break;
case 4: removeBook(); break;
case 5: System.out.println("Exiting..."); break;
default: System.out.println("Invalid choice.");
}
} while (choice != 5);
}
}