Skip to content

Commit a5de7e4

Browse files
authored
Merge pull request AY2425S2-CS2113-T12-2#222 from ShengBin-101/shengbin-PED-fixes
Change book category from string to enum
2 parents 1fa8a90 + 50f5244 commit a5de7e4

File tree

8 files changed

+99
-31
lines changed

8 files changed

+99
-31
lines changed

docs/DeveloperGuide.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ The following UML sequence diagram shows how the `add-book BOOK_TITLE a/AUTHOR c
144144
If the book does not exist in the inventory:
145145

146146
- A new `Book` object is created using the parsed arguments. The `note` field is optional and defaults to an empty string if not provided.
147+
- The `category` string is converted to a `Category` enum using `Category.fromString(...)`.
147148
- The `condition` string is converted to a `Condition` enum using `Condition.fromString(...)`.
148149
- The book is added to the `BookList` using `bookList.addBook(...)`.
149150

@@ -639,22 +640,23 @@ BookKeeper gives you full control over your collection in a clean, offline-frien
639640

640641
## Appendix B: User Stories
641642

642-
| Version | As a... | I want to... | So that I can |
643-
| ------- | ------------- | ---------------------------------------------------------------------- | ------------------------------------------------------------------- |
644-
| `v1.0` | Librarian | View inventory, including book count | See my existing books |
645-
| `v1.0` | Librarian | Add new books to the system easily | Update my inventory when acquiring new books |
646-
| `v1.0` | Librarian | Remove books when lost or permanently borrowed | Maintain an accurate inventory |
647-
| `v1.0` | Librarian | Add book loans, including borrower details and return dates | Ensure books are returned on time and inform others of availability |
648-
| `v1.0` | Librarian | Delete book loans, including borrower details and return dates | Maintain accurate loan records |
649-
| `v1.0` | Librarian | View on-going loans | Keep track of what books are loaned out |
650-
| `v2.0` | Librarian | Categorize my inventory | Make searching for books more convenient |
651-
| `v2.0` | Librarian | Manage/Update book availability, including borrowed and reserved books | Efficiently allocate books |
652-
| `v2.0` | Librarian | Track book conditions (e.g good, fair, poor) | Maintain detailed records |
653-
| `v2.0` | Librarian | Add personal notes about individual books | Maintain detailed records |
654-
| `v2.0` | Librarian | Edit existing book loans' due dates | Better track by updating book loans |
655-
| `v2.0` | Librarian | Add contact details for borrowers | Easily reach out to borrowers when needed |
656-
| `v2.0` | Librarian | Keep track of where available books are in the library | Help visitors find books |
643+
| Version | As a... | I want to... | So that I can |
644+
|---------| -------- |------------------------------------------------------------------------|---------------------------------------------------------------------|
645+
| `v1.0` | Librarian | View inventory, including book count | See my existing books |
646+
| `v1.0` | Librarian | Add new books to the system easily | Update my inventory when acquiring new books |
647+
| `v1.0` | Librarian | Remove books when lost or permanently borrowed | Maintain an accurate inventory |
648+
| `v1.0` | Librarian | Add book loans, including borrower details and return dates | Ensure books are returned on time and inform others of availability |
649+
| `v1.0` | Librarian | Delete book loans, including borrower details and return dates | Maintain accurate loan records |
650+
| `v1.0` | Librarian | View on-going loans | Keep track of what books are loaned out |
651+
| `v2.0` | Librarian | Categorize my inventory | Make searching for books more convenient |
652+
| `v2.0` | Librarian | Manage/Update book availability, including borrowed and reserved books | Efficiently allocate books |
653+
| `v2.0` | Librarian | Track book conditions (e.g good, fair, poor) | Maintain detailed records |
654+
| `v2.0` | Librarian | Add personal notes about individual books | Maintain detailed records |
655+
| `v2.0` | Librarian | Edit existing book loans' due dates | Better track by updating book loans |
656+
| `v2.0` | Librarian | Add contact details for borrowers | Easily reach out to borrowers when needed |
657+
| `v2.0` | Librarian | Keep track of where available books are in the library | Help visitors find books |
657658
| `v2.0` | New Librarian | View a list of available commands | Learn how to use the application |
659+
| `v2.1` |Librarian | Update existing personal notes about individual books | Maintain accurate records |
658660

659661
## Appendix C: Non-Functional Requirements
660662

docs/UserGuide.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# User Guide (v2.0)
1+
# User Guide (v2.1)
22

33
## Table of Contents
44

5-
- [User Guide (v2.0)](#user-guide-v20)
5+
- [User Guide (v2.1)](#user-guide-v21)
66
- [Table of Contents](#table-of-contents)
77
- [Introduction](#introduction)
88
- [Quick Start](#quick-start)
@@ -44,7 +44,7 @@ BookKeeper is a Command Line Interface (CLI) library manager application for eff
4444
Welcome to BookKeeper! This guide will help you get started with using the system for managing your library's books and loan records.
4545

4646
1. Ensure that you have Java 17 or above installed.
47-
2. Download the latest version of `BookKeeper` from [here](https://github.com/AY2425S2-CS2113-T12-2/tp/releases/tag/Release-v2.0).
47+
2. Download the latest version of `BookKeeper` from [here](https://github.com/AY2425S2-CS2113-T12-2/tp/releases/tag/Release-v2.1).
4848
3. Copy the file to the folder you want to use as the home folder for BookKeeper.
4949
4. Open a command prompt/terminal and navigate to the folder where you placed the jar file.
5050
5. Run the application using: `java -jar BookKeeper.jar`
@@ -63,6 +63,9 @@ Adds a book to the library collection.
6363

6464
Format: `add-book BOOK_TITLE a/AUTHOR cat/CATEGORY cond/CONDITION loc/LOCATION [note/NOTE]`
6565

66+
Notes:
67+
- Valid categories are: romance, adventure, action, horror, mystery, fiction, nonfiction, scifi, education.
68+
6669
Example:
6770

6871
```

docs/diagrams/classDiagram.puml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class LoanList {
2525
class Book {
2626
- title : String
2727
- author : String
28-
- category : String
2928
- onLoan : boolean
3029
- location : String
3130
- note : String
@@ -46,6 +45,18 @@ enum Condition <<enumeration>> {
4645
POOR
4746
}
4847

48+
enum Category <<enumeration>> {
49+
ROMANCE
50+
ADVENTURE
51+
ACTION
52+
HORROR
53+
MYSTERY
54+
FICTION
55+
NONFICTION
56+
SCIFI
57+
EDUCATION
58+
}
59+
4960
class Storage {
5061
- FOLDER_PATH: String
5162
- INVENTORY_FILE_NAME: String
@@ -87,6 +98,7 @@ InputHandler ..> Storage
8798
InputHandler ..> InputParser
8899
BookKeeper ..> InputHandler
89100
Book -> "1" Condition
101+
Book -> "1" Category
90102
Loan --> "1" Book
91103
Storage ..> BookList
92104
Storage ..> LoanList

src/main/java/bookkeeper/list/BookList.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import bookkeeper.model.Book;
44
import bookkeeper.ui.Formatter;
5+
import bookkeeper.model.Category;
56

67
import java.util.ArrayList;
78

@@ -47,12 +48,19 @@ public ArrayList<Book> findBooksByKeyword(String keyword) {
4748
return filteredBookList;
4849
}
4950

50-
public ArrayList<Book> findBooksByCategory(String category){
51+
public ArrayList<Book> findBooksByCategory(String category) {
5152
ArrayList<Book> filteredBookList = new ArrayList<>();
52-
for (Book book : bookList) {
53-
if (book.getCategory().equalsIgnoreCase(category)) {
54-
filteredBookList.add(book);
53+
try {
54+
// Normalize the input category string to a Category enum
55+
Category targetCategory = Category.fromString(category);
56+
for (Book book : bookList) {
57+
if (book.getCategory() == targetCategory) { // Compare using the enum value
58+
filteredBookList.add(book);
59+
}
5560
}
61+
} catch (IllegalArgumentException e) {
62+
// Handle invalid category input (optional)
63+
Formatter.printBorderedMessage("Invalid category: " + category);
5664
}
5765
return filteredBookList;
5866
}

src/main/java/bookkeeper/model/Book.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class Book {
44
private String title;
55
private String author;
6-
private String category;
6+
private Category category;
77
private Condition condition;
88
private boolean onLoan;
99
private String location;
@@ -13,7 +13,7 @@ public class Book {
1313
public Book(String title, String author, String category, String condition, String location, String note) {
1414
this.title = title;
1515
this.author = author;
16-
this.category = category;
16+
this.category = Category.fromString(category);
1717
this.condition = Condition.fromString(condition);
1818
this.note = note;
1919
this.location = location;
@@ -33,7 +33,7 @@ public String getAuthor() {
3333
return author;
3434
}
3535

36-
public String getCategory() {
36+
public Category getCategory() {
3737
return category;
3838
}
3939

@@ -62,7 +62,7 @@ public void setCondition(String condition){
6262
}
6363

6464
public void setCategory(String category){
65-
this.category = category;
65+
this.category = Category.fromString(category);
6666
}
6767

6868
public void setAuthor(String author){
@@ -84,7 +84,7 @@ public void setLocation(String location) {
8484
public String toFileString() {
8585
String title = getTitle();
8686
String author = getAuthor();
87-
String category = getCategory();
87+
Category category = getCategory();
8888
Condition condition = getCondition();
8989
boolean onLoan = isOnLoan();
9090
String location = getLocation();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package bookkeeper.model;
2+
3+
public enum Category {
4+
ROMANCE, ADVENTURE, ACTION, HORROR, MYSTERY, FICTION, NONFICTION, SCIFI, EDUCATION;
5+
6+
public static Category fromString(String category) {
7+
switch (category.toLowerCase()) {
8+
case "romance":
9+
return ROMANCE;
10+
case "adventure":
11+
return ADVENTURE;
12+
case "action":
13+
return ACTION;
14+
case "horror":
15+
return HORROR;
16+
case "mystery":
17+
return MYSTERY;
18+
case "fiction":
19+
return FICTION;
20+
case "non-fiction":
21+
case "nonfiction":
22+
return NONFICTION;
23+
case "scifi":
24+
return SCIFI;
25+
case "education":
26+
return EDUCATION;
27+
default:
28+
throw new IllegalArgumentException("Invalid category: " + category + "\nValid categories are: " +
29+
"romance, adventure, action, horror, mystery, fiction, nonfiction, scifi, education");
30+
}
31+
}
32+
}

src/main/java/bookkeeper/storage/Storage.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ public static ArrayList<Book> loadInventory() {
112112

113113
// Skip invalid book entries
114114
if (book == null) {
115-
Formatter.printBorderedMessage("Invalid book entry skipped: " + line);
116115
continue;
117116
}
118117

@@ -159,7 +158,6 @@ public static ArrayList<Loan> loadLoans(BookList bookList) {
159158

160159
// Skip invalid loans
161160
if (loan == null) {
162-
Formatter.printBorderedMessage("Invalid loan entry skipped: " + line);
163161
continue;
164162
}
165163

@@ -200,8 +198,17 @@ private static Book parseBookFromString(String line) {
200198
String location = parts[5].trim();
201199
String note = (parts.length == 7) ? parts[6].trim() : "";
202200

201+
Book book;
203202
// Normalize case for title, author, and category
204-
return new Book(title, author, category, condition, location, note);
203+
try {
204+
book = new Book(title, author, category, condition, location, note);
205+
}
206+
catch (IllegalArgumentException e) {
207+
// Handle invalid book creation
208+
Formatter.printBorderedMessage("Invalid book entry skipped: " + line + "\nReason: " + e.getMessage());
209+
return null; // Skip this book
210+
}
211+
return book;
205212
}
206213

207214
private static Loan parseLoanFromString(String line, BookList bookList) {

src/test/java/bookkeeper/BookListTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1010
import static org.junit.jupiter.api.Assertions.assertNotNull;
1111
import static org.junit.jupiter.api.Assertions.assertNull;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
1213

1314
import java.util.ArrayList;
1415

@@ -74,14 +75,17 @@ void testFindBooksByKeyword() {
7475

7576
@Test
7677
void testFindBooksByCategory() {
78+
Book book4 = new Book("Book Four", "Author Four", "nonfiction", "Good", "Shelf 4");
7779
bookList.addBook(book1);
7880
bookList.addBook(book2);
7981
bookList.addBook(book3);
82+
bookList.addBook(book4);
8083

8184
ArrayList<Book> foundBooks = bookList.findBooksByCategory("Non-Fiction");
8285
assertEquals(foundBooks.get(0), book2);
8386
assertEquals(foundBooks.get(1), book3);
8487
assertNotEquals(foundBooks.get(0), book1);
8588
assertNotEquals(foundBooks.get(1), book1);
89+
assertTrue(foundBooks.contains(book4));
8690
}
8791
}

0 commit comments

Comments
 (0)