12
1 CS 215 – Fall 2020 Project 2 Version 2 changes since V1 are highlighted below Learning Objectives: - Use of a Partial Array of Structures to store data. - Writing functions from a detailed design document - Reading data from files and writing data to files - More problem solving with variables, decisions and loops General Description: You are to implement an Automatic Teller Machine (ATM) for a small bank. Each day, the machine is started up by the bank’s IT support. At this time, the ATM reads a list of current accounts from a text file and starts normal operation. Once started, customers use the ATM to make deposits, withdrawals and balance inquiries. For each customer, the ATM asks for and validates the account number and PIN number, then performs the deposit/withdrawal/balance-inquiry for the customer. IT support may enter a secret account number and PIN (“admin” and 9999) to conduct maintenance on account data. Maintenance includes adding a new account, removing an account, printing an accounts or transactions report, sorting the account data by name or account number, and shutting the ATM down. For each transaction completed by a customer or IT support person, a transaction record is created. The ATM has a limited number of transactions that may be done in one active session. Once this limit is reached, no more transactions may be done until the ATM is shutdown and restarted. Transactions and updated Accounts are written to a data file when the shutdown occurs. An Account consists of the following data fields: (the program does not have to enforce widths, data types, or assumptions when data is entered; widths are specified for using setw()) - Account Number: any digits or characters, assumed to have no spaces (field width = 10) - Account Name: customer name which may contain spaces (field width = 30) - PIN: a 4-digit number, but no math is performed on it, so implement as a string (field width = 4) - Balance: a dollar amount that does not exceed $99,999.99 (field width 8 ) For ease of testing, there will be a maximum of five (5) accounts the ATM can handle. You may temporarily change this number while working on the project. A Transaction consists of the following data fields: - A 1 character Transaction Code: W withdrawal by the customer N new account added by admin D deposit by the customer R old account removed by admin I balance inquiry by the customer - Account Number: same as the Account Number on an Account - Transaction Amount: a dollar amount (field width = 8) - Balance: after transaction applied; same assumptions/width as Account Balance. For ease of testing, there will be a maximum of seven (7) transactions the ATM can handle. You may temporarily change this number while working on the project.

CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

1

CS 215 – Fall 2020 Project 2

Version 2 changes since V1 are highlighted below

Learning Objectives: - Use of a Partial Array of Structures to store data. - Writing functions from a detailed design document - Reading data from files and writing data to files - More problem solving with variables, decisions and loops

General Description: You are to implement an Automatic Teller Machine (ATM) for a small bank. Each day, the machine is started up by the bank’s IT support. At this time, the ATM reads a list of current accounts from a text file and starts normal operation. Once started, customers use the ATM to make deposits, withdrawals and balance inquiries. For each customer, the ATM asks for and validates the account number and PIN number, then performs the deposit/withdrawal/balance-inquiry for the customer. IT support may enter a secret account number and PIN (“admin” and 9999) to conduct maintenance on account data. Maintenance includes adding a new account, removing an account, printing an accounts or transactions report, sorting the account data by name or account number, and shutting the ATM down. For each transaction completed by a customer or IT support person, a transaction record is created. The ATM has a limited number of transactions that may be done in one active session. Once this limit is reached, no more transactions may be done until the ATM is shutdown and restarted. Transactions and updated Accounts are written to a data file when the shutdown occurs. An Account consists of the following data fields: (the program does not have to enforce widths, data

types, or assumptions when data is entered; widths are specified for using setw()) - Account Number: any digits or characters, assumed to have no spaces (field width = 10) - Account Name: customer name which may contain spaces (field width = 30) - PIN: a 4-digit number, but no math is performed on it, so implement as a string (field width = 4) - Balance: a dollar amount that does not exceed $99,999.99 (field width 8 )

For ease of testing, there will be a maximum of five (5) accounts the ATM can handle. You may temporarily change this number while working on the project. A Transaction consists of the following data fields:

- A 1 character Transaction Code:

W withdrawal by the customer N new account added by admin

D deposit by the customer R old account removed by admin

I balance inquiry by the customer

- Account Number: same as the Account Number on an Account - Transaction Amount: a dollar amount (field width = 8) - Balance: after transaction applied; same assumptions/width as Account Balance.

For ease of testing, there will be a maximum of seven (7) transactions the ATM can handle. You may

temporarily change this number while working on the project.

Page 2: CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

2

Detailed Description: a copy of an executable is available on the website. Start Up: The logo is displayed containing your name (not Ada Byron). You may also change the company name and “re-decorate” if you wish. The input data file “acctsIn.txt” is then read (see the file format in the specifications below). When the input file fails to open (you can test this by temporarily renaming the file), the program prints a failure message and the program ends. Note no writing of output files is done in this situation, but the ATM shutdown message is displayed. When the input data is read correctly, the program prints a data read message and proceeds to print the logo and the customer menu for the first customer.

Main Operation: this process is repeated until the administrator shuts down the ATM

- The logo is printed. - The user is asked to enter the Account Number and PIN. - One of the following then occurs (should be checked in this order):

o When the admin account number and pin are entered, the Maintenance Menu (see below) is started. This process may result in the shutdown process being initiated.

o When the number of transactions is maxed out, an unavailable message is printed. o When the account number and PIN entered are found in the Accounts list, the Customer

Transaction Menu (see below) is started. o When none of the above occurs, an invalid account number/pin message is printed.

The ATM shutdown message is printed when the above menu ends, just before the program ends.

Page 3: CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

3 Transaction (Customer) Menu Operation: Unlike most menus, this one will not repeat until an “exit” option is chosen. The Transaction Menu options are displayed as shown in the example. The program asks the user to choose an option (1-4). The user’s option is validated, forcing a correct answer to be entered. [It is assumed all input will be integers]. Once a correct answer is entered, one of the following actions is chosen:

- Deposit: The current balance is displayed, and the user is asked to enter a deposit amount. The amount should be validated to be between 0 and (99999.99 – current balance: so the deposit will not allow the new balance to exceed 99,999.99). Once a valid amount is entered, the account balance is updated (increased), a transaction is (invisibly) logged [added to the transaction list], and the updated balance displayed as shown in the example.

- Withdrawal: The current balance is displayed, and the user is asked to enter a withdrawal amount. The amount should be validated so that it is between 0 and the current balance on the account. Once a valid amount is entered, the account balance is updated (decreased), a transaction is logged, and the updated balance displayed as shown in the example.

- Balance Inquiry: The current balance is displayed, and a

transaction (with transaction amount 0) is logged.

- Cancel Transaction: a message is displayed and no transaction is logged.

After the completion of one of the four options, control is returned to the main operation loop, where the logo is displayed and the account number for the next customer is prompted. Maintenance Menu Operation: Like most menus, this one will repeat the operation until the exit option (4) is chosen. The user input is assumed to be integer, but the input should be validated to be between 1 and 8. Once a valid option is chosen, one of the following 8 operations will occur, and the menu will be repeated.

Page 4: CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

4 Add new account: First the program ensures the maximum number of accounts and maximum number of transactions have not been reached. If one has reached maximum, the appropriate message is printed and the operation does not continue. When there is room for a new account the program asks the user to enter the account number, name, PIN and balance. This data is stored as a new account in the account list, and a transaction is logged with the entered amount as both the transaction and new balance amounts. The account added message is displayed as shown. Remove account: A check for “account list full” is not needed, but a check for transaction list full is needed as described above for Add New Account. When there is room for another transaction, the program asks the user to enter the account number of the account to remove. The account list is searched, and if no account has the entered account number, a not found message is printed as shown. When the entered account number is found, the account is removed from the account list and a transaction is logged with transaction amount of 0. A removed message is then printed. Shut down ATM The Maintenance Menu simply returns a signal and exits the menu. The Main Operation catches the shutdown signal and does the following:

- writes the current transaction data to a file (see specs below).

- writes the current account data to a file (see specs below).

- exits the main operation loop. - prints the shutdown message. - the program ends.

Exit maintenance This simply exits the Maintenance Menu and returns control to the Main Operation loop, where it repeats to process the next customer.

Page 5: CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

5 Print current accounts: Prints a report of all current accounts in the format shown in the example. The field widths on the items are:

Account Number: 12 PIN: 4 Balance: 9 Name: 30

The number of accounts is printed at the bottom of the report. There is a blank line before and after the report. Print current transactions: Prints a report of all current transactions in the format shown in the example. The field widths on the items are:

Account Number: 12 Trans amount: 9 Trans (Code): 1 Balance: 9

The number of transactions is printed at the bottom of the report. There is a blank line before and after the report. Sort accounts by account number: Simply sorts the account list by account number in ascending (lowest to highest) order. Only a message is printed as shown. The results can be tested by the user by selecting Print current accounts on the next iteration of the menu. Sort accounts by Name: Simply sorts the account list by name in ascending (lowest to highest) order. Only a message is printed as shown. The results can be tested by the user by selecting Print current accounts on the next iteration of the menu.

Coding Specifications:

In general: Style:

- use meaningful identifier names; data usually has “noun” names and functions have “verbs”. - use good indentation and some standard of style on all statements. - For each function, there should be a comment box at the top, and the name of the function in a

comment on the ending curly brace. Example: //--------------------------------------------------------- // displayLogo //--------------------------------------------------------- void displayLogo() {

} // displayLogo()

- Remember the comment box at the top, including // I received help from: (possibly no one) -

Page 6: CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

6 Coding Standards: include…

- no use of C++ statements: continue, break, goto - no use of “anonymous” loops: while (true) { … } while (1==1) { … }

- no functions more than 30 lines long, unless the “number of ideas” is small. Ex: a function to translate 50 state abbreviations into 50 state names: if (abrev == “AK”) state = “Alaska”; else if (abrev == “AL”) state = “Alabama”; // and 48 more else if’s

- no naked constants: not if (temp <= 32) but if (temp <= freezingPoint) Some exceptions: starting a for() at 0 or 1; common algebraic manipulations (pennies = dollars * 100;)

- no declaration or use of Global Variables: this may get you a failing grade, even if your program works “perfectly”!!!!

Major Declarations: - Structures for an Account and a Transaction:

o See the descriptions on page 1. o The declarations should be outside of and before any functions.

- Constants: see the declarations at the end of this document. Constants should be declared for

any maximums, menu options, the admin account number and PIN, plus possibly others (ie. notFound for the search())

- Partial Array of Transactions: and - Partial Array of Accounts: these are declared at the top of (and inside of) main() and are passed

as arguments to functions where they are needed. MOST ESPECIALLY, THESE SHOULD NOT BE GLOBAL VARIABLES (declared outside any function).

Function Specifications: You are required to write the following functions using the specified names, arguments, and return types. If your function gets too long, you may add sub-functions and invoke those. Any deviation from this design must be approved by the instructor via email. Givens are function arguments (parameters) that are pass by value. Modifies are function arguments (parameters) that are pass by reference. Arrays are always pass by reference, and the ampersand (&) is never used on them. Determine the return type of the function from the Returns: specification. None means void.

Name Givens Modifies Returns

displayLogo()

Prints the program logo on the screen. It must include your name and the name of your Bank/ATM/etc. Normally it should be in a box as shown in the examples above, but you may have some “creative license” in designing your logo.

Page 7: CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

7

Name Givens Modifies Returns

startup() partial array of Accounts true – file open error false – no file open error

Opens an input file called “acctsIn.txt” and reads data into the partial array. The file format is as follows: - File name: “acctsIn.txt” - First Line: the number of accounts - Each following line, one per account, with a single space

between each value: o account number (no spaces) o current balance o PIN (no spaces) o customer name (possibly with spaces)

Note: to read the name, use (yes, that’s a space, not a newline!): ifstream f; …

if (f.peek() == ' ') f.ignore(); getline(f, acctName);

When the file open fails, print Unable to open input file acctsIn.txt! and return true When the file open succeeds, print Account data read! and return false

Name Givens Modifies Returns

search() - partial array of Accounts - account num to search for

index where found; or not found (-1)

Search the given partial array of Account structures for an account that has an account number matching the given search-for account number. When found, return the index of the array where the account was found; otherwise return not found (-1).

Name Givens Modifies Returns

addTrans() - transaction type - account number - transaction amount (money) - updated balance (money)

partial array of Transactions none/nothing

When there is room in the partial array to add another transaction: - copy the givens into the next available element of the array - increment the number of transactions

When the number of transactions in the array is maxed, just print No more transactions can be added!

Name Givens Modifies Returns

getMaintOption() option (1 to 8) [can be string or int]

Displays the Maintenance Menu as shown in the examples above. Uses a validation loop to force the user to enter a valid option (1 to 8), and returns the valid option entered.

Page 8: CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

8

Name Givens Modifies Returns

askAcctData() an Account (single, not an array)

Asks the user to enter the fields of an Account, storing the data in the modified argument. Remember, the name may have spaces, and you will need the if(peek) before using getline(). See Add new account in the description above for the required formatting.

Name Givens Modifies Returns

addAcct() - partial array of Accounts - partial array of Transactions

First checks that there is room to add a new account to the Accounts array, and a new transaction to the Transactions array. If either has max number of elements, a message is printed and nothing else is done. When there is room, the function invokes askAcctData() on the next “empty” element of the Accounts array and prints New account added!. It then invokes the addTrans() function with a code of ‘A’, 0 for the transaction amount, and the initial balance.

Name Givens Modifies Returns

removeAcct() - partial array of Transactions - partial array of Accounts

First, a check is made to see if there is room to add another transaction to the Transactions array. If not, a message is printed No more transactions may be added! and nothing else is done. This function asks the user to enter the account number to remove. I then invokes search() on the account number entered. When not found, it simply prints No account with account number acct# found! where acct# is the account number entered by the user. When the entered account number is found, the account is removed from the Accounts array and addTrans() is invoked with code ‘R’, the removed account’s balance as the transaction amount, and 0 as the final balance. Finally the message Account removed! is printed.

Name Givens Modifies Returns

printAccts() - partial array of Accounts

Prints all current accounts as shown on page 5.

Name Givens Modifies Returns

printTrans() - partial array of Transactions

Prints all current accounts as shown on page 5.

Name Givens Modifies Returns

sortByName() - partial array of Accounts

Sorts the array by name and prints Accounts sorted by Name.

Page 9: CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

9

Name Givens Modifies Returns

sortByNum() - partial array of Accounts

Sorts the array by account number and prints Accounts sorted by Account Number.

Name Givens Modifies Returns

doMaintenance() - partial array of Accounts - partial array of Transactions

true – when shutdown is chosen false – when shutdown not chosen

This is the menu-controlled loop for the maintenance menu. It repeats until the user enters the Exit option or until the Shutdown option is chosen. It invokes getMaintOption() to print the menu and get the validated option. It then invokes one of addAcct(), removeAcct(), printAccts(), printTrans(), sortByName() or sortByOption() depending on the option entered. It should return true when the shutdown option is chosen or false when any other option is chosen.

Name Givens Modifies Returns

getCustOption()

the valid option (1-4) entered [can be string or int]

This function displays the Customer Menu as shown above. It asks the user Enter option: It uses a validation loop to repeat the question until the user enters a valid option, including the error message Invalid option! Try again! As always, for integer input, you may assume the user enters an integer (not a double or string, etc). The function returns the valid option entered.

Name Givens Modifies Returns

doWithdrawal() - an (one) Account object - partial array of Transactions

This function prints the current balance of the modified account and asks the user Enter withdrawal amount: as described above. A validation loop should be used to validate that the input is not negative and is less than or equal to the account’s current balance, using the error message: Invalid amount. Enter a number 0.00 to bal where bal is the current balance. Once a valid amount is entered, subtract the amount from the current balance to get the new balance in the modified account and print Your new balance is: bal Finally, invoke addTrans() with code ‘W, the amount withdrawn, and the updated balance.

Page 10: CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

10

Name Givens Modifies Returns

doDeposit() - an (one) Account object - partial array of Transactions

This function is almost identical to doWithdrawal(), except the amount should be validated to be between 0 and (MAX_BALANCE – currentBalance) to prevent the MAX_BALANCE from being exceeded. The transaction code for a Deposit is ‘D’ when invoking addTrans().

Name Givens Modifies Returns

serveCustomer() - an (one) Account object - partial array of Transactions

Note: There is no menu loop here like with doMaintenance(). This function invokes getCustOpt() to display the Customer Menu and get the customer’s selection. It then invokes doWithdrawal(), doDeposit() or prints one of the following:

- Your current balance is: bal

- Transaction canceled! The balance inquiry should also invoke addTrans() with a code of ‘I’ and a transaction amount of 0. These two lines of code may be moved to a doInquiry() function if you like; but it is so short, that is not necessary.

Name Givens Modifies Returns

writeAccounts() - partial array of Accounts

This function opens a text file called “acctsOut.txt” for writing. When the open fails, the function prints Unable to open output file acctsOut.txt! and does nothing else. It writes the current account data to a file, using the same file format as for the input accounts; that is:

- first line: the number of accounts - for each account, one line with account number, balance, pin and name, with 1 space between

each.

Remember to close() the file.

Name Givens Modifies Returns

writeTrans() - partial array of Transactions

This function opens a text file called “transactions.txt” for writing. When the open fails, the function prints Unable to open output file transactions.txt! and does nothing else. It writes the current transaction data to a file, using this format:

- first line: the number of accounts - for each transaction: transaction type, account number,

transaction amount, new balance amount; with a single space between.

Remember to close() the file.

Page 11: CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

11

Name Givens Modifies Returns

doShutdown() - partial array of Accounts - partial array of Transactions

Invokes writeAccts() and writeTrans().

Name Givens Modifies Returns

getAcct() - partial array of Accounts

administrator index (-2); or index into Accts array of account found; or NOT_FOUND (-1)

This is the “start up” of the customer processing. It should start by invoking displayLogo(). Next it should ask the user to enter their account number and pin. No validation needs to be done on these values. Invoke search() on the given account number. When the administrator account number and pin are entered: return -2 (indicating admin has logged in) When search() returns NOT_FOUND: return NOT_FOUND. When search() returns an array index, and the found account PIN does NOT match the one entered:

return NOT_FOUND When search() returns an array index, and the found account PIN matches the one entered: return the

index of the found account (value returned by search()).

Name Givens Modifies Returns

main()

0

Declares the partial arrays of Accounts and Transactions. Invokes startup(). When startup() returns false (error), ATM shut down. is printed and the program ends. The function repeats the main function until the administrator shuts down the ATM:

- invokes getAcct(), which returns an account index. - when the account index is the administrator (-2)

o invokes doMaintenance() o if doMaintenance() returns true, invoke doShutdown(). The program then prints ATM

shut down. and the program ends. - when the number of transactions has reached the maximum, print

Sorry, this ATM is unavailable. Please check back later.

- when the account index is not found (-1), print: Invalid account number and/or pin!

- when the account index is 0 or greater: invoke serveCustomer()

Submission: Submit your .cpp file in Canvas. You do NOT need to submit any data files (input or output). As always, demonstrations to a TA or the instructor is not required.

Page 12: CS 215 Spring 2020 Project 2 Learning Objectiveskwjoiner/cs215/proj/proj2/proj2.pdf · Project 2 Due: March 23 (+10% extra credit if submitted by March 13) Version 1 (March 2) Learning

12

Constant declarations: You are not required to use these, but they may give you some good ideas: const int MAX_TRANS = 9; // max number of transactions in partial array const int MAX_ACCTS = 9; // max number of transactions in partial array const int notFound = -1; // returned by search(), etc const double MAX_BALANCE = 99999.99; // account balance max (for doDeposit() ) const string adminAcct = "admin"; // current administrator account number const string adminPin = "9999"; // current administrator pin number // Menu Options: First and last option for each menu. For range checking const int mntOptFirst = 1; // first option on Maintenance Menu const int mntOptLast = 8; // last option on Maintenance Menu const int custOptFirst = 1; // first option on Customer Menu const int custOptLast = 4; // last option on Customer Menu