I am a new C++ programmer. I am still having trouble with certain data
types and constructors, among other things. I'm not sure if I've
used "std::strin g" properly throughout this program. I need to fix
that, as well as add exception-handling, make sure everything is
documented properly, and make certain that the code compiles and runs
correctly. I'm under a very tight deadline, as this needs to run
perfectly within the next 24 hours.
Header File: computer.h #if !defined COMPUTER_H
#define COMPUTER_H
enum TComputerType { ctLaptop = 1, ctDesktop, ctGraphics, ctScientific,
ctMainframe, ctMidrange };
struct Computer
{
Computer( std::string const& aModel, operatingsystem , TComputerType
aCategory )
: model( aModel ), operatingsystem ( aOperatingsyste m ), category(
aCategory )
{}
std::string model;
std::string operatingsystem ;
TComputerType category;
};
#endif // COMPUTER_H
Source File: computer.cpp // Computer.cpp: implementation of the
Computer class.
#include <iostream>
#include <string>
using namespace std;
#include "Computer.h "
//--------------------------------------------------------------------
Computer::Compu ter()
{
setMakeModel("N o Computer Selected");
setOperatingsys tem(Windows);
setCategory(ctL aptop);
}
//--------------------------------------------------------------------
Computer::Compu ter(const char *MM, std::string O, std::string Cat)
{
setMakeModel(MM );
setOperatingSys tem(O);
setCategory(Cat );
}
//--------------------------------------------------------------------
Computer::~Comp uter()
{
// delete [] MakeModel;
}
//--------------------------------------------------------------------
void Computer::setMa keModel(const char *m)
{
MakeModel = new char[strlen(m) + 1];
strcpy(MakeMode l, m);
}
//--------------------------------------------------------------------
char* Car::getMakeMod el() const
{
return MakeModel;
}
//--------------------------------------------------------------------
void ComputerOperati ngsystem::setCo mputerOperating system(std::str ing y)
{
ComputerOperati ngsystem = y;
}
//--------------------------------------------------------------------
std::string Computer::getCo mputerOperating system() const
{
return ComputerOperati ngsystem;
}
//--------------------------------------------------------------------
void Computer::setCa tegory(const int c)
{
Category = c;
}
//--------------------------------------------------------------------
int Computer::getCa tegory() const
{
return Category;
}
//--------------------------------------------------------------------
Header File: customer.h #if !defined CUSTOMER_H
#define CUSTOMER_H
class Customer
{
private:
std::string FirstName;
std::string LastName;
std::string Address;
std::string City;
std::string State;
long ZIPCode;
public:
void setFirstName(co nst char *FN);
char* getFirstName() const { return FirstName; }
void setLastName(con st char *LN);
char* getLastName() const { return LastName; }
char* FullName() const;
void setAddress(cons t 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(cons t 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 "
//---------------------------------------------------------------------------
member objects:
std::string FirstName, LastName;
function:
std::string Customer::FullN ame() const
{
std::string sName = FirstName + " " + LastName;
return sName;
}
//---------------------------------------------------------------------------
Customer::Custo mer()
: ZIPCode(0)
{
FirstName = new char[20];
strcpy(FirstNam e, "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::Custo mer(char * FName, char * LName)
: ZIPCode(0)
{
FirstName = new char[strlen(FName) + 1];
strcpy(FirstNam e, 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::Custo mer(char *FName, char *LName, char *Adr,
char *Ct, char *St, long ZIP)
: ZIPCode(ZIP)
{
FirstName = new char[strlen(FName) + 1];
strcpy(FirstNam e, 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::Custo mer(const Customer &Pers)
: ZIPCode(Pers.ZI PCode)
{
FirstName = new char[strlen(Pers.Fir stName) + 1];
strcpy(FirstNam e, Pers.FirstName) ;
LastName = new char[strlen(Pers.Las tName) + 1];
strcpy(LastName , Pers.LastName);
Address = new char[strlen(Pers.Add ress) + 1];
strcpy(Address, Pers.Address);
City = new char[strlen(Pers.Cit y) + 1];
strcpy(City, Pers.City);
State = new char[strlen(Pers.Sta te) + 1];
strcpy(State, Pers.State);
}
//---------------------------------------------------------------------------
void Customer::setFi rstName(const char *FN)
{
strcpy(FirstNam e, FN);
}
//---------------------------------------------------------------------------
void Customer::setLa stName(const char *LN)
{
strcpy(LastName , LN);
}
//---------------------------------------------------------------------------
void Customer::setAd dress(const char *Adr)
{
strcpy(Address, Adr);
}
//---------------------------------------------------------------------------
void Customer::setCi ty(const char *CT)
{
strcpy(City, CT);
}
//---------------------------------------------------------------------------
void Customer::setSt ate(const char *St)
{
strcpy(State, St);
}
//---------------------------------------------------------------------------
void Customer::setZI PCode(const long ZIP)
{
ZIPCode = ZIP;
}
//---------------------------------------------------------------------------
Customer::~Cust omer()
{
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 "Computer.h "
#include "RentDate.h "
class Invoice
{
public:
Invoice();
virtual ~Invoice();
Customer CustomerRegistr ation();
void CustomerInforma tion(const Customer& Pers);
Computer ComputerSelecti on();
void ComputerSelecte d(const Computer& Machine);
double CalculatePrice( const Computer& Machine, double& Rate, int
&Days);
void setMemory(const long g);
long getMemory() const;
void setProcessorPow er(const char *v);
char* getProcessorPow er() const;
void setComputerCond ition(const char *c);
char* getComputerCond ition() const;
void ProcessOrder();
void ComputerExamina tion();
void ShowOrder();
private:
std::string Memory;
std::string ProcessorPower;
std::string ComputerConditi on;
};
#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::Invoic e()
{
setMemory("384M BofRAM");
setComputerCond ition("Good");
}
//--------------------------------------------------------------------
Invoice::~Invoi ce()
{
}
//--------------------------------------------------------------------
Customer Invoice::Custom erRegistration( )
{
std::string FName, LName, Addr, CT, St;
long ZC = 0;
cout << "Enter Customer Information\n";
cout << "First Name: "; cin >> FName;
cout << "Last Name: "; cin >> LName;
cout << "Address: "; cin >> ws;
cin.getline(Add r, 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;
}
//--------------------------------------------------------------------
Computer Invoice::Comput erSelection()
{
std::string ComputerType, ModelChosen;
std::string ComputerSelecte d;
int ComputerSelecte dOperatingsyste m = Windows;
cout << "What type of computer would you like to rent?";
do {
cout << "\n1 - Laptop | 2 - Desktop | 3 - Graphics"
<< "\n4 - Scientific | 5 - Mainframe | 6 - Midrange";
cout << "\nYour Choice: ";
cin >> ComputerType;
if( ComputerType < 1 || ComputerType > 6 )
cout << "\nPlease type a number between 1 and 6";
} while(ComputerT ype < 1 || ComputerType > 6);
switch(Computer Type)
{
case ctLaptop:
cout << "\nFor the Laptop type, we have:"
<< "\n1 - HP Pavilion | 2 - Compaq Presario";
cout << "\nWhich one would you prefer? ";
cin >> ModelChosen;
if(ModelChosen == 1)
{
strcpy(strCompu terSelected, "HP Pavillion");
ComputerSelecte dOperatingsyste m = Windows;
}
else
{
strcpy(strCompu terSelected, "Compaq Presario");
ComputerSelecte dOperatingsyste m = Windows;
}
break;
case ctDesktop:
cout << "\nFor the Desktop type, we have:"
<< "\n1 - Gateway DX110S | 2 - Apple Macmini"
<< "\n3 - Dell DimensionB110 | 4 - eMachines T3120";
cout << "\nWhich one would you prefer? ";
cin >> ModelChosen;
if(ModelChosen == 1)
{
strcpy(strCompu terSelected, "Gateway DX110s");
ComputerSelecte dOperatingsyste m = Windows;
}
else if(ModelChosen == 2)
{
strcpy(strCompu terSelected, "Apple Macmini");
ComputerSelecte dOperatingsyste m = MacOSX;
}
else if(ModelChosen == 3)
{
strcpy(strCompu terSelected, "Dell DimensionB110") ;
ComputerSelecte dOperatingsyste m = Windows;
}
else
{
strcpy(strCompu terSelected, "eMachines T3120");
ComputerSelecte dOperatingsyste m = Windows;
}
break;
case ctGraphics:
cout << "\nFor the Graphics type, we have:"
<< "\n1 - Silicon Graphics Tezro | 2 - Apple PowerMacG5";
cout << "\nWhich one would you prefer? ";
cin >> ModelChosen;
if(ModelChosen == 1)
{
strcpy(strCompu terSelected, "Silicon Graphics Tezro");
ComputerSelecte dOperatingsyste m = IRIX;
}
else
{
strcpy(strCompu terSelected, "Apple Power Mac G5");
ComputerSelecte dOperatingsyste m = MacOSXTiger;
}
break;
case ctScientific:
cout << "\nFor the Scientific type, we have:"
<< "\n1 - Sun Ultra 45 | 2 - Silicon Graphics Fuel | 3 - Silicon
Graphics Prism";
cout << "\nWhich one would you prefer? ";
cin >> ModelChosen;
if(ModelChosen == 1)
{
strcpy(strCompu terSelected, "Sun Ultra 45");
ComputerSelecte dOperatingsyste m = Solaris10;
}
else if(ModelChosen == 2)
{
strcpy(strCompu terSelected, "Silicon Graphics Fuel");
ComputerSelecte dOperatingsyste m = IRIX;
}
else
{
strcpy(strCompu terSelected, "Silicon Graphics Prism");
ComputerSelecte dOperatingsyste m = Linux;
}
break;
case ctMainframe:
cout << "\nFor the Mainframe type, we have:"
<< "\n1 - IBM zSeries 990 | 2 - IBM zSeries 890"
<< "\n3 - IBM zSeries 900 | 4 - IBM System z9";
cout << "\nWhich one would you prefer? ";
cin >> ModelChosen;
if(ModelChosen == 1)
{
strcpy(strCompu terSelected, "IBM zSeries 990");
ComputerSelecte dOperatingsyste m = LinuxOnZSeries;
}
else if(ModelChosen == 2)
{
strcpy(strCompu terSelected, "IBM zSeries 890");
ComputerSelecte dOperatingsyste m = LinuxOnZSeries;
}
else if(ModelChosen == 3)
{
strcpy(strCompu terSelected, "IBM zSeries 900");
ComputerSelecte dOperatingsyste m = LinuxOnZSeries;
}
else
{
strcpy(strCompu terSelected, "IBM System z9");
ComputerSelecte dOperatingsyste m = LinuxOnSystemZ;
}
break;
case ctMidrange:
cout << "\nFor the Midrange type, we have:"
<< "\n1 - IBM eServer i5 520 | 2 - IBM eServer i5 550"
<< "\n3 - IBM eServer i5 570 | 4 - IBM eServer i5 595";
cout << "\nWhich one would you prefer? ";
cin >> ModelChosen;
if(ModelChosen == 1)
{
strcpy(strCompu terSelected, "IBM eServer i5 520");
ComputerSelecte dOperatingsyste m = IBMEServerI5OS;
}
else if(ModelChosen == 2)
{
strcpy(strCompu terSelected, "IBM eServer i5 550");
ComputerSelecte dOperatingsyste m = IBMEServerI5OS;
}
else if(ModelChosen == 3)
{
strcpy(strCompu terrSelected, "IBM eServer i5 570");
ComputerSelecte dOperatingsyste m = IBMEServerI5OS;
}
else
{
strcpy(strCompu terSelected, "IBM eServer i5 595");
ComputerSelecte dOperatingsyste m = IBMEServerI5OS;
}
break;
}
Computer Selected(strCom puterSelected,
ComputerSelecte dOperatingsyste m, ComputerType);
return Selected;
}
//--------------------------------------------------------------------
void Invoice::setMem ory(std::string g)
{
Memory = g;
}
//--------------------------------------------------------------------
long Invoice::getMem ory() std::string
{
return Memory;
}
//--------------------------------------------------------------------
void Invoice::setPro cessorPower std::string v
{
ProcessorPower = std::string v;
strcpy(Processo rPower, v);
}
//--------------------------------------------------------------------
std:string Invoice::getPro cessorPower
{
return ProcessorPower;
}
//--------------------------------------------------------------------
void Invoice::setCom puterCondition( const char *c)
{
ComputerConditi on = new char[strlen(c) + 1];
strcpy(Computer Condition, c);
}
//--------------------------------------------------------------------
char* Invoice::getCom puterCondition( ) const
{
return ComputerConditi on;
}
//--------------------------------------------------------------------
void Invoice::Proces sOrder()
{
int NbrOfDays;;
double Rate, TotalPrice;
// Enter Customer Information
Customer Person = CustomerRegistr ation();
cout << "\nProcess Computer Information\n";
Computer Usage = ComputerSelecti on();
TotalPrice = CalculatePrice( Usage, Rate, NbrOfDays);
ComputerExamina tion();
// This function works for both Borland C++ Builder and MSVC
system("cls");
cout << " - San Francisco Computer Rental -";
cout << "\n============ =============== ==";
CustomerInforma tion(Person);
cout << "\n------------------------------";
ComputerSelecte d(Usage);
cout << setiosflags(ios ::fixed) << setprecision(2) ;
cout << "\n------------------------------";
cout << "\nComputer Memory: " << getMemory();
cout << "\nConditio n: " << getComputerCond ition();
cout << "\nProcessorPow er : " << getProcessorPow er();
cout << "\n# of Days: " << NbrOfDays;
cout << "\n------------------------------";
cout << "\nRate: $" << Rate;
cout << "\nTotal Price: $" << TotalPrice;
cout << "\n============ =============== ===\n";
}
//--------------------------------------------------------------------
void Invoice::Custom erInformation(c onst Customer& Pers)
{
cout << "\nEmployee Identification" ;
cout << "\nFull Name: " << Pers.FullName() ;
cout << "\nAddress: " << Pers.getAddress ();
cout << "\nCity: " << Pers.getCity() << ", "
<< Pers.getState() << " " << Pers.getZIPCode ();
}
//--------------------------------------------------------------------
void Invoice::Comput erSelected(cons t Computer& Machine)
{
cout << "\nModel: " << Machine.getMake Model();
cout << "\nOperatingsys tem: " <<
Machine.getComp uterOperatingsy stem();
}
//--------------------------------------------------------------------
double Invoice::Calcul atePrice(const Computer& Machine, double&
DayRate,
int &NumberOfDay s)
{
// char WeekEndResponse ;// Does the customer rent the computer 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(Machine. getCategory())
{
case ctDesktop:
DayRate = 24.95;
OneDayRate = 29.95;
WeekDay = 24.95;
WeekEnd = 19.95;
break;
case ctLaptop:
DayRate = 34.95;
OneDayRate = 39.95;
WeekDay = 34.95;
WeekEnd = 25.95;
break;
case ctGraphics:
DayRate = 38.95;
OneDayRate = 49.95;
WeekDay = 38.95;
WeekEnd = 32.95;
break;
case ctScientific:
DayRate = 44.95;
OneDayRate = 69.95;
WeekDay = 44.95;
WeekEnd = 35.95;
break;
case ctMidrange:
DayRate = 54.95;
OneDayRate = 89.95;
WeekDay = 54.95;
WeekEnd = 42.95;
break;
case ctMainframe:
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::Comput erExamination()
{
char Cond;
std::string ProcessorPower;
cout << "Rate the computer's condition (e=Excellent/g=Good/d=Useable):
";
cin >> Cond;
if( Cond == 'e' || Cond == 'E' )
strcpy(Computer Condition, "Excellent" );
else if( Cond == 'g' || Cond == 'G' )
strcpy(Computer Condition, "Good");
else if( Cond == 'd' || Cond == 'D' )
strcpy(Computer Condition, "Useable");
else
strcpy(Computer Condition, "Can't Decide");
cout << "Enter the computer memory: ";
cin >> Memory;
do {
cout << "ProcessorPower "
<< "\n1 - 1.6 GHz"
<< "\n2 - 1.9 GHz"
<< "\n3 - 2.5 GHz"
<< "\n4 - 2.7 GHz"
<< "\n5 - 3.7 GHz";
cout << "\nEnter the ProcessorPower: ";
cin >> ProcessorPower;
}while(Processo rPower < 3.7GHz || ProcessorPower > 1.6GHz);
switch(Processo rPower)
{
case 1:
setProcessorPow er("1.6 GHz");
break;
case 2:
setProcessorPow er("1.9 GHz");
break;
case 3:
setProcessorPow er("2.5 GHz");
break;
case 4:
setProcessorPow er("2.7 GHz");
break;
case 5:
setProcessorPow er("3.7 GHz");
break;
}
}
//--------------------------------------------------------------------
Main File: Main.cpp
#include "Invoice.h"
void main()
{
CInvoice Order;
Order.ProcessOr der();
Order.ShowOrder ();
}
dte