-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryManagementSystem.java
More file actions
202 lines (185 loc) · 6.47 KB
/
LibraryManagementSystem.java
File metadata and controls
202 lines (185 loc) · 6.47 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
194
195
196
197
198
199
200
201
package librarymanagementsystem;
import java.util.*;
class Book {
String name;
String author;
double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
@Override
public String toString() {
return "Book Name: " + name + ", Author: " + author + ", Price: " + price;
}
}
public class LibraryManagementSystem {
private static List<Book> books = new ArrayList<>();
private static List<Book> soldBooks = new ArrayList<>();
private static double totalSellPrice = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\nLibrary Management System");
System.out.println("1. Add Book");
System.out.println("2. Search Book");
System.out.println("3. Remove Book");
System.out.println("4. View All Books");
System.out.println("5. Sort Books by Price");
System.out.println("6. Sell Book");
System.out.println("7. View Sold Books and Total Sales");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addBook(scanner);
break;
case 2:
searchBook(scanner);
break;
case 3:
removeBook(scanner);
break;
case 4:
viewAllBooks();
break;
case 5:
sortBooksByPrice();
viewAllBooks(); // Display sorted books
break;
case 6:
sellBook(scanner);
break;
case 7:
viewSoldBooksAndTotalSales();
break;
case 8:
System.out.println("Exiting... Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 8);
}
private static void addBook(Scanner scanner) {
System.out.print("Enter Book Name: ");
String name = scanner.nextLine();
System.out.print("Enter Author Name: ");
String author = scanner.nextLine();
System.out.print("Enter Book Price: ");
double price = scanner.nextDouble();
books.add(new Book(name, author, price));
System.out.println("Book added successfully.");
}
private static void searchBook(Scanner scanner) {
System.out.print("Enter Book Name to Search: ");
String name = scanner.nextLine();
books.sort(Comparator.comparing(book -> book.name)); // Ensure the list is sorted
int index = binarySearch(books, name);
if (index != -1) {
System.out.println("Book Found: " + books.get(index));
} else {
System.out.println("Book not found.");
}
}
private static int binarySearch(List<Book> books, String name) {
int low = 0, high = books.size() - 1;
while (low <= high) {
int mid = (low + high) / 2;
int comparison = books.get(mid).name.compareToIgnoreCase(name);
if (comparison == 0) {
return mid;
} else if (comparison < 0) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
private static void removeBook(Scanner scanner) {
System.out.print("Enter Book Name to Remove: ");
String name = scanner.nextLine();
Iterator<Book> iterator = books.iterator();
boolean removed = false;
while (iterator.hasNext()) {
if (iterator.next().name.equalsIgnoreCase(name)) {
iterator.remove();
removed = true;
System.out.println("Book removed successfully.");
break;
}
}
if (!removed) {
System.out.println("Book not found.");
}
}
private static void viewAllBooks() {
if (books.isEmpty()) {
System.out.println("No books available.");
} else {
System.out.println("\nBooks in Library:");
for (Book book : books) {
System.out.println(book);
}
}
}
private static void sortBooksByPrice() {
quickSort(books, 0, books.size() - 1);
System.out.println("Books sorted by price successfully.");
}
private static void quickSort(List<Book> books, int low, int high) {
if (low < high) {
int pi = partition(books, low, high);
quickSort(books, low, pi - 1);
quickSort(books, pi + 1, high);
}
}
private static int partition(List<Book> books, int low, int high) {
double pivot = books.get(high).price;
int i = low - 1;
for (int j = low; j < high; j++) {
if (books.get(j).price <= pivot) {
i++;
Collections.swap(books, i, j);
}
}
Collections.swap(books, i + 1, high);
return i + 1;
}
private static void sellBook(Scanner scanner) {
System.out.print("Enter Book Name to Sell: ");
String name = scanner.nextLine();
Iterator<Book> iterator = books.iterator();
boolean sold = false;
while (iterator.hasNext()) {
Book book = iterator.next();
if (book.name.equalsIgnoreCase(name)) {
soldBooks.add(book);
totalSellPrice += book.price;
iterator.remove();
System.out.println("Book sold successfully.");
sold = true;
break;
}
}
if (!sold) {
System.out.println("Book not found.");
}
}
private static void viewSoldBooksAndTotalSales() {
if (soldBooks.isEmpty()) {
System.out.println("No books have been sold yet.");
} else {
System.out.println("\nSold Books:");
for (Book book : soldBooks) {
System.out.println(book);
}
System.out.println("\nTotal Sales: " + totalSellPrice);
}
}
}