-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrarian.java
More file actions
233 lines (227 loc) · 8.42 KB
/
Copy pathLibrarian.java
File metadata and controls
233 lines (227 loc) · 8.42 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package javalibrary;
import java.io.*;
import java.util.*;
public class Librarian implements User, Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String email;
private String password;
private int borrowcount;
private double revenue;
// Static data structure to hold all librarian instances
static Scanner input = new Scanner(System.in);
static HashMap<Integer, Librarian> librarianmap = new HashMap<>();
// Constructor for Librarian
public Librarian(int id, String name, String email, String password, int borrowcount, float revenue) {
this.id = id;
this.name = name;
this.email = email;
this.password = password;
this.borrowcount = borrowcount;
this.revenue = revenue;
}
@Override
public int getId() {
return id;
}
@Override
public String getName() {
return name;
}
@Override
public String getEmail() {
return email;
}
@Override
public String getPassword() {
return password;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public void setEmail(String email) {
this.email = email;
}
@Override
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean login(int id, String password) {
readfile();
for (Librarian librarian : librarianmap.values()) {
if (librarian.getId() == id && librarian.getPassword().equals(password)) {
System.out.println("Login successful for " + librarian.getName());
return true;
}
}
System.out.println("Login failed: Invalid ID or password.");
return false;
}
// Getters and setters for borrowcount and revenue
public int getBorrowcount() {
return borrowcount;
}
public double getRevenue() {
return revenue;
}
public void setBorrowcount(int borrowcount) {
this.borrowcount = borrowcount;
}
public void setRevenue(double revenue) {
this.revenue = revenue;
}
// Save librarian data to file
public static void savefile() {
try (ObjectOutputStream writing = new ObjectOutputStream(new FileOutputStream("Librarian.bin"))) {
writing.writeObject(librarianmap);
System.out.println("Librarian data saved to file successfully.");
} catch (IOException e) {
System.out.println("Error while saving Librarian: " + e.getMessage());
}
}
// Read librarian data from file
public static void readfile() {
try (ObjectInputStream loading = new ObjectInputStream(new FileInputStream("Librarian.bin"))) {
librarianmap = (HashMap<Integer, Librarian>) loading.readObject();
System.out.println("Librarians loaded from file successfully.");
} catch (IOException | ClassNotFoundException e) {
System.err.println("Error while loading Librarian: " + e.getMessage());
}
}
// Add a new librarian
public static boolean addLibrarian(int id, String name, String email, String password, int borrowcount, float revenue) {
readfile();
if (librarianmap.containsKey(id)) {
return false;
}
Librarian librarian = new Librarian(id, name, email, password, borrowcount, revenue);
librarianmap.put(id, librarian);
savefile();
return true;
}
// Edit librarian details
public static void editLibrarian(int librarianId, String name, String email, String password, int borrowcount, float revenue){
readfile();
if (!librarianmap.containsKey(librarianId)) {
System.out.println("No data found for ID " + librarianId + ".");
return;
}
Librarian librarian = librarianmap.get(librarianId);
System.out.println("Editing librarian: " + librarian.getName());
if (!name.isEmpty())
{
librarian.setName(name);
}
if (!email.isEmpty())
{
librarian.setEmail(email);
}
if (!password.isEmpty())
{
librarian.setPassword(password);
}
librarianmap.put(librarianId, librarian);
savefile();
System.out.println("Librarian data updated successfully.");
}
public static void deleteLibrarian(int librarianId) {
readfile();
if (librarianmap.containsKey(librarianId))
{
librarianmap.remove(librarianId);
savefile();
System.out.println("Librarian with ID " + librarianId + " has been deleted.");
} else {
System.out.println("No librarian found with ID " + librarianId + ".");
}
}
public static void searchLibrarian(String searchterm) {
readfile();
boolean found = false;
for (Librarian lib : librarianmap.values())
{
if (String.valueOf(lib.getId()).equals(searchterm) ||
lib.getName().equalsIgnoreCase(searchterm) ||
String.valueOf(lib.getEmail()).equalsIgnoreCase(searchterm)
)
{
found = true;
continue;
}
}
if (!found) {
System.out.println("No librarian found with the given search term.");
}
}
// Display all librarians
public static void displayLibrarians() {
readfile();
if (librarianmap.isEmpty()) {
System.out.println("No librarians available to display.");
return;
}
System.out.println("Librarian List:");
for (Librarian librarian : librarianmap.values()) {
System.out.println("ID: " + librarian.getId());
System.out.println("Name: " + librarian.getName());
System.out.println("Email: " + librarian.getEmail());
System.out.println("Borrow Count: " + librarian.getBorrowcount());
System.out.println("Revenue: $" + librarian.getRevenue());
}
}
// Find librarian with the highest borrow count
public static void maxborrowcount() {
readfile();
if (librarianmap.isEmpty())
{
//System.out.println("No Librarian available to evaluate.");
return;
}
Librarian maxBorrower = null;
for (Librarian librarian : librarianmap.values()) {
if (maxBorrower == null || librarian.getBorrowcount() > maxBorrower.getBorrowcount()) {
maxBorrower = librarian;
}
}
if (maxBorrower != null)
{
}
}
public static void maxtotalrevenue()
{
readfile();
if (librarianmap.isEmpty())
{
// System.out.println("No Librarian available to evaluate.");
return;
}
Librarian maxRevenueBorrower = null;
for (Librarian librarian : librarianmap.values())
{
if (maxRevenueBorrower == null || librarian.getRevenue() > maxRevenueBorrower.getRevenue())
{
maxRevenueBorrower = librarian;
}
}
if (maxRevenueBorrower != null)
{
}
}
// Method to create a borrowing record
public static boolean CreateBorrowing(int librarianId, int borrowingId, int borrowerId, int bookId, String borrowingDate, String returnDate)
{
return Borrowing.createBorrowing(librarianId, borrowingId, borrowerId, bookId, borrowingDate, returnDate, true) ;
}
// Method to cancel a borrowing
public static boolean CancelBorrowing(int BorrowingId) {
return Borrowing.cancelBorrowing(BorrowingId);
}
}