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

金融证券系统

开发平台:

Java

  1. // <html><head><title>ATM Simulation Implementation - the Bank</title></head><body><h2>ATM Simulation Implementation - the Bank</h2><pre>
  2. /*
  3.  * Example ATM simulation - file Bank.java
  4.  *
  5.  * This file implements the class that manages communication with the bank.
  6.  *
  7.  * Copyright (c) 1997 - Russell C. Bjork
  8.  *
  9.  */
  10. package atm;
  11. import atm.util.Money;
  12. import atm.util.Status;
  13. //</pre><hr><h3>Class Bank</h3><pre>
  14. public class Bank
  15.   {
  16.     //</pre><hr><pre>
  17.     public Bank()
  18.       { }
  19.     
  20.     //</pre><hr><pre>
  21.     // The different types of account that can be linked to a card
  22.     private static final int CHECKING     = 0, 
  23.                              SAVINGS      = 1, 
  24.                              MONEY_MARKET = 2;
  25.     //</pre><hr><pre>
  26.     public int initiateWithdrawl(int cardNumber,
  27.                                  int PIN,
  28.                                  int ATMnumber,
  29.                                  int serialNumber,
  30.                                  int from,
  31.                                  Money amount,
  32.                                  Money newBalance /* return value */, 
  33.                                  Money availableBalance /* return value */)
  34.       { 
  35.         if (cardNumber < 1 || cardNumber > NUMBER_ACCOUNTS)
  36.             return Status.UNKNOWN_CARD;
  37.     
  38.         if (PIN != _PIN [ cardNumber ] )
  39.             return Status.INVALID_PIN;
  40.     
  41.         _currentTransactionCard = cardNumber;
  42.         _currentTransactionAccount = _accountNumber [ cardNumber ] [ from ];
  43.         if (_currentTransactionAccount == 0)
  44.             return Status.NO_SUCH_ACCOUNT;
  45.     
  46.         if (from == SAVINGS)
  47.             return Status.CANT_WITHDRAW_FROM_ACCOUNT;
  48.     
  49.         if (_availableBalance [ _currentTransactionAccount ].less(amount))
  50.             return Status.INSUFFICIENT_AVAILABLE_BALANCE;
  51.     
  52.         if (MAXIMUM_WITHDRAWL_AMOUNT_PER_DAY.less(
  53.                  Money.add(_withdrawlsToday [ cardNumber ], amount)))
  54.             return Status.DAILY_WITHDRAWL_LIMIT_EXCEEDED;
  55.     
  56.         _currentTransactionAmount = amount;
  57.         newBalance.set(Money.subtract(
  58.             _currentBalance [ _currentTransactionAccount ], amount));
  59.         availableBalance.set(Money.subtract(
  60.             _availableBalance [ _currentTransactionAccount], amount));
  61.     
  62.         return Status.SUCCESS;
  63.       }
  64.     
  65.     //</pre><hr><pre>
  66.     public void finishWithdrawl(int ATMnumber,
  67.                                 int serialNumber, 
  68.                                 boolean succeeded)
  69.       { 
  70.         if (succeeded)
  71.           { _withdrawlsToday [ _currentTransactionCard ].add(
  72.                     _currentTransactionAmount);
  73.             _currentBalance [ _currentTransactionAccount ].subtract(
  74.                     _currentTransactionAmount);
  75.             _availableBalance [ _currentTransactionAccount ].subtract(
  76.                     _currentTransactionAmount);
  77.           }
  78.       }
  79.     
  80.     //</pre><hr><pre>
  81.     public int initiateDeposit(int cardNumber,
  82.                                int PIN,
  83.                                int ATMnumber,
  84.                                int serialNumber,
  85.                                int to,
  86.                                Money amount,
  87.                                Money newBalance,
  88.                                Money availableBalance)
  89.       { 
  90.         if (cardNumber < 1 || cardNumber > NUMBER_ACCOUNTS)
  91.             return Status.UNKNOWN_CARD;
  92.     
  93.         if (PIN != _PIN[cardNumber])
  94.             return Status.INVALID_PIN;
  95.     
  96.         _currentTransactionCard = cardNumber;
  97.         _currentTransactionAccount = _accountNumber [ cardNumber ] [ to ];
  98.         if (_currentTransactionAccount == 0)
  99.             return Status.NO_SUCH_ACCOUNT;
  100.     
  101.         _currentTransactionAmount = amount;
  102.         newBalance.set(Money.add(_currentBalance[_currentTransactionAccount], amount));
  103.         availableBalance.set(_availableBalance[_currentTransactionAccount]);
  104.         return Status.SUCCESS;
  105.       }
  106.     
  107.     //</pre><hr><pre>
  108.     public void finishDeposit(int ATMnumber,
  109.                               int serialNumber, 
  110.                               boolean succeeded)
  111.       {
  112.         if (succeeded)
  113.             _currentBalance [ _currentTransactionAccount ].add(
  114.                 _currentTransactionAmount);
  115.       }
  116.     
  117.     //</pre><hr><pre>
  118.     public int doTransfer(int cardNumber,
  119.                           int PIN,
  120.                           int ATMnumber,
  121.                           int serialNumber,
  122.                           int from,
  123.                           int to,
  124.                           Money amount,
  125.                           Money newBalance,
  126.                           Money availableBalance)
  127.       { 
  128.         if (cardNumber < 1 || cardNumber > NUMBER_ACCOUNTS)
  129.             return Status.UNKNOWN_CARD;
  130.     
  131.         if (PIN != _PIN [ cardNumber ])
  132.             return Status.INVALID_PIN;
  133.     
  134.         int fromAccount = _accountNumber [ cardNumber ] [ from ],
  135.             toAccount = _accountNumber [ cardNumber ] [ to ];
  136.         if (fromAccount == 0 || toAccount == 0)
  137.             return Status.NO_SUCH_ACCOUNT;
  138.     
  139.         if (from == SAVINGS)
  140.             return Status.CANT_WITHDRAW_FROM_ACCOUNT;
  141.     
  142.         if (_availableBalance [ fromAccount ].less(amount))
  143.             return Status.INSUFFICIENT_AVAILABLE_BALANCE;
  144.     
  145.         _currentBalance [ fromAccount ].subtract(amount);
  146.         _currentBalance [ toAccount ].add(amount);
  147.         _availableBalance [ fromAccount ].subtract(amount);
  148.         _availableBalance [ toAccount ].add(amount);
  149.     
  150.         newBalance.set(_currentBalance [ toAccount ]);
  151.         availableBalance.set(_availableBalance [ toAccount ]);
  152.     
  153.         return Status.SUCCESS;
  154.       }
  155.     
  156.     //</pre><hr><pre>
  157.     public int doInquiry(int cardNumber,
  158.                          int PIN,
  159.                          int ATMnumber,
  160.                          int serialNumber,
  161.                          int from,
  162.                          Money newBalance,
  163.                          Money availableBalance)
  164.       {
  165.         if (cardNumber < 1 || cardNumber > NUMBER_ACCOUNTS)
  166.             return Status.UNKNOWN_CARD;
  167.     
  168.         if (PIN != _PIN [ cardNumber ] )
  169.             return Status.INVALID_PIN;
  170.     
  171.         int account = _accountNumber [ cardNumber ] [ from ];
  172.         if (account == 0)
  173.             return Status.NO_SUCH_ACCOUNT;
  174.     
  175.         newBalance.set(_currentBalance [ account ]);
  176.         availableBalance.set(_availableBalance [ account ]);
  177.     
  178.         return Status.SUCCESS;
  179.       }
  180.     
  181.     //</pre><hr><pre>
  182.     public int chooseAccountType(String purpose, ATM atm)
  183.       { String menu[] = { "Checking", "Savings", "Money Market" };
  184.         String prologue = "Please choose an account to ";
  185.         int choice = atm.getMenuChoice(prologue + purpose, 3, menu);
  186.         return choice - 1;
  187.       }
  188.     
  189.     //</pre><hr><pre>
  190.     public String accountName(int type)
  191.       { return accountNames[type];
  192.       }
  193.             
  194.     //</pre><hr><pre>
  195.     public String rejectionExplanation(int code)
  196.       { return explanation[code - Status.UNKNOWN_CARD];
  197.       }
  198.     //</pre><hr><pre>
  199.     // These constants are used by the accountName and rejectionDescription methods
  200.     
  201.     private static final String accountNames[] = { "CHKNG ", "SVNGS ", "MNYMKT" };
  202.     private static final String explanation[] = 
  203.       { 
  204.         "Your card number was not recognized",
  205.         "Invalid PIN",
  206.         "No account of the type you requested is linked to your card",
  207.         "You cannot withdraw money from this type of account at an ATM",
  208.         "The available balance in your account is not sufficient",
  209.         "You have exceeded the daily limit on cash withdrawls with your card" 
  210.       };
  211.       
  212.     //</pre><hr><pre>
  213.     // A complete simulation of the bank is not needed for the purposes of this
  214.     // example.  The code below implements a very limited simulation having a few
  215.     // ATM cards and a handful of accounts.  Information about these is stored in
  216.     // variables (and is thus not preserved between runs of the program), and is
  217.     // initialized to "hardwired" constant values at startup.  We disallow
  218.     // ATM withdrawls/transfers from SAVINGS.
  219.     
  220.     // A more realistic implementation is left as an exercise to the student!
  221.     private static final int NUMBER_CARDS = 2;
  222.     private static final int NUMBER_ACCOUNTS = 3;
  223.     // PIN for each card:
  224.     private static int _PIN [] =
  225.       { 0, // dummy for nonexistent card 0
  226.         42, 1234 
  227.       };
  228.     // Total withdrawls so far today using each card
  229.     private static Money _withdrawlsToday  [] =
  230.       { new Money(0),  //dummy for nonexistent card 0
  231.         new Money(0),
  232.         new Money(0)
  233.       };
  234.     private static final Money MAXIMUM_WITHDRAWL_AMOUNT_PER_DAY = new Money(300);
  235.     // Accounts (CHECKING, SAVINGS, MONEY_MARKET) linked to each card - 0 = none
  236.     private static final int _accountNumber [] [] =
  237.       { { 0, 0, 0 }, // dummies for nonexistent card 0
  238.         { 1, 2, 0 },
  239.         { 1, 0, 3 }
  240.       };
  241.     // Current and available balances of each account
  242.     private static Money _currentBalance [] =
  243.       { new Money(0), // dummy for nonexistent account 0
  244.         new Money(100), 
  245.         new Money(1000),
  246.         new Money(5000) 
  247.       }; 
  248.     private static Money _availableBalance [] =
  249.       { new Money(0), // dummy for nonexistent account 0
  250.         new Money(100), 
  251.         new Money(1000),
  252.         new Money(5000) 
  253.       }; 
  254.     // A real system would use the transaction commit/rollback mechanisms of an 
  255.     // underlying DBMS - we just save the most recent transaction initiated in
  256.     // a variable and complete if when we get the finish call.
  257.     
  258.     private static int _currentTransactionCard;
  259.     private static int _currentTransactionAccount;
  260.     private static Money _currentTransactionAmount;
  261.     
  262.   }
  263. //</pre></body></html>