11package bookkeeper ;
22
33import java .util .logging .Logger ;
4+
45import bookkeeper .exceptions .BookNotFoundException ;
56import 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.
0 commit comments