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

you are requested to implement a simple cars’ management system. The system shou

ID: 3773704 • Letter: Y

Question

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. • 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: 1- 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. 2- 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. 3- 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. 4- 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. 5- List cars Displays cars by price in increasing order, each field is displayed in a separate column with a width of 10 characters. 6- Show statistics. Displays for each brand of cars in the database, the number of cars of that brand. 7-Exit Asks the user to confirm exiting the program.

Explanation / Answer

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

class car
{
   int ID;
   string BrandName;
   string ModelName;
   int Price;  
   public:
       int getID()
       {
           return ID;
       }
       car(){
       }
       void setData(string brand,string model,int cost)
       {
           BrandName=brand;
           ModelName=model;
           Price=cost;
       }
       car(int id,string brand,string model,int cost)
       {
           ID=id;
           BrandName=brand;
           ModelName=model;
           Price=cost;
       }
      
       void deleteData()
       {
           ID=0;
           BrandName="";
           ModelName="";
           Price=0;
       }
       string getCardata()
       {
           string msg;
           msg="CarID: ";
           stringstream ss;
           ss << ID;
           string str = ss.str();
           msg.append(str);msg.append(" ");
           msg.append("Brand: ");
           msg.append(BrandName);
           msg.append(" ");
           msg.append("Model: ");
           msg.append(ModelName);
           msg.append(" ");
           ss << Price;
           str = ss.str();
           msg.append("Price: ");msg.append(str);
           msg.append(" ");
           return msg.c_str();
       }
};
int find(int id,int n ,car cars[])
{
   int ans=-1;
   for(int i=0;i<n;i++)
   {
       if(id==cars[i].getID())
       {
           ans=i;
           break;
       }
   }
   return ans;
}
int main()
{
   int option;
   car cars[10];
   int n=0;
   int id;
   string brand;
   string model;
   int cost;
   while(true)
   {
   cout<<endl<<"1-Add a car";
   cout<<endl<<"2-Delete a car";
   cout<<endl<<"3-Update car";
   cout<<endl<<"4-Find car";
   cout<<endl<<"5-List cars";
   cout<<endl<<"6-Show statistics";
   cout<<endl<<"7-Exit";
   cout<<endl<<"Enter your option";
   cin>>option;
   switch(option)
   {
       /*1- 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.*/
       case 1:
       int flag=0;
       while(flag==0)
       {
       cout<<endl<<"Enter CarID:";
       cin>>id;
       if(id>0)
       {
           int n=0;
           int id1=id;
           while(id1>0)
           {
               id1=id1/10;
               n++;
           }
           if(n==4)
           {
               flag=1;
               break;
           }
           else
               cout<<endl<<"Car ID should be positive integer and has 4 digits.";
       }
       }
      
       flag=0;
       while(flag==0)
       {
       cout<<endl<<"Enter price:";
       cin>>cost;
       if(id>0)
       {
           flag=1;
           break;
       }
       else
           cout<<"price should be greater than zero.";
       }
      
      
       cout<<endl<<"Enter Brand name:";
       cin>>brand;
      
  
       cout<<endl<<"Enter Model name:";
       cin>>model;
      
       cars[n++]=car(id,brand,model,cost);
       break;
      
       case 2:
           /*2- 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.*/
           cout<<endl<<"Enter the ID to delete: " ;
           cin>>id;
           int k=find(id,n,cars);
           if(k!=-1)
           {
               cars[k].deleteData();
               cout<<endl<<"deleted successfully";
           }
           else
               cout<<endl<<"Car does not exist";
           break;
       case 3:
           cout<<endl<<"Enter the ID to update: " ;
           cin>>id;
           int k=find(id,n,cars);
           if(k!=-1)
           {
               cout<<endl<<"Enter brand name";
               cin>>brand;
               cout<<endl<<"Enter model name";
               cin>>model;
               cout<<endl<<"Enter brand name";
               cin>>cost;
               car[k].setData(brand,model,cost);
               cout<<endl<<"updated successfully";
           }
           else
               cout<<endl<<"Car does not exist";
           break;
       case 4:
           cout<<endl<<"Enter the ID to find: " ;
           cin>>id;
           int k=find(id,n,cars);
           if(k!=-1)
           {
               cout<<endl<<car[k].getCardata();
           }
           else
               cout<<endl<<"Car does not exist";
           break;
       case 5:
          
           break;
       case 6:
          
           break;
       case 7: char ch;
       cout<<endl<<"Are you sure to exit? <y/n>";
               cin>>ch;
               if(ch=='y')
                   exit(0);
               else
               break;
   }
   }
  
   return 0;
}