State Pattern Lazy Implementation (Will Improve)

master
Hammy 4 years ago
parent f663f90424
commit 50404a969b

@ -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);
}
}

@ -0,0 +1,9 @@
package state.atm;
public interface ATMState {
void insertCard();
void ejectCard();
void insertPin(int pinEntered);
void requestCash(int cashToWithdraw);
}

@ -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;
}
}
}

@ -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));
}
}

@ -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();
}
}
}

@ -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");
}
}

@ -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");
}
}
Loading…
Cancel
Save