Skip to content

Commit 4a15287

Browse files
authored
Merge pull request AY2425S2-CS2113-T12-2#243 from ShengBin-101/shengbin-PED-fixes
Fix InputParser to handle names with "s/o"
2 parents ae493b8 + 1c69735 commit 4a15287

File tree

5 files changed

+60
-33
lines changed

5 files changed

+60
-33
lines changed

docs/UserGuide.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ Adds a book to the library collection.
6363
Format: `add-book BOOK_TITLE a/AUTHOR cat/CATEGORY cond/CONDITION loc/LOCATION [note/NOTE]`
6464

6565
Notes:
66-
66+
- Books in inventory are unique.
6767
- Valid categories are: romance, adventure, action, horror, mystery, fiction, nonfiction, scifi, education.
68+
- Valid conditions are: poor, fair, good.
6869

6970
Example:
7071

@@ -242,12 +243,12 @@ Here are the books in your inventory:
242243

243244
### Adding a Loan: `add-loan`
244245

245-
Adds a loan using the book title.
246+
Adds a loan using the book title.
246247

247248
Format: `add-loan BOOK_TITLE n/BORROWER_NAME d/RETURN_DATE p/PHONE_NUMBER e/EMAIL`
248249

249250
Notes:
250-
251+
- The book to be loaned out must exist in the inventory.
251252
- The RETURN_DATE must be in the format DD-MM-YYYY.
252253
- The RETURN_DATE cannot be in the past.
253254
- The PHONE_NUMBER accepts Singapore numbers only (8 numbers, starting with 9, 8 or 6).
@@ -435,20 +436,19 @@ Warning Message:
435436

436437
## Command Summary
437438

438-
| Action | Format |
439-
| -------------- | -------------------------------------------------------------------------------------- |
440-
| Add Book | `add-book BOOK_TITLE a/AUTHOR cat/CATEGORY cond/CONDITION loc/LOCATION [note/NOTE]` |
441-
| Remove Book | `remove-book BOOK_TITLE` |
442-
| Update Book | `update-book BOOK_TITLE a/AUTHOR cat/CATEGORY cond/CONDITION loc/LOCATION [note/NOTE]` |
443-
| Search Book | `search-title KEYWORD` |
444-
| View Inventory | `view-inventory` |
445-
| Add Note | `add-note BOOK_TITLE note/NOTE` |
446-
| Update note | `update-note BOOK_TITLE note/NOTE` |
447-
| Delete Note | `delete-note BOOK_TITLE` |
448-
| List Category | `list-category CATEGORY` |
449-
| Add Loan | `add-loan BOOK_TITLE n/BORROWER_NAME d/RETURN_DATE p/PHONE_NUMBER e/EMAIL` |
450-
| Delete Loan | `delete-loan BOOK_TITLE` |
451-
| Edit Loan | `edit-loan BOOK_TITLE n/BORROWER_NAME d/RETURN_DATE p/PHONE_NUMBER e/EMAIL` |
452-
| View Loans | `view-loans` |
453-
| Display Help | `help` |
454-
| Exit Program | `exit` |
439+
| Action | Format |
440+
|-----------------|------------------------------------------------------------------------------------------------|
441+
| Add Book | `add-book BOOK_TITLE a/AUTHOR cat/CATEGORY cond/CONDITION loc/LOCATION [note/NOTE]` |
442+
| Remove Book | `remove-book BOOK_TITLE` |
443+
| Update Book | `update-book BOOK_TITLE [a/AUTHOR] [cat/CATEGORY] [cond/CONDITION] [loc/LOCATION] [note/NOTE]` |
444+
| Update Title | `update-title BOOK_TITLE new/NEW_TITLE` |
445+
| Search Book | `search-title KEYWORD` |
446+
| View Inventory | `view-inventory` |
447+
| Delete Note | `delete-note BOOK_TITLE` |
448+
| List Category | `list-category CATEGORY` |
449+
| Add Loan | `add-loan BOOK_TITLE n/BORROWER_NAME d/RETURN_DATE p/PHONE_NUMBER e/EMAIL` |
450+
| Delete Loan | `delete-loan BOOK_TITLE` |
451+
| Edit Loan | `edit-loan BOOK_TITLE [n/BORROWER_NAME] [d/RETURN_DATE] [p/PHONE_NUMBER] [e/EMAIL]` |
452+
| View Loans | `view-loans` |
453+
| Display Help | `help` |
454+
| Exit Program | `exit` |

src/main/java/bookkeeper/exceptions/ErrorMessages.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public final class ErrorMessages {
4545
"Expected format: add-loan BOOK_TITLE n/BORROWER_NAME d/RETURN_DATE p/PHONE_NUMBER e/EMAIL";
4646

4747
public static final String INVALID_PHONE_NUMBER_ADD_LOAN = "Invalid argument for add-loan.\n" +
48-
"Invalid phone number";
48+
"Invalid phone number! \n"+
49+
"Expected format: Singapore numbers only (8 numbers, starting with 9, 8 or 6)";
4950

5051
public static final String INVALID_EMAIL_ADD_LOAN = "Invalid argument for add-loan.\n" +
5152
"Invalid email";
@@ -62,7 +63,8 @@ public final class ErrorMessages {
6263
"No fields provided for edits";
6364

6465
public static final String INVALID_PHONE_NUMBER_EDIT_LOAN = "Invalid argument for edit-loan.\n" +
65-
"Invalid phone number";
66+
"Invalid phone number! \n"+
67+
"Expected format: Singapore numbers only (8 numbers, starting with 9, 8 or 6)";
6668

6769
public static final String INVALID_EMAIL_EDIT_LOAN = "Invalid argument for edit-loan.\n" +
6870
"Invalid email";

src/main/java/bookkeeper/logic/InputParser.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public static String[] extractAddBookArgs(String input) throws IncorrectFormatEx
4444
String note = "";
4545

4646
Set<String> processedPrefixes = new HashSet<>();
47-
String[] parts = input.trim().split("\\s+(?=\\w+/|$)");
47+
String[] parts = input.trim().split(" (?=\\b(?:a/\\s*|cat/\\s*|cond/\\s*|loc/\\s*|note/\\s*)\\b)");
48+
4849

4950
if (parts.length == 0 || parts[0].startsWith("a/") || parts[0].startsWith("cat/") ||
5051
parts[0].startsWith("cond/") || parts[0].startsWith("loc/") || parts[0].startsWith("note/")) {
@@ -159,7 +160,7 @@ public static String[] extractUpdateBookArgs(String input) throws IncorrectForma
159160
String note = null;
160161

161162
Set<String> processedPrefixes = new HashSet<>();
162-
String[] parts = input.trim().split("\\s+(?=\\w+/|$)");
163+
String[] parts = input.trim().split(" (?=\\b(?:a/\\s*|cat/\\s*|cond/\\s*|loc/\\s*|note/\\s*)\\b)");
163164

164165
if (parts.length == 0 || parts[0].startsWith("a/") || parts[0].startsWith("cat/") ||
165166
parts[0].startsWith("cond/") || parts[0].startsWith("loc/") || parts[0].startsWith("note/")) {
@@ -212,7 +213,7 @@ public static String[] extractUpdateBookArgs(String input) throws IncorrectForma
212213
* [3] - Phone number
213214
* [4] - Email
214215
* @throws IncorrectFormatException if the input format is invalid.
215-
* @throws InvalidArgumentException
216+
* @throws InvalidArgumentException if the input argument is invalid.
216217
*/
217218
public static String[] extractAddLoanArgs(String input) throws IncorrectFormatException, InvalidArgumentException {
218219
// Initialize variables for each argument
@@ -226,7 +227,7 @@ public static String[] extractAddLoanArgs(String input) throws IncorrectFormatEx
226227
Set<String> processedPrefixes = new HashSet<>();
227228

228229
// Split the input into parts based on spaces
229-
String[] parts = input.trim().split("\\s+(?=\\w+/|$)");
230+
String[] parts = input.trim().split(" (?=\\b(?:n/\\s*|d/\\s*|p/\\s*|e/\\s*)\\b)");
230231

231232
// Validate and extract the first argument as the book title
232233
if (parts.length == 0 || parts[0].startsWith("n/") || parts[0].startsWith("d/") ||
@@ -318,7 +319,7 @@ public static String[] extractEditLoanArgs(String input) throws IncorrectFormatE
318319
String email = null;
319320

320321
Set<String> processedPrefixes = new HashSet<>();
321-
String[] parts = input.trim().split("\\s+(?=\\w+/|$)");
322+
String[] parts = input.trim().split(" (?=\\b(?:n/\\s*|d/\\s*|p/\\s*|e/\\s*)\\b)");
322323

323324
title = parts[0].trim();
324325

src/test/java/bookkeeper/BookListTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package bookkeeper;
22

3+
import bookkeeper.exceptions.IncorrectFormatException;
34
import bookkeeper.list.BookList;
5+
import bookkeeper.logic.InputParser;
46
import bookkeeper.model.Book;
57
import org.junit.jupiter.api.BeforeEach;
68
import org.junit.jupiter.api.Test;
79

10+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
811
import static org.junit.jupiter.api.Assertions.assertEquals;
912
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1013
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -108,4 +111,12 @@ void viewBookList_oneBookBookList() {
108111
String output = outputStreamCaptor.toString().trim();
109112
assertTrue(output.contains("Book One"));
110113
}
114+
115+
@Test
116+
void extractAddBookArgs_authorWithSlash_success() throws IncorrectFormatException {
117+
String[] arguments = InputParser.extractAddBookArgs("The Great Gatsby a/F. Scott s/o Fitzgerald " +
118+
"cat/Fiction cond/Good loc/Shelf 1");
119+
String[] output = new String[]{"The Great Gatsby", "F. Scott s/o Fitzgerald", "Fiction", "Good", "Shelf 1", ""};
120+
assertArrayEquals(arguments, output);
121+
}
111122
}

src/test/java/bookkeeper/LoanListTest.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package bookkeeper;
22

3+
import bookkeeper.exceptions.IncorrectFormatException;
4+
import bookkeeper.exceptions.InvalidArgumentException;
35
import bookkeeper.list.BookList;
46
import bookkeeper.list.LoanList;
7+
import bookkeeper.logic.InputParser;
58
import bookkeeper.model.Book;
69
import bookkeeper.model.Loan;
710
import org.junit.jupiter.api.BeforeEach;
811
import org.junit.jupiter.api.Test;
912

10-
import static org.junit.jupiter.api.Assertions.assertEquals;
11-
import static org.junit.jupiter.api.Assertions.assertNotNull;
12-
import static org.junit.jupiter.api.Assertions.assertNull;
13-
import static org.junit.jupiter.api.Assertions.assertThrows;
14-
import static org.junit.jupiter.api.Assertions.assertTrue;
15-
1613
import java.io.ByteArrayOutputStream;
1714
import java.io.PrintStream;
1815
import java.time.LocalDate;
1916
import java.time.format.DateTimeFormatter;
2017
import java.util.ArrayList;
2118

19+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
import static org.junit.jupiter.api.Assertions.assertNull;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
2226
public class LoanListTest {
2327
private LoanList loanList;
2428
private Book book1;
@@ -194,4 +198,13 @@ void viewLoanList_oneLoanLoanList() {
194198
String output = outputStreamCaptor.toString().trim();
195199
assertTrue(output.contains("The Great Gatsby"));
196200
}
201+
202+
@Test
203+
void extractAddLoanArgs_borrowerWithSlash_success() throws IncorrectFormatException, InvalidArgumentException {
204+
String[] arguments = InputParser.extractAddLoanArgs("The Great Gatsby n/John s/o Doe " +
205+
"d/01-12-2025 p/98765432 e/john.doe@example.com");
206+
String[] output = new String[]{"The Great Gatsby", "John s/o Doe", "01-12-" +
207+
"2025", "98765432", "john.doe@example.com"};
208+
assertArrayEquals(arguments, output);
209+
}
197210
}

0 commit comments

Comments
 (0)