Skip to content

Commit

Permalink
Merge pull request #5 from ifeanyichukwuOtiwa-sports/BP-10010-use-com…
Browse files Browse the repository at this point in the history
…mand-pattern

Bp 10010 use command pattern
  • Loading branch information
ifeanyichukwuOtiwa-sports authored Jun 27, 2022
2 parents 15c8366 + 2030abd commit c5aa0e2
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 10 deletions.
49 changes: 39 additions & 10 deletions src/main/java/io/codewithwinnie/BankClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,33 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.codewithwinnie.commandstrategy.impl.AddInterestCmd;
import io.codewithwinnie.commandstrategy.impl.AuthorizeLoanCmd;
import io.codewithwinnie.commandstrategy.impl.DepositCmd;
import io.codewithwinnie.commandstrategy.impl.NewCmd;
import io.codewithwinnie.commandstrategy.impl.QuitCmd;
import io.codewithwinnie.commandstrategy.impl.SelectCmd;
import io.codewithwinnie.commandstrategy.impl.SetForeignCmd;
import io.codewithwinnie.commandstrategy.impl.ShowAllCmd;
import io.codewithwinnie.commandstrategy.intface.InputCommand;

public class BankClient {
private static final Logger LOG = LoggerFactory.getLogger(BankClient.class);

private int current = -1;
private final Scanner scanner;
private boolean done = false;
private final Bank bank;
private static final InputCommand[] commands = {
new QuitCmd(),
new NewCmd(),
new SelectCmd(),
new DepositCmd(),
new AuthorizeLoanCmd(),
new ShowAllCmd(),
new AddInterestCmd(),
new SetForeignCmd()
};

public BankClient(Scanner scanner, Bank bank) {
this.bank = bank;
Expand All @@ -38,36 +58,37 @@ public void run() {
scanner.close();
}

private void processCommand(final int cmd) {
if (cmd == 0) quit();
else if (cmd == 1) newAccount();
else if (cmd == 2) select();
else if (cmd == 3) deposit();
else if (cmd == 4) authorizeLoan();
else if (cmd == 5) showAll();
else if (cmd == 6) addInterest();
else if (cmd == 7) setForeign();
else LOG.info("Illegal Command");
private void processCommand(final int cnum) {
InputCommand cmd = commands[cnum];
current = cmd.execute(scanner, bank, current);
if (current < 0) {
done = true;
}
}

@Deprecated(forRemoval = true, since = "BP-10010")
private void setForeign() {
bank.setForeign(current, requestForeign());
}

@Deprecated(forRemoval = true, since = "BP-10010")
private boolean requestForeign() {
System.out.println("Enter \n\t1 for Foreign\n\t2 for domestic");
int val = scanner.nextInt();
return val == 1;
}

@Deprecated(forRemoval = true, since = "BP-10010")
private void addInterest() {
bank.addInterest();
}

@Deprecated(forRemoval = true, since = "BP-10010")
private void showAll() {
System.out.println(bank.toString());
}

@Deprecated(forRemoval = true, since = "BP-10010")
private void authorizeLoan() {
LOG.info("Enter loan amount: ");
int loanAmt = scanner.nextInt();
Expand All @@ -77,33 +98,41 @@ private void authorizeLoan() {
LOG.info("Your loan is denied");
}

/**
* replaced with @link{DepositCmd}
*/
@Deprecated(forRemoval = true, since = "BP-10010")
private void deposit() {
System.out.println("Enter Deposit Amount: ");
int amt = scanner.nextInt();
bank.deposit(current, amt);
System.out.println("Credit Alert\nYour current balance is " + bank.getBalance(current));
}

@Deprecated(forRemoval = true, since = "BP-10010")
private void select() {
System.out.println("Enter account number: ");
current = scanner.nextInt();
int balance = bank.getBalance(current);
System.out.println("the balance of " + current + " is " + balance);
}

@Deprecated(forRemoval = true, since = "BP-10010")
private void newAccount() {
boolean isForeign = requestForeign();
int type = requestType();
current = bank.newAccount(type, isForeign);
System.out.println("Your new account Number is " + current );
}

@Deprecated(forRemoval = true, since = "BP-10010")
private int requestType() {
System.out.println("Enter \n\t1 Savings\n\t2 Regular Checking\n\t3 Interest Checking");
return scanner.nextInt();
}


@Deprecated(forRemoval = true, since = "BP-10010")
private void quit() {
done = true;
LOG.info("Good Bye!");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.codewithwinnie.commandstrategy.impl;

import java.util.Scanner;

import io.codewithwinnie.Bank;
import io.codewithwinnie.commandstrategy.intface.InputCommand;

/**
* Created by @author Ifeanyichukwu Otiwa
* 27/06/2022
*/

public class AddInterestCmd implements InputCommand {
@Override
public int execute(final Scanner scanner, final Bank bank, final int current) {
bank.addInterest();
return current;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.codewithwinnie.commandstrategy.impl;

import java.util.Scanner;

import io.codewithwinnie.Bank;
import io.codewithwinnie.commandstrategy.intface.InputCommand;

/**
* Created by @author Ifeanyichukwu Otiwa
* 27/06/2022
*/

public class AuthorizeLoanCmd implements InputCommand {
@Override
public int execute(final Scanner scanner, final Bank bank, final int current) {
System.out.println(("Enter loan amount: "));
int loanAmt = scanner.nextInt();
if (bank.authorizeLoan(loanAmt, current))
System.out.println(("Your loan is approved"));
else
System.out.println(("Your loan is denied"));

return current;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.codewithwinnie.commandstrategy.impl;

import java.util.Scanner;

import io.codewithwinnie.Bank;
import io.codewithwinnie.commandstrategy.intface.InputCommand;

/**
* Created by @author Ifeanyichukwu Otiwa
* 25/06/2022
*/

public class DepositCmd implements InputCommand {
@Override
public int execute(final Scanner scanner, final Bank bank, final int current) {
System.out.println("Enter Deposit Amount: ");
int amt = scanner.nextInt();
bank.deposit(current, amt);
System.out.println("Credit Alert\nYour current balance is " + bank.getBalance(current));
return current;
}

@Override
public String toString() {
return "Deposit";
}
}
25 changes: 25 additions & 0 deletions src/main/java/io/codewithwinnie/commandstrategy/impl/NewCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.codewithwinnie.commandstrategy.impl;

import java.util.Scanner;

import io.codewithwinnie.Bank;
import io.codewithwinnie.commandstrategy.intface.InputCommand;

/**
* Created by @author Ifeanyichukwu Otiwa
* 25/06/2022
*/

public class NewCmd implements InputCommand {
@Override
public int execute(final Scanner scanner, final Bank bank, int current) {
System.out.println("Enter \n\t1 for Foreign\n\t2 for domestic");
boolean isForeign = scanner.nextInt() == 1;
System.out.println("Enter \n\t1 Savings\n\t2 Regular Checking\n\t3 Interest Checking");
int type = scanner.nextInt();
current = bank.newAccount(type, isForeign);
System.out.println("Your new account Number is " + current );
return current;
}

}
23 changes: 23 additions & 0 deletions src/main/java/io/codewithwinnie/commandstrategy/impl/QuitCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.codewithwinnie.commandstrategy.impl;

import java.util.Scanner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.codewithwinnie.Bank;
import io.codewithwinnie.commandstrategy.intface.InputCommand;

/**
* Created by @author Ifeanyichukwu Otiwa
* 25/06/2022
*/

public class QuitCmd implements InputCommand {
private static final Logger LOG = LoggerFactory.getLogger(QuitCmd.class);
@Override
public int execute(final Scanner scanner, final Bank bank, final int current) {
LOG.info("Good Bye!");
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.codewithwinnie.commandstrategy.impl;

import java.util.Scanner;

import io.codewithwinnie.Bank;
import io.codewithwinnie.commandstrategy.intface.InputCommand;

/**
* Created by @author Ifeanyichukwu Otiwa
* 25/06/2022
*/

public class SelectCmd implements InputCommand {
@Override
public int execute(final Scanner scanner, final Bank bank, int current) {
System.out.println("Enter account number: ");
final int curr = scanner.nextInt();
int balance = bank.getBalance(curr);
System.out.println("the balance of " + curr + " is " + balance);
return curr;
}

@Override
public String toString() {
return "Select";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.codewithwinnie.commandstrategy.impl;

import java.util.Scanner;

import io.codewithwinnie.Bank;
import io.codewithwinnie.commandstrategy.intface.InputCommand;

/**
* Created by @author Ifeanyichukwu Otiwa
* 27/06/2022
*/

public class SetForeignCmd implements InputCommand {
@Override
public int execute(final Scanner scanner, final Bank bank, final int current) {
System.out.println("Enter \n\t1 for Foreign\n\t2 for domestic");
int val = scanner.nextInt();
bank.setForeign(current, val == 1);
return current;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.codewithwinnie.commandstrategy.impl;

import java.util.Scanner;

import io.codewithwinnie.Bank;
import io.codewithwinnie.commandstrategy.intface.InputCommand;

/**
* Created by @author Ifeanyichukwu Otiwa
* 27/06/2022
*/

public class ShowAllCmd implements InputCommand {
@Override
public int execute(final Scanner scanner, final Bank bank, final int current) {
System.out.println(bank.toString());
return current;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.codewithwinnie.commandstrategy.intface;

import java.util.Scanner;

import io.codewithwinnie.Bank;

@FunctionalInterface
public interface InputCommand {
public int execute(Scanner scanner, Bank bank, int current);
}

0 comments on commit c5aa0e2

Please sign in to comment.