-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrarySystem.java
More file actions
136 lines (119 loc) · 5 KB
/
Copy pathLibrarySystem.java
File metadata and controls
136 lines (119 loc) · 5 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
import java.io.*;
import java.util.*;
// Represents a Library Member
class Member implements Serializable {
String id, name;
public Member(String id, String name) { this.id = id; this.name = name; }
@Override
public String toString() { return "Member [ID=" + id + ", Name=" + name + "]"; }
}
// Represents a Book with tracking logic
class Book implements Serializable {
String id, title, issuedToId;
boolean isIssued;
public Book(String id, String title) {
this.id = id;
this.title = title;
this.isIssued = false;
this.issuedToId = null;
}
@Override
public String toString() {
String status = isIssued ? "Issued to Member: " + issuedToId : "Available";
return String.format("ID: %-5s | Title: %-20s | %s", id, title, status);
}
}
public class LibrarySystem {
private static final String DATA_FILE = "library_store.dat";
private Map<String, Book> inventory = new HashMap<>();
private Map<String, Member> members = new HashMap<>();
private Scanner scanner = new Scanner(System.in);
public LibrarySystem() { loadData(); }
// --- Core Functionality ---
public void addMember() {
System.out.print("Enter Member ID: "); String id = scanner.nextLine();
System.out.print("Enter Name: "); String name = scanner.nextLine();
members.put(id, new Member(id, name));
saveData();
System.out.println("Member registered successfully.");
}
public void issueBook() {
System.out.print("Enter Book ID: "); String bId = scanner.nextLine();
System.out.print("Enter Member ID: "); String mId = scanner.nextLine();
if (inventory.containsKey(bId) && members.containsKey(mId)) {
Book b = inventory.get(bId);
if (!b.isIssued) {
b.isIssued = true;
b.issuedToId = mId;
saveData();
System.out.println("Book '" + b.title + "' issued to " + members.get(mId).name);
} else {
System.out.println("Error: Book is already issued.");
}
} else {
System.out.println("Error: Invalid Book or Member ID.");
}
}
public void returnBook() {
System.out.print("Enter Book ID to return: ");
String id = scanner.nextLine();
if (inventory.containsKey(id) && inventory.get(id).isIssued) {
inventory.get(id).isIssued = false;
inventory.get(id).issuedToId = null;
saveData();
System.out.println("Book returned and inventory updated.");
} else {
System.out.println("Error: Book was not issued or doesn't exist.");
}
}
// --- Persistence Logic ---
private void saveData() {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(DATA_FILE))) {
oos.writeObject(inventory);
oos.writeObject(members);
} catch (IOException e) { System.out.println("Persistence Error: " + e.getMessage()); }
}
@SuppressWarnings("unchecked")
private void loadData() {
File file = new File(DATA_FILE);
if (!file.exists()) return;
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(DATA_FILE))) {
inventory = (Map<String, Book>) ois.readObject();
members = (Map<String, Member>) ois.readObject();
} catch (Exception e) { System.out.println("Welcome! Starting fresh session."); }
}
// --- UI/UX Console ---
public void runMenu() {
while (true) {
System.out.println("\n--- Library Dashboard ---");
System.out.println("1. Register Member");
System.out.println("2. Add Book");
System.out.println("3. Issue Book");
System.out.println("4. Return Book");
System.out.println("5. View All");
System.out.println("6. Exit");
System.out.print("Choice: ");
String choice = scanner.nextLine();
switch (choice) {
case "1" -> addMember();
case "2" -> {
System.out.print("ID: "); String id = scanner.nextLine();
System.out.print("Title: "); String t = scanner.nextLine();
inventory.put(id, new Book(id, t));
saveData();
}
case "3" -> issueBook();
case "4" -> returnBook();
case "5" -> {
System.out.println("\nINVENTORY:"); inventory.values().forEach(System.out::println);
System.out.println("\nMEMBERS:"); members.values().forEach(System.out::println);
}
case "6" -> System.exit(0);
default -> System.out.println("Invalid entry.");
}
}
}
public static void main(String[] args) {
new LibrarySystem().runMenu();
}
}