-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary management system.TXT
More file actions
82 lines (74 loc) · 3.07 KB
/
library management system.TXT
File metadata and controls
82 lines (74 loc) · 3.07 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
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class LibraryManagementSystem {
private Map<String, Integer> bookInventory;
public LibraryManagementSystem() {
bookInventory = new HashMap<>();
}
public void addBook(String bookTitle, int quantity) {
if (bookInventory.containsKey(bookTitle)) {
int currentQuantity = bookInventory.get(bookTitle);
bookInventory.put(bookTitle, currentQuantity + quantity);
} else {
bookInventory.put(bookTitle, quantity);
}
System.out.println("Book(s) added successfully!");
}
public void borrowBook(String bookTitle) {
if (bookInventory.containsKey(bookTitle)) {
int quantity = bookInventory.get(bookTitle);
if (quantity > 0) {
bookInventory.put(bookTitle, quantity - 1);
System.out.println("You have borrowed \"" + bookTitle + "\".");
} else {
System.out.println("Sorry, the book \"" + bookTitle + "\" is currently out of stock.");
}
} else {
System.out.println("Sorry, the book \"" + bookTitle + "\" is not available in the library.");
}
}
public void displayInventory() {
if (bookInventory.isEmpty()) {
System.out.println("The library inventory is empty.");
} else {
System.out.println("Library Inventory:");
for (Map.Entry<String, Integer> entry : bookInventory.entrySet()) {
System.out.println(entry.getKey() + " - Quantity: " + entry.getValue());
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
LibraryManagementSystem library = new LibraryManagementSystem();
while (true) {
System.out.println("\n1. Add Book\n2. Borrow Book\n3. Display Inventory\n4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1:
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
scanner.nextLine(); // Consume newline character
library.addBook(title, quantity);
break;
case 2:
System.out.print("Enter book title to borrow: ");
String bookToBorrow = scanner.nextLine();
library.borrowBook(bookToBorrow);
break;
case 3:
library.displayInventory();
break;
case 4:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a number between 1 and 4.");
}
}
}
}