-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathLibraryManagementSystem.java
More file actions
193 lines (167 loc) · 6.56 KB
/
LibraryManagementSystem.java
File metadata and controls
193 lines (167 loc) · 6.56 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
import java.io.*;
import java.util.*;
class Book {
private String title;
private String author;
private boolean checkedOut;
public Book(String title, String author) {
this.title = title;
this.author = author;
this.checkedOut = false;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean isCheckedOut() {
return checkedOut;
}
public void setCheckedOut(boolean checkedOut) {
this.checkedOut = checkedOut;
}
}
class Library {
private List<Book> books;
public Library() {
this.books = new ArrayList<>();
}
public void addBook(String title, String author) {
Book book = new Book(title, author);
books.add(book);
}
public void searchBook(String keyword) {
List<Book> searchResults = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
searchResults.add(book);
}
}
if (searchResults.isEmpty()) {
System.out.println("No books found matching the search keyword.");
} else {
System.out.println("Search results:");
for (Book book : searchResults) {
System.out.println(book.getTitle() + " by " + book.getAuthor());
}
}
}
public void checkoutBook(String title) {
for (Book book : books) {
if (book.getTitle().equals(title)) {
if (!book.isCheckedOut()) {
book.setCheckedOut(true);
System.out.println("Book '" + title + "' checked out successfully.");
} else {
System.out.println("Book '" + title + "' is already checked out.");
}
return;
}
}
System.out.println("Book '" + title + "' not found in the library.");
}
public void returnBook(String title) {
for (Book book : books) {
if (book.getTitle().equals(title)) {
if (book.isCheckedOut()) {
book.setCheckedOut(false);
System.out.println("Book '" + title + "' returned successfully.");
} else {
System.out.println("Book '" + title + "' is not checked out.");
}
return;
}
}
System.out.println("Book '" + title + "' not found in the library.");
}
public void saveBooksToFile(String filename) {
try (PrintWriter writer = new PrintWriter(filename)) {
for (Book book : books) {
writer.println(book.getTitle() + "," + book.getAuthor() + "," + book.isCheckedOut());
}
System.out.println("Books saved to file: " + filename);
} catch (IOException e) {
System.out.println("Error saving books to file: " + e.getMessage());
}
}
public void loadBooksFromFile(String filename) {
try (Scanner scanner = new Scanner(new File(filename))) {
books.clear();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
String title = parts[0];
String author = parts[1];
boolean checkedOut = Boolean.parseBoolean(parts[2]);
Book book = new Book(title, author);
book.setCheckedOut(checkedOut);
books.add(book);
}
System.out.println("Books loaded from file: " + filename);
} catch (FileNotFoundException e) {
System.out.println("File not found: " + filename);
}
}
}
public class LibraryManagementSystem {
public static void RunLibraryManagementSystem(String[] args) {
Library library = new Library();
// Loading books from a file (if available)
library.loadBooksFromFile("library.txt");
Scanner scanner = new Scanner(System.in);
int choice = -1;
while (choice != 0) {
System.out.println("----- Library Management System -----");
System.out.println("1. Add a book");
System.out.println("2. Search for books");
System.out.println("3. Check out a book");
System.out.println("4. Return a book");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
if (scanner.hasNextInt()) {
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
System.out.println();
switch (choice) {
case 1:
System.out.print("Enter the title of the book: ");
String title = scanner.nextLine();
System.out.print("Enter the author of the book: ");
String author = scanner.nextLine();
library.addBook(title, author);
System.out.println("Book added successfully.\n");
break;
case 2:
System.out.print("Enter the search keyword: ");
String keyword = scanner.nextLine();
library.searchBook(keyword);
System.out.println();
break;
case 3:
System.out.print("Enter the title of the book to check out: ");
String checkoutTitle = scanner.nextLine();
library.checkoutBook(checkoutTitle);
System.out.println();
break;
case 4:
System.out.print("Enter the title of the book to return: ");
String returnTitle = scanner.nextLine();
library.returnBook(returnTitle);
System.out.println();
break;
case 0:
// Saving books to a file
library.saveBooksToFile("library.txt");
break;
default:
System.out.println("Invalid choice. Please try again.\n");
}
} else {
System.out.println("Invalid input. Please try again.\n");
scanner.nextLine(); // Consume invalid input
}
}
// scanner.close();
}
}