Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add application logic to main method of BinBash class #38

Merged
merged 10 commits into from
Mar 14, 2024
Merged
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ test {
}

application {
mainClass.set("seedu.binbash.Duke")
mainClass.set("seedu.binbash.BinBash")
}

shadowJar {
archiveBaseName.set("duke")
archiveBaseName.set("binbash")
archiveClassifier.set("")
}

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/seedu/binbash/BinBash.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.binbash;

import seedu.binbash.ui.Ui;
import seedu.binbash.command.Command;
import seedu.binbash.command.ByeCommand;

public class BinBash {
/**

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding an empty line here

* Main entry-point for the BinBash application.
*/
public static void main(String[] args) {
Ui userInterface = new Ui();
ItemList itemList = new ItemList();
Parser inputParser = new Parser(itemList);

userInterface.greet();

while (userInterface.isUserActive()) {
String userInput = userInterface.readUserCommand();
Command userCommand = inputParser.parseCommand(userInput);

if (userCommand instanceof ByeCommand) {
userInterface.setUserAsInactive();
continue;
}

String executionResult = userCommand.execute();
userInterface.talk(executionResult);
}

userInterface.farewell();
}
}
21 changes: 0 additions & 21 deletions src/main/java/seedu/binbash/Duke.java

This file was deleted.

3 changes: 2 additions & 1 deletion src/main/java/seedu/binbash/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.regex.Matcher;

import seedu.binbash.command.ByeCommand;
import seedu.binbash.command.Command;
import seedu.binbash.command.DeleteCommand;
import seedu.binbash.command.ListCommand;
Expand All @@ -24,7 +25,7 @@ public Command parseCommand(String userInput) {
case "list":
return parseListCommand(arguments);
default:
return null;
return new ByeCommand(itemList);
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/seedu/binbash/command/ByeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package seedu.binbash.command;

import seedu.binbash.ItemList;

public class ByeCommand extends Command {
public static final String COMMAND_STRING = "bye";

public ByeCommand(ItemList itemList) {
super(itemList);
}

@Override
public String execute() {
return "";
}
}
20 changes: 17 additions & 3 deletions src/main/java/seedu/binbash/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import java.util.Scanner;

public class Ui {
private static final String NEWLINE = System.lineSeparator();
private static final String LOGO = " ____ _ ____ _" + NEWLINE +
" | __ )(_)_ __ | __ ) __ _ ___| |__" + NEWLINE +
" | _ \\| | '_ \\| _ \\ / _` / __| '_ \\" + NEWLINE +
" | |_) | | | | | |_) | (_| \\__ \\ | | |" + NEWLINE +
" |____/|_|_| |_|____/ \\__,_|___/_| |_|" + NEWLINE + NEWLINE;
private static final String WELCOME_MESSAGE = "Welcome to BinBash!";
private static final String GOODBYE_MESSAGE = "Bye!";
private static final String LINE_DIVIDER = "-------------------------------------------------------------";

private Scanner in;
private final Scanner in;
private boolean isUserActive;

public Ui() {
Expand All @@ -19,15 +25,23 @@ public boolean isUserActive() {
return isUserActive;
}

public void setUserAsInactive() {
isUserActive = false;
}

public String readUserCommand() {
return in.nextLine();
}

public void greet() {
talk(WELCOME_MESSAGE);
talk(LOGO + WELCOME_MESSAGE);
}

public void farewell() {
talk(GOODBYE_MESSAGE);
}

public void talk(String line) {
System.out.println(LINE_DIVIDER + "\n" + line + "\n" + LINE_DIVIDER);
System.out.println(LINE_DIVIDER + NEWLINE + line + NEWLINE + LINE_DIVIDER);
}
}
62 changes: 62 additions & 0 deletions src/test/java/seedu/binbash/BinBashTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package seedu.binbash;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;

class BinBashTest {
private static final String NEWLINE = System.lineSeparator();
private static final String TEST_INPUT = "bye";
private static final String EXPECTED_OUTPUT = "-------------------------------------------------------------" +
NEWLINE +
" ____ _ ____ _" + NEWLINE +
" | __ )(_)_ __ | __ ) __ _ ___| |__" + NEWLINE +
" | _ \\| | '_ \\| _ \\ / _` / __| '_ \\" + NEWLINE +
" | |_) | | | | | |_) | (_| \\__ \\ | | |" + NEWLINE +
" |____/|_|_| |_|____/ \\__,_|___/_| |_|" + NEWLINE + NEWLINE +
"Welcome to BinBash!" + NEWLINE +
"-------------------------------------------------------------" + NEWLINE +
"-------------------------------------------------------------" + NEWLINE +
"Bye!" + NEWLINE +
"-------------------------------------------------------------" + NEWLINE;
private static final InputStream systemIn = System.in;
private static final PrintStream systemOut = System.out;
private ByteArrayInputStream testIn;
private ByteArrayOutputStream testOut;

@Test
public void main_readByeCommand_byeCommandExecuted() {
try {
setupInput();
setupOutput();
BinBash.main(new String[0]);
assertEquals(getOutput(), EXPECTED_OUTPUT);
} finally {
restoreSystemInputOutput();
}
}

private void setupInput() {
testIn = new ByteArrayInputStream(TEST_INPUT.getBytes());
System.setIn(testIn);
}

private void setupOutput() {
testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut));
}

private String getOutput() {
return testOut.toString();
}

private void restoreSystemInputOutput() {
System.setIn(systemIn);
System.setOut(systemOut);
}
}
12 changes: 0 additions & 12 deletions src/test/java/seedu/binbash/DukeTest.java

This file was deleted.

22 changes: 14 additions & 8 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
Hello from
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|
-------------------------------------------------------------
____ _ ____ _
| __ )(_)_ __ | __ ) __ _ ___| |__
| _ \| | '_ \| _ \ / _` / __| '_ \
| |_) | | | | | |_) | (_| \__ \ | | |
|____/|_|_| |_|____/ \__,_|___/_| |_|

What is your name?
Hello James Gosling
Welcome to BinBash!
-------------------------------------------------------------
-------------------------------------------------------------

-------------------------------------------------------------
-------------------------------------------------------------
Bye!
-------------------------------------------------------------
3 changes: 2 additions & 1 deletion text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
James Gosling
list
bye
Loading