Repository navigation
Expand file tree
/
Copy pathLibrary.java
More file actions
111 lines (100 loc) · 3.9 KB
/
Copy pathLibrary.java
File metadata and controls
111 lines (100 loc) · 3.9 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
import java.util.ArrayList;
import java.util.HashMap;
import java.io.*;
// Library.java - core business logic
// Demonstrates: Collections (ArrayList, HashMap), Exception handling, File I/O
public class Library {
private ArrayList<Book> books = new ArrayList<>();
private ArrayList<Member> members = new ArrayList<>();
private HashMap<Integer, Integer> issuedBooks = new HashMap<>(); // bookId -> memberId
private static final String FILE_NAME = "library_data.txt";
public void addBook(Book book) {
books.add(book);
System.out.println("Book added: " + book.getTitle());
}
public void addMember(Member member) {
members.add(member);
System.out.println("Member added: " + member.getName());
}
// Issue a book - throws custom exception if not available
public void issueBook(int bookId, int memberId) throws BookNotAvailableException {
Book book = findBookById(bookId);
if (book == null) {
throw new BookNotAvailableException("Book with ID " + bookId + " does not exist.");
}
if (book.isIssued()) {
throw new BookNotAvailableException("Book \"" + book.getTitle() + "\" is already issued.");
}
book.setIssued(true);
issuedBooks.put(bookId, memberId);
System.out.println("Book \"" + book.getTitle() + "\" issued successfully.");
}
// Return a book
public void returnBook(int bookId) throws BookNotAvailableException {
Book book = findBookById(bookId);
if (book == null || !book.isIssued()) {
throw new BookNotAvailableException("This book was not issued, cannot return.");
}
book.setIssued(false);
issuedBooks.remove(bookId);
System.out.println("Book \"" + book.getTitle() + "\" returned successfully.");
}
public Book findBookById(int bookId) {
for (Book b : books) {
if (b.getBookId() == bookId) {
return b;
}
}
return null;
}
public void displayAllBooks() {
if (books.isEmpty()) {
System.out.println("No books in library.");
return;
}
for (Book b : books) {
System.out.println(b);
}
}
public void displayAllMembers() {
if (members.isEmpty()) {
System.out.println("No members registered.");
return;
}
for (Member m : members) {
System.out.println(m);
}
}
// Save book data to a text file - demonstrates File I/O
public void saveToFile() {
try (PrintWriter writer = new PrintWriter(new FileWriter(FILE_NAME))) {
for (Book b : books) {
writer.println(b.getBookId() + "," + b.getTitle() + "," + b.getAuthor() + "," + b.isIssued());
}
System.out.println("Library data saved to " + FILE_NAME);
} catch (IOException e) {
System.out.println("Error saving data: " + e.getMessage());
}
}
// Load book data from file
public void loadFromFile() {
File file = new File(FILE_NAME);
if (!file.exists()) {
return;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 4) {
Book b = new Book(Integer.parseInt(parts[0]), parts[1], parts[2]);
b.setIssued(Boolean.parseBoolean(parts[3]));
books.add(b);
}
}
System.out.println("Library data loaded from " + FILE_NAME);
} catch (IOException e) {
System.out.println("Error loading data: " + e.getMessage());
}
}
}