-
Notifications
You must be signed in to change notification settings - Fork 17
/
Library_management_system.java
101 lines (83 loc) · 2.78 KB
/
Library_management_system.java
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
import java.util.ArrayList;
import java.util.Scanner;
class Book {
private String title;
private String author;
private int id;
public Book(String title, String author, int id) {
this.title = title;
this.author = author;
this.id = id;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getId() {
return id;
}
}
class Library {
private ArrayList<Book> books;
public Library() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(int bookId) {
books.removeIf(book -> book.getId() == bookId);
}
public void listBooks() {
for (Book book : books) {
System.out.println("ID: " + book.getId());
System.out.println("Title: " + book.getTitle());
System.out.println("Author: " + book.getAuthor());
System.out.println();
}
}
}
public class LibraryManagementSystem {
public static void main(String[] args) {
Library library = new Library();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Library Management System");
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");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter book title: ");
String title = scanner.next();
System.out.print("Enter author: ");
String author = scanner.next();
int nextId = library.listBooks().size() + 1;
Book newBook = new Book(title, author, nextId);
library.addBook(newBook);
System.out.println("Book added successfully!");
break;
case 2:
System.out.print("Enter the ID of the book to remove: ");
int bookId = scanner.nextInt();
library.removeBook(bookId);
System.out.println("Book removed successfully!");
break;
case 3:
System.out.println("List of Books:");
library.listBooks();
break;
case 4:
System.out.println("Goodbye!");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}