Session.java
资源名称:ATM.zip [点击查看]
上传用户:ljt780218
上传日期:2022-07-30
资源大小:110k
文件大小:4k
源码类别:
金融证券系统
开发平台:
Java
- // <html><head><title>ATM Simulation Implementation - Individual Sessions</title></head><body><h2>ATM Simulation Implementation - Individual Sessions</h2><pre>
- /*
- * Example ATM simulation - file Session.java
- *
- * This file implements the class that represents a single customer session
- * with the ATM
- *
- * Copyright (c) 1997 - Russell C. Bjork
- *
- */
- package atm;
- import atm.transaction.Transaction;
- import atm.util.Status;
- import atm.util.Money;
- //</pre><hr><h3>Class Session</h3><pre>
- public class Session
- {
- //</pre><a name="Session::Session"><hr><pre>
- public Session(int cardNumber, ATM atm, Bank bank)
- { _cardNumber = cardNumber;
- _atm = atm;
- _bank = bank;
- _state = RUNNING;
- _PIN = 0;
- _currentTransaction = null;
- }
- //</pre><a name="Session::doSessionUseCase"><hr><pre>
- public void doSessionUseCase()
- {
- _PIN = _atm.getPIN();
- do
- {
- String anotherMenu[] = { "Yes", "No" };
- _currentTransaction = Transaction.chooseTransaction(this, _atm, _bank);
- int status = _currentTransaction.doTransactionUseCase();
- switch (status)
- {
- case Status.SUCCESS:
- if (1 != _atm.getMenuChoice
- ("Do you want to perform another transaction?",2,anotherMenu))
- _state = FINISHED;
- break;
- case Status.INVALID_PIN:
- _state = ABORTED;
- break;
- default:
- boolean doAnother = doFailedTransactionExtension(status);
- if (! doAnother)
- _state = FINISHED;
- }
- }
- while (_state == RUNNING);
- if (_state != ABORTED)
- _atm.ejectCard();
- }
- //</pre><a name="Session::doInvalidPINExtension"><hr><pre>
- public int doInvalidPINExtension()
- {
- int code;
- for (int i = 0; i < 3; i ++)
- { _PIN = _atm.reEnterPIN();
- code = _currentTransaction.sendToBank();
- if (code != Status.INVALID_PIN)
- return code;
- }
- _atm.retainCard();
- _state = ABORTED;
- return Status.INVALID_PIN;
- }
- //</pre><a name="Session:doFailedTransactionExtension"><hr><pre>
- public boolean doFailedTransactionExtension(int reason)
- { switch(reason)
- {
- case Status.TOO_LITTLE_CASH:
- return _atm.reportTransactionFailure(
- "Sorry, there is not enough cash available to satisfy your request");
- case Status.ENVELOPE_DEPOSIT_TIMED_OUT:
- return _atm.reportTransactionFailure(
- "Envelope not deposited - transaction cancelled");
- default:
- return _atm.reportTransactionFailure(
- _bank.rejectionExplanation(reason));
- }
- }
- //</pre><a name="Session::cardNumber"><hr><pre>
- public int cardNumber()
- { return _cardNumber;
- }
- //</pre><a name="Session::PIN"><hr><pre>
- public int PIN()
- { return _PIN;
- }
- //</pre><hr><pre>
- // Possible values for _state instance variable
- private static final int RUNNING = 0;
- private static final int FINISHED = 1;
- private static final int ABORTED = 2;
- // Instance variables
- private int _cardNumber;
- private ATM _atm;
- private Bank _bank;
- private int _state;
- private int _PIN;
- private Transaction _currentTransaction;
- }
- //</pre></body></html>