Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello! I desperately need help with this project for my computer science class. Please let me know if any clarification or additional information is needed.

Hello! I desperately need help with this project for my computer science class. Please let me know if any clarification or additional information is needed. Compliance with the outlined project requirements is crucial. Thank you!!!

PROJECT 4

Objectives: The main objectives of this project is to test your ability to create and use C++ classes, with multiple constructors, static members/functions, and expand to operator overloading. A review of pointers, structs, arrays, iostream, file I/O and C-style strings is also included.

Description: This project will significantly expand upon Project 3 by adding additional functionality, and implementing more abstract data types (ADTs) and their operations through classes. Pointers must be used for array manipulation, including arrays with ADTs (structs, classes) e.g, rental cars, rental agencies. Pointers must be used in function prototypes and function parameter lists - not square brackets. Make sure all your C-string functions (e.g. string copy, string compare, etc.) work with pointers (parameters list and function implementation). Const should be used in parameter lists, functions, and function signatures as appropriate. Square brackets should be used only when declaring an array, or if otherwise you specify your own overloaded operator[] . Pointers can only be moved by incrementing or decrementing (i.e., ++ or - -), or by setting the pointer back to the base address using the array name. You should use the arrow operator (->) with any pointers where appropriate.

The additional functionality is as follows: You are given an updated data file where there is 1 Agency location (Agency) which has 5 cars (Car) which can potentially be of a high-tech type. Each car can incorporate up to 3 (0-3) special driving sensors (Sensor). You will have similar menu options, but the functionality has been updated below. Note: using multiple helper functions to do smaller tasks will make this project significantly easier.

The Sensor Class will contain the following private data members:

m_type, a C-string char array of 256 max characters (name of sensor type), valid strings for Sensor m_type are "gps", "camera", "lidar", "radar", "none".

m_extracost, a float (additional rent cost per day for the car that carries the sensor, for "gps":=$5.0/day, for "camera":=$10.0/day, for "lidar":=$15.0/day, for "radar":=$20.0/day, for "none":=$0.0/day)

gps_cnt, a static int member (keeps track of existing gps-type sensors)

camera_cnt, a static int member (keeps track of existing camera-type sensors)

lidar_cnt, a static int member (keeps track of existing lidar-type sensors)

radar_cnt, a static int member (keeps track of existing radar-type sensors)

and will have the following methods:

Default Constructor will set the aforementioned data members to default initial values.

Parameterized Constructor will create a new object based on a C-string value passed into it (sensor type being instantiated). Hint: bear in mind what a sensor type implies about its other data members.

Copy Constructor will create a new object which duplicates an input Sensor Object.

Get/Set methods for appropriate data member(s).

A Get and a Reset static member function to return and to reset each of the static member variables.

A Method to check if 2 Sensor Objects are the same. You should make this an operator overload of (operator==). You may want to try for practice to make it a non-Class Member function which will have to access member data over Class methods.

The Car Class will contain the following private data members:

m_make, a C-string char array of 256 max characters (car make)

m_model, a C-string char array of 256 max characters (car model)

m_year, an int (year of production)

m_sensors, a Sensor class type array of size 3 (max allowable number of sensors per car). Hint: You are allowed to use an auxiliary member variable of your choice to keep track of how many actual sensors exist onboard, this will also help for instance in case adding a new sensor is required.

m_baseprice, a float (price per day for the sensorless vehicle)

m_finalprice, a float (price per day with the increased cost of the car sensors)

m_available, a bool (1 = true; 0 = false; try to display true/false using the "std::boolalpha" manipulator like: cout << boolalpha << boolVariable; )

m_owner, a C- string char array of 256 max characters (the current lessee; if no lessee, i.e. the Car object is available), set to a \0-starting (0-length) C-string).

and will have the following methods:

Default Constructor will set the aforementioned data members to default initial values.

Parameterized Constructor will create a new object based on the values passed into it for the make, model, year, baseprice, and sensors.

Copy Constructor will create a new object which duplicates an input Car object.

Get methods for data members.

Set methods for data members except the m_sensors, and m_finalprice.

UpdatePrice a method to update the m_finalprice after any potential changes (to the m_baseprice or the m_sensors)

Print will print out all the cars data.

EstimateCost will estimate the cars cost given (a parameter passed to it) a number of days to rent it for.

A Method to Add a Sensor to the Car object. You should make this an operator overload of (operator+). Hint: a Car Class member method will be the better choice, since it will have access to private members. You may want to try that for practice.

A Method to Add a lessee (the name of a lessee) to the Car object. You should make this an operator overload of (operator+). Hint: bear in mind what adding a renter to a Car object might imply about other data members.

The Agency Class will contain the following private data members:

m_name, a C-string char array of 256 max characters

m_zipcode, a preferably const int number of size 5

m_inventory, an array of Car objects with a size of 5

and will have the following methods:

Default Constructor will set the aforementioned data members to default initial values.

Get/Set (IF possible) methods for m_name and m_zipcode data members by-Value.

A Method to Index by-Reference an Object of the m_inventory data (i.e. you should use return by-Reference). Hint: This will allow you to access (read and write) to the agencys inventory like in Project_3.) You should make this an operator overload of (operator[]). Reminder: Any calls to this operator are excluded from the Projects restrictions about using brackets.

ReadAllData read all of the data for the agency from a user-provided file.

PrintAllData - prints out all of the data for an agency (including car info).

PrintAvailableCars prints out all of the data (including Car info) only for the available Car Objects of the agency.

The menu must have the following updated functionality:

1) Read ALL data from file. The provided sample file HighTechAgency.txt is structured : The first line is the car agency info, followed by 5 cars. For each car the order is: year make model baseprice {sensors} available [lessee]. The sensors are enclosed in {braces} and can be 0 up to 3 ws-separated names. The lessee name is [optional], it will only be there if the car is available.

2) Print to terminal ALL data for the Agency and all its corresponding Cars in a way that demonstrates this relationship (similarly to Project_3).

3) Print to terminal the TOTAL number of sensors built to equip the agencys car fleet (total number by sensor type).

4) Find the most expensive available car ask the user if they want to rent it update that cars lessee and availability status if the user says yes.

5) Exit program.

The following minimum functionality and structure is required:

Ask the user for the input file name.

The list of Sensors must be stored in an array of Objects.

The list of Cars must be stored in an array of Objects.

Use character arrays to hold your strings (i.e., C-style) exclusively (using the string data type is still not allowed).

Write multiple functions (Hint: You could have each menu option be a function).

At least one function must use Pass-by-Reference.

At least one function must Return-a-Reference.

Otherwise, as before, you are free to use pass by-Value, pass by-Reference, pass by-Address for your function parameters.

Variables, data members, functions, function signatures, should all be const in a perfect program, unless there is no other way for the program to work. (This might seem as an overstatement. However, try to remember the const keyword and design around it as much as you can). You are required to at least try.

Pointers must be used for all array manipulation (iterating over elements to read/modify cannot be performed with bracket operator accessing). The only exception on [] is if you are using your own overload of operator[].

Pointers must be used in function prototypes and function parameter lists (the bracket notation is not allowed in parameters lists).Pointers can only be moved by incrementing or decrementing: double d[3] = {1,2,3}; double * d_Pt = d; for (int i=0; i<3; ++i,++d_Pt){ cout << *d_Ptd; }

Or by setting the pointer back to the base address using the array name. d_Pt = d; cout << *d_Pt << endl;

Write your own C-string length, compare, copy, concatenate functions. Their prototypes will have the form (use the prototypes exactly as provided, with char * parameters):

// counts characters in str array until a NULL-character '\0' is found, then it returns that number excluding the '\0' one // the return type size_t represents an unsigned integral number large enough to contain the maximum possible number of a storage size that can appear on a target architecture size_t myStringLength(const char * str);

// returns 0 when the C-strings match, i.e. their characters are equal one-by-one until a NULL-character '\0' is found in both strings and at the same position as well // returns a value < -1 if the first character that does not match has a lower value in str1 than in str2 // returns a value > 1 if the first character that does not match has a higher value in str1 than in str2 int myStringCompare(const char * str1, const char * str2);

// copies characters from source to destination array until a NULL-character '\0' is found in source, then it NULL-terminates destination too // returns a pointer to the destination array char * myStringCopy(char * destination, const char * source);

// appends the content of source to the destination array // this means that the NULL-terminator of destination is overwritten by the first character of source and a NULL-character '\0' is appended at the end of the concatenated Cstring in destination

// returns a pointer to the destination array char * myStringCat(char * destination, const char * source);

The other functionality and structure of the program should remain the same as Project #3, including writing to screen and file, as well as restrictions on string libraries, global variables and constants, etc.

Implement the concepts of encapsulation and data hiding (necessary)! Use the const keyword where appropriate (almost everywhere you can make it)! Implement Class Constructors with Initializer-Lists as much as possible (advised)!

Implement operator overloads as much as you can !

This is a chance to experiment as much as possible with more advanced concepts and the intricacies of classes. It is not a strict requirement that const data members which can only be instantiated with a list initialization are there in your code right now, neither is it necessary to make your member functions in the operator overload approach. Try your best in order to acquaint yourself with these new concepts however, and figure out what might be giving you a hard time understanding and/or implementing at this early point.

The supplied txt file for the program HighTechAgency.txt is as follows:

Enterprise 89502 2014 Toyota Tacoma 115.12 {gps} 1 2012 Honda CRV 85.10 {camera lidar} 0 Mike 2011 Toyota Rav4 65.02 {} 0 Oscar 2009 Dodge Neon 45.25 {camera lidar radar} 1 2015 Ford Fusion 90.89 {lidar} 0 Juliet

Below is my code for Project 3, as mentioned in the project instructions:

project3.cpp:

//This program acts as a car rental service and allows the user to interact with a menu to achieve desired outcome.

#include

#include

#include "proj3.h"

using namespace std;

void printMenu(); //prints menu for user-selection

void select(int &selection); //prompts user for menu choice

int strCompare(char *str1, char *str2); //compares two agency names

void option1(RentalCar::agency *aptr); //reads user-specified file into inventory array

void option2(bool availableOnly, RentalCar::agency *aptr); //prints all data to terminal

void option3(RentalCar::agency *aptr); //estimates total price for a user-specified car & number of days

void option4(RentalCar::agency *aptr); //finds most expensive car and prints to terminal

void option5(bool availableOnly, RentalCar::agency *aptr); //prints only available cars to user-specified output file as well as terminal

int main(){

RentalCar::agency agencies[3];

RentalCar::agency *aptr = agencies;

int selection = 0;

while(selection != 6){

printMenu();

select(selection); //menu selection

aptr = agencies; //reset pointer

switch(selection){

case 1:

option1(aptr);

cout << endl;

break;

case 2:

option2(false, aptr);

break;

case 3:

option3(aptr);

cout << endl;

break;

case 4:

option4(aptr);

cout << endl;

break;

case 5:

option5(true, aptr);

cout << endl;

break;

case 6:

break;

default:

cout << "Invalid selection. ";

cout << endl;

break;

}

}

return 0;

}

void printMenu(){

cout << "Menu options: " << "1) Choose input file. " << "2) Print all data (all agencies and corresponding cars) to terminal. " << "3) Price estimate for user-inputed agency, car (1-5), and number of days to rent. " << "4) Print most expensive car out of all 3 agencies. " << "5) Print available cars to user-specified output file. " << "6) Exit Program. ";

}

void select(int &selection){

cout << "Select menu uption: ";

cin >> selection;

}

void option1(RentalCar::agency *aptr){

ifstream inputFile;

string inputName;

cout << "Enter name of input file: ";

cin >> inputName;

inputFile.open(inputName.c_str());

if(inputFile.is_open()){

for(int i = 0; i < 3; i++){

int *zptr = (*aptr).zipcode;

//pull info from file

inputFile >> (*aptr).name;

for(int j = 0; j < 5; j++){

char zip;

inputFile >> zip;

int zipNum = (int)zip - 48;

*zptr = zipNum;

zptr++;

}

RentalCar::rentalCar *rptr;

rptr = aptr->inventory;

for(int k = 0; k < 5; k++){

inputFile >> (*rptr).m_year >> (*rptr).m_make >> (*rptr).m_model >> (*rptr).m_price >> (*rptr).m_available;

rptr++;

}

aptr++;

}

}

else

cout << "Failed to open file. ";

}

void option2(bool availableOnly, RentalCar::agency *aptr){

cout << endl;

for(int i = 0; i < 3; i++){

int *zptr = (*aptr).zipcode;

//agency name and zip

cout << (*aptr).name << " ";

for(int j = 0; j < 5; j++){

cout << *zptr;

zptr++;

}

cout << endl;

//car info

RentalCar::rentalCar *rptr;

rptr = aptr->inventory;

for(int k = 0; k < 5; k++){

if((*rptr).m_available != false || availableOnly != true){

cout << (*rptr).m_year << " " << (*rptr).m_make << " " << (*rptr).m_model << ", $" << (*rptr).m_price << " per day, ";

if((*rptr).m_available == 1)

cout << "Available: true";

else

cout << "Available: false";

cout << endl;

}

rptr++;

}

cout << endl;

aptr++;

}

}

void option3(RentalCar::agency *aptr){

char agencyName[20];

int carNumber;

int dayCount;

char *ptrName;

cout << "Enter Agency Name: ";

cin >> agencyName;

cout << "Enter car number (1-5): ";

cin >> carNumber;

cout << "Enter number of days you want to rent: ";

cin >> dayCount;

ptrName = (*aptr).name;

while(strCompare(ptrName, agencyName) == 0){ //find selected agency

aptr++;

ptrName = (*aptr).name;

}

RentalCar::rentalCar *rptr;

rptr = aptr->inventory;

rptr += (carNumber - 1); //choice changed to index number

float cost = (*rptr).m_price * dayCount;

cout << "Your estimated total cost is: $" << cost << endl;

}

void option4(RentalCar::agency *aptr){

float topPrice = 0.0f;

RentalCar::rentalCar *topptr;

for(int i = 0; i < 3; i++){

RentalCar::rentalCar *rptr;

rptr = aptr->inventory;

for(int j = 0; j < 5; j++){

if((*rptr).m_price > topPrice){

topPrice = (*rptr).m_price;

topptr = rptr;

}

rptr++;

}

aptr++;

}

cout << "The most expensive car is the " << (*topptr).m_year << " " << (*topptr).m_make << " " << (*topptr).m_model << " at $" << (*topptr).m_price << endl;

}

int strCompare(char *str1, char *str2){

while((*str1 != '\0') && (*str2 != '\0')){

if(*str1 != *str2){

return 0;

}

else{

str1++;

str2++;

}

}

return 1; //if same

}

void option5(bool availableOnly, RentalCar::agency *aptr){

ofstream outputFile;

string outputName;

cout << "Enter name of output file: ";

cin >> outputName;

outputFile.open(outputName.c_str());

for(int i = 0; i < 3; i++){

int *zptr = (*aptr).zipcode;

//agency name and zip

cout << (*aptr).name << " ";

outputFile << (*aptr).name << " ";

for(int j = 0; j < 5; j++){

cout << *zptr;

outputFile << *zptr;

zptr++;

}

cout << endl;

outputFile << endl;

RentalCar::rentalCar *rptr;

rptr = aptr->inventory;

for(int k = 0; k < 5; k++){

if((*rptr).m_available != false || availableOnly != true){

cout << (*rptr).m_year << " " << (*rptr).m_make << " " << (*rptr).m_model << ", $" << (*rptr).m_price << " per day, ";

outputFile << (*rptr).m_year << " " << (*rptr).m_make << " " << (*rptr).m_model << ", $" << (*rptr).m_price << " per day, ";

if((*rptr).m_available == 1){

cout << "Available: true";

outputFile << "Available: true";

}

else{

cout << "Available: false";

outputFile << "Available: false";

}

cout << endl;

outputFile << endl;

}

rptr++;

}

cout << endl;

outputFile << endl;

aptr++;

}

}

proj3.h

//Project 3 header file

class RentalCar{

public:

//members

struct rentalCar{

int m_year;

char m_make[30];

char m_model[30];

float m_price;

bool m_available;

};

struct agency{

char name[20];

int zipcode[5];

rentalCar inventory[5];

};

};

agencies.txt (sample input file for testing)

Hertz 93619 2014 Toyota Tacoma 115.12 1 2012 Honda CRV 85.10 0 2015 Ford Fusion 90.89 0 2013 GMC Yukon 110.43 0 2009 Dodge Neon 45.25 1

Alamo 89502 2011 Toyota Rav4 65.02 1 2012 Mazda CX5 86.75 1 2016 Subaru Outback 71.27 0 2015 Ford F150 112.83 1 2010 Toyota Corolla 50.36 1

Budget 93035 2008 Ford Fiesta 42.48 0 2009 Dodge Charger 55.36 1 2012 Chevy Volt 89.03 0 2007 Subaru Legacy 59.19 0 2010 Nissan Maxima 51.68 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions