Car Rental Order Processing Exercise |
|
Header File: car.h
#if !defined CAR_H #define CAR_H enum TCarType { ctEconomy = 1, ctCompact, ctStandard, ctFullSize, ctMiniVan, ctSUV }; class Car { public: Car(); Car(char *MM, int Y, int Cat); virtual ~Car(); void setMakeModel(const char *mm); char* getMakeModel() const; void setCarYear(const int y); int getCarYear() const; void setCategory(const int c); int getCategory() const; private: char* MakeModel; int CarYear; int Category; }; #endif // CAR_H |
Source File: car.cpp
// Car.cpp: implementation of the Car class. #include <iostream> #include <string> using namespace std; #include "Car.h" //-------------------------------------------------------------------- Car::Car() { setMakeModel("No Car Selected"); setCarYear(2000); setCategory(1); } //-------------------------------------------------------------------- Car::Car(char *MM, int Y, int Cat) { setMakeModel(MM); setCarYear(Y); setCategory(Cat); } //-------------------------------------------------------------------- Car::~Car() { // delete [] MakeModel; } //-------------------------------------------------------------------- void Car::setMakeModel(const char *m) { MakeModel = new char[strlen(m) + 1]; strcpy(MakeModel, m); } //-------------------------------------------------------------------- char* Car::getMakeModel() const { return MakeModel; } //-------------------------------------------------------------------- void Car::setCarYear(const int y) { CarYear = y; } //-------------------------------------------------------------------- int Car::getCarYear() const { return CarYear; } //-------------------------------------------------------------------- void Car::setCategory(const int c) { Category = c; } //-------------------------------------------------------------------- int Car::getCategory() const { return Category; } //-------------------------------------------------------------------- |
Header File: customer.h
#if !defined CUSTOMER_H #define CUSTOMER_H class Customer { private: char* FirstName; char* LastName; char* Address; char* City; char* State; long ZIPCode; public: void setFirstName(const char *FN); char* getFirstName() const { return FirstName; } void setLastName(const char *LN); char* getLastName() const { return LastName; } char* FullName() const; void setAddress(const char *Adr); char* getAddress() const { return Address; } void setCity(const char *CT); char* getCity() const { return City; } void setState(const char *St); char* getState() const { return State; } void setZIPCode(const long ZIP); long getZIPCode() const { return ZIPCode; } Customer(); Customer(char *FName, char *LName, char *Adr, char *Ct, char *St, long ZIP); Customer(const Customer &Pers); Customer(char * FName, char * LName); ~Customer(); }; #endif // CUSTOMER_H |
Source File: customer.cpp
//--------------------------------------------------------------------------- #include <iostream> using namespace std; #pragma hdrstop #include "Customer.h" //--------------------------------------------------------------------------- char *Customer::FullName() const { char *FName = new char[40]; strcpy(FName, FirstName); strcat(FName, " "); strcat(FName, LastName); return FName; } //--------------------------------------------------------------------------- Customer::Customer() : ZIPCode(0) { FirstName = new char[20]; strcpy(FirstName, "John"); LastName = new char[20]; strcpy(LastName, "Doe"); Address = new char[40]; strcpy(Address, "123 Main Street Apt A"); City = new char[32]; strcpy(City, "Great City"); State = new char[30]; strcpy(State, "Our State"); } //--------------------------------------------------------------------------- Customer::Customer(char * FName, char * LName) : ZIPCode(0) { FirstName = new char[strlen(FName) + 1]; strcpy(FirstName, FName); LastName = new char[strlen(LName) + 1]; strcpy(LastName, LName); Address = new char[40]; strcpy(Address, "123 Main Street Apt A"); City = new char[32]; strcpy(City, "Great City"); State = new char[30]; strcpy(State, "Our State"); } //--------------------------------------------------------------------------- Customer::Customer(char *FName, char *LName, char *Adr, char *Ct, char *St, long ZIP) : ZIPCode(ZIP) { FirstName = new char[strlen(FName) + 1]; strcpy(FirstName, FName); LastName = new char[strlen(LName) + 1]; strcpy(LastName, LName); Address = new char[40]; strcpy(Address, Adr); City = new char[32]; strcpy(City, Ct); State = new char[30]; strcpy(State, St); } //--------------------------------------------------------------------------- Customer::Customer(const Customer &Pers) : ZIPCode(Pers.ZIPCode) { FirstName = new char[strlen(Pers.FirstName) + 1]; strcpy(FirstName, Pers.FirstName); LastName = new char[strlen(Pers.LastName) + 1]; strcpy(LastName, Pers.LastName); Address = new char[strlen(Pers.Address) + 1]; strcpy(Address, Pers.Address); City = new char[strlen(Pers.City) + 1]; strcpy(City, Pers.City); State = new char[strlen(Pers.State) + 1]; strcpy(State, Pers.State); } //--------------------------------------------------------------------------- void Customer::setFirstName(const char *FN) { strcpy(FirstName, FN); } //--------------------------------------------------------------------------- void Customer::setLastName(const char *LN) { strcpy(LastName, LN); } //--------------------------------------------------------------------------- void Customer::setAddress(const char *Adr) { strcpy(Address, Adr); } //--------------------------------------------------------------------------- void Customer::setCity(const char *CT) { strcpy(City, CT); } //--------------------------------------------------------------------------- void Customer::setState(const char *St) { strcpy(State, St); } //--------------------------------------------------------------------------- void Customer::setZIPCode(const long ZIP) { ZIPCode = ZIP; } //--------------------------------------------------------------------------- Customer::~Customer() { delete [] FirstName; delete [] LastName; delete [] Address; delete [] City; delete [] State; } //--------------------------------------------------------------------------- |
Header File: Invoice.h
#if !defined INVOICE_H #define INVOICE_H #include "Customer.h" #include "Car.h" #include "RentDate.h" class Invoice { public: Invoice(); virtual ~Invoice(); Customer CustomerRegistration(); void CustomerInformation(const Customer& Pers); Car CarSelection(); void CarSelected(const Car& Vehicle); double CalculatePrice(const Car& Vehicle, double& Rate, int &Days); void setMileage(const long g); long getMileage() const; void setTankLevel(const char *v); char* getTankLevel() const; void setCarCondition(const char *c); char* getCarCondition() const; void ProcessOrder(); void CarExamination(); void ShowOrder(); private: long Mileage; char *TankLevel; char *CarCondition; }; #endif // INVOICE_H |
Source File: Invoice.cpp
// Invoice.cpp: implementation of the Invoice class. // ////////////////////////////////////////////////////////////////////// #include <iostream> #include <iomanip> #include <string> using namespace std; #include "Invoice.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// Invoice::Invoice() { setTankLevel("Empty"); setCarCondition("Good"); } //-------------------------------------------------------------------- Invoice::~Invoice() { } //-------------------------------------------------------------------- Customer Invoice::CustomerRegistration() { char FName[20], LName[20], Addr[40], CT[32], St[30]; long ZC = 0; cout << "Enter Customer Information\n"; cout << "First Name: "; cin >> FName; cout << "Last Name: "; cin >> LName; cout << "Address: "; cin >> ws; cin.getline(Addr, 40); cout << "City: "; cin.getline(CT, 32); cout << "State: "; cin.getline(St, 30); cout << "Zip Code: "; cin >> ZC; Customer Cust(FName, LName, Addr, CT, St, ZC); return Cust; } //-------------------------------------------------------------------- Car Invoice::CarSelection() { int CarType, ModelChosen; char strCarSelected[20]; int CarSelectedYear = 2000; cout << "What type of car would you like to rent?"; do { cout << "\n1 - Economy | 2 - Compact | 3 - Standard" << "\n4 - Full Size | 5 - Mini Van | 6 - Sports Utility"; cout << "\nYour Choice: "; cin >> CarType; if( CarType < 1 || CarType > 6 ) cout << "\nPlease type a number between 1 and 6"; } while(CarType < 1 || CarType > 6); switch(CarType) { case ctEconomy: cout << "\nFor the Economy type, we have:" << "\n1 - Daewoo Lanos | 2 - Cheverolet Metro"; cout << "\nWhich one would you prefer? "; cin >> ModelChosen; if(ModelChosen == 1) { strcpy(strCarSelected, "Daewoo Lanos"); CarSelectedYear = 1999; } else { strcpy(strCarSelected, "Chevrolet Metro"); CarSelectedYear = 1998; } break; case ctCompact: cout << "\nFor the Compact type, we have:" << "\n1 - Chevrolet Cavalier | 2 - Dogde Neon" << "\n3 - Nissan Sentra | 4 - Toyota Corolla"; cout << "\nWhich one would you prefer? "; cin >> ModelChosen; if(ModelChosen == 1) { strcpy(strCarSelected, "Chevrolet Cavalier"); CarSelectedYear = 1999; } else if(ModelChosen == 2) { strcpy(strCarSelected, "Dodge Neon"); CarSelectedYear = 2001; } else if(ModelChosen == 3) { strcpy(strCarSelected, "Nissan Sentra"); CarSelectedYear = 1998; } else { strcpy(strCarSelected, "Toyota Corrolla"); CarSelectedYear = 2002; } break; case ctStandard: cout << "\nFor the Standard type, we have:" << "\n1 - Chevrolet Monte Carlo | 2 - Toyota Camri"; cout << "\nWhich one would you prefer? "; cin >> ModelChosen; if(ModelChosen == 1) { strcpy(strCarSelected, "Chevrolet Monte Carlo"); CarSelectedYear = 2000; } else { strcpy(strCarSelected, "Toyota Camri"); CarSelectedYear = 1998; } break; case ctFullSize: cout << "\nFor the Full Size type, we have:" << "\n1 - Chrysler 300M | 2 - Buick Century | 3 - Infinity I30"; cout << "\nWhich one would you prefer? "; cin >> ModelChosen; if(ModelChosen == 1) { strcpy(strCarSelected, "Chrysler 300M"); CarSelectedYear = 2000; } else if(ModelChosen == 2) { strcpy(strCarSelected, "Buick Century"); CarSelectedYear = 1999; } else { strcpy(strCarSelected, "Infinity I30"); CarSelectedYear = 2003; } break; case ctMiniVan: cout << "\nFor the Mini-Van type, we have:" << "\n1 - Dodge Caravan | 2 - Dodge Caravan" << "\n3 - Pontiac Montana | 4 - Pontiac Montana | 5 - Chevrolet Astro Van"; cout << "\nWhich one would you prefer? "; cin >> ModelChosen; if(ModelChosen == 1) { strcpy(strCarSelected, "Dodge Caravan"); CarSelectedYear = 2001; } else if(ModelChosen == 2) { strcpy(strCarSelected, "Dodge Caravan"); CarSelectedYear = 2003; } else if(ModelChosen == 3) { strcpy(strCarSelected, "Pontiac Montana"); CarSelectedYear = 2003; } else { strcpy(strCarSelected, "Chevrolet Astro Van"); CarSelectedYear = 2000; } break; case ctSUV: cout << "\nFor the Sport Utility type, we have:" << "\n1 - GMC Jimmy | 2 - Jeep Cherokee" << "\n3 - Chevrolet Blazer | 4 - Toyota Pathfinder"; cout << "\nWhich one would you prefer? "; cin >> ModelChosen; if(ModelChosen == 1) { strcpy(strCarSelected, "GMC Jimmy"); CarSelectedYear = 1998; } else if(ModelChosen == 2) { strcpy(strCarSelected, "Jeep Cherokee"); CarSelectedYear = 2003; } else if(ModelChosen == 3) { strcpy(strCarSelected, "Chevrolet Blazer"); CarSelectedYear = 2001; } else { strcpy(strCarSelected, "Toyota Pathfinder"); CarSelectedYear = 2000; } break; } Car Selected(strCarSelected, CarSelectedYear, CarType); return Selected; } //-------------------------------------------------------------------- void Invoice::setMileage(const long g) { Mileage = g; } //-------------------------------------------------------------------- long Invoice::getMileage() const { return Mileage; } //-------------------------------------------------------------------- void Invoice::setTankLevel(const char *v) { TankLevel = new char[strlen(v) + 1]; strcpy(TankLevel, v); } //-------------------------------------------------------------------- char* Invoice::getTankLevel() const { return TankLevel; } //-------------------------------------------------------------------- void Invoice::setCarCondition(const char *c) { CarCondition = new char[strlen(c) + 1]; strcpy(CarCondition, c); } //-------------------------------------------------------------------- char* Invoice::getCarCondition() const { return CarCondition; } //-------------------------------------------------------------------- void Invoice::ProcessOrder() { int NbrOfDays;; double Rate, TotalPrice; // Enter Customer Information Customer Person = CustomerRegistration(); cout << "\nProcess Car Information\n"; Car Driving = CarSelection(); TotalPrice = CalculatePrice(Driving, Rate, NbrOfDays); CarExamination(); // This function works for both Borland C++ Builder and MSVC system("cls"); cout << " - Bethesda Car Rental -"; cout << "\n============================="; CustomerInformation(Person); cout << "\n------------------------------"; CarSelected(Driving); cout << setiosflags(ios::fixed) << setprecision(2); cout << "\n------------------------------"; cout << "\nCar Mileage: " << getMileage(); cout << "\nCondition: " << getCarCondition(); cout << "\nTank Level: " << getTankLevel(); cout << "\n# of Days: " << NbrOfDays; cout << "\n------------------------------"; cout << "\nRate: $" << Rate; cout << "\nTotal Price: $" << TotalPrice; cout << "\n==============================\n"; } //-------------------------------------------------------------------- void Invoice::CustomerInformation(const Customer& Pers) { cout << "\nEmployee Identification"; cout << "\nFull Name: " << Pers.FullName(); cout << "\nAddress: " << Pers.getAddress(); cout << "\nCity: " << Pers.getCity() << ", " << Pers.getState() << " " << Pers.getZIPCode(); } //-------------------------------------------------------------------- void Invoice::CarSelected(const Car& Vehicle) { cout << "\nModel: " << Vehicle.getMakeModel(); cout << "\nYear: " << Vehicle.getCarYear(); } //-------------------------------------------------------------------- double Invoice::CalculatePrice(const Car& Vehicle, double& DayRate, int &NumberOfDays) { // char WeekEndResponse;// Does the customer rent the car for the week-end? double OneDayRate, // If renting for less than 5 days including week-end WeekDay, // If renting for at least 5 days, regardless of the days WeekEnd = 0;// If renting for more than two days from Friday to monday //double DayRate; // Rate applied based on the number of days double TotalRate; switch(Vehicle.getCategory()) { case ctEconomy: DayRate = 24.95; OneDayRate = 29.95; WeekDay = 24.95; WeekEnd = 19.95; break; case ctCompact: DayRate = 34.95; OneDayRate = 39.95; WeekDay = 34.95; WeekEnd = 25.95; break; case ctStandard: DayRate = 38.95; OneDayRate = 49.95; WeekDay = 38.95; WeekEnd = 32.95; break; case ctFullSize: DayRate = 44.95; OneDayRate = 69.95; WeekDay = 44.95; WeekEnd = 35.95; break; case ctMiniVan: DayRate = 54.95; OneDayRate = 89.95; WeekDay = 54.95; WeekEnd = 42.95; break; case ctSUV: DayRate = 64.95; OneDayRate = 89.95; WeekDay = 64.95; WeekEnd = 50.95; break; } cout << "\nHow many days? "; cin >> NumberOfDays; TotalRate = DayRate * NumberOfDays; return TotalRate; } //-------------------------------------------------------------------- void Invoice::CarExamination() { char Cond; int GasLevel; cout << "Rate the car's condition (e=Excellent/g=Good/d=Driveable): "; cin >> Cond; if( Cond == 'e' || Cond == 'E' ) strcpy(CarCondition, "Excellent"); else if( Cond == 'g' || Cond == 'G' ) strcpy(CarCondition, "Good"); else if( Cond == 'd' || Cond == 'D' ) strcpy(CarCondition, "Driveable"); else strcpy(CarCondition, "Can't Decide"); cout << "Enter the car mileage: "; cin >> Mileage; do { cout << "Gas level in the tank" << "\n1 - Considered Empty" << "\n2 - 1/4 Full" << "\n3 - Half Full" << "\n4 - 3/4 Full" << "\n5 - Full Tank"; cout << "\nEnter the gas level: "; cin >> GasLevel; }while(GasLevel < 1 || GasLevel > 5); switch(GasLevel) { case 1: setTankLevel("Empty"); break; case 2: setTankLevel("1/4 Full"); break; case 3: setTankLevel("Half Full"); break; case 4: setTankLevel("3/4 Full"); break; case 5: setTankLevel("Full Tank"); break; } } //-------------------------------------------------------------------- |
Main File: Main.cpp
#include "Invoice.h" void main() { CInvoice Order; Order.ProcessOrder(); Order.ShowOrder(); } |
The following class was not tested and was not (yet) integrated in the program. I don't find time to do it, I hope you will. The idea is that the clerk should be able to ask the customer for the starting date to rent the car and the project date of the end of lease. Of course, you (the programmer) must calculate the number of days, find out if the date range falls on a week-end or on a week days. If the date range is more than 5 days, the rate should be lower than that of 5 days, which should be lower than the rate for 3 days; in fact the rate should even be lower if the customer wants the car for 20 days or more, which would apply a (low) monthly rate.
Header File: RentDate.h
class CRentDate { public: CRentDate(); CRentDate(int m, int d, int y); CRentDate(const CRentDate& d); void setDate(int m, int d, int y); int getMonth(); int getDay(); int getYear(); virtual ~CRentDate(); private: int Month; int Day; int Year; }; |
// RentDate.cpp: implementation of the CRentDate class. // ////////////////////////////////////////////////////////////////////// #include <iostream.h> #include "RentDate.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CRentDate::CRentDate() : Month(1), Day(1), Year(1990) { } //--------------------------------------------------------------------- CRentDate::CRentDate(int m, int d, int y) : Month(m), Day(d), Year(y) { } //--------------------------------------------------------------------- CRentDate::CRentDate(const CRentDate& d) { Month = d.Month; Day = d.Day; Year = d.Year; } //--------------------------------------------------------------------- CRentDate::~CRentDate() { } //--------------------------------------------------------------------- void CRentDate::setDate(int m, int d, int y) { Month = m; Day = d; Year = y; } //--------------------------------------------------------------------- int CRentDate::getMonth() { return Month; } //--------------------------------------------------------------------- int CRentDate::getDay() { return Day; } //--------------------------------------------------------------------- int CRentDate::getYear() { return Year; } //--------------------------------------------------------------------- |
|
||
C++ Tutorial | Copyright © 2001-2009 FunctionX, Inc. | |
|