/****************************************************** * * * Professor Lloyd Parker * * Course: OOP244 * * Assignment: 3 * * Student: Siddharth Dahiya * * Student #: 016-194-086 * * Student userid: sdahiya1 * * This File Name: Account.cpp * * * ******************************************************* * * * This assignment represents my own work in * * accordance with Seneca Academic Policy * * * * Signature: Siddharth Dahiya * ******************************************************/ // Definition file Account.cpp for the class Account. #include #include #include #include "Account.h" //Definition for the Constructor Account::Account(){ customer = new char[251]; //Set Cutomer array to blank values namely. strcpy(customer, ",,,;"); strcpy(accountNumber, "000000000000000"); //Set accountNumber to all zeroes. balance = 0; //Set account balance to zero. } //Contructor for Account Account::Account(const Account &inAccount){ strcpy(customer, inAccount.customer); strcpy(accountNumber, inAccount.accountNumber); //Set accountNumber to all zeroes. balance = inAccount.balance; } Account::Account(const char c[], char num[], int b){ customer = new char[strlen(c) +1]; strcpy(customer, c); cleanSpace(customer); strcpy(accountNumber, num); //Set accountNumber to all zeroes. balance = b; } void Account::init(const char c[], char num[], int b) { customer = new char[strlen(c) +1]; strcpy(customer, c); cleanSpace(customer); //Copy 'num' to 'accountNumber'. strcpy(accountNumber, num); //Save 'b' as the 'balance'. balance = b; } void Account::cleanSpace( char s[]) { int i = 0, j = 0, // general loop index first = 0, // first non-whitespace character last = 0; // last non-whitespace character // loop to find the first non-whitespace character // and save its position as 'first'. for (i = 0; s[i] != '\0'; i++) { if (s[i] != 32 && s[i] != 9 && s[i] != 10) { first = i; break; } } // loop to find the last non-whitespace character // and save its position as 'last'. for (i = strlen(s) - 1; i > first; i--) { if (s[i] != 32 && s[i] != 9 && s[i] != 10) { last = i; break; } } // for loop to copy all characters in string 's' // between 'first' & 'last' to string 's'. for (i = 0; first <= last ; i++) { s[i] = s[first]; first++; } s[i] = '\0'; //for loop to find and replace white space characters //with spaces. for(i = 0; i < (strlen(s) + 1); i++) { if (s[i] >= 9 && s[i] <= 10) { s[i] = 32; } } //for loop to remove excess whitespace if there are //more than one consecutive whitespaces. i = 0; while (s[i] != '\0') { if(s[i] == 32 && s[i+1] == 32) { //Since excess white space was found, we shift the whole string //so as to take out the whitespace, regardless of what character //is being copied. for(j = i; s[j] != '\0'; j++) { s[j] = s[j+1]; } } // If whitespace was not found, we increase index 'i' and // start looking at the next character. else { i++; } } } void Account::changeCustomerInfo(const char c[]) { customer = new char[strlen(c) + 1]; strcpy(customer, c); //Call cleanSpace to clear whitespace cleanSpace(customer); } void Account::changeAccountNumber(const char num[]) { strcpy(accountNumber, num); } void Account::changeBalance(int b) { balance = b; } void Account::getAccountNumber(char st[]) { int i = 0, j = 0; while (customer[i+1] != ',') { st[j] = customer[i]; i++; j++; } st[j] = customer[i]; st[j+1] = '\0'; //Add null byte to the last element of the Array. } void Account::getLastName(char st[]) { int i = 0, j = 0; //local index //Copy the element of the customer array to st array until ',' is encountered. while (customer[i+1] != ',') { st[j] = customer[i]; i++; j++; } st[j] = customer[i]; st[j+1] = '\0'; //Add null byte to the last element of the Array. } void Account::getFirstName(char st[]) { int i = 0, j = 0; //local index //Search for the first ','. while (customer[i] != ',') { i++; } i++; //Copy the element of the customer array until the next ',' is encountered. while (customer[i+1] != ',') { st[j] = customer[i]; i++; j++; } st[j] = customer[i]; st[j+1] = '\0'; //Add null byte to the last element of the Array. } void Account::getCity(char st[]) { int i = 0, j = 0; //local index //Search for the first ','. while (customer[i] != ',') { i++; } //Search for the second ','. while (customer[i+1] != ',') { i++; } i = i + 2; //Copy the element of the customer array until the next ',' is encountered. while (customer[i+1] != ',') { st[j] = customer[i]; i++; j++; } st[j] = customer[i]; st[j+1] = '\0'; //Add null byte to the last element of the Array. } void Account::getPhoneNumber(char st[]) { int i = 0, j = 0; //local index //Search for the first ','. while (customer[i] != ',') { i++; } //Search for the second ','. while (customer[i+1] != ',') { i++; } i = i + 2; //Search for the third ','. while (customer[i+1] != ',') { i++; } i = i + 2; //Copy the element of the customer array until ';' is encountered. while (customer[i+1] != ';') { st[j] = customer[i]; i++; j++; } st[j] = customer[i]; st[j+1] = '\0'; //Add null byte to the last element of the Array. } const char* Account::getCustomer() const { return customer; } int Account::getBalance() const { return balance; } const char* Account::getAccountNumber() const { return accountNumber; } const Account& Account::operator=(const Account &right){ strcpy(customer, right.customer); strcpy(accountNumber, right.accountNumber); balance = right.balance; return *this; } Account::~Account(){ if (customer){ delete customer; } }