View Single Post
Old 02-10-2005, 12:31 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Here is my code using C++ only. You are mixing C with C++.
- You need to add a copy constructor.
- You need to add an overloaded operator=().
- You need to implement serial number for the movie class and the manager class.
Code:
#ifndef CUSTOMER_H
#define CUSTOMER_H

#include <string>
#include <iostream>

using std::ostream;

class Customer
{
public:
  Customer() :
   sName_(""), sCustCode_(""), Age_(0)
   {   }
   Customer(std::string n, std::string c, unsigned a) :
   sName_(n), sCustCode_(c), Age_(a)
   {   }
   //
   void setCustomer(std::string n, std::string c, int a)
   {
     sName_ = n;
     sCustCode_ =  c;
     Age_ = a;
   }
   // Too lazy to make the proper accessors.
   std::string& name()
   {
     return sName_;
   }
   std::string& code()
   {
     return sCustCode_;
   }
   unsigned& age()
   {
     return Age_;
   }
private:
  unsigned Age_;
  std::string sName_;
  std::string sCustCode_;
  static int cCount;
  friend ostream& operator<<(ostream& os, const Customer& cst);
};

ostream& operator<<(ostream& os, const Customer& cst)
{
  os << cst.sName_ << " " << cst.Age_ << " " << cst.sCustCode_;
  return os;
}


#endif //CUSTOMER_H
Code:
#ifndef MOVIE_H
#define MOVIE_H

#include <string>
#include <iostream>

using std::ostream;

class Movie
{
public:
  static int mCount;
  Movie() :
    sTitle_(""), sDesc_(""), serialNum_(0)
    {  }
  Movie(std::string t, std::string d, int s) :
    sTitle_(t), sDesc_(d), serialNum_(s)
    {  }
    //
    std::string getTitle() const
    {
      return sTitle_;
    }
    //
    void setMovie(std::string t, std::string d)
    {
      sTitle_ = t;
      sDesc_ = d;
    }
private:
  std::string sTitle_;
  std::string sDesc_;
  int serialNum_;
  friend ostream& operator<<(ostream& os, const Movie& mvy);
};

ostream& operator<<(ostream& os, const Movie& mvy)
{
  os << mvy.sTitle_ << " " << mvy.sDesc_ << " " << mvy.serialNum_;
  return os;
}

#endif //MOVIE_H
Code:
#ifndef RENTALMANAGER_H
#define RENTALMANAGER_H

#include "Customer.h"
#include "Movie.h"
#include <string>
#include <iostream>
#include <climits>

using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::getline;

class RentalManager
{
public:
  RentalManager() : rc_(0), rcnSize_(0)
  { }
  ~RentalManager()
  {
    delete[] rc_;
  }
  //
  void add_new_record()
  {
    resize_record_with_one();
    
    cout<<"Customer Code: ";
    string c;
    getline(cin, c);
    cout<<"Title: ";
    string t;
    getline(cin, t);
    cout<<"DVD (d) or VCD (v): ";
    string d;
    cin>>d;
    cin.ignore(INT_MAX, '\n');
    
    //Does customer code (and therefore, is he a member) exist?
    unsigned i = 0;
    for(; i < rcnSize_; ++i)
    {
      if(rc_[i].Customer_.code() == c)
      {
        break; //It does exist.
      }
    }

    if(i ==  rcnSize_) //Customer Code does not exist.
    {
      string n;
      unsigned a;
      cout<<"NOTIFICATION: Customer is not a member."<<endl;
      cout<<"Customer Name: ";
      getline(cin, n);
      cout<<"Customer Age: ";
      cin>>a;
      cin.ignore(INT_MAX, '\n');
      rc_[rcnSize_-1].Customer_.setCustomer(n, c, a);
    }
    else //Customer Code exists.
    {
      rc_[rcnSize_-1].Customer_.code() = c;
      //Customer is member already so let's find his name/age by Code.
      for(unsigned i = 0; i < rcnSize_; ++i)
      {
        if(rc_[i].Customer_.code() == c)
        {
          rc_[rcnSize_-1].Customer_.name() = rc_[i].Customer_.name();
          rc_[rcnSize_-1].Customer_.age() = rc_[i].Customer_.age();
          break;
        }
      }
    }
    rc_[rcnSize_-1].Movie_.setMovie(t, d);
  }
  void view_all_actions() const
  {
    for(unsigned i = 0; i < rcnSize_; ++i)
    {
      cout<<"Code: "<<rc_[i].Customer_.code()<<endl;
      cout<<"Name: "<<rc_[i].Customer_.name()<<endl;
      cout<<"Age: "<<rc_[i].Customer_.age()<<endl;
      cout<<"Movie Data (title, descr., serial #): "<<rc_[i].Movie_<<endl<<endl;
    }
  }
  //
private:
  struct Record
  {
    Customer Customer_;
    Movie    Movie_;
  };
  Record* rc_;
  unsigned rcnSize_;
  void resize_record_with_one()
  {
    Record* temp = new Record[rcnSize_+1];
    for(int i = 0; i < rcnSize_; ++i)
    {
      temp[ i ] = rc_[ i ];
    }
    delete[] rc_;
    rc_ = temp;
    ++rcnSize_;
  }
};

#endif // RENTALMANAGER_H
Code:
#include "RentalManager.h"
#include <iostream>


using namespace std;

int main(int argc, char *argv[])
{
  RentalManager RM;
  RM.add_new_record();
  cout<<endl;
  RM.add_new_record();
  
  cout<<endl;
  cout<<"*** current status ***"<<endl;
  RM.view_all_actions();
  cout<<endl;
  
  return 0;
}
__________________
Valmont is offline   Reply With Quote