Skip to content

Commit

Permalink
Merge pull request nus-cs2113-AY2324S2#41 from yeozongyao/branch-zong…
Browse files Browse the repository at this point in the history
…yao-categoriseBooks

Categorise the books with personalised labels and with the genre
  • Loading branch information
yeozongyao committed Mar 28, 2024
2 parents 1533432 + 00dff76 commit 726ca9a
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/main/java/seedu/bookbuddy/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public class Book {

public String title;
protected boolean isRead;
protected String label;
protected String genre;


/**
Expand All @@ -14,6 +16,44 @@ public class Book {
public Book(String title) {
this.title = title; // Description of the book
this.isRead = false; //Completion status of the book (True: Read, False: Unread)
this.label = "";
this.genre = "";
}

/**
* Sets the genre for this book.
*
* @param genre The label to set for the book.
*/
public void setGenre(String genre) {
this.label = genre; // Set the label for the book
}

/**
* Returns the genre of the book.
*
* @return The genre of the book.
*/
public String getGenre() {
return this.genre;
}

/**
* Sets the label for this book.
*
* @param label The label to set for the book.
*/
public void setLabel(String label) {
this.label = label; // Set the label for the book
}

/**
* Returns the label of the book.
*
* @return The label of the book.
*/
public String getLabel() {
return this.label;
}

/**
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/seedu/bookbuddy/BookDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package seedu.bookbuddy;

import static seedu.bookbuddy.BookList.books;

public class BookDetails {
/**
* Sets the label of the book at the specified index.
*
* @param index The index of the book in the list.
* @param label The label to set for the book.
* @throws IndexOutOfBoundsException if the index is out of range.
*/
public static void setBookLabelByIndex(int index, String label) throws IndexOutOfBoundsException {
// Check for valid index
if (index < 0 || index >= books.size()) {
throw new IndexOutOfBoundsException("Invalid book index. Please enter a valid index.");
}

// Set the label for the book at the specified index
books.get(index).setLabel(label);
String title = books.get(index).getTitle();
Ui.labelBookMessage(title, label);
}

/**
* Sets the genre of the book at the specified index.
*
* @param index The index of the book in the list.
* @param genre The genre to set for the book.
* @throws IndexOutOfBoundsException if the index is out of range.
*/
public static void setBookGenreByIndex(int index, String genre) throws IndexOutOfBoundsException {
// Check for valid index
if (index < 0 || index >= books.size()) {
throw new IndexOutOfBoundsException("Invalid book index. Please enter a valid index.");
}

// Set the genre for the book at the specified index
books.get(index).setGenre(genre);
String title = books.get(index).getTitle();
Ui.setGenreBookMessage(title, genre);
}

}
42 changes: 42 additions & 0 deletions src/main/java/seedu/bookbuddy/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class Parser {
public static final String UNMARK_COMMAND = "unmark";
public static final String EXIT_COMMAND = "bye";
public static final String HELP_COMMAND = "help";
public static final String LABEL_COMMAND = "label";
public static final String GENRE_COMMAND = "set-genre";

/**
* Scans the user input for valid commands and handles them accordingly.
Expand Down Expand Up @@ -87,6 +89,46 @@ public static void parseCommand(String input, BookList books) {
case HELP_COMMAND:
Ui.helpMessage();
break;
case LABEL_COMMAND:
assert inputArray.length >= 2 : "Command requires additional arguments";
String[] labelMessageParts = inputArray[1].split(" ", 2);
// Split the message into index and label message
assert labelMessageParts.length == 2 : "Command requires an index and a label message";

try {
index = Integer.parseInt(labelMessageParts[0]);
assert index >= 0 : "Index should be non-negative";
String label = labelMessageParts[1];
BookDetails.setBookLabelByIndex(index-1, label);
} catch (NumberFormatException e) {
System.out.println("Invalid input: " + labelMessageParts[0]
+ " is not a valid number. Please enter a valid numeric index.");
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid book index. Please enter a valid index.");
} catch (Exception e) {
System.out.println("An error occurred while setting the label: " + e.getMessage());
}
break;
case GENRE_COMMAND:
assert inputArray.length >= 2 : "Command requires additional arguments";
String[] genreMessageParts = inputArray[1].split(" ", 2);
// Split the message into index and label message
assert genreMessageParts.length == 2 : "Command requires an index and a label message";

try {
index = Integer.parseInt(genreMessageParts[0]);
assert index >= 0 : "Index should be non-negative";
String label = genreMessageParts[1];
BookDetails.setBookGenreByIndex(index-1, label);
} catch (NumberFormatException e) {
System.out.println("Invalid input: " + genreMessageParts[0]
+ " is not a valid number. Please enter a valid numeric index.");
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid book index. Please enter a valid index.");
} catch (Exception e) {
System.out.println("An error occurred while setting the genre: " + e.getMessage());
}
break;
case EXIT_COMMAND:
Ui.printExitMessage();
System.exit(0);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/seedu/bookbuddy/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public static void addBookMessage(String title) {
System.out.println("okii added [" + title + "] to the list.");
System.out.println("remember to read it soon....");
}
public static void labelBookMessage(String title, String label) {
System.out.println("okii labeled [" + title + "] as [" + label + "]");
System.out.println("remember to read it soon....");
}
public static void setGenreBookMessage(String title, String genre) {
System.out.println("okii categorised [" + title + "] as [" + genre + "]");
System.out.println("remember to read it soon....");
}
public static void removeBookMessage(int index) {
System.out.println("alright.. i've removed " + BookList.books.get(index).getTitle() + " from the list.");
}
Expand Down

0 comments on commit 726ca9a

Please sign in to comment.