ATM Simulation Program
CSS AP442 Autumn ’98
Assignment 1:
Object Relationships
Prepared by:
Instructor:
Dr. Stiber
October 25, 1998
Purpose
The purpose of this assignment is to experience the design and implementation of a simulated ATM machine using a collection of interacting objects. This is achieved by extend one new classes from the Counters hierarchy presented in the textbook (JOFA). In addition, these classes use UML/OMT notation to demonstration the object-oriented design data structure.
Problem Statement
This program is implemented to simulate the operation of an ATM, or automatic teller machine. It divides into two packages, "AtmBank" and "AtmMenus." The "AtmBank" package provides all functionality that needed to operate the ATM and the "AtmMenus" package manages the user-interface.
Package AtmBank Design
The Bank class is extended from the WarningCounter class that presented in the textbook. It uses the methods like numberCounterIs(), count(), etc. provided by the base classes to manage the number of accounts in the bank. Bank is a container class that contains many Accounts. The Account class is generated as part of ATMBank package that holds current account information. The AutoTellerException class manages the exception for this package. The AtmConstants class defines global constants used in the entire program. The hierarchy of AtmBank package is shown in figure 1 below. Note: Counters is renamed to AtmBank.
Figure 1 Hierarchy Diagram for AtmBank Package
Class Bank
A Bank class owns a fixed number of Accounts. The maximum account size is set at 50. It imports all the Accounts information currently stored in file "bank.dat" when a bank object is instantiated (importAccount method). Account can be created (addAccount method), updated (save method), and retrieved (getAccount method). The class diagram is given in figure 2 below and its attribute and actions are explained subsequently.
"bank.dat" is a text-based database file that comprises of account number, checking balance, and saving balance for each of the accounts in a bank. A comma is used to separate each piece of account information and the end of line character (‘\n’) is used to separate each account record. For this assignment, the file is setup with 10 accounts. The account number starts with one and is sequentially incremented by one. An arbitrary checking and saving balance are provided for each account.
Figure 2 Class Diagram for Bank
Bank()
Purpose: Constructor.
Argument: None.
Return Value: None.
Precondition: A WarningCounter class and the availability of database file ‘bank.dat’.
Postcondition: Bank accounts are imported from the database file ‘bank.dat.’
Pseudocode: Call base class constructor, passing the minimum and maximum account size.
Set the maximum account size.
Initialize all accounts in the bank by calling the importAccount() method.
importAccount()
Purpose: Reads the database file that holds all account records.
Argument: None.
Return Value: None.
Precondition: A text-based database file in the directory as the application.
Postcondition: Bank accounts are made available in the Bank class.
Pseudocode: Read until the end of file.
Use readLine() to read one line from the input file.
Parse out the account number, checking and saving balances.
Convert account number to integer and the checking and saving balances to double.
Throw an error if the maximum account is reached.
Call addAccount() to insert the account into account list.
Catch the I/O and runtime errors.
save()
Purpose: Writes all accounts information into the database file so their records are updated.
Argument: None.
Return Value: None.
Precondition: A text-based database file.
Postcondition: All records are updated if the file is open successfully, otherwise an IOException is thrown.
Pseudocode: Open the bank database file in overwrites mode.
Loop through each account in the list for numberCountedIs() times.
Write the Account into the file in ‘account #, checking balance, saving balance’ format.
End of loop.
Close the database file.
getAccount()
Purpose: Gets an Account.
Argument: Account number.
Return Value: Returns Account object if Account found, otherwise throws an exception.
Precondition: A valid account number.
Postcondition: Account object is returned if Account exists, otherwise an AutoTellerException is thrown.
Pseudocode: Iterates through all Accounts (size of accounts is determined by numberCountedIs()).
If the Account object matches the given account number, return the Account.
Else try next one.
End of loop.
Throw account-not-found exception.
addAccount()
Purpose: Adds a new Account.
Argument: Account.
Return Value: None.
Precondition: An existing Account object.
Postcondition: New Account appended to the list if successful, otherwise a RuntimeException is thrown.
Pseudocode: Adds new Account into the bank and increases the count by one until the maximum is reached. An
exception is thrown if the bank account list has reached its maximum.
Class Account
The Account class manages a single account information. The class diagram is given in figure 3 and its attributes and actions are explained in the following sections.
Figure 3 Class Diagram for Account
Account()
Purpose: Default constructor.
Argument: None.
Return Value: None.
Precondition: None
Postcondition: Initializes the Account to zero.
Pseudocode: Set the class’s attributes to zero.
Account()
Purpose: Alternate constructor.
Argument: Account number, checking balance, and saving balance.
Return Value: None.
Precondition: None
Postcondition: Initializes the Account to the given values.
Pseudocode: Set the class’s attributes to the given argument values.
getAccountNo()
Purpose: Gets the account number of an Account object.
Argument: None.
Return Value: The account number.
Precondition: A valid Account object.
Postcondition: An account number is returned.
Pseudocode: Return the account number.
deposit()
Purpose: Performs the deposit action.
Argument: Account type and its amount.
Return Value: None.
Precondition: A valid account.
Postcondition: Balance of the given type is updated with the amount if the account type is valid, otherwise an
AutoTellerException is thrown. The most recent transaction type is set to the type passed in.
Pseudocode: Add the requested amount to the checking or saving balance accordingly.
withdraw()
Purpose: Performs the withdraw action.
Argument: Account type and its amount.
Return Value: None.
Precondition: A valid account.
Postcondition: Balance is updated with the amount if the account type is valid and there is sufficient fund for
withdrawing, otherwise an AutoTellerException is thrown. The most recent transaction type is set to the type passed in.
Pseudocode: Withdraw the requested amount from the checking or saving balance accordingly.
getBalance()
Purpose: Gets checking or saving balance.
Argument: Account type.
Return Value: Checking or saving balance for the given account type; an AutoTellerException is thrown if the given type is invalid.
Precondition: A valid account.
Postcondition: Returns checking or saving balance if the account type is valid, otherwise an AutoTellerException
is thrown. The most recent transaction type is set to the type passed in.
Pseudocode: Return the checking or saving balance accordingly.
transfer()
Purpose: Performs the transfer action within the same account number.
Argument: Account type and its amount.
Return Value: None.
Precondition: A valid amount.
Postcondition: Balance is updated with the transfer amount if the account type is valid and there is sufficient fund
for transferring, otherwise an AutoTellerException is thrown. . The most recent transaction type is set to the type passed in.
Pseudocode: Check if the account is to be transferred from has sufficient fund; if not, throw an exception.
Subtract the requested amount from the account that is transferring from.
Add the request amount to the account that is transferring to.
toString()
Purpose: Displays current account information.
Argument: None.
Return Value: String.
Precondition: A valid Account.
Postcondition: Returns a string that contains the account information.
Pseudocode: Return a string that includes the account number, transaction type, checking balance, and saving
balance.
Class AtmConstants
The AtmConstants class defines the constants that are used in this program. The class diagram is given in figure 4 below and its actions are explained in the following sections.
Figure 4 Class Diagram for AtmConstants
AtmConstants()
Purpose: Constructor.
Argument: None.
Return Value: None.
Precondition: None
Postcondition: Does nothing.
Pseudocode: None.
toString()
Purpose: Returns a string corresponding to the transaction type.
Argument: Transaction type.
Return Value: Returns a string corresponding to the transaction type if this type is within the predefined list,
otherwise an AutoTellerException is thrown.
Precondition: None
Postcondition: A string that corresponding to the transaction type is returned.
Pseudocode: Use the switch statements to map the given transaction type to a string and return it. Throw
exception if the transaction type is invalid.
Class AutoTellerException
The AutoTellerException class handles the exception of this project. It is extended from the RuntimeException provided by Java. It does nothing but passing the reason string to the base class and let the base class handles the rest. The class diagram is given in figure 5 below and its constructor is explained in the following sections.
Figure 5 Class Diagram for AutoTellerException
AutoTellerException()
Purpose: Constructor.
Argument: the reason string.
Return Value: None.
Precondition: An exception is arrived.
Postcondition: Creation of the exception object.
Pseudocode: Pass the reason string to the base class.
Package AtmMenus Design
The AtmMenus Package consists of the BasicMenu class that all other classes derive from and four customized classes: GreetingMenu, TransactionMenu, AmountMenu, and EndingMenu. The BasicMenu class allows the user to interact with a screen that includes a title, possible option selections, and a prompt. It also manages the user responses. The four customized menus simply use the functionality provided by BasicMenu class to carry out their operations. The class hierarchy of AtmMenus package is shown in figure 6 below.
Figure 6 Hierarchy Diagram for AtmMenus
Class BasicMenu
The BasicMenu class manages display screen and user interaction. The display has title, selection options, input prompt, and input return type. This class is designed to serve as the base class for customizing user interaction menu screen. This class diagram is given in figure 7 below and its attributes and actions are explained in the following sections.
Figure 7 Class Diagram for BasicMenu
BasicMenu()
Purpose: Constructor.
Argument: Title, a list of options, prompt, and a boolean parameter indicating to use alphabet or numerical
labels as option selection index.
Return Value: None.
Precondition: None.
Postcondition: Initializes the class to the given values.
Pseudocode: Set the class’s attributes to the given argument values.
getOptionAsChar()
Purpose: Gets the user option selection as a character
Argument: None.
Return Value: An alphabet character.
Precondition: The option selection list is displayed to the user.
Postcondition: The selected option by the user.
Pseudocode: Call ShowMenu() to display the menu screen.
Call getOption() to get the user input as an alphabet character and return it.
getOptionAsInt()
Purpose: Gets the user option selection as an integer value.
Argument: None.
Return Value: An integer value.
Precondition: The option selection list is displayed to the user.
Postcondition: The selected option by the user.
Pseudocode: Call getOptionAsChar() to get the user option selection input as a alpha character.
Convert the character to an integer and return it.
showMenu()
Purpose: Displays the user interface screen that consists of title, options list, and prompt.
Argument: None.
Return Value: None.
Precondition: The title, options list, and prompt string are set during object construction.
Postcondition: The screen is displayed to the user.
Pseudocode: If there is a title, display it.
If there is an options list,
Use alphabetical label starting from ‘A’ for option lists, otherwise use numerical label starting from ‘1’.
For each item in the options list,
Display the label, followed by a ‘.’ character, then the text of the option item.
Set label to label+1.
End of loop.
End of if.
If there is a prompt, display it.
showPrompt()
Purpose: Displays the prompt.
Argument: None.
Return Value: None.
Precondition: The prompt string is set during object construction.
Postcondition: Displays the prompt on the screen.
Pseudocode: If the prompt is set, display it to the screen. Otherwise does nothing.
getOption()
Purpose: Gets the character corresponds to the options list from the user.
Argument: None.
Return Value: The character corresponds to the option the user selects.
Precondition: Shows the user a list of options to select from.
Postcondition: A valid user response character.
Pseudocode: Loop until a valid selection character is entered:
Read one line of the user input using keyboard.readline().
If there is a response, get the first character of the input.
Otherwise set the first character to an invalid character.
Convert the character to uppercase.
If the character is a valid character corresponding to an option, return the character.
Otherwise show the user a message and then display the options list again.
Catch any IO errors.
getInput()
Purpose: Presents a screen to the user and gets the user input.
Argument: None.
Return Value: A string of the user input.
Precondition: None.
Postcondition: A string of the user response is returned.
Pseudocode: Show the user interface screen with title, options, and prompt.
Flush the output buffer.
Uses keyboard.readline() to read one line of string entered by the user.
Catch any IO errors.
Return the string entered.
Class GreetingMenu
The GreetingMenu class provides a greeting title of this program and prompt for user for an account number. It is extended from the BasicMenu class. The class diagram is given in figure 8 below and its attributes and actions are explained in the following sections.
Figure 8 Class Diagram for GreetingMenu
GreetingMenu()
Purpose: Constructor.
Argument: None.
Return Value: None.
Precondition: A BasicMenu class.
Postcondition: Constructs the class to include a greeting title and account number prompt.
Pseudocode: Set the greeting title, option selections, account number prompt string, and string return type using the base
class’s constructor.
getAccountNo()
Purpose: Gets an account number.
Argument: None.
Return Value: Returns an account number.
Precondition: The user enters an account number.
Postcondition: An account number is returned.
Pseudocode: Show the greeting title and ask the user to enter a numerical account number.
If the user enters anything other than an integer value, throw an exception.
Otherwise return an account number as integer.
Class TransactionMenu
The TransactionMenu class provides a transaction menu for the user to select one of the supported transaction types. It is extended from the BasicMenu class. The class diagram is given in figure 9 below and its attributes and actions are explained in the following sections.
Figure 9 Class Diagram for TransactionMenu
TransactionMenu()
Purpose: Constructor.
Argument: None.
Return Value: None.
Precondition: A BasicMenu class.
Postcondition: Constructs the class to include title, selection options, and transaction type prompt.
Pseudocode: Set the display title, option selections, prompt string, and string return type using the base class’s
constructor.
getTransaction()
Purpose: Gets a transaction type from the user.
Argument: None.
Return Value: The transaction type selected.
Precondition: The title, option selections, and prompt string are set.
Postcondition: A transaction type is selected.
Pseudocode: Show the option selection for the user to select.
Get the selection index from the user.
Map the selection index to a predefined transaction type constant.
Throw an exception if the selection is invalid.
Return the transaction type.
Class AmountMenu
The AmountMenu class provides a prompt to ask user for a transaction amount. It verifies the amount is in correct format (up to cent). It is extended from the BasicMenu class. The class diagram is given in figure 10 below and its attributes and actions are explained in the following sections.
Figure 10 Class Diagram for AmountMenu
AmountMenu()
Purpose: Constructor.
Argument: None.
Return Value: None.
Precondition: A BasicMenu class.
Postcondition: Constructs the class to include a title and transaction amount prompt.
Pseudocode: Set the display title, prompt string, and numerical return type using the base class’s constructor.
getAmount()
Purpose: Gets the transaction amount.
Argument: None.
Return Value: The transaction amount.
Precondition: The title and prompt string are set meaningfully.
Postcondition: The transaction amount is returned.
Pseudocode: Display the menu screen that includes the transaction amount title and prompt.
Get the input string from the user.
If the input is not a correct dollar format (up to cent), then prompt the user for re-entry.
Otherwise return the amount in double format. Numerical format exception is thrown if the input does not conform to numerical string format.
decimalOK()
Purpose: Verifies there are only up to 2 decimal digits in the transaction amount.
Argument: A string.
Return Value: True if there are only up to 2 decimal digits in the transaction amount, otherwise false.
Precondition: An input string.
Postcondition: Indicating whether the input string contains no more than 2 decimal digits.
Pseudocode: Locate the position of ‘.’ character.
Return true if there is no ‘.’ character of the length of the string and the position of the ‘.’ characters differ by less than 3.
Class EndingMenu
The EndingMenu class presents the user a thank-you message and provides information whether or not the user wants to do another transaction. This class is extended from the BasicMenu class. The class diagram is given in figure 11 below and its attributes and actions are explained in the following sections.
Figure 11 Class Diagram for EndingMenu
EndingMenu()
Purpose: Constructor.
Argument: None.
Return Value: None.
Precondition: A BasicMenu class.
Postcondition: Constructs the class to include a thank-you message and continue transaction prompt.
Pseudocode: Set the display title, prompt string, and string return type using the base class’s constructor.
getContinue()
Purpose: Gets a continue response from the user.
Argument: None.
Return Value: Returns true if the user choose to continue doing another transaction; otherwise returns false.
Precondition: None.
Postcondition: The user response to the prompt is returned as a boolean value.
Pseudocode: Display the menu screen that includes the message title and prompt.
Get the input string from the user.
Convert the input string to upper case.
Return true if the first character of the input string is a ‘Y’ or ‘y’. Otherwise return false.
Driver
Finally, a driver ATMDemo class is created to test this program as shown in figure 12 below and the instance diagram and object relationships are shown in figure 13 follow that.
Figure 12 Class Diagram for ATMDemo
Figure 13 Instance Diagram for ATM Program
Input Data
Inputs by the user.
Bank accounts read from a file.
Output Data
Prompt displays on the screen.
Menu displays on the screen.
Displays the result to the user.
Bank accounts save to a file.
Error Handling Procedures
Output to user if illegal input was entered.
Assumption
Bank account size is limit to 50 and the possible accounts are range between 1 to 50.
Account is created from the input file ‘bank.dat.’
User will enter appropriate responses.
User will enter one character on option selection list. If enters more than one character, the program will using the 1st character and discard the remaining characters.
Exceptions are thrown but the compiler will catch them.
The compiler will automatically deallocate the allocated memory once they are done.