We help IT Professionals succeed at work.
Get Started

C++ Class

claracruz
claracruz asked
on
535 Views
Last Modified: 2012-06-21
G'day experts,

Please I need help with Classes in C++, this is my first time working with them, I am having trouble understaning its implementation.

Please have a lok at the following code, this is in my customer.h file;
class Customer {

      int prev; // previous reading
      int cur; // current reading
      char type; // type of customer (individual=I, business=B)
      char name[25];
      char address[50];
      char telephone[15];
      int lastMeterReading;
      int AccountNumber, confirm;
      ofstream outCustomer, outAccountNum; //output file
      ifstream inAccountNum; //input file

      /*the below method is private and used only from within this
       class and transparent to oustide function*/
      bool stringCheck(char*,int);

public:
      void add(int);
      bool compare(char*);
      void display();
      char* returnName();
      
unsigned int GasUsage()
      {
            // for wrap-around counter.
            // 33000 is the wrap-around point
            if(prev>cur)
                  return 33000-(prev-cur);
            else
                  return cur-prev;
      }

      float AmountDue()
      {
            if(type=='I')
            {
                  return float(GasUsage()*0.40);
            }
            else if(type=='B')
            {
                  return float(GasUsage()*0.36);
            }
            else // type=0 (error)
            {
                  return 0;
            }
      }
      void Display()
      {
            cout << "Customer name : " << name << '\n';
            cout << "Gas usage : " << GasUsage() << '\n';
            
            // check if there is an error in customer code
            if(type=='I'||type=='B')
            {
                  cout << "Expanded rate : " << type << '\n';
                  cout << "Amount due : " << AmountDue() << '\n';
            }
            else // cust[a].type=0 (error)
            {
                  cout << "***ERROR***\n";
            }
      }

};

void Customer::add(int flag)
{
      
      while (!cin.get()) {};

      if (flag==1)
      {

            outCustomer.open("customer.dat", ios::app); // opens file in append mode.
            
      //generate new account number
            inAccountNum.open("account.dat", ios::in);//assign file to input stream from account number


            if (inAccountNum.is_open())
            {
                  inAccountNum >> AccountNumber; //get account number from account.dat
                  inAccountNum.close();
            }
            else
            {
                  cout << "the file has not been open";
            }

            AccountNumber = AccountNumber + 1;//add one to account number
            outAccountNum.open("account.dat");
            outAccountNum << AccountNumber;//replace account number in account.dat with new figure for future use
            outAccountNum.close();

            //Get customer details, ask for input until acceptable input is received.

            do
            {
                  cout << "\n Enter customer's full name : ";
                  cin.getline(name,1000,'\n');
                  strupr(name);
            }while (!stringCheck(name,1));//checks that acceptable input is received.

            do
            {
                  cout << "\n Enter customer's address : ";
                  cin.getline(address,1000,'\n');
                  strupr(address);
            }while(!stringCheck(address,2));


            do
            {
                  cout << "\n Enter customer's telephone number : ";
                  cin.getline(telephone,1000,'\n');
            }while (!stringCheck(telephone,3));

            do
            {
                  cout << "\n Enter customer's current meter reading : ";
                  cin >> lastMeterReading;
            }while (33000 <= (lastMeterReading));

            do
            {
            cout << "\n Enter customer type(I=individual B= business) : ";
            cin >> type;
            }while (type > 'I');

            
            //Confirm Information
            cout << "\n" << "\n"<< "Confirm Customer Details (Enter 1. to confirm new account or 2. to cancel transaction);";
            cin >> confirm;
            cout << "\n";


            switch(confirm)
            {
 
            //save customer details to file and display ID, Gas meter Reading and Customer type.
            system("CLS");
            case 1: cout << "     New Account Created for:" << " " << name << "\n";
            
            cout <<"\n     ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»";
            cout <<"\n     º                                                    º";
            cout <<"\n     º CUSTOMER ASSIGNED ID:"<<" "<<AccountNumber<<"           ";
            cout <<"\n     º Gas Meter Reading Set To:"<<" "<<lastMeterReading<<"    ";
            
            if (type == 'I' || type == 'i')
            {
            cout <<"\n     º ACCOUNT TYPE CREATED: INDIVIDUAL CUSTOMER ACCOUNT        ";
            }
            else if (type == 'B'|| type =='b')
            {
                  cout <<"\n     º ACCOUNT TYPE CREATED: BUSINESS CUSTOMER ACCOUNT      ";
            }

            else
            {
                  cout <<"\n     º ACCOUNT TYPE CREATED: ***ERROR***     ";
            }
            cout <<"\n     º                                                    º";
            cout <<"\n     ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n";


            outCustomer << AccountNumber << '\t' << name << '\t' << address << '\t' << telephone << '\t'
            << lastMeterReading<< '\t' << type << '\n';
            outCustomer.close();
            cout << "\n"<< "     Details saved to Customer file." << "\n";
                  break;
      
                  //cancel transaction
            case 2: cout << "Transaction Cancelled!!" << "\n";
                  break;
                  default: cout << "Invalid Option;";
            }
                        
      }
      else //if editing customer information
      {
            
            do
            {
                  cout << "\n Enter new address for customer : ";
                  cin.getline(address,50,'\n');
                  strupr(address);
            }while(!stringCheck(address,2));


            do
            {
                  cout << "\n Enter new telephone number for customer : ";
                  cin.getline(telephone,15,'\n');
            }while (!stringCheck(telephone,3));
      }
}


//will be used to search for customer in customer file by name.
bool Customer::compare(char *string)
{
      unsigned int length=strlen(string),i,same=0;
      
      
      for (i=0;i<length;i++)
      {
            if (string[i] == name[i])
                  same++;
            else
                  same = 0;
      }

      if (same==length)
            return true;
      else
            return false;
}


//this function used to validate user inputs when creating new customer account.
bool Customer::stringCheck(char* s,int n)
{
      unsigned int i;
      
      if (n==4)// Validate customer type input, check if type is empty string
      {                              
            if (strlen(s)==0)
            {
                  cout << "\n(Please Enter [I] for individual or [B] for business) : " << "\n\n";
                  return false;
            }
            else
                  return true;
      }

      if (n==1)// Validate customer name input, check that string not empty or over 25 charcters
      {
                              
            if (strlen(s)==0 || strlen(s) > 25)
            {
                  cout << "\nName cannot be empty or more than 25 characters";
                  return false;
            }
            else
                  return true;
      }

      if (n==2)// validate customer address input
      {
                              
            if (strlen(s)==0 || strlen(s) > 50)
            {
                  cout << "\nAddress cannot be empty or more than 50 characters";
                  return false;
            }
            else
                  return true;
      }



      else if (n==3)// validate customer telephone number, ensure string not emapty and values are numeric
      {
            for (i = 0; i < strlen(s); i++)
            {
                  if (isalpha(s[i]))
                  {
                        cout << "\nPlease enter Numeric Values Only";
                        return false;
                  }
                  
            }

            if (strlen(s)==0 || strlen(s) > 15)
            {
                  cout << "\nTelephone cannot be empty or more than 15 characters\n";
                  return false;
            }
            else
                  return true;
      }
      else
            return true;

}


void Customer::display(void)
{
      cout << setiosflags(ios::left) << setw(20)<<name;
      cout << setw(25)<<address<<setw(13)<<telephone<<setw(22)<<lastMeterReading<<"\n";
}

char* Customer::returnName(void)
{
      return name;
}



This code is my class and I call it from my .cpp file.
THE QUESTION:
1.  please go through the code and let me know if this is way too much detail to be including in a class. I am not able to grasp the level of info that needs to be in a class. Also I am very new to c++ so some of the code might not be great, please point these out and explain what am doing wrong and how to correct this error.

2.  If this code does not a Class make, please advise me on what am doing wrong, and explain with as much detail as possible.

3. any additional refernce to good c++ class tutorials is appreciated.

thank you
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE