deep5050
2/22/2018 - 9:29 AM

project on new car registration and save them in a .csv file

project on new car registration and save them in a .csv file


#include <iostream>
#include <cstring>
#include <fstream>
#define max_size 100
using namespace std;

// global count for cars
int apc_count = 0, nap_count = 0, acpu_count = 0, napu_count = 0;

/////////////////////////// base Car class /////////////////
class car
{
protected:
  struct technical_details // a structure to hold all the technical details of the car
  {
    string engine_number; // stores the engine number
    string model_number;  //stores the model number
    string manufacturer;  // stores the manufacturer name
  } a;
  struct purchase_details
  {
    string date_of_purchase;
    int valid_till;       // stores only the next renewal year
    int amount;           //
    string showroom_name; // stores the showroom name from where the car has purchased
  } b;
  struct user_details
  {
    string buyers_name;
    int buyer_age;
    string sex;
    long long int mobile_no;      // stores the contact number of the buyer
    long long int aadhar_no;      // stores the aadhar no of buyer
    string bank_name;        // stores the bank name of the buyer
    long long int account_number; // stores the bank account number of the buyer
  } c;
  string parts_under_servicing; //parts checked during servicing
  int servicing_charge;         //servicing charge

public:
  int registration_number;
  bool is_registered; // 1 for already registred,0 if not  // this flg is for signaling whether a car is alredy registred or not :)
  car()
  {
    set_blank();
  }
  virtual ~car()
  {
    set_blank();
  }
  virtual void set_blank();
  virtual void set_all();
  virtual void show_cars();
  virtual void new_car_registration();
  void renewal_registration();
  void owner_transfer();
  void servicing();
  void discard_cars();
  void show_technical();
  void show_purchase();
  void show_buyer();
  string get_engine_no()
  {
    return a.engine_number;
  }
  string get_model()
  {
    return a.model_number;
  }
  string get_owner()
  {
    return c.buyers_name;
  }
  string get_p_date()
  {
    return b.date_of_purchase;
  }
  long long int get_contact()
  {
    return c.mobile_no;
  }
};
/////////////// set blank ///////////////
void car ::set_blank()
{
  registration_number = -1;
  a.engine_number = "N/A";
  a.model_number = "N/A";
  a.manufacturer = "N/A";
  b.date_of_purchase = "UNKNOWN";
  b.valid_till = 0;
  b.amount = 0;
  b.showroom_name = "N/A";
  c.buyers_name = "N/A";
  c.buyer_age = 0;
  c.sex = "N/A";
  c.mobile_no = 0;
  c.aadhar_no = 0;
  c.bank_name = "UNKNOWN";
  c.account_number = 0;
  is_registered = 0;
}
///////// new car registration ///////
void car ::new_car_registration()
{
  if (is_registered != 1)
  {
    set_all();
    // cout << "your registration number is:";
  }

  else
    cout << "already registred!!" << endl;
}
////////////////////////////// show_technical()//////////////////////////
void car ::show_technical()
{
  //show only technical details
  cout << "Engine no.: " << a.engine_number << endl;
  cout << "manufacturer :" << a.manufacturer << endl;
  cout << "model no. :" << a.model_number << endl;
}
//////////////////////////////////// show_purchase()//////////////////////////
void car ::show_purchase()
{

  // show only purchase details
  cout << "purchase date :" << b.date_of_purchase << endl;
  cout << "registration end date :" << b.valid_till << endl;
  cout << "amount :" << b.amount << endl;
  cout << "showroom name :" << b.showroom_name << endl;
}
///////////////////////////////// show_buyer()///////////////////////////
void car ::show_buyer()
{
  //show only user details
  cout << "buyers name :" << c.buyers_name << endl;
  cout << "contact no. :" << c.mobile_no << endl;
  cout << "aadhar number :" << c.aadhar_no << endl;
}
////////////////////////////// show_cars() /////////////////////////////////
void car ::show_cars()
{

  if (is_registered == 1) // if already registered
  {
    cout << "\n====================================" << endl;
    cout << "registration number :" << registration_number << endl;
    // show everything
    // cout << "\n.............. buyer's details ..........." << endl;
    show_buyer();
    // cout << "\n........... purchase details..........." << endl;
    show_purchase();
    // cout << "\n........... technical details .........." << endl;
    show_technical();
    cout << endl;
  }
  else
    cout << "\nnot registered !!" << endl;
}

////////////////////////// set_all() ////////////////////////////////
void car ::set_all()
{
  cout << "***************** ENTER ALL THE DETAILS ****************" << endl;
  cout << "enter engine no:";
  fflush(stdin);
  cin >> a.engine_number;
  cout << "manufacturer :";
  fflush(stdin);
  cin >> a.manufacturer;
  cout << "model no. :";
  fflush(stdin);
  cin >> a.model_number;

  cout << "purchase date (dd/mm/yyyy) :";
  fflush(stdin);
  cin >> b.date_of_purchase;
  cout << "registration valid till (yyyy) :";
  fflush(stdin);
  cin >> b.valid_till;
  cout << "showroom name :";
  fflush(stdin);
  cin >> b.showroom_name;
  cout << "amount :";
  fflush(stdin);
  cin >> b.amount;

  cout << "buyers name :";
  fflush(stdin);
  cin >> c.buyers_name;
  cout << "buyers sex (F/M):";
  fflush(stdin);
  cin >> c.sex;
  cout << "buyers age :";
  fflush(stdin);
  cin >> c.buyer_age;
  cout << "buyers aadhar no.:";
  fflush(stdin);
  cin >> c.aadhar_no;
  cout << "contact no. ";
  fflush(stdin);
  cin >> c.mobile_no;
  cout << "bank name :";
  fflush(stdin);
  cin >> c.bank_name;
  cout << "account no. ";
  fflush(stdin);
  cin >> c.account_number;
  fflush(stdin);
  cout << "************* registration successful **************" << endl;
  cout << "============= YOUR REGISTRATION NUMBER IS :" << registration_number << "=================" << endl;
  is_registered = 1; // registration successful // now we can perform other actions
}

/////////////////// renewal registration ////////////
void car ::renewal_registration()
{

  if (is_registered == 1) // if alreday registered
  {
    b.valid_till = b.valid_till + 3; // assuming each registration valid for 3years
    cout << "done ! now your registration valid till :" << b.valid_till << endl;
  }
  else
    cout << "not registered yet !!" << endl;
}

///////////// ownership transfer /////////////////////
void car ::owner_transfer()
{
  if (is_registered == 1) // if alreday registered
  {
    cout << "enter new users details ....." << endl;
    cout << "buyers name :";
    fflush(stdin);
    cin >> c.buyers_name;
    cout << "buyers sex (F/M):";
    fflush(stdin);
    cin >> c.sex;
    cout << "buyers aadhar no.:";
    fflush(stdin);
    cin >> c.aadhar_no;
    cout << "contact no. ";
    fflush(stdin);
    cin >> c.mobile_no;
    cout << "bank name :";
    fflush(stdin);
    cin >> c.bank_name;
    cout << "account no. ";
    fflush(stdin);
    cin >> c.account_number;
    fflush(stdin);
    cout << "";
    cout << "<<<<<<<<<<<<<<<<<<>>>>>>>>>>" << endl;
    show_cars();
  }
  else
    cout << "not registered yet !!" << endl;
}

//////////////// servicing ///////
void car ::servicing()
{
  if (is_registered == 1) //if already registered
  {
    cout << "enter the parts under servicing :";
    fflush(stdin);
    cin >> parts_under_servicing;
    cout << "servicing charge : ";
    fflush(stdin);
    cin >> servicing_charge;
    fflush(stdin);
  }
  else
    cout << "this car is not registered yet !!" << endl;
}
/////////// discard cars //////////
void car ::discard_cars()
{
  if (is_registered == 1) //if already registered
  {
    set_blank();
    cout << " Discarded!!" << endl;
  }
  else
    cout << "register first !!" << endl;
}

/////////////////// private cars ////////////////////
class private_car : public virtual car
{
public:
  private_car() : car()
  {
    // nothing extra
  }
  virtual void set_all();
  string music_system_name;
  string navigation_system;
  string lcd_panel;
};
void private_car::set_all()
{
  car::set_all();
  cout << "\n.......... need some extra information ......" << endl;
  cout << "music system name:" << endl;
  fflush(stdin);
  cin >> music_system_name;
  cout << "navigation system ? (y/n):" << endl;
  fflush(stdin);
  cin >> navigation_system;
  cout << "lcd panel available ?(y/n): " << endl;
  fflush(stdin);
  cin >> lcd_panel;
  fflush(stdin);
}

//////////////////// derived classes //////////////////
//////////////// class ac_private_car /////////////
class ac_private_car : public virtual private_car
{
public:
  int ac_capacity;
  ac_private_car() : private_car()
  {
    ac_capacity = 0;
  }
  void set_all();
  void show_cars();
};
void ac_private_car::set_all()
{
  private_car::set_all();
  cout << "ac capacity (in ton) :" << endl;
  fflush(stdin);
  cin >> ac_capacity;
  fflush(stdin);
}

void ac_private_car::show_cars()
{
  if (is_registered == 1)
  {
    private_car::show_cars();
    cout << "ac capacity: " << ac_capacity;
    cout << endl;
    cout << "====================================\n";
  }
  else
    cout << "not registered yet !!" << endl;
}

/////////////////// class ac_private_car /////////////////
class non_ac_private_car : public virtual private_car
{
public:
  non_ac_private_car() : private_car()
  {
    //nothing extra
  }
};

//////////////// public car //////////
class public_car : virtual public car
{
public:
  public_car() : car()
  {
    // nothing extra
  }
  virtual void set_all();
  int base_fare; // some constant parameters for public type cars
  int rate_per_km;
};

void public_car::set_all()
{
  car::set_all();
}
////////////////// derived classes /////////

//////////// ac public car ///////////////
class ac_public_car : public virtual public_car
{
public:
  ac_public_car() : public_car()
  {
    base_fare = 20;
    rate_per_km = 9;
    ac_capacity = 0;
  }
  float ac_capacity;
  void show_cars();
  void set_all();
};

void ac_public_car::set_all()
{
  public_car::set_all(); // all public car features alongwith some extra info regarding AC
  cout << "ac capacity ?" << endl;
  fflush(stdin);
  cin >> ac_capacity;
  fflush(stdin);
}

void ac_public_car::show_cars()
{
  if (is_registered == 1)
  {
    car::show_cars();
    cout << "ac capacity: " << ac_capacity << endl;
    cout << "base fare:" << base_fare << endl;
    cout << "rate per km: " << rate_per_km << endl;
    cout << endl;
    cout << "====================================\n";
  }
  else
    cout << "not registered yet !!" << endl;
}

/////////// non ac public car //////////
class non_ac_public_car : virtual public public_car
{
public:
  non_ac_public_car() : public_car()
  {
    base_fare = 8; // set base fare for non ac type public car
    rate_per_km = 3;
  }

};


////////////////////////// storage for registered cars /////////////////////////
non_ac_private_car nap[max_size];
ac_private_car apc[max_size];
non_ac_public_car napu[max_size];
ac_public_car acpu[max_size];

int menu()
{

  int choice;
  cout << "-------------------" << endl;
  cout << "1. AC PRIVATE CAR\n2. NON AC PRIVATE CAR\n3. AC PUBLIC CAR\n4. NON AC PUBLIC CAR\n5. SHOW ALL REGISTERED CARS \n6. SEARCH A CAR AND UPDATE \n0. EXIT" << endl;
  cout << "-------------------" << endl;
  fflush(stdin);
  cin >> choice;
  fflush(stdin); // return value assigned to each car type
  return choice;
}

////////////////////////// show all available functions //////////////
int functions()
{
  int choice;
  cout << "----------------------------------------" << endl;
  cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<  WHAT TO DO WITH THIS CAR ? >>>>>>>>>>>>>>>>>>>>>>>>>>>>" << endl;
  cout << "1. NEW REGISTRATION\n2. RENEWAL REGESTRATION\n3. OWNER TRANSFER\n4. SERVICING\n5. SHOW DETAILS\n6. DISCARD\n0. RETURN TO MAIN" << endl;
  cout << "-----------------------------------" << endl;
  fflush(stdin);
  cin >> choice;
  fflush(stdin);
  return choice;
}

////////////// call functions ///////////

template <class any_car>               // template function to handle any object(car)
void call_functions(any_car &temp_car) // get the any_car type objects address
{
  any_car *car_ptr; // pointer to the car to achieve runtime polymorphism.
  car_ptr = &temp_car;
  int flag;
  do
  {
    flag = functions();
    switch (flag) // swith case for all available functions
    {
    case 1:
      car_ptr->new_car_registration(); // calls derived class :: new_car_registration() not of its base class !
      break;
    case 2:
      car_ptr->renewal_registration();
      break;
    case 3:
      car_ptr->owner_transfer();
      break;
    case 4:
      car_ptr->servicing();
      break;
    case 5:
      car_ptr->show_cars(); // calls derived class :: show_cars() not of its base class !
      break;
    case 6:
      car_ptr->discard_cars();
      break;
    case 0:
      cout << "*********** LETS CHOOSE ANOTHER CAR ***********\n\n";
      break;
    default:
      cout << "enter correct choice !!" << endl;
    }
  } while (flag != 0);
}

///////////////////// store cars //////////////
void store_nap(non_ac_private_car temp, int i)
{
  nap[i] = temp;
}
void store_apc(ac_private_car temp, int i)
{
  apc[i] = temp;
}
void store_napu(non_ac_public_car temp, int i)
{
  napu[i] = temp;
}
void store_acpu(ac_public_car temp, int i)
{
  acpu[i] = temp;
}

////////////////// show_stored_cars() /////////////
void display_nap(int nap_count) // non ac private
{
  if (nap_count == 0)
  {
    cout << "[ EMPTY ]" << endl;
    return;
  }
  // non_ac_private_car *ptr;
  int i = 0;
  for (i = 0; i < nap_count; i++)
  {
    nap[i].show_cars();
  }
}

void display_apc(int apc_count) // ac private
{
  if (apc_count == 0)
  {
    cout << "[ EMPTY ]" << endl;
    return;
  }
  int i = 0;
  for (i = 0; i < apc_count; i++)
  {
    apc[i].show_cars();
  }
}

void display_napu(int napu_count) // non ac public
{
  if (napu_count == 0)
  {
    cout << "[ EMPTY ]" << endl;
    return;
  }
  int i = 0;
  for (i = 0; i < napu_count; i++)
  {
    napu[i].show_cars();
  }
}

void display_acpu(int acpu_count) // ac public
{
  if (acpu_count == 0)
  {
    cout << "[ EMPTY ]" << endl;
    return;
  }
  int i = 0;
  for (i = 0; i < acpu_count; i++)
  {
    acpu[i].show_cars();
  }
}

////////// show _stored_class() ////////////
int show_stored_cars()
{
  int choice;
  cout << "1. non ac private car \n2.ac private car \n3. non ac public car \n4. ac public car" << endl;
  cout << "enter a choice :";
  cin >> choice;
  return choice;
}
///////////////// select and update ///////

void select_and_update()
{
  int i, temp_reg;
  cout << "Enter the registration number: ";
  cin >> temp_reg;
  if (temp_reg >= 1000 and temp_reg <= 1999)
  {
    for (i = 0; i <= apc_count; i++) // ac private car
    {
      if (apc[i].registration_number == temp_reg) // if a match
      {
        cout << "((((((((((((((((( AC PRIVATE CAR )))))))))))))))" << endl;
        call_functions<ac_private_car>(apc[i]);
        return;
      }
    }
  }

  else if (temp_reg >= 2000 and temp_reg <= 2999)
  {
    for (i = 0; i <= nap_count; i++) //non ac private car
    {
      if (nap[i].registration_number == temp_reg) // if a match
      {
        cout << "((((((((((((((((( NON AC PRIVATE CAR )))))))))))))))" << endl;
        call_functions<non_ac_private_car>(nap[i]);
        return;
      }
    }
  }

  else if (temp_reg >= 3000 and temp_reg <= 3999)
  {
    for (i = 0; i <= acpu_count; i++) // AC PUBLIC CAR
    {
      if (acpu[i].registration_number == temp_reg) // if a match
      {
        cout << "((((((((((((((((( AC PUBLIC CAR )))))))))))))))" << endl;
        call_functions<ac_public_car>(acpu[i]);
        return;
      }
    }
  }

  else if (temp_reg >= 4000 and temp_reg <= 4999)
  {
    for (i = 0; i <= napu_count; i++) // ac private car
    {
      if (napu[i].registration_number == temp_reg) // if a match
      {
        cout << "(((((((((((((((((NON AC PUBLIC CAR )))))))))))))))" << endl;
        call_functions<non_ac_public_car>(napu[i]);
        return;
      }
    }
  }
  else
    cout << "enter correct registrationo number !!" << endl;
}

///////////////////////////////////////////// store in different  excel sheet ///////////////////////
template <class any_car>
void create_Excel_sheet(ofstream &new_csv, any_car temp)
{

  // csv format for all cars (basic details only)

  // reg_no | engine_no |model no | owners_name |owners_contact | purchase_date | 

  // create  a row
  new_csv << temp.registration_number << "," << temp.get_engine_no() << "," << temp.get_model() << "," << temp.get_owner() << "," << temp.get_contact() << "," << temp.get_p_date() << "\n";
}
//////////////////////////////////////////// handle csv ////////////////// //////

void handle_csv()
{
  int i;
  //write to csv file.....
  // non ac private

  ofstream _csv_nap_("non_ac_private.csv", ios::ate);
  // create header
  _csv_nap_ << "REGISTRATION_NO,ENGINE_NO,MODEL NO,OWNER NAME,CONTACT DETAILS,PURCHASE DATE\n";
  for (i = 0; i < nap_count; i++)
  {
    create_Excel_sheet(_csv_nap_, nap[i]);
  }
  _csv_nap_.close();

  // ac private
  ofstream _csv_apc_("ac_private.csv", ios::ate);
  _csv_apc_ << "REGISTRATION_NO,ENGINE_NO,MODEL NO,OWNER NAME,CONTACT DETAILS,PURCHASE DATE\n";
  for (i = 0; i < apc_count; i++)
  {
    create_Excel_sheet(_csv_apc_, apc[i]);
  }
  _csv_apc_.close();

  // ac public
  ofstream _csv_acpu_("ac_public.csv", ios::ate);
  _csv_acpu_ << "REGISTRATION_NO,ENGINE_NO,MODEL NO,OWNER NAME,CONTACT DETAILS,PURCHASE DATE\n";
  for (i = 0; i < acpu_count; i++)
  {
    create_Excel_sheet(_csv_acpu_, acpu[i]);
  }
  _csv_acpu_.close();

  // non ac public
  ofstream _csv_napu_("non_ac_public.csv", ios::ate);
  _csv_napu_ << "REGISTRATION_NO,ENGINE_NO,MODEL NO,OWNER NAME,CONTACT DETAILS,PURCHASE DATE\n";
  for (i = 0; i < napu_count; i++)
  {
    create_Excel_sheet(_csv_napu_, napu[i]);
  }
  _csv_napu_.close();
}

/////////////////////////// driver program /////////////////
int main()
{
  int flag2 = 0, u_id = 1000;
  string temp_reg;
  int choice, flag;
  do
  {
    choice = menu();
    switch (choice)
    {
    case 01: // ac private cars scope starts here
    {
      ac_private_car temp_car;
      temp_car.registration_number = (u_id * 1) + apc_count;
      store_apc(temp_car, apc_count); // store the car
      call_functions<ac_private_car>(apc[apc_count]);
      apc_count++;
      break;
    } // end of scope for ac_private_car type objects

    case 2: // non ac private car
    {
      non_ac_private_car temp_car;
      temp_car.registration_number = (u_id * 2) + nap_count; // generate registration no. between 2000 to max 2999
      store_nap(temp_car, nap_count);                        // store the car
      call_functions<non_ac_private_car>(nap[nap_count]);
      nap_count++;
      break;
    }
      // end of scope for non_ac_private_car type objects

    case 3:
    {
      ac_public_car temp_car;
      temp_car.registration_number = (u_id * 3) + acpu_count;
      store_acpu(temp_car, acpu_count); // store the car
      call_functions<ac_public_car>(acpu[acpu_count]);
      acpu_count++;
      break;
    } // end of scope for ac_public_car type objects

    case 4:
    {
      non_ac_public_car temp_car;
      temp_car.registration_number = (u_id * 4) + napu_count;
      store_napu(temp_car, napu_count); // store the car
      call_functions<non_ac_public_car>(napu[napu_count]);
      napu_count++;
      break;
    } // end of scope for non_ac_public_car type of objects
    case 6:
      select_and_update();
      break;
    case 0:
      handle_csv();
      cout << "************* THANK YOU ************ " << endl;
      cout << "$$$$$$$$$$$$$$ CHECK FOR CSV FILES IN CURRENT DIRECTORY $$$$$$$$$$$$$$$" << endl;
      return 0;
      break;
    case 5:
      flag2 = show_stored_cars();
      if (flag2 == 1) // non_ac_private cars
      {
        display_nap(nap_count);
        break;
      }
      else if (flag2 == 2) // ac private car
      {
        display_apc(apc_count);
        break;
      }
      else if (flag2 == 3) // non ac public car
      {
        display_napu(napu_count);
        break;
      }
      else if (flag2 == 4) // ac public car
      {
        display_acpu(acpu_count);
        break;
      }
      else
      {
        cout << "wrong entry !!" << endl;
        break;
      }

    default:
      cout << "wrong entry !!" << endl;
    } // end of switch case

  } while (1); // end of do-while loop

  return 0; // exit from main()
}