Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(This class is so basic so no need to use advanced C++ codes) (We almost just ne

ID: 3773746 • Letter: #

Question

(This class is so basic so no need to use advanced C++ codes) (We almost just need: Use of functions and re-use of functions whenever possible, Use of arrays.( no vectors) ,Use of files,Use of loops.) - In this C++ program you are requested to implement a simple cars’ management system. The system should allow a user to perform a set of tasks through the following menu. 1-Add a car 2-Delete a car 3-Update car 4-Find car 5-List cars 6-Show statistics 7-Exit The system has to keep looping until the user select the Exit option. The system has to store cars’ data in a text file called “cars.txt”. This file will play the role of cars’ database. Just an example: Brand Model Price Toyota Corolla 53000 Nissan Patrol 200000 Toyota Camry 80000 Nissan Sunny 42000 Chevrolet Impala 61000 Chevrolet Malibu 65000 …. The data has to be read from the file when the program start and stored back in the file when the program ends. For each menu option you have to implement the following requirements: Add a car For each car you add you have to read the following details: ID, Brand name, Model name, price. ID is a positive integer with exactly 4 digits. price is an integer with only possible values. The Brand name and model names are strings built out of the English alphabet characters excluding the space character (no spaces). Each field you read for a cars has to be validated before it is accepted. Not that the car ID has to be unique among all cars. For storing the list of all cars in your program, you can use parallel arrays or vectors. Delete a car For this option the user has to enter the ID of the car to delete. If the ID is not found, a message is displayed to tell the user that the car does not exist. Update car For this option the user has to enter the car ID to update. If the car exists, the system displays each field of the car and request from the user if he wants to update it. If the user answers yes the system requests the new value for the field. Find car For this option the user has to enter the car ID. The details of the car with the entered ID is displayed if it is found, otherwise a message is displayed to tell the user the car does not exist. List cars Displays cars by price in increasing order, each field is displayed in a separate column with a width of 10 characters. Show statistics. Displays for each brand of cars in the database, the number of cars of that brand. Exit Asks the user to confirm exiting the program.

Explanation / Answer

//Program:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void showMenu();
void addCar();
void deleteCar();
void updateCar();
void findCar();
void listCar();
//void showStatistics();
bool isValid(int );
int main(){
           int choice;
           //define the needed variables and arrays.
do{
//display menu to user
showMenu();
//let the user enter a choice
cin>>choice;
cout<<endl<<endl;
//process the user's choice
switch (choice){
case 1:
addCar();
cout<<endl;
break;
case 2:
deleteCar();
cout<<endl;
break;
case 3:
updateCar();
cout<<endl;
break;
case 4:
findCar();
cout<<endl;
break;
case 5:
listCar();
cout<<endl;
break;
case 6:
//showStatistics();
cout<<endl;
break;
case 7:
cout<<"All data have been updated"<<endl;
cout<<" Check the file c:\cars.txt";
cout<<"Thank you for using our program"<<endl;
exit(0);
default:
cout<<"Please enter a choice between 1 and 7"<<endl;
}
  
}while (choice!=7);
return 0;
}
//______________________________________________________
bool isValid(int id)
{
   bool flag=false;
   if(id>0)
       {
           int n=0;
           int id1=id;
           while(id1>0)
           {
               id1=id1/10;
               n++;
           }
           if(n==4)
           {
               flag=true;
           }
       }
       return flag;
}
void findCar()
{
   int id;
   cout<<endl<<"Enter the carID to find";
int index=-1;
   const int NUM_CARS=10;
int ID[NUM_CARS]={}, price[NUM_CARS]={};
string model[NUM_CARS]={}, brand[NUM_CARS]={};
int n=0;
ifstream infile("c:\cars.txt");
while(!infile.eof())
{
   infile>>ID[n]>>brand[n]>>model[n]>>price[n++];
   }
   infile.close();
   for(int i=0;i<n;i++)
   {
       if(id==ID[i])
       {
           index=i;
           break;
       }
   }
   if(index!=-1)
   {
       cout<<endl<<"Details about the car:";
   cout<<endl<<"ID:"<<ID[index];
   cout<<endl<<"Brand:"<<brand[index];
   cout<<endl<<"Model:"<<model[index];
   cout<<endl<<"Price:"<<price[index];
   }
   else
       cout<<endl<<"Car does not exist";
}

void showMenu()
{
cout<<"Enter a choice from the menu please"<<endl;
cout<<"The Menu:-"<<endl;
cout<<"1. Add a Car."<<endl;
cout<<"2. Delete a Car."<<endl;
cout<<"3. Update Car."<<endl;
cout<<"4. Find Car ."<<endl;
cout<<"5. List Cars."<<endl;
cout<<"6. Show Statistics."<<endl;
cout<<"7. Exit."<<endl;
cout<<"Your choice: ";
}
//
void listCar()
{
   //readfile
   const int NUM_CARS=10;
int ID[NUM_CARS]={}, price[NUM_CARS]={};
string model[NUM_CARS]={}, brand[NUM_CARS]={};
int n=0;
ifstream infile("c:\cars.txt");
while(!infile.eof())
{
   infile>>ID[n]>>brand[n]>>model[n]>>price[n++];
   }
   infile.close();
   for(int i=0;i<n;i++)
   {
       for(int j=0;j<n;j++)
       {
           if(price[i]>price[j])
           {
               int t=price[i];
               price[i]=price[j];
               price[j]=t;
               t=ID[i];
               ID[i]=ID[j];
               ID[j]=t;
               string temp=brand[i];
               brand[i]=brand[j];
               brand[j]=temp;
               temp=model[i];
               model[i]=model[j];
               model[j]=temp;
           }
       }
   }
   cout<<endl<<"List of cars in increasing price order";
   cout<<endl<<"ID" <<setw(10)<<"Brand"<<setw(10)<<"Model"<<setw(10)<<"Price"<<endl;
for(int i=0;i<n;i++)
{
cout<< ID[i]<<setw(10)<<model[i]<<setw(10)<<brand[i]<<setw(10)<<price[i]<<endl;
}
}
//______________________________________________________________________________________________________
void addCar()
{
int ID, price;
string model, brand;
bool flag=false;
while(flag==false)
{
    cout<<"Enter 4 numbers for the car ID please"<<endl;
cin>>ID;
if(isValid(ID))
flag=true;
   }

cout<<"Enter the car brand (please do not enter spaces, use "_" as needed): ";

cin>>brand;

cout<<"Enter the car model (please do not enter spaces, use "_" as needed): ";

cin>>model;

cout<<"Enter the price of the car please: "<<endl;
cin>>price;

//Input validation
while(price<0)
{
cout<<"Please Enter a positive number for the price"<<endl;
cin>>price;
}

ofstream outfile("c:\cars.txt", ios::app);
if(outfile.fail())
cout<<"Fail"<<endl;
else
//the information is written to the file.
outfile<<endl<<ID<<" "<<brand<<" "<<model<<" "<<price<<" ";
outfile.close();
}
void updateCar()
{
   //readfile
const int NUM_CARS=10;
int ID[NUM_CARS]={}, price[NUM_CARS]={};
string model[NUM_CARS]={}, brand[NUM_CARS]={};
int n=0;
ifstream infile("c:\cars.txt");
while(!infile.eof())
{
   infile>>ID[n]>>brand[n]>>model[n]>>price[n++];
   }
   infile.close();
int Id;
cout<<"please enter the ID number to be updated"<<endl;
cin>>Id;
int i;
bool check=false;
for ( i=0; i<n;i++)
{
if ( ID[i]==Id) {

cout<<"Enter the care brand (please do not enter spaces, use "_" as needed): ";

cin>>brand[i];

cout<<"Enter the car model (please do not enter spaces, use "_" as needed): ";

cin>>model[i];

cout<<"Enter the price of the car please: "<<endl;
cin>>price[i];

//Input validation
while(price[i]<0)
{
cout<<"Please Enter a positive number for the price"<<endl;
cin>>price[i];
}
cout<<"***************"<<endl;
cout<<"car has been updated"<<endl;
cout<<"***************"<<endl;
check=true;
break;
}
}
if (i==n && check==false)
{
cout<<"**********"<<endl;
cout<< "ID not valid"<<endl;
cout<<"**********"<<endl;
}
if(check==true)
{
               ofstream show; //to display the file after updating records
ofstream outfile;
outfile.open("c:\cars.txt");
for(int i=0; i<n;i++)
outfile<<ID[i]<<" "<<brand[i]<<" "<<model[i]<<" "<<price[i]<<" "<<endl;
outfile.close();
show.open("c:\cars.txt");
for(int i=0; i<n;i++)
show<<ID[i]<<" "<<brand[i]<<" "<<model[i]<<" "<<price[i]<<" "<<endl;
show.close();
   }
}
//_____________________________________________________________________________________________________________________________
void deleteCar()
{
   //readfile
const int NUM_CARS=10;
int ID[NUM_CARS]={}, price[NUM_CARS]={};
string model[NUM_CARS]={}, brand[NUM_CARS]={};
int n=0;
ifstream infile("c:\cars.txt");
while(!infile.eof())
{
   infile>>ID[n]>>brand[n]>>model[n]>>price[n++];
   }
   infile.close();
int Id;
cout<<"please enter the ID number to be deleted"<<endl;
cin>>Id;
int i;
bool check=false;
for ( i=0; i<n;i++)
{
if ( ID[i]==Id) {
ofstream outfile;
outfile.open("c:\deletedCars.txt");
outfile<<ID[i]<<" "<<brand[i]<<" "<<model[i]<<" "<<price[i]<<" "<<endl;
outfile.close();
ID[i]=-1;
brand[i]="";
model[i]="";
price[i]=-1;
cout<<"***************"<<endl;
cout<<"car has been deleted"<<endl;
cout<<"***************"<<endl;
check=true;
ofstream show; //to display the file after deleteing records
show.open("c:\cars.txt");
for(int i=0; i<n;i++)
show<<ID[i]<<" "<<brand[i]<<" "<<model[i]<<" "<<price[i]<<" "<<endl;
show.close();
break;
}
}
if (i==n && check==false)
{
cout<<"**********"<<endl;
cout<< "ID not valid"<<endl;
cout<<"**********"<<endl;
}
}