1 / 19

Making the bank object oriented

Making the bank object oriented. In upcoming lectures we will be looking at efficiency issues In order to study these issues we need implement the banking example as classes and vectors By making these changes we will be able to swap algorithms for the bank functions and measure efficiency.

lotte
Télécharger la présentation

Making the bank object oriented

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Making the bank object oriented • In upcoming lectures we will be looking at efficiency issues • In order to study these issues we need implement the banking example as classes and vectors • By making these changes we will be able to swap algorithms for the bank functions and measure efficiency. • The two things we need to do are • Change the acct struct into an object • Make the bank a class that stores its data in a vector Computer Science I - Martin Hardwick

  2. Making the struct an object • We want to convert this struct to an object • An object has data and methods • An object has public and private sections • Good practice puts the data in the private • Good practice puts access methods also known as get and put methods in the public section • After we have done the conversion we can add other methods to the object. • For example add_funds (double amount) is_bankrupt() struct acct { int acctnum; string name; double balance; }; Computer Science I - Martin Hardwick

  3. Contents of the acct.h file #include <string> class acct { public: acct (); acct (int num, string name, double bal); string get_name (); int get_acctnum (); double get_balance (); double put_balance (double bal); private: int acctnum; string name; double balance; }; There must be a corresponding account.cpp file with the member functions’ implementations. The ‘acct’ functions create the object. One with default values and the other with new values The get functions return each of the values. The put function allows the balance to be changed. The semi-colon at the end of the class is very important Account Class Computer Science I - Martin Hardwick

  4. Contents of the acct.cpp file #include <string> #include “acct.h” double acct::get_bal () { return balance; } void acct::put_balance (double bal) { balance = bal; return; } string acct::get_name () { return name; } void acct::put_name (string nme) { name = nme; return; } A method is a function that starts with the name of the class followed by “::”. acct::get_bal is a method of the acct class A get method returns the value. A put method changes the value. Normally there are gets and puts for every data item You MUST use the :: notation or the method will not belong to your class. It will be an ordinary function that cannot access the member data So you will see compiler errors complaining about undefined variables. Acct implementation Computer Science I - Martin Hardwick

  5. More contents of the acct.cpp file // default for constructor for arrays etc. acct:acct () { acctnum = -1; name = “unknown”; balance = 0; } // sensible constructor for new values acct:acct (int num, string nme, double bal) { acctnum = num; name = nme; balance = bal; } A constructor method has the same name as the class. Constructor methods are used to set initial values for the object. There has to be a constructor that takes no arguments. This constructor is used to fill arrays and other data structures Usually there is at least one other constructor that sets initial values for all the private data. The default constructor should set the private data to values that show the data has not yet been set Our example sets the acctnum to -1 Constructor methods Computer Science I - Martin Hardwick

  6. #include <iostream> #include <iomanip> #include <string> #include <fstream> #include <vector> #include “acct.h” using namespace std; class banking { private: vector <acct> bank; // bank accounts public: banking (); int find(int goal); void insert(acct newacct); int size (); // current size of the bank acct get (int loc); // account at a location }; Next we will make a class for the bank This class wraps the functions that we made before so that we can find and insert bank accounts. We also have a function to return a bank account instance after it has been found using the find function A vector is used to hold the list of bank accounts. A bank account consists of an account number, the name of the person who owns it, and the current balance. Our new C++ class does this Bank Simulation (banking.h) Computer Science I - Martin Hardwick

  7. #include “banking.h” int main () //PURPOSE: simulate a small bank //PRECONDITIONS: existing // accounts in file account.txt // in project folder //POSTCONDITIONS: finds, inserts, // deletes, and lists accounts { // list of bank accounts banking my_bank; // file with list of bank accounts ifstream vault; // user request string command; // account number to find int goal; The main function creates the list of bank accounts and processes user commands to perform transactions. The number of accounts is unlimited. The existing accounts are read from a file at the start of the program. this creates the bank vector for the program to use User commands are typed as strings (e.g., find, insert, exit). Bank Simulation (2) Computer Science I - Martin Hardwick

  8. // subscript of account in bank int loc; // a bank account acct account; int num; string nme; double bal; // read existing accounts from file vault.open("accounts.txt"); vault >> num >> nme >> bal; while (!vault.eof()) { account = account (num, nme, bal); my_bank.insert (account); vault >> num >> nme >> bal; } // display dollar values correctly cout << setiosflags(ios::fixed) << setprecision(2); The program assumes that a file named accounts.txt contains the data for existing accounts. This is the standard while loop for reading data from a file until the end of file is reached. We now use the insert method to add the data to the bank Bank Simulation (3) Computer Science I - Martin Hardwick

  9. // get ready to process commands cout << "The bank is now open." ; cout << endl << endl; // loop to process user commands cout << endl << "----------" << endl; cout << "Enter command " << "(exit to stop): "; cin >> command; while (command != "exit") { if (command == "find") { // FIND COMMAND // get account to find cout << "Enter account num: "; cin >> goal; // search for account loc = my_bank.find(goal); First case is the find command. get account number to find from user call Member function find to search the account list for this account Bank Simulation (4) Computer Science I - Martin Hardwick

  10. int banking::find(int goal) //PURPOSE: search for a goal account // in the list of accounts //POSTCONDITIONS: returns the // subscript of goal account or -1 if // the goal account is not found { int k; // loop variable Parameters: list of accounts, number of accounts, number of the goal account to find. Return: subscript of goal account if found, or –1 to indicate that it is not in the list. Search the list from its beginning, element by element until: find the account reach the end of the list This is called a sequential search. Member Function Find (1) Computer Science I - Martin Hardwick

  11. //search for the goal account in //the list of accounts for (k=0; k<bank.size(); k++) { if (bank[k].get_num() == goal) { return k; } } // didn't find goal account return -1; } If the goal number is in the account list, it will be found by the for loop. if it is found at subscript k, return k and terminate the function If the loop ended because k is no longer less than numaccts, then the entire list was searched and the goal account was not found. in this case, return -1 Member Function find (2) Computer Science I - Martin Hardwick

  12. // display account data // or error message if (loc >= 0) { account = my_bank.get (loc) cout << "Account: " << account.get_num() << "\t" << "Owner: " << account.get_name() << "\t" << "Balance: " << account.get_balance() << endl; } else { cout << "Account " << goal <<" does not exist." << endl; } } Remember, just before this is the call to function find with the result saved in variable loc. If loc is greater than or equal to 0, it must be the subscript of the goal account in the account list. in this case, display the account data. First get the account from the bank Call get methods to get the values Bank Simulation (5) Computer Science I - Martin Hardwick

  13. else if (command == "list") { // LIST COMMAND cout << "List of accounts:" << endl; for (loc=0; loc<my_bank.size(); loc++) { account = my_bank.get(i); cout << "Account: " << account.get_num () << "\t" << "Owner: " << account.get_name() << "\t" << "Balance: " << account.get_balance() << endl; } } The second case inside the while loop that processes user commands is the list command. Display the account data for all accounts in the account list. Probably better to make this a method in the banking class Called dump or something similar banking::dump (); Bank Simulation (6) Computer Science I - Martin Hardwick

  14. else if (command == "insert") { // INSERT COMMAND cout << "Enter new account data:" << endl; cout << "Account number: "; cin >> account.num; cout << "Account name: "; cin >> account.name; cout << "Account balance: "; cin >> account.balance; my_bank.insert(account); } The third case inside the while loop that processes user commands is the insert command. This is now a method of the banking class Bank Simulation (7) Computer Science I - Martin Hardwick

  15. void banking::insert (acct newacct) //PURPOSE: insert a new account into // the account list // //POSTCONDITIONS: account list // modified to add new account, // order of account list maintained { int k,j; // loop variables Parameters: new account to add to the list. Since the list order must be maintained, we must find where in the list the new account should be placed. Once we know where it goes, we must make room in the list to insert the new account. Function insert (1) Computer Science I - Martin Hardwick

  16. Inserting Into An Order List • Find where the new item goes. • Make room for the new item. • Insert the new item into the list. 8 Number of elements in the list: 9 0 10 20 30 40 50 45 60 50 70 60 70 Insert 45 Computer Science I - Martin Hardwick

  17. // find place to put new account in list // to maintain order of list k=0; while ((k < bank.size()) && (bank[k].get_num < newacct.get_num()) ){ k = k + 1; } // add new account to list if (k == bank.size()) { // goes at end bank.push_back (newacct); } The loop terminates when: we find the first account in the list with a larger account number we reach the end of the list having found no larger account numbers If there are no larger account number in the list, then k equals the number of accounts at the end of the loop. in this case, add the new account to the end of the list Function insert (2) Computer Science I - Martin Hardwick

  18. else { // goes in middle // make room to add it bank.push_back (newacct); for (j=bank.size()-1; j>k; j--) { bank[j] = bank[j-1]; } // add it to vacated spot bank[k] = newacct; } } Otherwise, the new account belongs in element k of the list. Move all elements from k to the end of the list, one element to the right. it is easiest to do this in reverse order Now we can put the new account into element k of the list without overwriting another account. Function insert (3) Computer Science I - Martin Hardwick

  19. else { // INVALID COMMAND cout << "Invalid command." << endl; } cout << endl << "----------" << endl; cout << "Enter a command "; << "(exit to stop): "; cin >> command; } // exit command from user cout << endl << "----------" << endl; cout << "The bank is now closed."; cout << endl << endl; return 0; } The final case in the while loop processing user commands is the default case for invalid commands. display a suitable error message When the user types the exit command, the while loop terminates. display a message closing the bank and terminate the program a “real” bank program would write the new account list to a file this file would be loaded into the program the next time it runs Bank Simulation (8) Computer Science I - Martin Hardwick

More Related