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

金融证券系统

开发平台:

Java

  1. //<html><head><title>ATM Simulation Implementation - Individual Transactions</title></head><body><h2>ATM Simulation Implementation - Individual Transactions</h2><pre>
  2. /*
  3.  * Example ATM simulation - file Transaction.java
  4.  *
  5.  * This file contains the classes that represent the various kinds of
  6.  * transactions that the ATM can perform.  The class Transaction serves as the
  7.  * base class, and is the only publically-accessible classes.  The remaining
  8.  * classes implement specific types of transaction.  Objects of these classes are
  9.  * created by the chooseTransaction method of Transaction.
  10.  *
  11.  * Copyright (c) 1997 - Russell C. Bjork
  12.  *
  13.  */
  14. package atm.transaction;
  15. import atm.ATM;
  16. import atm.Bank;
  17. import atm.Session;
  18. import atm.util.Money;
  19. import atm.util.Status;
  20. //</pre><hr><h3>Class Transaction</h3><pre>
  21. public abstract class Transaction
  22.   {
  23.     //</pre><a name="Transaction::Transaction"><hr><pre>
  24.     
  25.     public Transaction(Session session, ATM atm, Bank bank)
  26.       { _session = session;
  27.         _atm = atm;
  28.         _bank = bank;
  29.         _serialNumber = ++ _lastSerialNumberAssigned;
  30.         _newBalance = new Money();
  31.         _availableBalance = new Money();
  32.       }
  33.     
  34.     //</pre><a name="Transaction::chooseTransaction"><hr><pre>
  35.     
  36.     public static Transaction chooseTransaction(Session session, ATM atm, Bank bank)
  37.       { String transactionMenu[] =
  38.           { "Cash Withdrawl",
  39.             "Deposit",
  40.             "Transfer Funds Between Accounts",
  41.             "Balance Inquiry"
  42.           };
  43.          switch (atm.getMenuChoice("Please choose a transaction type:",
  44.                                    4, 
  45.                                    transactionMenu))
  46.           {
  47.             case 1: return new WithdrawlTransaction(session, atm, bank);
  48.             case 2: return new DepositTransaction(session, atm, bank);
  49.             case 3: return new TransferTransaction(session, atm, bank);
  50.             case 4: return new InquiryTransaction(session, atm, bank);
  51.             default: return null; // To keep the compiler happy
  52.           }
  53.       }
  54.     
  55.     //</pre><a name="Transaction::doTransactionUseCase"><hr><pre>
  56.     
  57.     public int doTransactionUseCase()
  58.       {
  59.         int code;
  60.         code = getTransactionSpecificsFromCustomer();
  61.         if (code != Status.SUCCESS)
  62.             return code;
  63.         code = sendToBank();
  64.         if (code == Status.INVALID_PIN)
  65.           {
  66.             code = _session.doInvalidPINExtension();
  67.             if (code == Status.INVALID_PIN)
  68.                 return code;
  69.           }
  70.         if (code == Status.SUCCESS)
  71.             code = finishApprovedTransaction();
  72.         return code;
  73.       }
  74.     
  75.     //</pre><a name="Transaction::getTransactionSpecificsFromCustomer"><hr><pre>
  76.     
  77.     public abstract int getTransactionSpecificsFromCustomer();
  78.     
  79.     /* </pre>View Implementation for:  
  80.     <a href="#WithdrawlTransaction::getTransactionSpecificsFromCustomer"> WithdrawlTransaction</a>
  81.     <a href="#DepositTransaction::getTransactionSpecificsFromCustomer"> DepositTransaction</a>
  82.     <a href="#TransferTransaction::getTransactionSpecificsFromCustomer"> TransferTranacstion</a>
  83.     <a href="#InquiryTransaction::getTransactionSpecificsFromCustomer"> InquiryTransaction</a><pre>
  84.     */
  85.     
  86.     //</pre><a name="Transaction::sendToBank"><hr><pre>
  87.     
  88.     public abstract int sendToBank();
  89.     
  90.     /* </pre>View Implementation for:  
  91.     <a href="#WithdrawlTransaction::sendToBank"> WithdrawlTransaction</a>
  92.     <a href="#DepositTransaction::sendToBank"> DepositTransaction</a>
  93.     <a href="#TransferTransaction::sendToBank"> TransferTranacstion</a>
  94.     <a href="#InquiryTransaction::sendToBank"> InquiryTransaction</a><pre>
  95.     */
  96.     
  97.     //</pre><a name="Transaction::finishApprovedTransaction"><hr><pre>
  98.     
  99.     public abstract int finishApprovedTransaction();
  100.     /* </pre>View Implementation for:  
  101.     <a href="#WithdrawlTransaction::finishApprovedTransaction"> WithdrawlTransaction</a>
  102.     <a href="#DepositTransaction::finishApprovedTransaction"> DepositTransaction</a>
  103.     <a href="#TransferTransaction::finishApprovedTransaction"> TransferTranacstion</a>
  104.     <a href="#InquiryTransaction::finishApprovedTransaction"> InquiryTransaction</a><pre>
  105.     */
  106.     
  107.     //</pre><hr><pre>
  108.     
  109.     // Instance variables common to all kinds of transaction
  110.     
  111.     protected Session _session;
  112.     protected ATM     _atm;
  113.     protected Bank    _bank;
  114.     // Every transaction is assigned a unique serial number that is used
  115.     // to identify it in communications with the bank and on the receipt.
  116.     protected int     _serialNumber;
  117.     // Every transaction gets both the updated balance and available
  118.     // balance for printing on the receipt.
  119.     protected Money   _newBalance,
  120.                       _availableBalance;
  121.     // This class member is used to assign serial numbers sequentially
  122.     
  123.     private static int _lastSerialNumberAssigned = 0;
  124.   }
  125. //</pre><hr><h3>Class WithdrawlTransaction</h3><pre>
  126. class WithdrawlTransaction extends Transaction
  127.   {    
  128.     //</pre><a name="WithdrawlTransaction::WithdrawlTransaction"><hr><pre>
  129.     
  130.     public WithdrawlTransaction(Session session, ATM atm, Bank bank)
  131.       { super(session, atm, bank);
  132.       } 
  133.     
  134.     //</pre><a name="WithdrawlTransaction::getTransactionSpecificsFromCustomer"><hr><pre>
  135.     
  136.     public int getTransactionSpecificsFromCustomer()
  137.       {
  138.         _fromAccount = _bank.chooseAccountType("withdraw from", _atm);
  139.         String menu[] =
  140.           { "$ 20", "$ 40", "$ 60", "$ 80", "$ 100", "$ 200", "$ 300" };
  141.         switch (_atm.getMenuChoice("Please choose an amount:", 7, menu))
  142.           {
  143.             case 1: _amount = new Money(20); break;
  144.             case 2: _amount = new Money(40); break;
  145.             case 3: _amount = new Money(60); break;
  146.             case 4: _amount = new Money(80); break;
  147.             case 5: _amount = new Money(100); break;
  148.             case 6: _amount = new Money(200); break;
  149.             case 7: _amount = new Money(300); break;
  150.           }
  151.         if (_atm.checkIfCashAvailable(_amount))
  152.            return Status.SUCCESS;
  153.         else
  154.            return Status.TOO_LITTLE_CASH;
  155.       }
  156.     
  157.     
  158.     //</pre><a name="WithdrawlTransaction::sendToBank"><hr><pre>
  159.     
  160.     public int sendToBank()
  161.       {
  162.         return _bank.initiateWithdrawl(_session.cardNumber(),
  163.                                        _session.PIN(),
  164.                                        _atm.number(),
  165.                                        _serialNumber,
  166.                                        _fromAccount,
  167.                                        _amount,
  168.                                        _newBalance,
  169.                                        _availableBalance);
  170.       }
  171.     
  172.     //</pre><a name="WithdrawlTransaction::finishApprovedTransaction"><hr><pre>
  173.     public int finishApprovedTransaction()
  174.       {
  175.         _atm.dispenseCash(_amount);
  176.         _bank.finishWithdrawl(_atm.number(), _serialNumber, true);
  177.         _atm.issueReceipt(_session.cardNumber(),
  178.                           _serialNumber, 
  179.                           "WITHDRAWL FROM " + _bank.accountName(_fromAccount),
  180.                           _amount,
  181.                           _newBalance,
  182.                           _availableBalance);
  183.          return Status.SUCCESS;
  184.       }
  185.     // </pre><hr><pre>
  186.     
  187.     // Instance variables of WithdrawlTransaction
  188.     private int   _fromAccount;
  189.     private Money _amount;
  190.   }    
  191. //</pre><hr><h3>Class DepositTransaction</h3><pre>
  192. class DepositTransaction extends Transaction
  193.   {
  194.     //</pre><a name="DepositTransaction::DepositTransaction"><hr><pre>
  195.     
  196.     public DepositTransaction(Session session, ATM atm, Bank bank)
  197.       { super(session, atm, bank);
  198.       }
  199.     
  200.     //</pre><a name="DepositTransaction::getTransactionSpecificsFromCustomer"><hr><pre>
  201.     
  202.     public int getTransactionSpecificsFromCustomer()
  203.       {
  204.         _toAccount = _bank.chooseAccountType("deposit to", _atm);
  205.         _amount = _atm.getAmountEntry();
  206.         return Status.SUCCESS;
  207.       }
  208.     
  209.     //</pre><a name="DepositTransaction::sendToBank"><hr><pre>
  210.     
  211.     public int sendToBank()
  212.       {
  213.         try
  214.           { Thread.sleep(3 * 1000); }
  215.         catch (InterruptedException e)
  216.           { }
  217.         return _bank.initiateDeposit(_session.cardNumber(),
  218.                                      _session.PIN(),
  219.                                      _atm.number(),
  220.                                      _serialNumber,
  221.                                      _toAccount,
  222.                                      _amount,
  223.                                      _newBalance,
  224.                                      _availableBalance);
  225.       }
  226.     
  227.     //</pre><a name="DepositTransaction::finishApprovedTransaction"><hr><pre>
  228.     
  229.     public int finishApprovedTransaction()
  230.       {
  231.         boolean envelopeAccepted = _atm.acceptEnvelope();
  232.         _bank.finishDeposit(_atm.number(), _serialNumber, envelopeAccepted);
  233.         if (envelopeAccepted)
  234.           {
  235.             _atm.issueReceipt(_session.cardNumber(),
  236.                               _serialNumber,
  237.                               "DEPOSIT TO " + _bank.accountName(_toAccount),                        
  238.                               _amount,
  239.                               _newBalance,
  240.                               _availableBalance);
  241.             return Status.SUCCESS;             
  242.           }
  243.         else
  244.             return Status.ENVELOPE_DEPOSIT_TIMED_OUT;
  245.       }
  246.       
  247.     //</pre><hr><pre>
  248.     
  249.     // Instance variables of DepositTransaction
  250.     private int   _toAccount;
  251.     private Money _amount;
  252.   }
  253.   
  254. //</pre><hr><h3>Class TransferTransaction</h3><pre>
  255. class TransferTransaction extends Transaction
  256.   {    
  257.     
  258.     //</pre><a name="TransferTransaction::TransferTransaction"><hr><pre>
  259.     
  260.     public TransferTransaction(Session session, ATM atm, Bank bank)
  261.       { super(session, atm, bank);
  262.       }
  263.     
  264.     //</pre><a name="TransferTransaction::getTransactionSpecificsFromCustomer"><hr><pre>
  265.     
  266.     public int getTransactionSpecificsFromCustomer()
  267.       {
  268.         _fromAccount = _bank.chooseAccountType("transfer from", _atm);
  269.         _toAccount = _bank.chooseAccountType("transfer to", _atm);
  270.         _amount = _atm.getAmountEntry();
  271.         return Status.SUCCESS;
  272.       }
  273.     
  274.     //</pre><a name="TransferTransaction::sendToBank"><hr><pre>
  275.     
  276.     public int sendToBank()
  277.       {
  278.         return _bank.doTransfer(_session.cardNumber(),
  279.                                 _session.PIN(),
  280.                                 _atm.number(),
  281.                                 _serialNumber,
  282.                                 _fromAccount,
  283.                                 _toAccount,
  284.                                 _amount,
  285.                                 _newBalance,
  286.                                 _availableBalance);
  287.       }
  288.     
  289.     //</pre><a name="TransferTransaction::finishApprovedTransaction"><hr><pre>
  290.         
  291.     public int finishApprovedTransaction()
  292.       {
  293.         _atm.issueReceipt(_session.cardNumber(),
  294.                           _serialNumber,
  295.                           "TRANSFER " + _bank.accountName(_fromAccount) +
  296.                              " TO " + _bank.accountName(_toAccount),
  297.                           _amount,
  298.                           _newBalance,
  299.                           _availableBalance);
  300.          return Status.SUCCESS;
  301.       }
  302.     //</pre><hr><pre>
  303.     
  304.     // Instance variables of TransferTransaction
  305.     private int   _fromAccount;
  306.     private int   _toAccount;
  307.     private Money _amount;
  308.   }
  309.   
  310. //</pre><hr><h3>Class InquiryTransaction</h3><pre>
  311. class InquiryTransaction extends Transaction
  312.   {    
  313.     //</pre><a name="InquiryTransaction::InquiryTransaction"><hr><pre>
  314.     
  315.     public InquiryTransaction(Session session, ATM atm, Bank bank)
  316.       { super(session, atm, bank); }
  317.     //</pre><a name="InquiryTransaction::getTransactionSpecificsFromCustomer"><hr><pre>
  318.     
  319.     public int getTransactionSpecificsFromCustomer()
  320.       {
  321.         _fromAccount = _bank.chooseAccountType("balance for", _atm);
  322.         return Status.SUCCESS;
  323.       }
  324.     
  325.     //</pre><a name="InquiryTransaction::sendToBank"><hr><pre>
  326.     
  327.     public int sendToBank()
  328.       {
  329.         return _bank.doInquiry(_session.cardNumber(),
  330.                                _session.PIN(),
  331.                                _atm.number(),
  332.                                _serialNumber,
  333.                                _fromAccount,
  334.                                _newBalance,
  335.                                _availableBalance);
  336.       }
  337.     
  338.     //</pre><a name="InquiryTransaction::finishApprovedTransaction"><hr><pre>
  339.     
  340.     public int finishApprovedTransaction()
  341.       {
  342.         _atm.issueReceipt(_session.cardNumber(),
  343.                           _serialNumber,
  344.                           "INQUIRY FROM " + _bank.accountName(_fromAccount),
  345.                           new Money(0),  // will cause this line to be omitted
  346.                           _newBalance,
  347.                           _availableBalance);
  348.          return Status.SUCCESS;
  349.       }
  350.     //</pre><hr><pre>
  351.     
  352.     // Instance variables of InquiryTransaction
  353.     private int   _fromAccount;
  354.   }   
  355.     
  356. //</pre></body></html>