This project is maintained by abrarShariar
View On Github




  Phone.h

This header file deifnes the class Phone



Brief Description:


- Creates a new Phone

- Methods to find and change a particular phone number

- Methods to add/update phone credit/balance

- A Call History containing all the calls made and their details


Data Members [private]

  • string phoneNumber
  • double phoneCredit

  • Member Functions [public]

  • Phone()   [default constructor]
  • Phone(int id)   [parameterize constructor]
  • void setPhoneNumber(intpersonId)
  • void setPhoneCredit(double phoneCredit)
  • void newPhoneCall(string num)
  • string getPhoneNumber()
  • double getPhoneCredit()
  • void newPhoneCall(string num)
  • int getCallHistorySize()
  • string getYear()
  • PhoneCall getCallHistory( int index)

  • Static Members [public]

  • string basePhoneNumber
  • int phoneCount
  • int getPhoneCount()

  • Containers

  • vector<PhoneCall> callHistory

  • Code

    /*
        This Class contains members and member functions for a Phone object
        Phone objects access PhoneCall members so, PhoneCall.h is included
         is introduced here, the following references contributed to this code:
    
        http://stackoverflow.com/questions/8221702/accessing-elements-of-a-vector-in-c
        http://www.cplusplus.com/reference/vector/vector/operator[]/
    */
    
    #include "PhoneCall.h"
    #include< vector >
    using namespace std;
    
    class Phone{
        private:
            string phoneNumber;
            double phoneCredit;
            vectorcallHistory;
        public:
            Phone();
            Phone(int id);
    
            void setPhoneNumber(int personId);
            void setPhoneCredit(double phoneCredit);
    
            string getPhoneNumber();
            double getPhoneCredit();
    
    
            static string basePhoneNumber;
            static int phoneCount;
            static int getPhoneCount();
    
            void newPhoneCall(string num);
            PhoneCall getCallHistory(int index);
            int getCallHistorySize();
            string getYear();
    };
    Phone::Phone(){
        this->phoneNumber="";
        this->phoneCredit=0;
    }
    
    Phone::Phone(int id){
            phoneCount++;
            this->setPhoneNumber(id);
            this->phoneCredit=0;
    }
    
    void Phone::setPhoneNumber(int personId){
        stringstream ss1,ss2,ss3,ss4,ss5;
        string temp;
        long int i_number;
    
        string number=basePhoneNumber+getYear();
        number=number+"000";
        ss1<>i_number;
    
        i_number=i_number+personId;
        ss2<>number;
    
        number=number+"00";
        ss3<>i_number;
    
        i_number=i_number+phoneCount;
        ss4<>number;
    
        int lnum=rand()%10;
        ss5<>temp;
        number="0"+number+temp;
    
        this->phoneNumber=number;
    }
    void Phone::setPhoneCredit(double phoneCredit){
            this->phoneCredit=phoneCredit;
    }
    
    void Phone::newPhoneCall(string num){
            PhoneCall call(num);
            double credit=this->getPhoneCredit()-call.getCallCost();
            this->setPhoneCredit(credit);
            this->callHistory.push_back(call);
    }
    
    string Phone::getPhoneNumber(){
            return this->phoneNumber;
    }
    double Phone::getPhoneCredit(){
            return this->phoneCredit;
    }
    
    //static members
    string Phone::basePhoneNumber="017";
    int Phone::phoneCount=0;
    int Phone::getPhoneCount(){
            return phoneCount;
    }
    
    PhoneCall Phone::getCallHistory(int index){
        return this->callHistory[index];
    }
    int Phone::getCallHistorySize(){
        return this->callHistory.size();              //returns the size of the callHistory vector
    }
    
    string Phone::getYear(){
            time_t clock=time(0);
            tm *now=localtime(&clock);
            int year=1900+(now->tm_year);
    
            string newYear="";
            for(int i=0;i<2;i++){
                stringstream ss;
                string stDigit;
                int digit=year%10;
                year=year/10;
                ss<>stDigit;
                newYear=stDigit+newYear;
            }
            return newYear;
    }