Skip to content

Commit

Permalink
Merge pull request #67 from Gan868611/handle-undefine-command
Browse files Browse the repository at this point in the history
Handle undefine command
  • Loading branch information
Gan868611 authored Mar 15, 2023
2 parents 33074d8 + 0605b65 commit 4a578f6
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 47 deletions.
8 changes: 4 additions & 4 deletions src/main/java/seedu/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
public class Duke {

private static Ui ui;
private static final AccountList account = new AccountList();
private static final AccountList accounts = new AccountList();

/**
* Runs the main input loop until the exit command is called
*/
public static void run() {
public static void run () {
boolean isExit = false;
while (!isExit) {
try {
String fullCommand = ui.getUserInput();
ui.printSpacer();
Command c = Parser.parseInput(fullCommand);
c.execute(ui, account);
c.execute(ui, accounts);
isExit = c.isExit();
} catch (IllegalArgumentException e) {
ui.printMessage(e.getMessage());
Expand All @@ -33,7 +33,7 @@ public static void run() {
/**
* Main entry-point for the java.duke.Duke application.
*/
public static void main(String[] args) {
public static void main (String[] args) {
ui = new Ui();
ui.printGreeting();
ui.printSpacer();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/duke/commands/BalanceCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ private void printCurrencies (ArrayList<Account> accountArrayList, Ui ui) {
* Gets the currencies from the AccountList and displays it onto the screen.
*/
@Override
public void execute (Ui ui, AccountList account) {
public void execute (Ui ui, AccountList accounts) {
try {
String currencyString = processCommand();
ArrayList<Account> accountArrayList = getAccounts(currencyString, account);
ArrayList<Account> accountArrayList = getAccounts(currencyString, accounts);
printCurrencies(accountArrayList, ui);
} catch (InvalidBalanceCommandException e) {
ui.printMessage(ErrorMessage.MORE_THAN_ONE_CURRENCY_PROVIDED);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/duke/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public abstract class Command {
protected final boolean isExit;
protected final String input;

public Command(boolean isExit, String input){
public Command (boolean isExit, String input) {
this.isExit = isExit;
this.input = input;
}
Expand All @@ -16,7 +16,7 @@ public Command(boolean isExit, String input){
* Executes the command implemented by the subclass
*/

public abstract void execute(Ui ui, AccountList account);
public abstract void execute (Ui ui, AccountList accounts);

public boolean isExit () {
return isExit;
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/seedu/duke/commands/CommandType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.duke.commands;

import seedu.duke.constants.Message;

public enum CommandType {
ADD("add"),
WITHDRAW("withdraw"),
Expand All @@ -12,15 +14,15 @@ public enum CommandType {
HELP("help");
private String command;

CommandType(String command) {
CommandType (String command) {
this.command = command;
}

public String getCommand() {
public String getCommand () {
return command;
}

public static CommandType get(String command) {
public static CommandType get (String command) {
if (command == null) {
throw new NullPointerException("Command is null");
}
Expand All @@ -29,6 +31,6 @@ public static CommandType get(String command) {
return commandType;
}
}
throw new IllegalArgumentException("No command found named " + command);
throw new IllegalArgumentException(Message.ERR_UNKNOWN_COMMAND.getMessage());
}
}
6 changes: 3 additions & 3 deletions src/main/java/seedu/duke/commands/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
/**
* Command to exit the program
*/
public class ExitCommand extends Command{
public ExitCommand() {
public class ExitCommand extends Command {
public ExitCommand () {
super(true, "");
}

@Override
public void execute(Ui ui, AccountList account) {
public void execute (Ui ui, AccountList accounts) {
ui.printFarewell();
}
}
4 changes: 2 additions & 2 deletions src/main/java/seedu/duke/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import seedu.duke.ui.Ui;

public class HelpCommand extends Command {
public HelpCommand(String input) {
public HelpCommand (String input) {
super(false, input);
}

@Override
public void execute(Ui ui, AccountList account) {
public void execute (Ui ui, AccountList accounts) {
ui.printMessage(Message.HELP.getMessage());
}
}
22 changes: 11 additions & 11 deletions src/test/java/seedu/duke/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class AddCommandTest {
@Test
public void getCurrency_invalidCurrencyProvided_shouldThrowException() {
public void getCurrency_invalidCurrencyProvided_shouldThrowException () {
try {
Method method = AddCommand.class.getDeclaredMethod("getCurrency", String.class);
method.setAccessible(true);
Expand All @@ -27,7 +27,7 @@ public void getCurrency_invalidCurrencyProvided_shouldThrowException() {
}

@Test
public void getCurrency_validCurrencyProvided_shouldReturnCorrespondingCurrency() {
public void getCurrency_validCurrencyProvided_shouldReturnCorrespondingCurrency () {
try {
Method method = AddCommand.class.getDeclaredMethod("getCurrency", String.class);
method.setAccessible(true);
Expand All @@ -39,7 +39,7 @@ public void getCurrency_validCurrencyProvided_shouldReturnCorrespondingCurrency(
}

@Test
public void processCommand_commandLessThanThreeWords_shouldThrowException() {
public void processCommand_commandLessThanThreeWords_shouldThrowException () {
try {
Method method = AddCommand.class.getDeclaredMethod("processCommand");
method.setAccessible(true);
Expand All @@ -51,7 +51,7 @@ public void processCommand_commandLessThanThreeWords_shouldThrowException() {
}

@Test
public void processCommand_amountNotInt_shouldThrowException() {
public void processCommand_amountNotInt_shouldThrowException () {
try {
Method method = AddCommand.class.getDeclaredMethod("processCommand");
method.setAccessible(true);
Expand All @@ -63,7 +63,7 @@ public void processCommand_amountNotInt_shouldThrowException() {
}

@Test
public void processCommand_correctInputFormat_shouldNotThrowException() {
public void processCommand_correctInputFormat_shouldNotThrowException () {
try {
Method method = AddCommand.class.getDeclaredMethod("processCommand");
method.setAccessible(true);
Expand All @@ -77,15 +77,15 @@ public void processCommand_correctInputFormat_shouldNotThrowException() {
}

@Test
public void execute_correctInputProvided_shouldUpdateAmount() {
public void execute_correctInputProvided_shouldUpdateAmount () {
try {
AccountList account = new AccountList();
account.addAccount(Currency.KRW, 4000.0f);
AccountList accounts = new AccountList();
accounts.addAccount(Currency.KRW, 4000.0f);
AddCommand command = new AddCommand("add KRW 200.00");
Ui ui = new Ui();
command.execute(ui, account);
command.execute(ui, accounts);

int expectedAmount = (int) account.getAccount(Currency.KRW).getBalance();
int expectedAmount = (int) accounts.getAccount(Currency.KRW).getBalance();

assertEquals(4200, expectedAmount);
} catch (Exception e) {
Expand All @@ -94,7 +94,7 @@ public void execute_correctInputProvided_shouldUpdateAmount() {
}

@Test
public void processCommand_amountLessThanZero_shouldThrowException() {
public void processCommand_amountLessThanZero_shouldThrowException () {
try {
Method method = AddCommand.class.getDeclaredMethod("processCommand");
method.setAccessible(true);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/seedu/duke/commands/BalanceCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ public void getBalance_ifCurrencyIsNotSpecified_shouldReturnAllCurrencies () {
@Test
public void getBalance_ifNoAccountExists_shouldThrowException () {
try {
AccountList account = new AccountList();
AccountList accounts = new AccountList();
Method method = BalanceCommand.class.getDeclaredMethod("getAccounts", String.class, AccountList.class);
method.setAccessible(true);
BalanceCommand command = new BalanceCommand("balance");
assertThrows(InvocationTargetException.class, () -> method.invoke(command, "CNY", account));
assertThrows(InvocationTargetException.class, () -> method.invoke(command, "CNY", accounts));
} catch (Exception e) {
fail();
}
Expand Down
35 changes: 18 additions & 17 deletions src/test/java/seedu/duke/commands/DeleteAccountCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class DeleteAccountCommandTest {
@Test
public void getCurrency_invalidCurrencyProvided_shouldThrowException() {
public void getCurrency_invalidCurrencyProvided_shouldThrowException () {
try {
Method method = DeleteAccountCommand.class.getDeclaredMethod("getCurrency", String.class);
method.setAccessible(true);
Expand All @@ -28,7 +28,7 @@ public void getCurrency_invalidCurrencyProvided_shouldThrowException() {
}

@Test
public void getCurrency_validCurrencyProvided_shouldReturnCorrespondingCurrency() {
public void getCurrency_validCurrencyProvided_shouldReturnCorrespondingCurrency () {
try {
Method method = DeleteAccountCommand.class.getDeclaredMethod("getCurrency", String.class);
method.setAccessible(true);
Expand All @@ -40,7 +40,7 @@ public void getCurrency_validCurrencyProvided_shouldReturnCorrespondingCurrency(
}

@Test
public void processCommand_commandLessThanTwoWords_shouldThrowException() {
public void processCommand_commandLessThanTwoWords_shouldThrowException () {
try {
Method method = AddCommand.class.getDeclaredMethod("processCommand");
method.setAccessible(true);
Expand All @@ -52,44 +52,45 @@ public void processCommand_commandLessThanTwoWords_shouldThrowException() {
}

@Test
public void execute_accountNotEmpty_shouldThrowException() {
public void execute_accountNotEmpty_shouldThrowException () {
try {
AccountList account = new AccountList();
account.addAccount(Currency.KRW, 1000);
AccountList accounts = new AccountList();
accounts.addAccount(Currency.KRW, 1000);
DeleteAccountCommand command = new DeleteAccountCommand("delete-account KRW");
Ui ui = new Ui();
command.execute(ui, account);
command.execute(ui, accounts);

// Account should not be removed if account not empty
assertDoesNotThrow(()->account.getAccount(Currency.KRW));
assertDoesNotThrow(() -> accounts.getAccount(Currency.KRW));

} catch (Exception e) {
fail();
}
}

@Test
public void deleteAccount_nonExistCurrency_shouldThrowException() {
public void deleteAccount_nonExistCurrency_shouldThrowException () {
try {
Method method = AccountList.class.getDeclaredMethod(("deleteAccount"), Currency.class);
method.setAccessible(true);
AccountList account = new AccountList();
account.addAccount(Currency.KRW, 1000);
assertThrows(InvocationTargetException.class, () -> method.invoke(account, Currency.JPY));
AccountList accounts = new AccountList();
accounts.addAccount(Currency.KRW, 1000);
assertThrows(InvocationTargetException.class, () -> method.invoke(accounts, Currency.JPY));
} catch (Exception e) {
fail();
}
}

@Test
public void execute_correctInputProvided_shouldDeleteAccount() {
public void execute_correctInputProvided_shouldDeleteAccount () {
try {
AccountList account = new AccountList();
account.addAccount(Currency.KRW, 0);
AccountList accounts = new AccountList();
accounts.addAccount(Currency.KRW, 0);
DeleteAccountCommand command = new DeleteAccountCommand("delete-account KRW");
Ui ui = new Ui();
command.execute(ui, account);
command.execute(ui, accounts);

assertThrows(NoAccountException.class,()->account.getAccount(Currency.KRW));
assertThrows(NoAccountException.class, () -> accounts.getAccount(Currency.KRW));
} catch (Exception e) {
fail();
}
Expand Down

0 comments on commit 4a578f6

Please sign in to comment.