diff --git a/Personal/src/bank/assessment/BankAccount.java b/Personal/src/bank/assessment/BankAccount.java index ff5fe4c..f0bb979 100644 --- a/Personal/src/bank/assessment/BankAccount.java +++ b/Personal/src/bank/assessment/BankAccount.java @@ -1,33 +1,39 @@ package bank.assessment; import bank.assessment.states.BankAccountStates; +import bank.assessment.states.LoggedOutState; 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 overdraftLimit; + private boolean isPremium; 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.password = password; - this.accountNumber = accountNumber; 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.password = password; - this.accountNumber = accountNumber; this.sortCode = sortCode; + this.accountNumber = accountNumber; + this.isPremium = isPremium; this.overdraftLimit = overdraftLimit; - this.bankAccountState = NotCreatedState.getInstance(this); + this.bankAccountState = LoggedOutState.getInstance(this); } // Method Actions To Change State @@ -37,19 +43,15 @@ public abstract class BankAccount { public void logIn() { this.bankAccountState.logIn(); } - public void logOut() { - this.bankAccountState.logOut(); - } + public void logOut() { this.bankAccountState.logOut(); } public void deposit() { this.bankAccountState.deposit(); } - public boolean withdraw() { return this.bankAccountState.withdraw(); } + public void withdraw() { this.bankAccountState.withdraw(); } - public void viewDetails() { - this.bankAccountState.viewDetails(); - } + public void viewDetails() { this.bankAccountState.viewDetails(); } - public void deleteAccount() { this.bankAccountState.deleteAccount(); } + public void deleteAccount(Person person) { this.bankAccountState.deleteAccount(person); } // Getters & Setters public String getUsername() { @@ -72,22 +74,16 @@ public abstract class BankAccount { return accountNumber; } - public void setAccountNumber(String accountNumber) { - this.accountNumber = accountNumber; - } - - public String getSortCode() { - return sortCode; - } - - public void setSortCode(String sortCode) { - this.sortCode = sortCode; - } + public String getSortCode() { return sortCode; } public double getBalance() { return balance; } + public boolean isPremium() { return isPremium; } + + public void setPremium(boolean premium) { isPremium = premium; } + public void setBalance(double balance) { this.balance = balance; } @@ -100,8 +96,8 @@ public abstract class BankAccount { this.overdraftLimit = overdraftLimit; } - void displayBalance() { - System.out.println("Current Account Balance: " + balance); + public void displayBalance() { + System.out.println("Current Account Balance: " + String.format("£%.2f", balance)); } @Override diff --git a/Personal/src/bank/assessment/Main.java b/Personal/src/bank/assessment/Main.java index 3f403bf..ccd5492 100644 --- a/Personal/src/bank/assessment/Main.java +++ b/Personal/src/bank/assessment/Main.java @@ -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" :) Person person = new Person(); - person.setBankAccount(new BasicAccount()); + person.setBankAccount(new BankAccount()); getPersonDetails(person); int choice; @@ -36,7 +36,7 @@ public class Main { case 4 -> person.getBankAccount().deposit(); case 5 -> person.getBankAccount().withdraw(); case 6 -> person.getBankAccount().logOut(); - case 7 -> person.getBankAccount().deleteAccount(); + case 7 -> person.getBankAccount().deleteAccount(person); } } while (choice != 8); diff --git a/Personal/src/bank/assessment/states/BankAccountStates.java b/Personal/src/bank/assessment/states/BankAccountStates.java index c57bbc2..85cae9a 100644 --- a/Personal/src/bank/assessment/states/BankAccountStates.java +++ b/Personal/src/bank/assessment/states/BankAccountStates.java @@ -8,8 +8,8 @@ public interface BankAccountStates { void logIn(); void logOut(); void deposit(); - boolean withdraw(); + void withdraw(); void viewDetails(); - void deleteAccount(); + void deleteAccount(Person person); } diff --git a/Personal/src/bank/assessment/states/LoggedInState.java b/Personal/src/bank/assessment/states/LoggedInState.java index 1d39df6..dab7cc8 100644 --- a/Personal/src/bank/assessment/states/LoggedInState.java +++ b/Personal/src/bank/assessment/states/LoggedInState.java @@ -6,21 +6,17 @@ import bank.assessment.Person; public class LoggedInState implements BankAccountStates { - BankAccount bankAccount; + private static BankAccount bankAccount; private static LoggedInState instance; - private LoggedInState(BankAccount bankAccount) { - this.bankAccount = bankAccount; - } + public LoggedInState(BankAccount bankAccount) { LoggedInState.bankAccount = bankAccount; } - public static LoggedInState getInstance(BankAccount bankAccount) { - return instance == null ? instance = new LoggedInState(bankAccount) : instance; + public static LoggedInState getInstance(BankAccount ba) { + return instance == null || bankAccount != ba ? instance = new LoggedInState(ba) : instance; } @Override - public void createAccount(Person person) { - System.out.println("This Account Is Already Registered With Us. You Are Already Logged In!"); - } + public void createAccount(Person person) { System.out.println("This Account Is Already Registered With Us. You Are Already Logged In!"); } @Override public void logIn() { @@ -40,25 +36,33 @@ public class LoggedInState implements BankAccountStates { if (cashToDeposit >= 0) { bankAccount.setBalance(bankAccount.getBalance() + cashToDeposit); + System.out.printf("Deposit Of %s Successful!\n", cashToDeposit); + bankAccount.displayBalance(); } else { System.out.println("Amount Entered Not Valid! Try Again!"); } } @Override - public boolean withdraw() { - return bankAccount.withdraw(); + public void 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 - public void viewDetails() { - System.out.println("Your Current Bank Details:"); - System.out.println(bankAccount); - } + public void viewDetails() { System.out.println("Your Current Bank Details: \n" + bankAccount); } @Override - public void deleteAccount() { - bankAccount.changeBankAccountState(NotCreatedState.getInstance(bankAccount)); + public void deleteAccount(Person person) { + person.setBankAccount(new BankAccount()); System.out.println("Account Deleted! We're Sad To See You Go :("); } } diff --git a/Personal/src/bank/assessment/states/LoggedOutState.java b/Personal/src/bank/assessment/states/LoggedOutState.java index f359677..8c5ad45 100644 --- a/Personal/src/bank/assessment/states/LoggedOutState.java +++ b/Personal/src/bank/assessment/states/LoggedOutState.java @@ -6,15 +6,15 @@ import bank.assessment.Person; public class LoggedOutState implements BankAccountStates { - BankAccount bankAccount; + private static BankAccount bankAccount; private static LoggedOutState instance; private LoggedOutState(BankAccount bankAccount) { - this.bankAccount = bankAccount; + LoggedOutState.bankAccount = bankAccount; } - public static LoggedOutState getInstance(BankAccount bankAccount) { - return instance == null ? instance = new LoggedOutState(bankAccount) : instance; + public static LoggedOutState getInstance(BankAccount ba) { + return instance == null || bankAccount != ba ? instance = new LoggedOutState(ba) : instance; } @Override @@ -33,7 +33,8 @@ public class LoggedOutState implements BankAccountStates { 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()); + System.out.printf("Welcome Back '%s'\n", bankAccount.getUsername()); + System.out.println("Your Current Bank Details: \n" + bankAccount); bankAccount.changeBankAccountState(LoggedInState.getInstance(bankAccount)); } } @@ -49,9 +50,8 @@ public class LoggedOutState implements BankAccountStates { } @Override - public boolean withdraw() { + public void withdraw() { System.out.println("Please Log In Before Trying To Withdraw Funds!"); - return false; } @Override @@ -60,8 +60,8 @@ public class LoggedOutState implements BankAccountStates { } @Override - public void deleteAccount() { - bankAccount.changeBankAccountState(NotCreatedState.getInstance(bankAccount)); + public void deleteAccount(Person person) { + person.setBankAccount(new BankAccount()); System.out.println("Account Deleted! We're Sad To See You Go :("); } } diff --git a/Personal/src/bank/assessment/states/NotCreatedState.java b/Personal/src/bank/assessment/states/NotCreatedState.java index 329b91d..a22cf32 100644 --- a/Personal/src/bank/assessment/states/NotCreatedState.java +++ b/Personal/src/bank/assessment/states/NotCreatedState.java @@ -1,21 +1,21 @@ package bank.assessment.states; -import bank.assessment.*; +import bank.assessment.BankAccount; +import bank.assessment.Console; +import bank.assessment.Person; import java.util.Random; public class NotCreatedState implements BankAccountStates { - BankAccount bankAccount; - private final Random random = new Random(); + private static BankAccount bankAccount; private static NotCreatedState instance; + private final Random random = new Random(); - private NotCreatedState(BankAccount bankAccount) { - this.bankAccount = bankAccount; - } + private NotCreatedState(BankAccount bankAccount) { NotCreatedState.bankAccount = bankAccount; } - public static NotCreatedState getInstance(BankAccount bankAccount) { - return instance == null ? instance = new NotCreatedState(bankAccount) : instance; + public static NotCreatedState getInstance(BankAccount ba) { + return instance == null || bankAccount != ba ? instance = new NotCreatedState(ba) : instance; } @Override @@ -26,26 +26,24 @@ public class NotCreatedState implements BankAccountStates { 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(); + 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")) { 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)); + double overdraftLimit = Double.parseDouble(Console.INPUT.nextLine()); + bankAccount = new BankAccount(username, password, sortCode, accountNumber, true, overdraftLimit); } 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("Your Details Are: " + bankAccount); - bankAccount.changeBankAccountState(LoggedOutState.getInstance(bankAccount)); person.setBankAccount(bankAccount); } @@ -63,18 +61,13 @@ public class NotCreatedState implements BankAccountStates { } @Override - public boolean withdraw() { - System.out.println("Sorry, Please Log In First Before Trying Withdraw Funds!"); - return false; - } + public void withdraw() { System.out.println("Sorry, Please Log In First Before Trying Withdraw Funds!"); } @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!"); - } + public void deleteAccount(Person person) { System.out.println("No Account Found To Delete"); } private int generateRandomNum(Random random, int lower, int upper) { return lower + random.nextInt(upper) + 1;