Question: Not sure if my code is correct and numbers placed correctly. #include #include #include namespace sdds { class CarInventory { char * m _ type;

Not sure if my code is correct and numbers placed correctly.
#include
#include
#include
namespace sdds {
class CarInventory {
char* m_type;
char* m_brand;
char* m_model;
int m_year;
int m_code;
double m_price;
void resetInfo(){
delete[] m_type;
delete[] m_brand;
delete[] m_model;
m_type = nullptr;
m_brand = nullptr;
m_model = nullptr;
m_year =0;
m_code =0;
m_price =0.0;
}
public:
CarInventory() : m_type(nullptr), m_brand(nullptr), m_model(nullptr), m_year(0), m_code(0), m_price(0.0){
resetInfo();
}
CarInventory(const char* type, const char* brand, const char* model, int year =2024, int code =, double price =1.0)
: m_type(nullptr), m_brand(nullptr), m_model(nullptr), m_year(0), m_code(0), m_price(0.0){
resetInfo();
if (type && brand && model){
m_type = new char[strlen(type)+1];
m_brand = new char[strlen(brand)+1];
m_model = new char[strlen(model)+1];
strcpy(m_type, type);
strcpy(m_brand, brand);
strcpy(m_model, model);
if (year >=) m_year = year;
if (code >= && code <=999) m_code = code;
if (price >0) m_price = price;
}
}
~CarInventory(){
resetInfo();
}
CarInventory& setInfo(const char* type, const char* brand, const char* model, int year, int code, double price){
resetInfo();
if (type && brand && model){
m_type = new char[strlen(type)+1];
m_brand = new char[strlen(brand)+1];
m_model = new char[strlen(model)+1];
strcpy(m_type, type);
strcpy(m_brand, brand);
strcpy(m_model, model);
if (year >=) m_year = year;
if (code >= && code <=999) m_code = code;
if (price >0) m_price = price;
}
return *this;
}
void printInfo() const {
std::cout <<"|"<< std::setw(13)<< m_type <<"|"<< std::setw(18)<< m_brand <<"|"<< std::setw(18)<< m_model
<<"|"<< std::setw(6)<< m_year <<"|"<< std::setw(5)<< m_code <<"|"<< std::setw(10)<< std::fixed << std::setprecision(2)
<< m_price <<"|"<< std::endl;
}
bool isValid() const {
return (m_type && m_brand && m_model && m_year >=2024 && m_code >= && m_code <=999 && m_price >0);
}
bool isSimilarTo(const CarInventory& car) const {
return (strcmp(m_type, car.m_type)==0 && strcmp(m_brand, car.m_brand)==0 && m_year == car.m_year);
}
};
bool find_similar(CarInventory car[], const int num_cars){
for (int i =0; i < num_cars; i++){
for (int j = i +1; j < num_cars; j++){
if (car[i].isSimilarTo(car[j])){
return true;
}
}
}
return false;
}
}
int main(){
const int num_cars =9;
bool invalid_data = false;
sdds::CarInventory cars[num_cars]={
{},
{"Acura", "integra", 2024,25,1001},
{"Alfa Romeo", "Giulia", 2024,35,1002},
{"Cadillac","CT4",2024,50,1003},
{"Chevorlet", "Bolt", 2024,20,1004},
{"Dodge", "Hornet", 2024,30,1005},
{"Honda","CR-V",2024,20,1006},
{"Hyundai", "Santa Fe",2024,35,1007},
{"Infiniti","Q50",2024,40,1008},
{"Kia", "Carnival", 2024,30,1009}
};
if (cars[2].setInfo("").isValid()){
std::cout << "Information was set correctly!" << std::endl;
}
else {
std::cout << "Information was set incorrectly!" << std::endl;
}
if (cars[1].setInfo("").isValid()){
std::cout << "Information was set correctly!" << std::endl;
}
else {
std::cout << "Information was set incorrectly!" << std::endl;
}
std::cout << std::setw(60)<<"----- Car Inventory Information -----"<< std::endl
<< std::endl;
std::cout <<"| Type | Brand | Model | Year | Code | Price |"<< std::endl;
std::cout <<"+------------+------------------+------------------+------+------+-----------+"<< std::endl;
for (int i =0; i < num_cars; i++){
if (cars[i].isValid())
cars[i].printInfo();
else
invalid_data = true;
}
if (invalid_data){
std::cout << std::endl;
File Edit Format View Help
Rental Car Inventory Program Algorithm:
Function: Greet the Reservation Agent.
At program start, display greeting message.
Function: User Menu
View inventory
Add new car to inventory
Rent car to customer
Process returned vehicle
Remove discontinued car from inventory
Quit
Function: View inventory
Display car inventory
Display User Menu again
Function: Add new car to inventory
Prompt Reservation Agent to enter new car info
Give the Reservation Agent the option to return to User Menu in case they change their mind
Display provided car info for Reservation Agent verification
If the information is incorrect
Return to the first prompt in this function
If the information is correct
Check if the car is in current inventory
If the car is already in inventory
Do not add the car
Inform the Reservation Agent that the car is already in inventory and cannot be accepted
File Edit Format View Help
Inform the Reservation Agent that the car is already in inventory and cannot be accepted
Display User Menu again
If the car is not in inventory
Add the car
Update inventory
Display User Menu again

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!