Implement Own Implementation of State Design Pattern through Revisiting Previous Assessment

master
Hammy 4 years ago
parent 9ee08e7713
commit fbe7e2294d

@ -0,0 +1,118 @@
package bank.assessment;
import bank.assessment.states.BankAccountStates;
import bank.assessment.states.NotCreatedState;
public abstract class BankAccount {
private String username, password, accountNumber, sortCode;
private double balance;
private double overdraftLimit;
BankAccountStates bankAccountState;
BankAccount() { this.bankAccountState = NotCreatedState.getInstance(this); }
BankAccount(String username, String password, String accountNumber, String sortCode) {
this.username = username;
this.password = password;
this.accountNumber = accountNumber;
this.sortCode = sortCode;
this.bankAccountState = NotCreatedState.getInstance(this);
}
public BankAccount(String username, String password, String accountNumber, String sortCode, double overdraftLimit) {
this.username = username;
this.password = password;
this.accountNumber = accountNumber;
this.sortCode = sortCode;
this.overdraftLimit = overdraftLimit;
this.bankAccountState = NotCreatedState.getInstance(this);
}
// Method Actions To Change State
public void changeBankAccountState(BankAccountStates bankAccountState) { this.bankAccountState = bankAccountState; }
public void createAccount(Person person) { this.bankAccountState.createAccount(person); }
public void logIn() { this.bankAccountState.logIn(); }
public void logOut() {
this.bankAccountState.logOut();
}
public void deposit() { this.bankAccountState.deposit(); }
public boolean withdraw() { return this.bankAccountState.withdraw(); }
public void viewDetails() {
this.bankAccountState.viewDetails();
}
public void deleteAccount() { this.bankAccountState.deleteAccount(); }
// Getters & Setters
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getSortCode() {
return sortCode;
}
public void setSortCode(String sortCode) {
this.sortCode = sortCode;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getOverdraftLimit() {
return overdraftLimit;
}
public void setOverdraftLimit(double overdraftLimit) {
this.overdraftLimit = overdraftLimit;
}
void displayBalance() {
System.out.println("Current Account Balance: " + balance);
}
@Override
public String toString() {
return "BankAccount{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", accountNumber='" + accountNumber + '\'' +
", sortCode='" + sortCode + '\'' +
", balance=" + String.format("£%.2f", balance) +
", overdraftLimit=" + String.format("£%.2f", overdraftLimit) +
'}';
}
}

@ -0,0 +1,14 @@
package bank.assessment;
public class BasicAccount extends BankAccount {
public BasicAccount() { }
public BasicAccount(String username, String password, String accountNumber, String sortCode) {
super(username, password, accountNumber, sortCode);
}
public BasicAccount(String username, String password, String accountNumber, String sortCode, double overdraftLimit) {
super(username, password, accountNumber, sortCode, overdraftLimit);
}
}

@ -0,0 +1,7 @@
package bank.assessment;
import java.util.Scanner;
public class Console {
public static final Scanner INPUT = new Scanner(System.in);
}

@ -0,0 +1,60 @@
package bank.assessment;
public class Main {
public static void main(String[] args) {
// Note:
// For the first time in my life. This project will include no error handling for the
// 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.setBankAccount(new BasicAccount());
getPersonDetails(person);
int choice;
do
{
System.out.println("""
Welcome Glasgow University Banking\s
Press 1: Register With Us
Press 2: Log In
Press 3: View Details
Press 4: Deposit Money
Press 5: Withdraw Money
Press 6: Log Out
Press 7: Delete Account
Press 8: Quit
""");
choice = Integer.parseInt(Console.INPUT.nextLine());
switch (choice) {
case 1 -> person.getBankAccount().createAccount(person);
case 2 -> person.getBankAccount().logIn();
case 3 -> person.getBankAccount().viewDetails();
case 4 -> person.getBankAccount().deposit();
case 5 -> person.getBankAccount().withdraw();
case 6 -> person.getBankAccount().logOut();
case 7 -> person.getBankAccount().deleteAccount();
}
} while (choice != 8);
System.out.println("Thank You For Using Glasgow University Banking! We Hope to See You Soon :D");
System.exit(0);
}
private static void getPersonDetails(Person person) {
System.out.println("What is Your Forename?");
person.setForename(Console.INPUT.nextLine());
System.out.println("What is Your Surname?");
person.setSurname(Console.INPUT.nextLine());
System.out.println("What is Your Email Address?");
person.setEmailAddress(Console.INPUT.nextLine());
System.out.println("What is Your Date of Birth? (Format: DD/MM/YYYY)");
person.setDateOfBirth(Console.INPUT.nextLine());
}
}

@ -0,0 +1,65 @@
package bank.assessment;
public class Person {
private String forename, surname, emailAddress, dateOfBirth;
private BankAccount bankAccount;
public Person() { }
public Person(String forename, String surname, String emailAddress, String dateOfBirth) {
this.forename = forename;
this.surname = surname;
this.emailAddress = emailAddress;
this.dateOfBirth = dateOfBirth;
}
public String getForename() {
return forename;
}
public void setForename(String forename) {
this.forename = forename;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; }
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public BankAccount getBankAccount() {
return bankAccount;
}
public void setBankAccount(BankAccount bankAccount) {
this.bankAccount = bankAccount;
}
@Override
public String toString() {
return "Person{" +
"forename='" + forename + '\'' +
", surname='" + surname + '\'' +
", emailAddress='" + emailAddress + '\'' +
", dateOfBirth='" + dateOfBirth + '\'' +
", bankAccount=" + bankAccount +
'}';
}
}

@ -0,0 +1,26 @@
package bank.assessment;
public class PremiumAccount extends BasicAccount {
public PremiumAccount(String username, String password, String accountNumber, String sortCode, double overdraftLimit) {
super(username, password, accountNumber, sortCode, overdraftLimit);
}
@Override
public boolean withdraw() {
System.out.println("How Much Money Do You Want To Withdraw?");
double amount = Double.parseDouble(Console.INPUT.nextLine());
if (amount <= (getBalance() + getOverdraftLimit())) {
setBalance(getBalance() - amount);
System.out.println("Withdraw Successful!");
displayBalance();
return true;
} else {
System.out.println("Insufficient Funds Available to Withdraw! Try Again!");
return false;
}
}
}

@ -0,0 +1,15 @@
package bank.assessment.states;
import bank.assessment.Person;
public interface BankAccountStates {
void createAccount(Person person);
void logIn();
void logOut();
void deposit();
boolean withdraw();
void viewDetails();
void deleteAccount();
}

@ -0,0 +1,63 @@
package bank.assessment.states;
import bank.assessment.BankAccount;
import bank.assessment.Console;
import bank.assessment.Person;
public class LoggedInState implements BankAccountStates {
BankAccount bankAccount;
private static LoggedInState instance;
private LoggedInState(BankAccount bankAccount) {
this.bankAccount = bankAccount;
}
public static LoggedInState getInstance(BankAccount bankAccount) {
return instance == null ? instance = new LoggedInState(bankAccount) : instance;
}
@Override
public void createAccount(Person person) {
System.out.println("This Account Is Already Registered With Us. Please Log In!");
}
@Override
public void logIn() {
System.out.println("You Are Already Logged In!");
}
@Override
public void logOut() {
// Log out!
}
@Override
public void deposit() {
System.out.println("How Much Money Do You Want To Deposit?");
double cashToDeposit = Double.parseDouble(Console.INPUT.nextLine());
if (cashToDeposit >= 0) {
bankAccount.setBalance(bankAccount.getBalance() + cashToDeposit);
} else {
System.out.println("Amount Entered Not Valid! Try Again!");
}
}
@Override
public boolean withdraw() {
return bankAccount.withdraw();
}
@Override
public void viewDetails() {
System.out.println("Your Current Bank Details:");
System.out.println(bankAccount);
}
@Override
public void deleteAccount() {
bankAccount.changeBankAccountState(NotCreatedState.getInstance(bankAccount));
System.out.println("Account Deleted! We're Sad To See You Go :(");
}
}

@ -0,0 +1,72 @@
package bank.assessment.states;
import bank.assessment.BankAccount;
import bank.assessment.BasicAccount;
import bank.assessment.Console;
import bank.assessment.Person;
public class LoggedOutState implements BankAccountStates {
BankAccount bankAccount;
private static LoggedOutState instance;
private LoggedOutState(BankAccount bankAccount) {
this.bankAccount = bankAccount;
}
public static LoggedOutState getInstance(BankAccount bankAccount) {
return instance == null ? instance = new LoggedOutState(bankAccount) : instance;
}
@Override
public void createAccount(Person person) {
System.out.println("You Have Already Created An Account!");
}
@Override
public void logIn() {
System.out.println("Please Enter Your Login Username: ");
String loginUsername = Console.INPUT.nextLine();
System.out.println("Please Enter Your Login Password: ");
String loginPassword = Console.INPUT.nextLine();
if (!bankAccount.getUsername().equalsIgnoreCase(loginUsername) || !bankAccount.getPassword().equalsIgnoreCase(loginPassword)) {
System.out.println("Uh Oh Error! Incorrect Username Or Password Entered!");
} else {
System.out.printf("Welcome Back '%s'", bankAccount.getUsername());
bankAccount.changeBankAccountState(LoggedInState.getInstance(bankAccount));
}
}
@Override
public void logOut() {
System.out.println("You Have Already Logged Out! Please Log in!");
}
@Override
public void deposit() {
}
@Override
public boolean withdraw() {
System.out.println("Please Log In Before Trying To Withdraw Funds!");
return false;
}
@Override
public void viewDetails() {
}
@Override
public void deleteAccount() {
bankAccount = new BasicAccount();
System.out.println("Account Deleted! We're Sad To See You Go :(");
// Same as?
// bankAccount = new BasicAccount();
// System.out.println("Account Deleted! We're Sad To See You Go :(");
}
}

@ -0,0 +1,86 @@
package bank.assessment.states;
import bank.assessment.*;
import java.util.Random;
public class NotCreatedState implements BankAccountStates {
BankAccount bankAccount;
private final Random random = new Random();
private static NotCreatedState instance;
private NotCreatedState(BankAccount bankAccount) {
this.bankAccount = bankAccount;
}
public static NotCreatedState getInstance(BankAccount bankAccount) {
return instance == null ? instance = new NotCreatedState(bankAccount) : instance;
}
@Override
public void createAccount(Person person) {
System.out.println("Please Enter in a Suitable Username: ");
String username = Console.INPUT.nextLine();
System.out.println("Please Enter in a Suitable Password: ");
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)");
String choice = Console.INPUT.nextLine();
if (choice.equalsIgnoreCase("y")) {
System.out.println("What Would You Like Your Overdraft Limit To Be?");
String overdraftLimit = Console.INPUT.nextLine();
bankAccount = new PremiumAccount(username, password, accountNumber, sortCode, Double.parseDouble(overdraftLimit));
} else if (choice.equalsIgnoreCase("n")) {
bankAccount = new BasicAccount(username, password, accountNumber, sortCode);
}
System.out.println("Bank Account Successfully Created!");
System.out.println("Your Details Are: " + bankAccount);
bankAccount.changeBankAccountState(LoggedOutState.getInstance(bankAccount));
person.setBankAccount(bankAccount);
}
@Override
public void logIn() {
System.out.println("Sorry, Our Systems Couldn't Find Your Details. Please Try Again or Register!");
}
@Override
public void logOut() {
System.out.println("You Must Be Logged In Before You Can Log Out!");
}
@Override
public void deposit() {
System.out.println("Sorry, Please Log In First Before Trying Deposit Cash!");
}
@Override
public boolean withdraw() {
System.out.println("Sorry, Please Log In First Before Trying Withdraw Funds!");
return false;
}
@Override
public void viewDetails() {
System.out.println("There Are No Account Details To View, Please Log In or Register!");
}
@Override
public void deleteAccount() {
System.out.println("No Account Found To Delete, Please Log In!");
}
private int generateRandomNum(Random random, int lower, int upper) {
return lower + random.nextInt(upper) + 1;
}
}
Loading…
Cancel
Save