All pastes #1272032 Raw Edit

Bank Source

public cpp v1 · immutable
#1272032 ·published 2008-12-01 05:34 UTC
rendered paste body
/*******************************************************                                                     **  Professor                 Lloyd Parker             **  Course:                   OOP244                   **  Assignment:               3                        **  Student:                  Siddharth Dahiya         **  Student #:                016-194-086              **  Student userid:           sdahiya1                 **  This File Name:           Bank.cpp                 **                                                     *********************************************************                                                     **  This assignment represents my own work in          **  accordance with Seneca Academic Policy             **                                                     **       Signature: Siddharth Dahiya                   *******************************************************/// Source code for Bank class.#include <stdio.h>#include <stdlib.h>#include <string.h>#include <new>#include <iostream>#include <string>#include "Bank.h"using namespace std;#define ANUM 1#define BAL 2#define LNAME 3#define FNAME 4#define CITY 5#define PHONE 6#define CUSTINFO 7Bank::Bank(){	//set File pointer to zero	fp = NULL;	//set array size to zero	size = 0;	//set array size to zero	arraySize = 0;	//set savings to zero	savings = NULL;	}Bank::Bank (char strData[]){	{		int currentObject = 0;		fp = NULL;		size = 0;		arraySize = 0;		savings = NULL;		fp =  fopen("a34.dat", "r");		if (fp)			{ 				// Read and convert the first 2 characters to integers				char c[3];				c[0] = c[1] = 0;				//Set Array size as 'n', the first character				arraySize = c[0];				//gets 2 characters.				fgets(c, 3, fp); 				//remove the colon at the end				fgetc(fp); 				int totalCount = atoi(c);				//Allocate memory for n+10 objects				savings = new Account[totalCount + 10];				long indexStart, indexFinish, i, count;				char *strData;				while (!feof(fp))					{						indexStart = 0;						indexFinish = 0;						i = 0;						count = 0;						indexStart = ftell(fp);						indexFinish = indexStart;						while (fgetc(fp) != ';')  							{								++indexFinish;							}						//Return to the Start of Data						fseek(fp, indexStart, SEEK_SET);         						//Insert space for '\0' character at the end						strData = new char[indexFinish - indexStart + 1];						for (i = 0; i < (indexFinish - indexStart); i++)							{                              								strData[count] = fgetc(fp);								++count;							}						//Add null byte at end						strData[count] = '\0';						//Initialize the current Savings object from the strData: record						savings[size].changeAccountNumber(getPart(strData, ANUM));												//Get the balance, convert from string to int					    int newBal;                        string buffer = getPart(strData, BAL);                        newBal=atoi(buffer.c_str());                        savings[size].changeBalance(newBal);                                                //get the customer info, save all info as 1 string, change customer info                        savings[size].changeCustomerInfo(getPart(strData, CUSTINFO));						size++;						// Remove Semicolon at the end						fgetc(fp);  												//enpty the strData array to scan the next record						delete [] strData;                   				}			}		}	}int Bank::grow(int s){			int sizeOld = size;    if(savings != NULL){        Account *newSavingsArray = new Account[arraySize + s];        for ( int i = 0; i < arraySize; i++ ){              newSavingsArray[i] = savings[i];            }        delete savings;        savings = newSavingsArray;        //if(savings.GetSize() > arraySize){        //    arraysize = arraySize + s;        //    } else {        //        arraySize = size = 0;        //    }        //}             }else if (savings == NULL){            Account *savings = new Account[s];            arraySize = s;        }	return arraySize;	}	Bank::~Bank(){    if(savings){        delete savings;        }}char * Bank::getPart(char st[], int comma){          int i = 0, j = 0, n = 0;     int commaPos[6];     int stringSize = strlen(st) + 1;     char *tempArray = new char[stringSize];               //get the positions of the commas and the colon          for (i = 0, j = 0; i < strlen(st); i++){        if(tempArray[i] == ','){            commaPos[j] = i;            j++;        } else if (tempArray[i] == ';'){            commaPos[5] = i;            break;        }     }          if (comma == 1){        for(n = 0; n < commaPos[0]; i++){            st[n] = tempArray[n];            }                 } else if (comma == 7) {        for (n = commaPos[1] +1; n < commaPos[5]; i++){            st[n] = tempArray[n];        }          } else {        for (n = (commaPos[comma-2] + 1); n < commaPos[comma - 1]; i++){            st[n] = tempArray[n];            }     }          delete tempArray;          return st;        }        const char* Bank::operator+=(const char copyTo[]){	int newBal;	char tempArray[1000];	strcpy(tempArray, copyTo);    string buffer = getPart(tempArray, BAL);    newBal=atoi(buffer.c_str());    savings[size].changeBalance(newBal);	savings[size].changeAccountNumber(getPart(tempArray, ANUM));	savings[size].changeCustomerInfo(getPart(tempArray, CUSTINFO));		return copyTo;	}int Bank::changeCustomer( const char c[], int pos ){    if (pos < size){        savings[pos].changeCustomerInfo(c);        return true;    } else {        return false;        }}int Bank::changeAccountNumber(const char num[], int pos) {    if(pos < size){        savings[pos].changeAccountNumber(num);        return true;    } else {        return false;    }}int Bank::changeBalance(int b, int pos){    if(pos < size){        savings[pos].changeBalance(b);        return true;    } else {        return false;    }}void Bank::getLastName(char st[], int pos){    if (pos < size){        savings[pos].getLastName(st);    } else {    st[0] = NULL;    }}void Bank::getFirstName(char st[], int pos){    if(pos < size){        savings[pos].getFirstName(st);    } else {        st[0] = NULL;    }}void Bank::getCity(char st[], int pos){    if(pos < size){        savings[pos].getCity(st);        } else {            st[0] = NULL;        }    }void Bank::getPhoneNumber(char st[], int pos){    if(pos < size){        savings[pos].getPhoneNumber(st);        } else {            st[0] = NULL;        }    }const char * Bank::getCustomer(int pos) const{    if (pos < size){        return savings[pos].getCustomer();        } else {            return "";        }    }const char * Bank::getAccountNumber(int pos) const{    if (pos < size){        return savings[pos].getAccountNumber();        } else {            return "";        }    }int Bank::getBalance(int pos) const{    if (pos < size){        return savings[pos].getBalance();        } else {            return 0;        }    }int Bank::getBalance(char st[]) const{    int i = 0, notfound = 0;    for (i = 0; i <= size; i++){        if (savings[i].getAccountNumber() == st){            return savings[i].getBalance();            break;            } else {                notfound++;            }        }    if (notfound >= size){        return 0;        }    }int Bank::getArraySize() const{    return arraySize;    }int Bank::getSize() const{    return size;    }int Bank::total(){    int i = 0;    long double totalBalance = 0;    for (i = 0; i <= size; i++){        totalBalance = totalBalance + savings[i].getBalance();        }    return totalBalance;    }FILE * Bank::setFp(const char fileName[]){    if(fp != NULL){        fclose(fp);        }        fp = fopen(fileName, "r");    return fp;    }Bank::Bank(const Bank& inBank) {    savings[size].changeCustomerInfo(inBank.getCustomer(size));    savings[size].changeAccountNumber(inBank.getAccountNumber(size)); //Set accountNumber to all zeroes.    savings[size].changeBalance(inBank.getBalance(size));    }const Bank& Bank::operator =(const Bank &right){    savings[size].changeCustomerInfo(right.getCustomer(size));    savings[size].changeAccountNumber(right.getAccountNumber(size)); //Set accountNumber to all zeroes.    savings[size].changeBalance(right.getBalance(size));    return *this;    }