Take Home Lab #2 Problem 1: Bank Problem 1: Bank (1/5) 3 Manage customers’ bank account using following operations: • Create a new account given a customer’s name and initial account. • Deposit money to a customer’s account • Withdraw money from a customer’s account and report whether the transaction is successful or not. Problem 1: Bank (2/5) 4 Class Account Attributes: private: int balance; // store the current account balance string name; //store the customer’s name Problem 1: Bank (3/5) 5 Class Account Methods: public: //constructor Account (int balance, String name); //setter, getter string getName(); … void setName(string new_name); Problem 1: Bank (2/5) 6 Class Bank Attributes: private: int num_of_acc; //store the number of account in a bank double interest; //store the interest rate Account* acc_list[101]; //store the pointers to the accounts Problem 1: Bank (3/5) 7 Class Bank Methods: public: //constructor Bank (double new_interest); void create (string name, double value); void deposit(string name, double value); void withdraw(string name, double value); void getInfo(int month, Account* result[]); //getInfo transfers the account list to the result array //can also be implemented with vector<Account*> Problem 1: Bank (5/5) 8 Input: Process until “0” is read. cin >> operation; while (operation!=“0”) { //Process the input cin >> operation; } //Print the final balance of all customers. Problem 2: Social Network Pointers as returning values of Methods 1: Person * addPerson(string name){ 2: Person * p = new Person(name); 3: persons.push_back(p); 4: return p; 5: } 6: Person * x = addPerson(“Acer”) Pointers as returning values of Methods Line 4 returns the address of the newly created person (named “Acer”) to Line 6. Then x will point to the same Object as p does. x and p are not Persons, they are pointers to persons. x Person{“Acer”, <groups of p Acer>} Distinction 1’: Person addPerson(string name){ 2’: Person p = Person(name); 3’: persons.push_back(p); 4’: return p; 5’: } 6’: Person x = addPerson(“Acer”) Distinction Line 4 returns the newly created person (named “Acer”) to Line 6. Then x will have the same value as p. x and p are both Persons, and they points to different addresses which contain the same value. x p Person {“Acer”, <groups of Acer>} Person {“Acer”, <groups of Acer>} Social Network 1 Person name: string vector <int> group // the group ids of the person’s groups Group name: string id: int vector <Person *> members SocialNetwork vector<Person *> persons vector <Group *> groups. Methods to process queries. Tasks For all operations, find the person in the person list. Create a new person object if the person has not been mentioned before. Createjoin First, check whether the group name already exist. If not exist, create a new group with the assigned name. Add the person into the group. Quit Find the person in the member list of the group and remove it Find the group in the person’s group list, and remove it. Tasks Query 1 Just go through the group list to find the one with maximum member.size() Query 2 Go through every person x in the person list Counting the number of x’s acquaintances by going through every person y in the person list Check whether x knows y by going through x’s and y’s group list. If they are in some same group (if their group lists have some common elements), then they know each other. Update the person who has the maximum number of acquaintances. Code for Query 2 string SocialNetwork::answerQuery2(){ int k = -1; int max_know = -1; for (int i=0; i<persons.size(); i++){ int num_know = 0; for (int j=0; j<persons.size(); j++){ if (i==j) continue; if (persons[i]->know(persons[j])) num_know++; } if (num_know > max_know || num_know == max_know && persons[i] >getName() <persons[k]->getName()){ max_know = num_know; k = i; } } return persons[k]->getName(); } Problem 3: File Problem Create files and folders Delete a file from a folder Move a file to other folder Count the size of a folder Find the folder with largest size File Class File.h #include <iostream> #include <cstdlib> #include <string> using namespace std; class File { private: string name; int size; string foldername; public: File(); File(string name, int size, string foldername); string getName(); int getSize(); string getFolderName(); void setFolderName(string foldername); }; Folder Class Folder.h #include <cstdlib> #include <cstring> #include <vector> #include "File.h" using namespace std; class Folder { private: string name; vector<File*> files; public: Folder(string name); string getName(); void addFile(File* file); void deleteFile(File* file); int countSize(); }; deleteFile() loop through vector<File*> files, find the file you want to delete. void Folder::deleteFile(File* file) { for each tempFile in this->files: { if tempFile == file { this->files.erase(…); break; } } } countSize() loop through vector<File*> files, sum the size of each element. int Folder::countSize() { int totalSize = 0; for each file in files: totalSize += file->getSize(); return totalSize; } Main File Create two vectors vector<Folder*> folders; vector<File*> files; Createfolder if (operationType == "Createfolder") { string folderName; cin >> folderName; folders.push_back(new Folder(folderName)); } Createfil e File *newFile = new File(…); Loop through existing folders -Find the desired folder -Add file to folder Don’t forget to put the file in the folder! Deletefile Loop though existing files -Find the file -Loop through existing folders -Find the desired folder -Remove file from folder -Delete the file. Don’t forget to delete the file in the folder! Movefile To Move file A from B to C: Delete file A from C, Create file A in B. Find Largest Init largestSize = -1 Loop through all the folders -if countSize() > largestSize Common Errors 1. Constructor - Incorrect: Folder(string foldername) { foldername = foldername; } - Correct: Folder(string foldername) { this->foldername = foldername; } - Correct: Folder(string _foldername) { foldername = _foldername; } 2. Accessor When the method or attribute is private, you can’t use it in other classes. For example: In Folder.cpp, we have private: name; in the main program, folder->name will not work. You can use the getter method folder->getName(); since getName() is public. 3. Iterator When using iterator: Incorrect: *folder->getSize() Correct: (*folder)->getSize() 5. Accessing Method method like getName should be accessed using getName() with the parentheses case matters. 6. Case Sensitivity "FindLargest" is different from "Findlargest".