forked from nus-cs2113-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added features to remove genre and to list the current available genres
- Loading branch information
1 parent
3651acc
commit 2142474
Showing
13 changed files
with
140 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package seedu.bookbuddy.booklist; | ||
|
||
import exceptions.BookNotFoundException; | ||
import seedu.bookbuddy.Ui; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class GenreList { | ||
protected ArrayList<String> availableGenres = new ArrayList<String>(Arrays.asList( | ||
"Fiction", "Non-Fiction", "Mystery", "Science Fiction", "Fantasy" | ||
)); | ||
|
||
public GenreList() { | ||
|
||
} | ||
public ArrayList<String> getAvailableGenres() { | ||
return availableGenres; | ||
} | ||
|
||
public void setAvailableGenres(List<String> newAvailableGenres) { | ||
// Clear the existing list and add all from the new list to avoid reference issues | ||
availableGenres.clear(); | ||
availableGenres.addAll(newAvailableGenres); | ||
} | ||
|
||
public String getGenre(Integer index) { | ||
if (index < 0 || index > availableGenres.size()) { | ||
throw new BookNotFoundException("Genre index out of range."); | ||
} | ||
return availableGenres.get(index - 1); | ||
} | ||
/** | ||
* Removes a specified genre from the available genres list. | ||
* @param genreIndexToRemove The genre to remove from the list. | ||
*/ | ||
public void removeGenre(Integer genreIndexToRemove) { | ||
availableGenres.remove(genreIndexToRemove - 1); | ||
} | ||
|
||
public void printGenreList() { | ||
assert availableGenres != null : "Genre list should not be null since it has been initialised."; | ||
if (!availableGenres.isEmpty()) { | ||
Ui.printLine(); | ||
System.out.println("All Genres:"); | ||
for (int i = 0; i < availableGenres.size(); i++) { | ||
String genre = availableGenres.get(i); | ||
assert genre != null : "Book in list should not be null"; | ||
System.out.print((i + 1) + ". "); | ||
System.out.println(genre); | ||
} | ||
Ui.printShortLine(); | ||
} else { | ||
System.out.println("The list is empty. Add books by 'add [book]'"); | ||
} | ||
} | ||
|
||
public String saveGenresFormat() { | ||
return String.join(",", availableGenres); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/seedu/bookbuddy/parser/parsercommands/ParserRemoveGenre.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package seedu.bookbuddy.parser.parsercommands; | ||
|
||
import seedu.bookbuddy.Ui; | ||
import seedu.bookbuddy.booklist.BookList; | ||
import seedu.bookbuddy.parser.parservalidation.Exceptions; | ||
|
||
public class ParserRemoveGenre { | ||
static void parseRemoveGenre(BookList books, String[] inputArray) { | ||
int index; | ||
assert inputArray.length == 2 : "Command requires additional arguments"; | ||
Exceptions.validateCommandArguments(inputArray, 2, "The remove " + | ||
"Command requires a book index"); | ||
try { | ||
index = Integer.parseInt(inputArray[1].trim()); | ||
String genre = books.genreList.getGenre(index); | ||
if (index > 5) { | ||
books.genreList.removeGenre(index); | ||
Ui.printGenreRemovedMessage(genre); | ||
} else { | ||
System.out.println("sorrryy.. you cannot remove the default genres heehee"); | ||
} | ||
} catch (IndexOutOfBoundsException e) { | ||
System.out.println("Genre list is out of bounds :( " + | ||
"Use [list-genre] to see the genre list"); | ||
} catch (NumberFormatException e) { | ||
System.out.println(inputArray[1].trim() + " is not a valid index format :( "); | ||
} | ||
|
||
} | ||
|
||
public static void executeParseRemove (BookList books, String[] inputArray) { | ||
parseRemoveGenre(books, inputArray); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters