Advertisement
ivandrofly

State pattern - Derek Banas

Feb 9th, 2021
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.25 KB | None | 0 0
  1. public class TestATMMachine {
  2.    
  3.     public static void main(String[] args){
  4.        
  5.         ATMMachine atmMachine = new ATMMachine();
  6.        
  7.         atmMachine.insertCard();
  8.        
  9.         atmMachine.ejectCard();
  10.        
  11.         atmMachine.insertCard();
  12.        
  13.         atmMachine.insertPin(1234);
  14.        
  15.         atmMachine.requestCash(2000);
  16.        
  17.         atmMachine.insertCard();
  18.        
  19.         atmMachine.insertPin(1234);
  20.        
  21.     }  
  22. }
  23.  
  24. public class NoCash implements ATMState {
  25.    
  26.     ATMMachine atmMachine;
  27.    
  28.     public NoCash(ATMMachine newATMMachine){
  29.        
  30.         atmMachine = newATMMachine;
  31.        
  32.     }
  33.  
  34.     public void insertCard() {
  35.        
  36.         System.out.println("We don't have any money");
  37.         System.out.println("Your card is ejected");
  38.        
  39.     }
  40.  
  41.     public void ejectCard() {
  42.        
  43.         System.out.println("We don't have any money");
  44.         System.out.println("There is no card to eject");
  45.        
  46.     }
  47.  
  48.     public void requestCash(int cashToWithdraw) {
  49.        
  50.         System.out.println("We don't have any money");
  51.        
  52.     }
  53.  
  54.     public void insertPin(int pinEntered) {
  55.        
  56.         System.out.println("We don't have any money");
  57.        
  58.     }  
  59. }
  60.  
  61. public class HasPin implements ATMState {
  62.    
  63.     ATMMachine atmMachine;
  64.    
  65.     public HasPin(ATMMachine newATMMachine){
  66.        
  67.         atmMachine = newATMMachine;
  68.        
  69.     }
  70.  
  71.     public void insertCard() {
  72.        
  73.         System.out.println("You already entered a card");
  74.        
  75.     }
  76.  
  77.     public void ejectCard() {
  78.        
  79.         System.out.println("Your card is ejected");
  80.         atmMachine.setATMState(atmMachine.getNoCardState());
  81.        
  82.     }
  83.  
  84.     public void requestCash(int cashToWithdraw) {
  85.        
  86.         if(cashToWithdraw > atmMachine.cashInMachine){
  87.            
  88.             System.out.println("You don't have that much cash available");
  89.             System.out.println("Your card is ejected");
  90.             atmMachine.setATMState(atmMachine.getNoCardState());
  91.            
  92.         } else {
  93.            
  94.             System.out.println(cashToWithdraw + " is provided by the machine");
  95.             atmMachine.setCashInMachine(atmMachine.cashInMachine - cashToWithdraw);
  96.            
  97.             System.out.println("Your card is ejected");
  98.             atmMachine.setATMState(atmMachine.getNoCardState());
  99.            
  100.             if(atmMachine.cashInMachine <= 0){
  101.                
  102.                 atmMachine.setATMState(atmMachine.getNoCashState());
  103.                
  104.             }
  105.         }
  106.     }
  107.  
  108.     public void insertPin(int pinEntered) {
  109.        
  110.         System.out.println("You already entered a PIN");
  111.        
  112.     }  
  113. }
  114.  
  115. public class NoCard implements ATMState {
  116.    
  117.     ATMMachine atmMachine;
  118.    
  119.     public NoCard(ATMMachine newATMMachine){
  120.        
  121.         atmMachine = newATMMachine;
  122.        
  123.     }
  124.  
  125.     public void insertCard() {
  126.        
  127.         System.out.println("Please enter your pin");
  128.         atmMachine.setATMState(atmMachine.getYesCardState());
  129.        
  130.     }
  131.  
  132.     public void ejectCard() {
  133.        
  134.         System.out.println("You didn't enter a card");
  135.        
  136.     }
  137.  
  138.     public void requestCash(int cashToWithdraw) {
  139.        
  140.         System.out.println("You have not entered your card");
  141.        
  142.     }
  143.  
  144.     public void insertPin(int pinEntered) {
  145.        
  146.         System.out.println("You have not entered your card");
  147.        
  148.     }
  149. }
  150.  
  151. public class HasCard implements ATMState {
  152.    
  153.     ATMMachine atmMachine;
  154.    
  155.     public HasCard(ATMMachine newATMMachine){
  156.        
  157.         atmMachine = newATMMachine;
  158.        
  159.     }
  160.  
  161.     public void insertCard() {
  162.        
  163.         System.out.println("You can only insert one card at a time");
  164.        
  165.     }
  166.  
  167.     public void ejectCard() {
  168.        
  169.         System.out.println("Your card is ejected");
  170.         atmMachine.setATMState(atmMachine.getNoCardState());
  171.        
  172.     }
  173.  
  174.     public void requestCash(int cashToWithdraw) {
  175.        
  176.         System.out.println("You have not entered your PIN");
  177.        
  178.        
  179.     }
  180.  
  181.     public void insertPin(int pinEntered) {
  182.        
  183.         if(pinEntered == 1234){
  184.            
  185.             System.out.println("You entered the correct PIN");
  186.             atmMachine.correctPinEntered = true;
  187.             atmMachine.setATMState(atmMachine.getHasPin());
  188.            
  189.         } else {
  190.            
  191.             System.out.println("You entered the wrong PIN");
  192.             atmMachine.correctPinEntered = false;
  193.             System.out.println("Your card is ejected");
  194.             atmMachine.setATMState(atmMachine.getNoCardState());
  195.            
  196.         }  
  197.     }  
  198. }
  199.  
  200. public class ATMMachine {
  201.    
  202.     ATMState hasCard;    
  203.     ATMState noCard;
  204.     ATMState hasCorrectPin;
  205.     ATMState atmOutOfMoney;
  206.    
  207.     ATMState atmState;
  208.    
  209.     int cashInMachine = 2000;
  210.     boolean correctPinEntered = false;
  211.    
  212.     public ATMMachine(){
  213.        
  214.         hasCard = new HasCard(this);
  215.         noCard = new NoCard(this);
  216.         hasCorrectPin = new HasPin(this);
  217.         atmOutOfMoney = new NoCash(this);
  218.        
  219.         atmState = noCard;
  220.        
  221.         if(cashInMachine < 0){
  222.            
  223.             atmState = atmOutOfMoney;
  224.            
  225.         }
  226.        
  227.     }
  228.    
  229.     void setATMState(ATMState newATMState){
  230.        
  231.         atmState = newATMState;
  232.        
  233.     }
  234.    
  235.     public void setCashInMachine(int newCashInMachine){
  236.        
  237.         cashInMachine = newCashInMachine;
  238.        
  239.     }
  240.    
  241.     public void insertCard() {
  242.        
  243.         atmState.insertCard();
  244.        
  245.     }
  246.  
  247.     public void ejectCard() {
  248.        
  249.         atmState.ejectCard();
  250.        
  251.     }
  252.  
  253.     public void requestCash(int cashToWithdraw) {
  254.        
  255.         atmState.requestCash(cashToWithdraw);
  256.        
  257.     }
  258.  
  259.     public void insertPin(int pinEntered){
  260.        
  261.         atmState.insertPin(pinEntered);
  262.        
  263.     }
  264.    
  265.     public ATMState getYesCardState() { return hasCard; }
  266.     public ATMState getNoCardState() { return noCard; }
  267.     public ATMState getHasPin() { return hasCorrectPin; }
  268.     public ATMState getNoCashState() { return atmOutOfMoney; }
  269.    
  270. }
  271.  
  272. public interface ATMState {
  273.    
  274.     // Different states expected
  275.     // HasCard, NoCard, HasPin, NoCash
  276.    
  277.     void insertCard();
  278.    
  279.     void ejectCard();
  280.    
  281.     void insertPin(int pinEntered);
  282.    
  283.     void requestCash(int cashToWithdraw);
  284.    
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement