Skip to content

Commit c0dc0e6

Browse files
authored
Merge pull request #44 from ShengBin-101/shengbin-Test-v1.0
Fix handler to delete loans associated to book to be deleted
2 parents 0f8708c + a5af442 commit c0dc0e6

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/main/java/bookkeeper/InputHandler.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bookkeeper;
22

33
import java.util.logging.Logger;
4+
45
import bookkeeper.exceptions.BookNotFoundException;
56
import bookkeeper.exceptions.IncorrectFormatException;
67

@@ -17,6 +18,7 @@ public InputHandler() {
1718
this.loanList = new LoanList("Loan List");
1819
logger.info("InputHandler initialized");
1920
}
21+
2022
public void askInput() {
2123
boolean isAskingInput = true;
2224
String userInputLine;
@@ -113,7 +115,15 @@ private void addBook(String[] commandArgs) throws IncorrectFormatException {
113115
}
114116
String[] bookArgs = InputParser.extractAddBookArgs(commandArgs[1]);
115117
assert bookArgs.length == 4 : "Book arguments should contain exactly 4 elements";
116-
assert bookArgs[0] != null && !bookArgs[0].isEmpty() : "Book title cannot be null or empty";
118+
119+
// Trim whitespaces from the book title
120+
String bookTitle = bookArgs[0].trim();
121+
122+
// Check if book already exists in the inventory
123+
if (bookList.findBookByTitle(bookTitle) != null) {
124+
System.out.println("Book already exists in inventory: " + bookTitle);
125+
return;
126+
}
117127

118128
Book newBook = new Book(bookArgs[0], bookArgs[1], bookArgs[2], bookArgs[3]);
119129
bookList.addBook(newBook);
@@ -139,6 +149,7 @@ private void removeBook(String[] commandArgs) throws IncorrectFormatException, B
139149
System.out.println("Book not found in inventory: " + bookTitle);
140150
} else {
141151
assert toRemove.getTitle() != null : "Book to remove must have a valid title";
152+
loanList.removeLoansByBook(toRemove);
142153
bookList.removeBook(toRemove);
143154
System.out.println("Removed book: " + toRemove.getTitle());
144155
}
@@ -147,6 +158,7 @@ private void removeBook(String[] commandArgs) throws IncorrectFormatException, B
147158
/**
148159
* Extract arguments needed to delete loan and delete loan
149160
* Checks if book and loan exist before deleting
161+
*
150162
* @param commandArgs The parsed command arguments.
151163
* @throws IncorrectFormatException If the input format is invalid.
152164
* @throws BookNotFoundException If the book is not found in the inventory.

src/main/java/bookkeeper/InputParser.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ public static String[] extractCommandArgs(String input) throws IncorrectFormatEx
1212
return commandArgs;
1313
}
1414

15-
public static String[] extractXXXX(String input) {
16-
// Copy and Add Implementation
17-
return null;
18-
}
19-
2015
public static String[] extractAddBookArgs(String input) throws IncorrectFormatException {
2116
String[] commandArgs = new String[4];
2217
String[] splitInput = input.trim().split("( a/)|( cat/)|( cond/)", 4);
@@ -93,7 +88,7 @@ public static String[] extractDeleteLoanArgs(String input) throws IncorrectForma
9388
for (int i = 0; i < splitInput.length; i++) {
9489
if (splitInput[i].isBlank()) {
9590
throw new IncorrectFormatException("Invalid format for delete-loan. " +
96-
"Expected format: add-loan BOOK_TITLE n/BORROWER_NAME");
91+
"Expected format: delete-loan BOOK_TITLE n/BORROWER_NAME");
9792
}
9893
commandArgs[i] = splitInput[i].trim();
9994
}

src/main/java/bookkeeper/LoanList.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,10 @@ public Loan findLoan(Book book, String borrower) {
5858
}
5959
return null;
6060
}
61+
62+
public void removeLoansByBook(Book book) {
63+
assert book != null : "Book cannot be null";
64+
loanList.removeIf(loan -> loan.getBook().equals(book));
65+
logger.log(Level.INFO, "Removed all loans associated with book: {0}", book.getTitle());
66+
}
6167
}

0 commit comments

Comments
 (0)