From efd5d87536c73b2b4243fc5d8e43dfe83ea9d04a Mon Sep 17 00:00:00 2001 From: Hammy Date: Thu, 17 Dec 2020 20:21:57 +0000 Subject: [PATCH] State Pattern + Singleton Implementation --- .../state/atm/with/singleton/ATMMachine.java | 56 +++++++++++++++++++ .../state/atm/with/singleton/ATMState.java | 9 +++ .../src/state/atm/with/singleton/HasCard.java | 44 +++++++++++++++ .../src/state/atm/with/singleton/HasPin.java | 48 ++++++++++++++++ .../src/state/atm/with/singleton/Main.java | 31 ++++++++++ .../src/state/atm/with/singleton/NoCard.java | 36 ++++++++++++ .../src/state/atm/with/singleton/NoCash.java | 37 ++++++++++++ 7 files changed, 261 insertions(+) create mode 100644 Design Patterns/src/state/atm/with/singleton/ATMMachine.java create mode 100644 Design Patterns/src/state/atm/with/singleton/ATMState.java create mode 100644 Design Patterns/src/state/atm/with/singleton/HasCard.java create mode 100644 Design Patterns/src/state/atm/with/singleton/HasPin.java create mode 100644 Design Patterns/src/state/atm/with/singleton/Main.java create mode 100644 Design Patterns/src/state/atm/with/singleton/NoCard.java create mode 100644 Design Patterns/src/state/atm/with/singleton/NoCash.java diff --git a/Design Patterns/src/state/atm/with/singleton/ATMMachine.java b/Design Patterns/src/state/atm/with/singleton/ATMMachine.java new file mode 100644 index 0000000..dbfc3f1 --- /dev/null +++ b/Design Patterns/src/state/atm/with/singleton/ATMMachine.java @@ -0,0 +1,56 @@ +package state.atm.with.singleton; + +public class ATMMachine { + + private ATMState atmState; + private int cashInMachine; + private boolean correctPinEntered = false; + + public ATMMachine() { + this.atmState = NoCard.getInstance(this); + } + + public void changeATMMachineState(ATMState atmState) { + this.atmState = atmState; + } + + public void setCashInMachine(int cashInMachine){ + this.cashInMachine = cashInMachine; + } + + public void insertCard() { + atmState.insertCard(); + } + + public void ejectCard() { + atmState.ejectCard(); + } + + public void requestCash(int cashToWithdraw) { + atmState.requestCash(cashToWithdraw); + } + + public void insertPin(int pinEntered){ + atmState.insertPin(pinEntered); + } + + public ATMState getAtmState() { + return atmState; + } + + public void setAtmState(ATMState atmState) { + this.atmState = atmState; + } + + public int getCashInMachine() { + return cashInMachine; + } + + public boolean isCorrectPinEntered() { + return correctPinEntered; + } + + public void setCorrectPinEntered(boolean correctPinEntered) { + this.correctPinEntered = correctPinEntered; + } +} diff --git a/Design Patterns/src/state/atm/with/singleton/ATMState.java b/Design Patterns/src/state/atm/with/singleton/ATMState.java new file mode 100644 index 0000000..d55e79b --- /dev/null +++ b/Design Patterns/src/state/atm/with/singleton/ATMState.java @@ -0,0 +1,9 @@ +package state.atm.with.singleton; + +public interface ATMState { + + void insertCard(); + void ejectCard(); + void insertPin(int pinEntered); + void requestCash(int cashToWithdraw); +} diff --git a/Design Patterns/src/state/atm/with/singleton/HasCard.java b/Design Patterns/src/state/atm/with/singleton/HasCard.java new file mode 100644 index 0000000..ac5172c --- /dev/null +++ b/Design Patterns/src/state/atm/with/singleton/HasCard.java @@ -0,0 +1,44 @@ +package state.atm.with.singleton; + +public class HasCard implements ATMState { + + ATMMachine atmMachine; + private static HasCard instance; + + private HasCard(ATMMachine atmMachine) { + this.atmMachine = atmMachine; + } + + public static HasCard getInstance(ATMMachine atmMachine) { + return instance == null ? instance = new HasCard(atmMachine) : instance; + } + + @Override + public void insertCard() { + System.out.println("You can only insert one card at a time"); + } + + @Override + public void ejectCard() { + System.out.println("Your card is ejected"); + atmMachine.changeATMMachineState(NoCard.getInstance(atmMachine)); + } + + @Override + public void requestCash(int cashToWithdraw) { + System.out.println("You have not entered your PIN"); + } + + @Override + public void insertPin(int pinEntered) { + + if (pinEntered == 1234) { + System.out.println("Correct PIN entered"); + atmMachine.setCorrectPinEntered(true); + atmMachine.changeATMMachineState(HasPin.getInstance(atmMachine)); + } else { + System.out.println("You entered the wrong PIN"); + atmMachine.setCorrectPinEntered(false); + } + } +} diff --git a/Design Patterns/src/state/atm/with/singleton/HasPin.java b/Design Patterns/src/state/atm/with/singleton/HasPin.java new file mode 100644 index 0000000..edc408f --- /dev/null +++ b/Design Patterns/src/state/atm/with/singleton/HasPin.java @@ -0,0 +1,48 @@ +package state.atm.with.singleton; + +public class HasPin implements ATMState { + + ATMMachine atmMachine; + private static HasPin instance; + + private HasPin(ATMMachine atmMachine) { + this.atmMachine = atmMachine; + } + + public static HasPin getInstance(ATMMachine atmMachine) { + return instance == null ? instance = new HasPin(atmMachine) : instance; + } + + @Override + public void insertCard() { + System.out.println("You have already inserted a card"); + } + + @Override + public void ejectCard() { + System.out.println("Your card is ejected"); + atmMachine.changeATMMachineState(NoCard.getInstance(atmMachine)); + } + + @Override + public void insertPin(int pinEntered) { + System.out.println("You've already entered a pin"); + } + + @Override + public void requestCash(int cashToWithdraw) { + + if (cashToWithdraw > atmMachine.getCashInMachine()) { + System.out.println("You don't have enough cash to withdraw"); + } else { + System.out.println("Cash Withdrawn: " + cashToWithdraw); + atmMachine.setCashInMachine(atmMachine.getCashInMachine() - cashToWithdraw); + + if (atmMachine.getCashInMachine() <= 0) + atmMachine.changeATMMachineState(NoCash.getInstance(atmMachine)); + } + + System.out.println("Your card is ejected"); + atmMachine.changeATMMachineState(NoCash.getInstance(atmMachine)); + } +} diff --git a/Design Patterns/src/state/atm/with/singleton/Main.java b/Design Patterns/src/state/atm/with/singleton/Main.java new file mode 100644 index 0000000..388f521 --- /dev/null +++ b/Design Patterns/src/state/atm/with/singleton/Main.java @@ -0,0 +1,31 @@ +package state.atm.with.singleton; + +public class Main { + public static void main(String[] args) { + + ATMMachine atmMachine = new ATMMachine(); + atmMachine.setCashInMachine(200); + + sleep(1000); + + atmMachine.insertCard(); + atmMachine.ejectCard(); + + sleep(1000); + + atmMachine.insertCard(); + atmMachine.insertPin(1111); + + sleep(1000); + + atmMachine.insertPin(1234); + atmMachine.requestCash(100); + } + + static void sleep(int timeToSleep) { + try {Thread.sleep(timeToSleep);} + catch (InterruptedException e) { + e.printStackTrace(); + } + } +} diff --git a/Design Patterns/src/state/atm/with/singleton/NoCard.java b/Design Patterns/src/state/atm/with/singleton/NoCard.java new file mode 100644 index 0000000..cdf8d51 --- /dev/null +++ b/Design Patterns/src/state/atm/with/singleton/NoCard.java @@ -0,0 +1,36 @@ +package state.atm.with.singleton; + +public class NoCard implements ATMState { + + ATMMachine atmMachine; + private static NoCard instance; + + private NoCard(ATMMachine atmMachine) { + this.atmMachine = atmMachine; + } + + public static NoCard getInstance(ATMMachine atmMachine) { + return instance == null ? instance = new NoCard(atmMachine) : instance; + } + + @Override + public void insertCard() { + System.out.println("Card has been inserted"); + atmMachine.changeATMMachineState(HasCard.getInstance(atmMachine)); + } + + @Override + public void ejectCard() { + System.out.println("You didn't insert a card"); + } + + @Override + public void insertPin(int pinEntered) { + System.out.println("You have not entered your card"); + } + + @Override + public void requestCash(int cashToWithdraw) { + System.out.println("You have not inserted your card"); + } +} diff --git a/Design Patterns/src/state/atm/with/singleton/NoCash.java b/Design Patterns/src/state/atm/with/singleton/NoCash.java new file mode 100644 index 0000000..d0e3ebb --- /dev/null +++ b/Design Patterns/src/state/atm/with/singleton/NoCash.java @@ -0,0 +1,37 @@ +package state.atm.with.singleton; + +public class NoCash implements ATMState { + + ATMMachine atmMachine; + private static NoCash instance; + + private NoCash(ATMMachine atmMachine) { + this.atmMachine = atmMachine; + } + + public static NoCash getInstance(ATMMachine atmMachine) { + return instance == null ? instance = new NoCash(atmMachine) : instance; + } + + @Override + public void insertCard() { + System.out.println("We don't have any money"); + System.out.println("Your card is ejected"); + } + + @Override + public void ejectCard() { + System.out.println("We don't have any money"); + System.out.println("There is no card to eject"); + } + + @Override + public void insertPin(int pinEntered) { + System.out.println("We don't have any money"); + } + + @Override + public void requestCash(int cashToWithdraw) { + System.out.println("We don't have any money"); + } +}