State Pattern + Singleton Implementation

master
Hammy 4 years ago
parent 45e225c80b
commit efd5d87536

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

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

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

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

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

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

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