From 50404a969b938aa9d9e4c4347463c68f45820a40 Mon Sep 17 00:00:00 2001 From: Hammy Date: Thu, 17 Dec 2020 20:22:41 +0000 Subject: [PATCH] State Pattern Lazy Implementation (Will Improve) --- Design Patterns/src/state/atm/ATMMachine.java | 37 ++++++++++++++++ Design Patterns/src/state/atm/ATMState.java | 9 ++++ Design Patterns/src/state/atm/HasCard.java | 39 +++++++++++++++++ Design Patterns/src/state/atm/HasPin.java | 43 +++++++++++++++++++ Design Patterns/src/state/atm/Main.java | 31 +++++++++++++ Design Patterns/src/state/atm/NoCard.java | 31 +++++++++++++ Design Patterns/src/state/atm/NoCash.java | 32 ++++++++++++++ 7 files changed, 222 insertions(+) create mode 100644 Design Patterns/src/state/atm/ATMMachine.java create mode 100644 Design Patterns/src/state/atm/ATMState.java create mode 100644 Design Patterns/src/state/atm/HasCard.java create mode 100644 Design Patterns/src/state/atm/HasPin.java create mode 100644 Design Patterns/src/state/atm/Main.java create mode 100644 Design Patterns/src/state/atm/NoCard.java create mode 100644 Design Patterns/src/state/atm/NoCash.java diff --git a/Design Patterns/src/state/atm/ATMMachine.java b/Design Patterns/src/state/atm/ATMMachine.java new file mode 100644 index 0000000..086bdfa --- /dev/null +++ b/Design Patterns/src/state/atm/ATMMachine.java @@ -0,0 +1,37 @@ +package state.atm; + +public class ATMMachine { + + ATMState atmState; + int cashInMachine; + boolean correctPinEntered = false; + + public ATMMachine() { + this.atmState = new NoCard(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); + } + +} diff --git a/Design Patterns/src/state/atm/ATMState.java b/Design Patterns/src/state/atm/ATMState.java new file mode 100644 index 0000000..8c67ceb --- /dev/null +++ b/Design Patterns/src/state/atm/ATMState.java @@ -0,0 +1,9 @@ +package state.atm; + +public interface ATMState { + + void insertCard(); + void ejectCard(); + void insertPin(int pinEntered); + void requestCash(int cashToWithdraw); +} diff --git a/Design Patterns/src/state/atm/HasCard.java b/Design Patterns/src/state/atm/HasCard.java new file mode 100644 index 0000000..591bcbb --- /dev/null +++ b/Design Patterns/src/state/atm/HasCard.java @@ -0,0 +1,39 @@ +package state.atm; + +public class HasCard implements ATMState { + + ATMMachine atmMachine; + + public HasCard(ATMMachine atmMachine) { + this.atmMachine = atmMachine; + } + + @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(new NoCard(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.correctPinEntered = true; + atmMachine.changeATMMachineState(new HasPin(atmMachine)); + } else { + System.out.println("You entered the wrong PIN"); + atmMachine.correctPinEntered = false; + } + } +} diff --git a/Design Patterns/src/state/atm/HasPin.java b/Design Patterns/src/state/atm/HasPin.java new file mode 100644 index 0000000..d47ca3d --- /dev/null +++ b/Design Patterns/src/state/atm/HasPin.java @@ -0,0 +1,43 @@ +package state.atm; + +public class HasPin implements ATMState { + + ATMMachine atmMachine; + + public HasPin(ATMMachine atmMachine) { + this.atmMachine = atmMachine; + } + + @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(new NoCard(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.cashInMachine) { + System.out.println("You don't have enough cash to withdraw"); + } else { + System.out.println("Cash Withdrawn: " + cashToWithdraw); + atmMachine.setCashInMachine(atmMachine.cashInMachine - cashToWithdraw); + + if (atmMachine.cashInMachine <= 0) + atmMachine.changeATMMachineState(new NoCash(atmMachine)); + } + + System.out.println("Your card is ejected"); + atmMachine.changeATMMachineState(new NoCard(atmMachine)); + } +} diff --git a/Design Patterns/src/state/atm/Main.java b/Design Patterns/src/state/atm/Main.java new file mode 100644 index 0000000..ef0e8d9 --- /dev/null +++ b/Design Patterns/src/state/atm/Main.java @@ -0,0 +1,31 @@ +package state.atm; + +public class Main { + public static void main(String[] args) { + + ATMMachine atmMachine = new ATMMachine(); + atmMachine.cashInMachine = 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/NoCard.java b/Design Patterns/src/state/atm/NoCard.java new file mode 100644 index 0000000..2f05280 --- /dev/null +++ b/Design Patterns/src/state/atm/NoCard.java @@ -0,0 +1,31 @@ +package state.atm; + +public class NoCard implements ATMState { + + ATMMachine atmMachine; + + public NoCard(ATMMachine atmMachine) { + this.atmMachine = atmMachine; + } + + @Override + public void insertCard() { + System.out.println("Card has been inserted"); + atmMachine.changeATMMachineState(new HasCard(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/NoCash.java b/Design Patterns/src/state/atm/NoCash.java new file mode 100644 index 0000000..0d5317c --- /dev/null +++ b/Design Patterns/src/state/atm/NoCash.java @@ -0,0 +1,32 @@ +package state.atm; + +public class NoCash implements ATMState { + + ATMMachine atmMachine; + + public NoCash(ATMMachine atmMachine) { + this.atmMachine = atmMachine; + } + + @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"); + } +}