Create an Object Oriented application used to manage and sea
Create an Object Oriented application used to manage and search for cars.
Your task is to create an Object Oriented application used to maintain a catalog of used cars. The catalog keeps track of each car\'s make, model, year, and sale price.
The program begins with an empty catalog. The user is prompted to enter one of three possible commands:
add - add a single car to the catalog
import - allows you to import a file of cars into the catalog (CSV file)
list - list all cars in the catalog
search by make, model, year or price
search by type (sedan, truck, SUV, minivan)
quit - quit the application
If an unknown command is entered, the user should be informed and asked to enter another command. If the add or list commands are entered, the operation for that command is carried out, then the user is prompted for another command. The application continues until the quit command is entered.
The add command
If the add command is entered, the user will then be prompted to enter the car\'s make, model, year, and sales price, one-by-one. The make (e.g. Isuzu) and model (e.g. Trooper) can be arbitrary strings, while the year (e.g. 1990) should be an integer and the sales price (e.g. 2599.99) should be a floating point value. After the data is entered, the record should be stored so it can be recalled later by the list command. You may assume that the catalog will never hold more than 25,000 cars.
OO REQUIREMENTS:
Must be Object Oriented with at least 1 class. (HINT: Car?)
Solution
#include<iostream>
 #include<fstream>
 #include<string>
 //max number of cars a catelog can hold
 #define MAX 100
 using namespace std;
class car
 {
    string make;
    string model;
    string type;
    int year;
    float sale_price;
 public:
    car()
    {
        make=\"\";
        model=\"\";
        type=\"\";
        year=0;
        sale_price=0;
    }
    void add(string mak,string mod, string typ,int yr,float price)
    {
        //add this new car to catelog
        std::ofstream out(\"cars.csv\", std::ios_base::app | std::ios_base::out);
       if(!out)
        {
            cout<<\"Not able to open output file\"<<endl;
        }
        //write info to file
        out<<\"\ \"<<mak<<\" \"<<mod<<\" \"<<typ<<\" \"<<yr<<\" \"<<price<<endl;
        out.close();
   }
    //returns count of cars in catelog
    int import(car arr[])
    {
        //read from csv file and load into array
        ifstream in;
        in.open(\"cars.csv\");
       //check if file can be opened
        if(!in)
        {
            cout<<\"Not able to open input file\"<<endl;
        }
        int index=0;
        while(!in.eof())
        {
            in>>arr[index].make>>arr[index].model>>arr[index].type>>arr[index].year>>arr[index].sale_price;
             index++;
        }
        in.close();
        return index;
    }
    car *search(car arr[],int &size,string str)
    {
        car *tmp;
        tmp = new car[size];
        int j = 0;
        for( int i = 0 ; i <size;i++)
        {
            if(arr[i].make == str || arr[i].model == str || arr[i].type == str)
            {
                tmp[j++]= arr[i];
               
            }
        }
        size = j;
        return tmp;
    }
    car *search(car arr[],int &size,int year)
    {
        car *tmp;
        tmp = new car[size];
        int j = 0;
        for( int i = 0 ; i <size;i++)
        {
            if(arr[i].year == year)
            {
                tmp[j++]=arr[i];
               
            }
        }
        size = j;
        return tmp;
    }
    car *search(car arr[],int &size,float price)
    {
        car *tmp;
        tmp = new car[size];
        int j = 0;
        for( int i = 0 ; i <size;i++)
        {
            if(arr[i].sale_price == price)
            {
                tmp[j++]=arr[i];
            }
        }
        size = j;
        return tmp;
    }
    void list(car arr[],int size)
    {
        for( int i = 0 ; i < size; i++)
        {
            cout<<arr[i].make<<\" \"<<arr[i].model<<\" \"<<arr[i].type<<\" \"<<arr[i].year<<\" \"<<arr[i].sale_price<<endl;
        }
    }
    void display()
    {
       cout<<make<<\" \"<<model<<\" \"<<type<<\" \"<<year<<\" \"<<sale_price<<endl;
    }
    void export_file(car arr[],int size)
    {
        //save file before exiting to update car.csv file
        ofstream out;
        out.open(\"car.csv\");
        if(!out)
        {
            cout<<\"Cant open file for writting ..\"<<endl;
            return ;
        }
        for( int i = 0 ; i < size; i++)
        {
            out<<arr[i].make<<\" \"<<arr[i].model<<\" \"<<arr[i].type<<\" \"<<arr[i].year<<\" \"<<arr[i].sale_price<<endl;
        }
    }
 };
int main()
 {
    //declare catelog of car to hold MAX number of cars
    car catelog[MAX];
    car *car_arr;
    car newcar;
    //array size and array index variables
    int size=0, index=0;
    int choice;
    string model,make,type;
    int yr;
    float price;
    cout<<\"#######################Menu########################################\"<<endl;
    
    while(1)
    {
        cout<<\"1.Add new car\ 2.Import catelog\ 3.List cars\ 4.Search cars\ 5.Quit\ \";
        cin>>choice;
        if(choice == 5)
        {
            cout<<\"Quit application\"<<endl;
            break;
        }
        switch(choice)
        {
            case 1:
                    cout<<\"Add single car to catelog \"<<endl;
                    cout<<\"Make = \";
                    cin>>make;
                    cout<<\"Model = \";
                    cin>>model;
                    cout<<\"Type = \";
                    cin>>type;
                    cout<<\"Year = \";
                    cin>>yr;
                    do
                    {
                        cout<<\"sale_price = \";
                        cin>>price;
                        if(price > 2599.99)
                        {
                            cout<<\"Sale_price cant be more than 2599.99,please enter price once again\";
                            continue;
                        }
                        else
                            break;
                    }while(1);
                    newcar.add(make,model,type,yr,price);
                   
                   
                    break;
            case 2:
                cout<<\"import a file of cars into the catalog (CSV file)\"<<endl;
                size = newcar.import(catelog);
                break;
            case 3:
                cout<<\"List all cars in catelog \"<<endl;
                if( size == 0 )
                {
                    cout<<\"Import catelog to list the cars \"<<endl;
                    break;
                }
                newcar.list(catelog,size);
               
                break;
            case 4:
                int choice;
                int size=0;
                cout<<\"To search car Enter 1. by make 2. model 3. type 4.year 5.sale_price 6.Quit searching\ \";
                while(1)
                {
                    cin>>choice;
                   if(choice == 6)
                        break;
                    switch(choice)
                    {
                        case 1:
                            cout<<\"Enter make to search: \";
                            cin>>make;
                            car_arr =newcar.search(catelog,size,make);
                            for( int i = 0 ;i <size; i ++)
                            car_arr[i].display();
                                break;
                        case 2:
                                cout<<\"Enter model to search: \";
                                cin>>model;
                                car_arr=newcar.search(catelog,size,model);
                                for( int i = 0 ;i <size; i ++)
                                    car_arr[i].display();
                                break;
                        case 3:
                                cout<<\"Enter type to search: \";
                                cin>>type;
                                car_arr=newcar.search(catelog,size,type);
                                for( int i = 0 ;i <size; i ++)
                                    car_arr[i].display();
                                break;
                        case 4:
                                cout<<\"Enter type to search: \";
                                cin>>yr;
                                car_arr=newcar.search(catelog,size,yr);
                                for( int i = 0 ;i <size; i ++)
                                    car_arr[i].display();
                                //car_arr[i].display();
                                break;
                        case 5:
                                cout<<\"Enter type to search: \";
                                cin>>price;
                                car_arr =newcar.search(catelog,size,price);
                                for( int i = 0 ;i <size; i ++)
                                    car_arr[i].display();
                                //car_arr[i].display();
                                break;
                        default:
                                cout<<\"Invalid choice \"<<endl;
                                break;
                    }
                }
               
                break;
        }
    }
}





