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.
Merge pull request nus-cs2113-AY2324S2#14 from Joshuahoky/master
- Loading branch information
Showing
7 changed files
with
126 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,31 @@ | ||
package seedu.bookbuddy; | ||
|
||
import java.util.Objects; | ||
import java.util.Scanner; | ||
|
||
public class BookBuddy { | ||
private static BookList bookList = new BookList(); | ||
private static BookList books = new BookList(); | ||
public static void main(String[] args) { | ||
printWelcomeMessage(); | ||
getUserInput(books); | ||
} | ||
|
||
public static void printWelcomeMessage() { | ||
System.out.println("Hello! We are BookBuddy!"); | ||
System.out.println("How can I help you today?"); | ||
} | ||
|
||
System.out.println("Hello! We are bookbuddy!"); | ||
Scanner scanner = new Scanner(System.in); | ||
while(true) { | ||
String input = scanner.nextLine(); | ||
String command = input.split(" ", 2)[0]; | ||
if (Objects.equals(command, "addBook")) { | ||
String actualDescription = input.split(" ", 2)[1]; | ||
bookList.addBook(actualDescription); | ||
} else if (Objects.equals(command, "list")) { | ||
bookList.printAllBooks(); | ||
} | ||
public static void getUserInput(BookList books) { | ||
Scanner input = new Scanner(System.in); | ||
|
||
//noinspection InfiniteLoopStatement | ||
while (true) { | ||
String userInput = input.nextLine(); | ||
Parser.parseCommand(userInput, books); | ||
} | ||
} | ||
|
||
public static void printExitMessage() { | ||
System.out.println("Thank you for using BookBuddy! Hope to see you again!"); | ||
} | ||
|
||
} |
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,54 @@ | ||
package seedu.bookbuddy; | ||
|
||
/** | ||
* Parses inputs from the user in order to execute the correct commands. | ||
*/ | ||
|
||
public class Parser { | ||
public static final String ADD_COMMAND = "add"; | ||
public static final String REMOVE_COMMAND = "remove"; | ||
public static final String LIST_COMMAND = "list"; | ||
public static final String MARK_COMMAND = "mark"; | ||
public static final String UNMARK_COMMAND = "unmark"; | ||
public static final String EXIT_COMMAND = "bye"; | ||
|
||
/** | ||
* Scans the user input for valid commands and handles them accordingly. | ||
* @param input input from the user | ||
* @param books ArrayList of books | ||
*/ | ||
|
||
public static void parseCommand( String input, BookList books) { | ||
String[] inputArray = input.split(" ", 2); | ||
String command = inputArray[0].toLowerCase(); | ||
int index; | ||
|
||
switch (command) { | ||
case ADD_COMMAND: | ||
books.addBook(inputArray[1]); | ||
break; | ||
case REMOVE_COMMAND: | ||
index = Integer.parseInt(inputArray[1]); | ||
books.deleteBook(index); | ||
break; | ||
case LIST_COMMAND: | ||
books.printAllBooks(); | ||
break; | ||
case MARK_COMMAND: | ||
index = Integer.parseInt(inputArray[1]); | ||
books.markDoneByIndex(index); | ||
break; | ||
case UNMARK_COMMAND: | ||
index = Integer.parseInt(inputArray[1]); | ||
books.markUndoneByIndex(index); | ||
break; | ||
case EXIT_COMMAND: | ||
BookBuddy.printExitMessage(); | ||
System.exit(0); | ||
break; | ||
default: | ||
System.out.println("Sorry but that is not a valid command. Please try again"); | ||
} | ||
} | ||
|
||
} |
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,23 @@ | ||
package seedu.bookbuddy; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ParserTest { | ||
@Test | ||
void testParser() { | ||
BookList books = new BookList(); | ||
books.addBook("Don Quixote"); | ||
books.addBook("Gulliver's Travels"); | ||
assertEquals(2, books.getSize()); | ||
books.markDoneByIndex(1); | ||
assertEquals("[R] Don Quixote", books.getBook(0).toString()); | ||
assertEquals("[U] Gulliver's Travels", books.getBook(1).toString()); | ||
books.deleteBook(1); | ||
books.markDoneByIndex(1); | ||
assertTrue(books.getBook(0).isRead); | ||
assertEquals("[R] Gulliver's Travels", books.getBook(0).toString()); | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
Hello! We are bookbuddy! | ||
Hello! We are BookBuddy! | ||
How can I help you today? | ||
Sorry but that is not a valid command. Please try again |