// Ray Torres

// Project

//

// main.cpp

 

#include "Customer.h"

#include "Bicycle.h"

#include "Rental.h"

#include  <iostream>

 

using namespace std;

 

int main() {

      Customer ray("Ray Torres");

 

      try {

            Bicycle daughterBike("F Child Bicycle 1", CHILD);

            Bicycle grandpaBike("M Adult Bicycle 1", SENIOR);

            Bicycle dadBike("M Adult Bicycle 2", ADULT);

 

            Rental weekend( grandpaBike, 2);

            Rental week( daughterBike, 5);

            Rental day( dadBike, 1);

            ray.addRental( weekend );

            ray.addRental( week );

            ray.addRental( day );

           

            cout << ray.statement() << endl;

      }

      catch ( InvalidPriceCodeException ex ) { // exception handler

            cout << "Exception occurred: " << ex.what() << '\n';

      }

 

      return 0;

}

 

 

// Bicycle.h

 

#ifndef BICYCLE_H

#define BICYCLE_H

#include <string>

#include "Price.h"

 

using namespace std;

 

enum {

      ADULT = 0,

      CHILD,

      SENIOR

};

 

class Bicycle {

public:

      Bicycle( const string& bikeId, int priceCode = ADULT );

      Bicycle( const Bicycle& );

      ~Bicycle() {

            delete price;

      }

      Bicycle& operator=( const Bicycle& bike );

      int getPriceCode() const;

      void setPriceCode( int priceCode );

      double getCharge( int daysRented ) const;

      string getId() const {

            return id;

      }

private:

      string id;

      Price* price;

};

 

class InvalidPriceCodeException {

public:

   InvalidPriceCodeException()

      : message( "Invalid Price Code Used" ) { }

   const char *what() const { return message; }

private:

   const char *message;

};

 

#endif // BICYCLE_H

 

 

// Customer.h

 

#ifndef CUSTOMER_H

#define CUSTOMER_H

#include <vector>

#include <string>

#include "Rental.h"

 

using namespace std;

 

class Customer {

public:

      Customer() {}

      explicit Customer( const string& name ) : custName( name ) {}

 

      void addRental( const Rental& arg ) {

            rentals.push_back( arg );

      }

      string getName() const {

            return custName;

      }

      string statement();

 

private:

      string custName;

      vector< Rental > rentals;

      double getTotalCharge();

};

 

#endif // CUSTOMER_H

 

 

// Rental.h

 

#ifndef RENTAL_H

#define RENTAL_H

#include "Bicycle.h"

 

class Rental {

public:

      Rental( const Bicycle& bicycle, int daysRented ) : bicycle( bicycle ), numOfDaysRented( daysRented ) {}

      int getDaysRented() const {

            return numOfDaysRented;

      }

      const Bicycle & getBicycle() const {

            return bicycle;

      }

      double getCharge() const;

 

private:

      Bicycle bicycle;

      int numOfDaysRented;

};

 

#endif // RENTAL_H

 

 

// Price.h

 

#ifndef PRICE_H

#define PRICE_H

 

class Price {

public:

      virtual ~Price() {}

      virtual int getPriceCode() const = 0;

      virtual double getCharge( int daysRented ) const = 0;

};

 

#endif // PRICE_H

 

 

// AdultPrice.h

 

#ifndef ADULTPRICE_H

#define ADULTPRICE_H

#include "Price.h"

 

class AdultPrice : public Price {

public:

      virtual int getPriceCode() const;

      virtual double getCharge( int daysRented ) const;

};

 

#endif // ADULTPRICE_H

 

 

// ChildPrice.h

 

#ifndef CHILDPRICE_H

#define CHILDPRICE_H

#include "Price.h"

 

class ChildPrice : public Price {

public:

      virtual int getPriceCode() const;

      virtual double getCharge( int daysRented ) const;

};

 

#endif // CHILDPRICE_H

 

 

// SeniorPrice.h

 

#ifndef SENIORPRICE_H

#define SENIORPRICE_H

#include "Price.h"

 

class SeniorPrice : public Price {

public:

      virtual int getPriceCode() const;

      virtual double getCharge( int daysRented ) const;

};

 

#endif // SENIORPRICE_H

 

 

// Bicycle.cpp

 

#include "Bicycle.h"

#include "AdultPrice.h"

#include "ChildPrice.h"

#include "SeniorPrice.h"

 

Bicycle::Bicycle( const string& bikeId, int priceCode ) : id( bikeId ), price( 0 ) {

      setPriceCode( priceCode );

}

 

Bicycle::Bicycle( const Bicycle& bicycle ) : id( bicycle.id ), price( 0 ) {

      setPriceCode( bicycle.getPriceCode() );

}

 

Bicycle& Bicycle::operator=( const Bicycle& bike ) {

      if ( this == &bike )

            return *this;

      id = bike.id;

      setPriceCode( bike.getPriceCode() );

      return *this;

}

 

int Bicycle::getPriceCode() const {

      return price->getPriceCode();

}

 

void Bicycle::setPriceCode( int priceCode ) {

      delete price;

      switch ( priceCode ) {

            case ADULT:

                  price = new AdultPrice;

                  break;

            case CHILD:

                  price = new ChildPrice;

                  break;

            case SENIOR:

                  price = new SeniorPrice;

                  break;

            default:

                  throw InvalidPriceCodeException();

      }

}

 

double Bicycle::getCharge( int daysRented ) const {

      return price->getCharge( daysRented );

}

 

 

// Customer.cpp

 

#include <vector>

#include <sstream>

#include "Customer.h"

 

string Customer::statement() {

      vector< Rental >::iterator iter = rentals.begin();

      vector< Rental >::iterator iter_end = rentals.end();

      ostringstream result;

      result << "Rental Bike Record for " << getName() << "\n";

      for ( ; iter != iter_end; ++iter ) {

            Rental each = *iter;

           

            // show figures for this rental

            result << "\t" << each.getBicycle().getId() << "\t$"

                  << each.getCharge() << "\n";

      }

     

      // add footer lines

      result << "Total charge is $" << getTotalCharge() << "\n";

      return result.str();

}

 

double Customer::getTotalCharge() {

      double result = 0;

      vector< Rental >::iterator iter = rentals.begin();

      vector< Rental >::iterator iter_end = rentals.end();

      for ( ; iter != iter_end; ++iter ) {

            Rental each = *iter;

            result += each.getCharge();

      }

      return result;

}

 

 

// Rental.cpp

 

#include "Rental.h"

 

double Rental::getCharge() const {

      return bicycle.getCharge( numOfDaysRented );

}

 

 

// AdultPrice.cpp

 

#include "AdultPrice.h"

#include "Bicycle.h"

 

int AdultPrice::getPriceCode() const {

      return ADULT;

}

 

double AdultPrice::getCharge( int daysRented ) const {

      return daysRented * 10;

}

 

 

// ChildPrice.cpp

 

#include "ChildPrice.h"

#include "Bicycle.h"

 

int ChildPrice::getPriceCode() const {

      return CHILD;

}

 

double ChildPrice::getCharge( int daysRented ) const {

      double result = 7;

      if ( daysRented > 4 )

            result += ( daysRented - 4 ) * 5 ;

      return result;

}

 

 

// SeniorPrice.cpp

 

#include "SeniorPrice.h"

#include "Bicycle.h"

 

int SeniorPrice::getPriceCode() const {

      return SENIOR;

}

 

double SeniorPrice::getCharge( int daysRented ) const {

      double result = 8;

      if ( daysRented > 1 )

            result += ( daysRented - 1 ) * 6 ;

      return result;

}

 

 

 

C:\Documents and Settings\ratorres\My Documents\C++ IV\project\Debug>project

Rental Bike Record for Ray Torres

        M Adult Bicycle 1       $14

        F Child Bicycle 1       $12

        M Adult Bicycle 2       $10

Total charge is $36

Hosted by www.Geocities.ws

1