Scuffed Implementation Of State Design Pattern + Singleton

master
Hammy 4 years ago
parent 695dafcbe9
commit dcdb55b7a1

@ -1,33 +1,39 @@
package bank.assessment; package bank.assessment;
import bank.assessment.states.BankAccountStates; import bank.assessment.states.BankAccountStates;
import bank.assessment.states.LoggedOutState;
import bank.assessment.states.NotCreatedState; import bank.assessment.states.NotCreatedState;
public abstract class BankAccount { public class BankAccount {
private String username, password, accountNumber, sortCode; private String username, password;
private String accountNumber;
private String sortCode;
private double balance; private double balance;
private double overdraftLimit; private double overdraftLimit;
private boolean isPremium;
BankAccountStates bankAccountState; BankAccountStates bankAccountState;
BankAccount() { this.bankAccountState = NotCreatedState.getInstance(this); } public BankAccount() { this.bankAccountState = NotCreatedState.getInstance(this); }
BankAccount(String username, String password, String accountNumber, String sortCode) { public BankAccount(String username, String password, String sortCode, String accountNumber, boolean isPremium) {
this.username = username; this.username = username;
this.password = password; this.password = password;
this.accountNumber = accountNumber;
this.sortCode = sortCode; this.sortCode = sortCode;
this.bankAccountState = NotCreatedState.getInstance(this); this.accountNumber = accountNumber;
this.isPremium = isPremium;
this.bankAccountState = LoggedOutState.getInstance(this);
} }
public BankAccount(String username, String password, String accountNumber, String sortCode, double overdraftLimit) { public BankAccount(String username, String password, String sortCode, String accountNumber, boolean isPremium, double overdraftLimit) {
this.username = username; this.username = username;
this.password = password; this.password = password;
this.accountNumber = accountNumber;
this.sortCode = sortCode; this.sortCode = sortCode;
this.accountNumber = accountNumber;
this.isPremium = isPremium;
this.overdraftLimit = overdraftLimit; this.overdraftLimit = overdraftLimit;
this.bankAccountState = NotCreatedState.getInstance(this); this.bankAccountState = LoggedOutState.getInstance(this);
} }
// Method Actions To Change State // Method Actions To Change State
@ -37,19 +43,15 @@ public abstract class BankAccount {
public void logIn() { this.bankAccountState.logIn(); } public void logIn() { this.bankAccountState.logIn(); }
public void logOut() { public void logOut() { this.bankAccountState.logOut(); }
this.bankAccountState.logOut();
}
public void deposit() { this.bankAccountState.deposit(); } public void deposit() { this.bankAccountState.deposit(); }
public boolean withdraw() { return this.bankAccountState.withdraw(); } public void withdraw() { this.bankAccountState.withdraw(); }
public void viewDetails() { public void viewDetails() { this.bankAccountState.viewDetails(); }
this.bankAccountState.viewDetails();
}
public void deleteAccount() { this.bankAccountState.deleteAccount(); } public void deleteAccount(Person person) { this.bankAccountState.deleteAccount(person); }
// Getters & Setters // Getters & Setters
public String getUsername() { public String getUsername() {
@ -72,22 +74,16 @@ public abstract class BankAccount {
return accountNumber; return accountNumber;
} }
public void setAccountNumber(String accountNumber) { public String getSortCode() { return sortCode; }
this.accountNumber = accountNumber;
}
public String getSortCode() {
return sortCode;
}
public void setSortCode(String sortCode) {
this.sortCode = sortCode;
}
public double getBalance() { public double getBalance() {
return balance; return balance;
} }
public boolean isPremium() { return isPremium; }
public void setPremium(boolean premium) { isPremium = premium; }
public void setBalance(double balance) { public void setBalance(double balance) {
this.balance = balance; this.balance = balance;
} }
@ -100,8 +96,8 @@ public abstract class BankAccount {
this.overdraftLimit = overdraftLimit; this.overdraftLimit = overdraftLimit;
} }
void displayBalance() { public void displayBalance() {
System.out.println("Current Account Balance: " + balance); System.out.println("Current Account Balance: " + String.format("£%.2f", balance));
} }
@Override @Override

@ -8,7 +8,7 @@ public class Main {
// user inputs because the focus of the project is the design. Not going to handle any users that are "dumb" :) // user inputs because the focus of the project is the design. Not going to handle any users that are "dumb" :)
Person person = new Person(); Person person = new Person();
person.setBankAccount(new BasicAccount()); person.setBankAccount(new BankAccount());
getPersonDetails(person); getPersonDetails(person);
int choice; int choice;
@ -36,7 +36,7 @@ public class Main {
case 4 -> person.getBankAccount().deposit(); case 4 -> person.getBankAccount().deposit();
case 5 -> person.getBankAccount().withdraw(); case 5 -> person.getBankAccount().withdraw();
case 6 -> person.getBankAccount().logOut(); case 6 -> person.getBankAccount().logOut();
case 7 -> person.getBankAccount().deleteAccount(); case 7 -> person.getBankAccount().deleteAccount(person);
} }
} while (choice != 8); } while (choice != 8);

@ -8,8 +8,8 @@ public interface BankAccountStates {
void logIn(); void logIn();
void logOut(); void logOut();
void deposit(); void deposit();
boolean withdraw(); void withdraw();
void viewDetails(); void viewDetails();
void deleteAccount(); void deleteAccount(Person person);
} }

@ -6,21 +6,17 @@ import bank.assessment.Person;
public class LoggedInState implements BankAccountStates { public class LoggedInState implements BankAccountStates {
BankAccount bankAccount; private static BankAccount bankAccount;
private static LoggedInState instance; private static LoggedInState instance;
private LoggedInState(BankAccount bankAccount) { public LoggedInState(BankAccount bankAccount) { LoggedInState.bankAccount = bankAccount; }
this.bankAccount = bankAccount;
}
public static LoggedInState getInstance(BankAccount bankAccount) { public static LoggedInState getInstance(BankAccount ba) {
return instance == null ? instance = new LoggedInState(bankAccount) : instance; return instance == null || bankAccount != ba ? instance = new LoggedInState(ba) : instance;
} }
@Override @Override
public void createAccount(Person person) { public void createAccount(Person person) { System.out.println("This Account Is Already Registered With Us. You Are Already Logged In!"); }
System.out.println("This Account Is Already Registered With Us. You Are Already Logged In!");
}
@Override @Override
public void logIn() { public void logIn() {
@ -40,25 +36,33 @@ public class LoggedInState implements BankAccountStates {
if (cashToDeposit >= 0) { if (cashToDeposit >= 0) {
bankAccount.setBalance(bankAccount.getBalance() + cashToDeposit); bankAccount.setBalance(bankAccount.getBalance() + cashToDeposit);
System.out.printf("Deposit Of %s Successful!\n", cashToDeposit);
bankAccount.displayBalance();
} else { } else {
System.out.println("Amount Entered Not Valid! Try Again!"); System.out.println("Amount Entered Not Valid! Try Again!");
} }
} }
@Override @Override
public boolean withdraw() { public void withdraw() {
return bankAccount.withdraw(); System.out.println("How Much Money Do You Want To Withdraw?");
double cashToWithdraw = Double.parseDouble(Console.INPUT.nextLine());
if (cashToWithdraw <= (bankAccount.getBalance() + bankAccount.getOverdraftLimit())) {
bankAccount.setBalance(bankAccount.getBalance() - cashToWithdraw);
System.out.printf("Withdrawal Of %s Successful!\n", cashToWithdraw);
bankAccount.displayBalance();
} else {
System.out.println("Insufficient Funds Available to Withdraw! Try Again!");
}
} }
@Override @Override
public void viewDetails() { public void viewDetails() { System.out.println("Your Current Bank Details: \n" + bankAccount); }
System.out.println("Your Current Bank Details:");
System.out.println(bankAccount);
}
@Override @Override
public void deleteAccount() { public void deleteAccount(Person person) {
bankAccount.changeBankAccountState(NotCreatedState.getInstance(bankAccount)); person.setBankAccount(new BankAccount());
System.out.println("Account Deleted! We're Sad To See You Go :("); System.out.println("Account Deleted! We're Sad To See You Go :(");
} }
} }

@ -6,15 +6,15 @@ import bank.assessment.Person;
public class LoggedOutState implements BankAccountStates { public class LoggedOutState implements BankAccountStates {
BankAccount bankAccount; private static BankAccount bankAccount;
private static LoggedOutState instance; private static LoggedOutState instance;
private LoggedOutState(BankAccount bankAccount) { private LoggedOutState(BankAccount bankAccount) {
this.bankAccount = bankAccount; LoggedOutState.bankAccount = bankAccount;
} }
public static LoggedOutState getInstance(BankAccount bankAccount) { public static LoggedOutState getInstance(BankAccount ba) {
return instance == null ? instance = new LoggedOutState(bankAccount) : instance; return instance == null || bankAccount != ba ? instance = new LoggedOutState(ba) : instance;
} }
@Override @Override
@ -33,7 +33,8 @@ public class LoggedOutState implements BankAccountStates {
if (!bankAccount.getUsername().equalsIgnoreCase(loginUsername) || !bankAccount.getPassword().equalsIgnoreCase(loginPassword)) { if (!bankAccount.getUsername().equalsIgnoreCase(loginUsername) || !bankAccount.getPassword().equalsIgnoreCase(loginPassword)) {
System.out.println("Uh Oh Error! Incorrect Username Or Password Entered!"); System.out.println("Uh Oh Error! Incorrect Username Or Password Entered!");
} else { } else {
System.out.printf("Welcome Back '%s'", bankAccount.getUsername()); System.out.printf("Welcome Back '%s'\n", bankAccount.getUsername());
System.out.println("Your Current Bank Details: \n" + bankAccount);
bankAccount.changeBankAccountState(LoggedInState.getInstance(bankAccount)); bankAccount.changeBankAccountState(LoggedInState.getInstance(bankAccount));
} }
} }
@ -49,9 +50,8 @@ public class LoggedOutState implements BankAccountStates {
} }
@Override @Override
public boolean withdraw() { public void withdraw() {
System.out.println("Please Log In Before Trying To Withdraw Funds!"); System.out.println("Please Log In Before Trying To Withdraw Funds!");
return false;
} }
@Override @Override
@ -60,8 +60,8 @@ public class LoggedOutState implements BankAccountStates {
} }
@Override @Override
public void deleteAccount() { public void deleteAccount(Person person) {
bankAccount.changeBankAccountState(NotCreatedState.getInstance(bankAccount)); person.setBankAccount(new BankAccount());
System.out.println("Account Deleted! We're Sad To See You Go :("); System.out.println("Account Deleted! We're Sad To See You Go :(");
} }
} }

@ -1,21 +1,21 @@
package bank.assessment.states; package bank.assessment.states;
import bank.assessment.*; import bank.assessment.BankAccount;
import bank.assessment.Console;
import bank.assessment.Person;
import java.util.Random; import java.util.Random;
public class NotCreatedState implements BankAccountStates { public class NotCreatedState implements BankAccountStates {
BankAccount bankAccount; private static BankAccount bankAccount;
private final Random random = new Random();
private static NotCreatedState instance; private static NotCreatedState instance;
private final Random random = new Random();
private NotCreatedState(BankAccount bankAccount) { private NotCreatedState(BankAccount bankAccount) { NotCreatedState.bankAccount = bankAccount; }
this.bankAccount = bankAccount;
}
public static NotCreatedState getInstance(BankAccount bankAccount) { public static NotCreatedState getInstance(BankAccount ba) {
return instance == null ? instance = new NotCreatedState(bankAccount) : instance; return instance == null || bankAccount != ba ? instance = new NotCreatedState(ba) : instance;
} }
@Override @Override
@ -26,26 +26,24 @@ public class NotCreatedState implements BankAccountStates {
System.out.println("Please Enter in a Suitable Password: "); System.out.println("Please Enter in a Suitable Password: ");
String password = Console.INPUT.nextLine(); String password = Console.INPUT.nextLine();
String accountNumber = String.valueOf(generateRandomNum(random, 100000, 999999));
String sortCode = generateRandomNum(random, 10, 99) + "-" + generateRandomNum(random, 10,99) + "-" + generateRandomNum(random, 10,99);
System.out.println("Would You Like A Premium Account? (Format Y/N)"); System.out.println("Would You Like A Premium Account? (Format Y/N)");
String choice = Console.INPUT.nextLine(); String choice = Console.INPUT.nextLine();
String sortCode = generateRandomNum(random, 10, 99) + "-" + generateRandomNum(random, 10,99) + "-" + generateRandomNum(random, 10,99);
String accountNumber = String.valueOf(generateRandomNum(random, 100000, 999999));
if (choice.equalsIgnoreCase("y")) { if (choice.equalsIgnoreCase("y")) {
System.out.println("What Would You Like Your Overdraft Limit To Be?"); System.out.println("What Would You Like Your Overdraft Limit To Be?");
String overdraftLimit = Console.INPUT.nextLine(); double overdraftLimit = Double.parseDouble(Console.INPUT.nextLine());
bankAccount = new BankAccount(username, password, sortCode, accountNumber, true, overdraftLimit);
bankAccount = new PremiumAccount(username, password, accountNumber, sortCode, Double.parseDouble(overdraftLimit));
} else if (choice.equalsIgnoreCase("n")) { } else if (choice.equalsIgnoreCase("n")) {
bankAccount = new BasicAccount(username, password, accountNumber, sortCode); bankAccount = new BankAccount(username, password, sortCode, accountNumber, false);
} }
System.out.println("Bank Account Successfully Created!"); System.out.println("Bank Account Successfully Created!");
System.out.println("Your Details Are: " + bankAccount); System.out.println("Your Details Are: " + bankAccount);
bankAccount.changeBankAccountState(LoggedOutState.getInstance(bankAccount));
person.setBankAccount(bankAccount); person.setBankAccount(bankAccount);
} }
@ -63,18 +61,13 @@ public class NotCreatedState implements BankAccountStates {
} }
@Override @Override
public boolean withdraw() { public void withdraw() { System.out.println("Sorry, Please Log In First Before Trying Withdraw Funds!"); }
System.out.println("Sorry, Please Log In First Before Trying Withdraw Funds!");
return false;
}
@Override @Override
public void viewDetails() { System.out.println("There Are No Account Details To View, Please Log In or Register!"); } public void viewDetails() { System.out.println("There Are No Account Details To View, Please Log In or Register!"); }
@Override @Override
public void deleteAccount() { public void deleteAccount(Person person) { System.out.println("No Account Found To Delete"); }
System.out.println("No Account Found To Delete, Please Log In!");
}
private int generateRandomNum(Random random, int lower, int upper) { private int generateRandomNum(Random random, int lower, int upper) {
return lower + random.nextInt(upper) + 1; return lower + random.nextInt(upper) + 1;

Loading…
Cancel
Save