ATM.java
上传用户:ljt780218
上传日期:2022-07-30
资源大小:110k
文件大小:7k
源码类别:

金融证券系统

开发平台:

Java

  1. // <html><head><title>ATM Simulation Implementation - the ATM itself</title></head><body><h2>ATM Simulation Implementation - the ATM itself</h2><pre>
  2. /*
  3.  * Example ATM simulation - file ATM.java
  4.  *
  5.  * This file implements the class that manages the ATM itself
  6.  *
  7.  * Copyright (c) 1997 - Russell C. Bjork
  8.  *
  9.  */
  10. package atm;
  11. import java.awt.*;
  12. import atm.atmparts.*;
  13. import atm.util.Status;
  14. import atm.util.Money;
  15. //</pre><hr><h3>Class ATM</h3><pre>
  16. public class ATM
  17.   {
  18.     //</pre><a name="ATM::ATM"><hr><pre>
  19.     public ATM(int number, String location, Bank bank, Container container)
  20.       {
  21.         super();
  22.         
  23.         _number = number;
  24.         _location = location;
  25.         _bank = bank;
  26.         
  27.         _cardReader = new CardReader();
  28.         _display = new Display();
  29.         _keyboard = new Keyboard();
  30.         _cashDispenser = new CashDispenser();
  31.         _envelopeAcceptor = new EnvelopeAcceptor();
  32.         _receiptPrinter = new ReceiptPrinter();
  33.         _operatorPanel = new OperatorPanel();
  34.         GUILayout.doLayout(container,
  35.                            _cardReader, _display, _keyboard, _cashDispenser,
  36.                            _envelopeAcceptor, _receiptPrinter, _operatorPanel);
  37.       }
  38.     //</pre><a name="ATM::startupOperation"><hr><pre>
  39.     public synchronized Money startupOperation()
  40.       { 
  41.         // Wait for switch to be turned on.  Message will blink on and off
  42.         // to tell user what to do
  43.         while (! _operatorPanel.switchOn()) 
  44.             try
  45.               { Thread.sleep(1000); }
  46.             catch (InterruptedException e)
  47.               { }
  48.         _state = RUNNING;
  49.         return _operatorPanel.getInitialCash(); 
  50.       }
  51.     //</pre><a name="ATM::serviceCustomers"><hr><pre>
  52.     public void serviceCustomers(Money initialCash)
  53.       {
  54.         _cashDispenser.setCash(initialCash);
  55.         while (_state == RUNNING)
  56.           { 
  57.             int readerStatus = CardReader.NO_CARD;  // Initialization needed only
  58.                                                     // to keep the compiler happy!
  59.             _display.requestCard();
  60.             do
  61.               { 
  62.                 if (! _operatorPanel.switchOn())
  63.                     _state = STOPPED;
  64.                 else
  65.                     readerStatus = _cardReader.checkForCardInserted();
  66.               }
  67.             while (_state == RUNNING && readerStatus == CardReader.NO_CARD);
  68.             _display.clearDisplay();
  69.             
  70.             if (_state == RUNNING)
  71.               switch (readerStatus)
  72.                 {
  73.                   case CardReader.CARD_HAS_BEEN_READ:      
  74.                     {
  75.                       Session session = new Session(_cardReader.cardNumber(),
  76.                                                     this,
  77.                                                     _bank);
  78.                       session.doSessionUseCase();
  79.                       break;
  80.                     }
  81.                   case CardReader.UNREADABLE_CARD:
  82.                     _display.reportCardUnreadable();
  83.                     _cardReader.ejectCard();
  84.                     _display.clearDisplay();
  85.                 }
  86.           }
  87.       }
  88.     //</pre><a name="ATM::getPIN"><hr><pre>
  89.         
  90.     public int getPIN()
  91.       { _display.requestPIN();
  92.         int PIN = _keyboard.readPIN(_display);
  93.         _display.clearDisplay();
  94.         return PIN;
  95.       }
  96.     
  97.     
  98.     //</pre><a name="ATM::getMenuChoice"><hr><pre>
  99.     
  100.     public int getMenuChoice(String whatToChoose,
  101.                       int numItems,
  102.                       String items[])
  103.       { _display.displayMenu(whatToChoose, numItems, items);
  104.         int choice =  _keyboard.readMenuChoice(numItems);
  105.         _display.clearDisplay();
  106.         return choice;
  107.       }
  108.     
  109.     
  110.     //</pre><a name="ATM::getAmountEntry"><hr><pre>
  111.     
  112.     public Money getAmountEntry()
  113.       { _display.requestAmountEntry();
  114.         Money amount = _keyboard.readAmountEntry(_display);
  115.         _display.clearDisplay();
  116.         return amount;
  117.       }
  118.     
  119.     
  120.     //</pre><a name="ATM::checkIfCashAvailable"><hr><pre>
  121.     
  122.     public boolean checkIfCashAvailable(Money amount)
  123.       { return ! _cashDispenser.currentCash().less(amount);
  124.       }
  125.     
  126.     
  127.     //</pre><a name="ATM::dispenseCash"><hr><pre>
  128.     
  129.     public void dispenseCash(Money amount)
  130.       { _cashDispenser.dispenseCash(amount);
  131.       }
  132.     
  133.     
  134.     //</pre><a name="ATM::acceptEnvelope"><hr><pre>
  135.     
  136.     public boolean acceptEnvelope()
  137.       { return _envelopeAcceptor.acceptEnvelope(); 
  138.       }
  139.     
  140.     
  141.     //</pre><a name="ATM::issueReceipt"><hr><pre>
  142.     
  143.     public void issueReceipt(int cardNumber,
  144.                              int serialNumber,
  145.                              String description,
  146.                              Money amount,
  147.                              Money balance,
  148.                              Money availableBalance)
  149.       { 
  150.         _receiptPrinter.printReceipt(_number, _location, cardNumber, serialNumber,
  151.                                      description, amount,
  152.                                      balance, availableBalance);;
  153.       }
  154.     
  155.     
  156.     //</pre><a name="ATM::reEnterPIN"><hr><pre>
  157.     
  158.     public int reEnterPIN()
  159.       { _display.requestReEnterPIN();
  160.         int PIN = _keyboard.readPIN(_display);
  161.         _display.clearDisplay();
  162.         return PIN;
  163.       }
  164.     
  165.     
  166.     //</pre><a name="ATM::reportTransactionFailure"><hr><pre>
  167.     
  168.     public boolean reportTransactionFailure(String explanation)
  169.       { _display.reportTransactionFailure(explanation);
  170.         int response = _keyboard.readMenuChoice(2);
  171.         _display.clearDisplay();
  172.         return response == 1;
  173.       }
  174.     
  175.     
  176.     //</pre><a name="ATM::ejectCard"><hr><pre>
  177.     
  178.     public void ejectCard()
  179.       { _cardReader.ejectCard();
  180.       }
  181.     
  182.     
  183.     //</pre><a name="ATM::retainCard"><hr><pre>
  184.     
  185.     public void retainCard()
  186.       { _display.reportCardRetained();
  187.         _cardReader.retainCard(); 
  188.         _display.clearDisplay();
  189.       }
  190.       
  191.     
  192.     //</pre><a name="ATM::number"><hr><pre>
  193.     
  194.     public int number()
  195.       { return _number; }
  196.     
  197.     //</pre><hr><pre>
  198.     
  199.     // Private instance variables
  200.     private int              _number;
  201.     private String           _location;
  202.     private Bank             _bank;
  203.     
  204.     // Values for _state instance variable
  205.     private static final int RUNNING = 0;
  206.     private static final int STOPPED = 1;
  207.     
  208.     private int              _state;
  209.     private CardReader       _cardReader;
  210.     private Display          _display;
  211.     private Keyboard         _keyboard;
  212.     private CashDispenser    _cashDispenser;
  213.     private EnvelopeAcceptor _envelopeAcceptor;
  214.     private ReceiptPrinter   _receiptPrinter;
  215.     private OperatorPanel    _operatorPanel;
  216.   }
  217.         
  218. //</pre></body></html>