CISC 181 Project 3 Designing Classes for Bank Accounts Code Due: On or before 12 Midnight, Monday, Dec 8; hardcopy due at beginning of lecture, Tues, Dec 9 What You Need to Know This project is based on the material assigned through chapter 8. Specifications In your job as chief system analyst at the Diamond State Bank, you are asked to design and implement some new software for processing bank accounts. Fortunately, you know about object-oriented programming in C++ and know that a well-designed set of classes will be the right way to implement the new software. The Class BankAcct Design a class BankAcct. Each object of the class should contain the private data Name name; int acctno; double balance; The type Name will be a full-fledged class. That is, class Name { friend ostream& operator<<(ostream& out, const Name& n); friend istream& operator>>(istream& in, Name& n); Adapted from exercise 7.8 in D&D. 1
public: Name(const char* fname = "", const char* lname = ""); // Constructor Name(const Name& n); ~Name(); // Copy constructor // Destructor char* getfirstname() const; char* getlastname() const; private: }; char *firstname; char *lastname; The two constructors should dynamically allocate char[] arrays for the first and last names that are the exact size needed to hold the names. The destructor, ~Name(), should deallocate the dynamically allocated arrays. Overload the assignment operator if you wish to use assignment statements with Names. Overload the comparison operators, ==, <, and >, as is appropriate. Write and debug this class before beginning the implementation of the next class. The constructor for BankAcct should take four arguments: (1) a character string for the first name, (2) a character string for the last name, (3) an integer for the account number, and (4) a double (for the balance) with default value = 0.0. The default values for the first and last names should be the empty string "". The default value for the account number should be zero. The constructor should not allow initial balances to be negative. If a user of the class tries to set the initial balance to a negative value, issue an error message and set the balance to zero. Provide the following public member functions. For simplicity, use the type double for money amounts. 1. Name getname() const; Returns the account holder s name. 2. int getacctno() const; Returns the account s account number. 3. double getcurrentbal() const; Returns the current balance. 4. void deposit(double amt); Deposits amt. Make sure that amount is not negative. If the amount is negative, void the deposit. 5. double withdraw(double amt); Withdraws amt. Make sure that amount is greater than or equal zero and less than or equal the account balance. If not, make the withdrawal amount zero. Return the amount withdrawn. 2
6. void monthlyint(double annualrate); that calculates monthly interest by multiplying the balance by the annual rate divided by 12; this interest should be added to the balance. 7. friend istream& operator>>(istream& in, BankAcct& A); Overload the operator >> to read data for an individual account in the form given in the files $CLASSHOME/programming-projs/atm-acct-data and $CLASSHOME/programming-projs/passbook-acct-data. That is, in the form first-name last-name account-no balance 8. friend ostream& operator<<(ostream& out, const BankAcct& A); Overload the operator << to write individual account data. It writes data for an individual account in the format needed for input. Use the overloaded I/O operators to read/write an object of type Name when implementing the I/O operators for the class BankAcct. A smart programmer would now write a main driver program to test out the BankAcct class before proceeding with the rest of the project. Usually when implementing a large software project, it is a good strategy to implement the input and output functions first so that they can be used to help debug the other functions. See the section on debugging tips at the end of this write-up for more helpful information about how to debug your code. The Class AcctArray Now design a class AcctArray to process the bank s files of different accounts. Each file begins with a line like the following CurrentInterestRate = x.y% where x.y is the annual interest rate for this account. This line is then followed by lines for individual accounts in form given in the previous section. See the files atm-acct-data and passbook-acct-data. Each member of the class AcctArray should contain the private data BankAcct* Acct; int noaccts; int currentarraysize; double annualinterestrate; Acct is a pointer to a dynamically allocated array of accounts. noaccts is the current number of accounts, and annualinterestrate is the annual interest rate paid by this kind of account. The class should provide the following public member functions in addition to a constructor and destructor. The constructor should take two arguments, the size of the array to create with default value = 10 and the annual interest rate 3
with default value = 0.06. The destructor (see the constructors/destructors in the class String, Fig08-7 of D&D for an example that does something similar) should deallocate the dynamic memory allocated by the constructor and nothing more. 1. friend istream& operator>>(istream& in, AcctArray& A); Overload the operator>> to read a file of bank accounts (until EOF is encountered). This function should reallocate the array Acct as needed so that it is big enough to hold all the data. The file $CLASSHOME/example-progs/array-dynamic-read.cc demonstrates how to do this. 2. friend ostream& operator<<(ostream& out const AcctArray& A); Overload the operator<< to write a file of bank accounts. The implementation of the overloaded I/O operators for the AcctArray class should use the overloaded operators >> and << defined for the class BankAcct. 3. void processtransactionfile( char filename[] ); Process a file of transactions for the accounts. Each item in the file is of the form transactiontype accountnumber amount where the transaction type is the string deposit or the string withdraw, account number is a customer s account number, and amount is the amount to be deposited or withdrawn from the account. 4. void calcmonthlyint(); Calculates the monthly interest for each account in the array and adds (deposits) it to the account. 5. void SortByName(); Sorts array Acct by name. 6. void SortByAcctNo(); Sorts array Acct by account numbers with smallest first. The main() Program Use the class AcctArray to write a main program to process files for two different kinds of bank accounts one of ATM accounts and the other of pass book accounts. The program should read the information for the two kinds of accounts as found in the external files $CLASSHOME/programming-projs/atm-acct-data and $CLASSHOME/programming-projs/passbook-acct-data. Use the default initialization values for each variable declared of type AcctArray. For each input file main() performs the following functions. Prints the file as originally read. The bank might do this for accounting purposes. Processes the corresponding file of transactions. That is, the file $CLASSHOME/programming-progs/atm-transactions or the file $CLASSHOME/programming-progs/passbook-transactions. 4
Computes and adds the monthly interest to each account. Prints the file sorted by name and then prints it again sorted by account number. All functions for the classes should be member functions (i.e., do not use friends except when overloading operators). Use separate.h and.cc files for each class and a seventh file main.cc for the main program. What to Hand-in Via email Send a copy of each of your files of code to your TA 1 no later than 12 midnight on Monday, Dec 8. Hardcopy Hand-in a hard copy of a single file containing the following: the hardcopy is due at the beginning of lecture on Tuesday, Dec. 9. The file should be created using the Unix command script as done previously for projects and in lab. 1. A completed cover sheet (see the file $CLASSHOME/programming-projs/coversheet). Please note the distribution of points on the coversheet that shows how the project will be graded. It is important that you include this page. To do so, copy it to your directory, fill in the assignment name (i.e., Programming Project 3), your name, and your user number. Then use the Unix command cat to include the file in your script file. Not doing this properly can cost you up to five points. 2. A listing of all your program files. Your program should use appropriate variable names, contain clarifying comments and be well formatted. Use the programs given in class and in the text as models. Use the Unix command cat (do not use more, it does not work properly in script files) to list the file containing your program. 3. An execution of a.out to verify that the program executes correctly on the provided data. 4. Use the following commands to generate the script file. typescript cat coversheet cat name.h cat name.cc 1 That is either to gwaltz@cis.udel.edu, durandet@cis.udel.edu, or slyvers@udel.edu. 5
cat bankacct.h cat bankacct.cc cat acctarray.h cat acctarray.cc cat main.cc CC *.cc a.out exit Debugging Tips 1. Do not make the mistaken assumption that once your program compiles, it is almost finished. For anything except the most trivial examples, debugging is the most time consuming aspect of program development. Plan for it! 2. Do initial debugging of code as you write it. Individual pieces are much easier to debug than a big program. Write main programs for this purpose that you ultimately discard after all debugging is done. Once the individual pieces are known to work, it much easier to debug the end product. 3. Implement overloaded output operators (or provide print methods) for each class, if only to help with debugging. 4. Verify data inputs (that is, data read from a file, argument values for functions/methods). 5. A corollary to the previous tip: debug printing of function/method args provides an informative way to trace the flow of control through your program. 6. Precisely identify where errors are occurring via debugging statements. This makes the error(s) much easier to find, but even then there will be cases in which the error remains illusive. In some cases, it will be necessary to break complicated code into simpler statements to precisely identify where an error is occurring. 7. To identify exactly where an error is occurring, it is imperative that output be printed when an output statement is executed (instead of being buffered for later output). For this reason, beginning programmers should rarely if ever use " n" for new lines in output as these do not cause the output buffer to be flushed. Used the io manipulator endl instead. It always causes the output to be flushed and the output to immediately appear on the screen or in an output file. 6
8. Use simple data when debugging. That is, files with only a few inputs, numerical values for which it is easy to compute answers to be compared with output from debugging statements, etc. One of the most common mistakes of inexperienced programmers is using overly complicated data for debugging purposes. 9. Isolate your errors with debugging statements before asking the TA or Prof for help. 10. Finally, the most important tip of all: Start and finish before the deadline! Deadline pressures make debugging code much more error prone. Rules of the Game For programming projects, unlike labs, you are expected to do all the work on your own. Many students are tempted, especially later in the semester when they have much to do, to improperly obtain help from others. This is the most common form of academic dishonesty in computer science courses and is monitored carefully. Unfortunately, quite a few students have been referred to the Student Judicial System for such problems. So please do not resort to using the solutions of others as your own. Examples of actions that are specifically prohibited include: submitting another person s work as your own. using another person s solution as a model for your own work. except as explicitly directed, working together on an assignment, sharing the computer files or programs involved. knowingly allowing another student to look at, copy, or use one of your computer files. having a tutor or friend write code for you. Late projects will be penalized 2 n points where n is the number of days it is late. The penalty clock ticks each day at midnight. 7