Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h> //for exit() function
- #include <fstream> //for creating text file
- #include "Car.h" //child class
- using namespace std;
- clear
- //Global variables
- string manufacturer, type, carPlateNo, model;
- int seat;
- const int SIZE = 80; //SIZE is the number of objects that the c[] array can store
- Car c[SIZE]; //c[SIZE] is an array used to store all 80 car details
- string option; //vairable for y/n choices
- int choice; //choice is used for user selections(in menu selection, type selection)
- int total = 0; //total recorded cars(used to keep count of total cars)
- int main();
- //Type options/selection(used in addRecord() & updateRecord())
- string funcType() {
- string type;
- cout << "Select the type of the car\n " <<
- "1.Hatchback\n" <<
- " 2.Sedan\n" <<
- " 3.MPV\n" <<
- " 4.SUV\n" <<
- " 5.Crossover\n" <<
- " 6.Coupe\n" <<
- " 7.Convertible\n" <<
- " 8.Others \n";
- cout << "Choice: ";
- cin >> choice;
- cin.clear();
- cin.ignore(1, '\n');
- switch (choice) {
- case 1:
- return "Hatchback";
- break;
- case 2:
- return "Sedan";
- break;
- case 3:
- return "MPV";
- break;
- case 4:
- return "SUV";
- break;
- case 5:
- return "Crossover";
- break;
- case 6:
- return "Coupe";
- break;
- case 7:
- return "Convertible";
- break;
- case 8:
- cout << "Enter Type: ";
- getline(cin, type);
- return type;
- break;
- default:
- //print error messege and allow user to re-enter choice
- cout << choice << " is not valid choice. Please choose again" << endl;
- return funcType();
- }
- }
- //Detects special characters and certain alphabets in car plate number(used in addRecord() & updateRecord())
- char checker(string carPlateNo) {
- char cpnError;
- string cpnc(carPlateNo);
- if (cpnc.find_first_not_of("abcdefghjklmnpqrstuvwxyABCDEFGHJKLMNPQRSTUVWXY0123456789 ") != string::npos) { //check until the end of the string
- cout << "Error. There are only alphabets(except for I,O & Z) and numbers in a car plate number.\n";
- return cpnError = 'a';
- } else {
- return cpnError = 'b';
- }
- }
- //Detects if there is anything other than y or n ((used in addRecord() & updateRecord())
- string checker2(string option) {
- string ynError(option);
- if (ynError.find_first_not_of("ynYN") != string::npos) {
- cout << "Invalid input. Only 'y' or 'n'." << endl;
- return ynError = "a";
- } else {
- return ynError = "b";
- }
- }
- //+1 total recorded cars(used in addRecord())
- void myPlus() {
- total++;
- }
- //-1 total recorded cars(used in deleteRecord())
- void myMinus() {
- total--;
- }
- //OPTION 1: Creating cars
- void addRecord() {
- cout << "------------------=CREATE=------------------" << endl;
- cout << "Note: Only a maximum of 80 cars can be created." << endl;
- cout << "------------------Car #" << total + 1 << "-------------------" << endl;
- //Manufacturer
- cout << "Please enter the manufacturer of the car: ";
- getline(cin, manufacturer);
- c[total].setManufacturer(manufacturer);
- //Type
- c[total].setType(funcType());
- //SeatNumber
- string ynError; //ynError is used to check for anything other than y or n
- do {
- cout << "Do you know what is the number of seats?(y/n)\n";
- cin >> option;
- ynError = checker2(option);
- } while ((ynError == "a")); //loop if option has anything other then y or n
- if ((option == "y") || (option == "Y")) {
- do {
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Please enter number of seats: \n";
- cin >> seat;
- if ((cin.fail()) || (seat <= 0)) {
- cout << endl;
- cout << "Invalid input. Please key in a positive integer." << endl; //print error message
- }
- } while ((cin.fail()) || (seat <= 0)); //condition to loop and allow user to re-enter
- c[total].setSeat(seat);
- } else {
- c[total].setSeat(1); //seats number is set to 1 if it is unknown(default)
- }
- //Car Plate Number
- char cpnError; //cpnError is used to check for special characters and certain alphabets
- cout << "Please enter the car plate number(e.g.: PNM1234): \n";
- cin.ignore(1, '\n');
- getline(cin, carPlateNo);
- cpnError = checker(carPlateNo);
- if (cpnError == 'a') { //if carPlateNo have any special characters or certain alphabets
- do {
- cout << "Please enter the car plate number(e.g.: PNM1234): \n";
- getline(cin, carPlateNo);
- cpnError = checker(carPlateNo);
- } while (cpnError == 'a'); //will loop and allow user to re-enter if special characters/certain alphabets was entered
- }
- c[total].setCarPlateNo(carPlateNo);
- //Model
- cout << "Please enter the model of the car: \n";
- getline(cin, model);
- c[total].setModel(model);
- //+1 to total
- myPlus();
- cout << "-------------------------------------------" << endl;
- cout << "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("clear"); //erase previous output
- main(); //return to main() function
- }
- //OPTION 2: OPTION 1: Display only one car of user's choice
- void disCar() {
- int sd; //sd(single display) is the number of the car that user wants to display;
- do {
- //user selects which car they want to display
- cout << "Enter 1 to display the first car that was created,2 for the second, etc." << endl;
- cout << "Please enter which vehicle you want to display: ";
- cin >> sd;
- //checks if the user chose within the number of recorded cars
- if ((sd <= 0) || (sd > total) || (cin.fail())) {
- system("clear");
- cout << "Invalid input. You only have " << total << " car(s) in the car record." << endl <<
- "Please enter a number within range." << endl;
- cout << endl;
- }
- } while ((sd <= 0) || (sd > total) || (cin.fail())); //allow user to re-enter
- //Print user's selected car details
- cout << "------------------Car #" << sd << "------------------" << endl;
- c[sd - 1].display();
- cout << "-------------------------------------------" << endl;
- getchar(); //pauses screen before previous output is erased
- cout << "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("clear");
- main();
- }
- //OPTION 2: OPTION 2: Display all cars that is in the records
- void disCreated() {
- for (int i = 0; i < total; i++) {
- cout << "------------------Car #" << i + 1 << "------------------" << endl;
- c[i].display();
- cout << "-------------------------------------------" << endl;
- }
- cout << "Press enter to continue . . .";
- getchar();
- system("clear");
- main();
- }
- //OPTION 2: Display Options Menu
- void displayRecord() {
- cout << "Select the choice"<<endl
- <<"1.Display a single car" <<endl
- <<"2.Display all cars in the car record"<<endl;
- cout << "Choice: ";
- cin >> choice;
- cin.clear();
- cin.ignore(1, '\n');
- system("clear");
- switch (choice) {
- case 1:
- //print error messege to user when there is no cars in record
- if (total == 0) {
- cout << "You have no created cars available." << endl;
- main();
- }
- //goes to disCar() function (display single car)
- disCar();
- break;
- case 2:
- if (total == 0) {
- cout << "You have no created cars available." << endl;
- main();
- }
- disCreated();
- break;
- default:
- //print error messege and allow user to re-enter choice
- cout << choice << " is not valid choice" << endl;
- displayRecord();
- }
- }
- //OPTION 3: Updating/Overriding previous car details of user's choice
- //Note: updateRecord() is similar to addRecord()
- void updateRecord() {
- int u; //u is the number of the vehicle that user wants to update;
- cout << "------------------=UPDATE=------------------" << endl;
- do {
- //user selects which car they want to update
- cout << "Enter 1 to update the first cars that was created,2 for the second, etc." << endl;
- cout << "Please enter which vehicle you want to update: ";
- cin >> u;
- //checks if the user chose within the number of recorded cars
- if ((u <= 0) || (u > total) || (cin.fail())) {
- system("CLS");
- cout << "Invalid input. You only have " << total << " car(s) in the car record." << endl <<
- "Please enter a number within range." << endl;
- cin.clear();
- cin.ignore(1000,'\n');
- cout << endl;
- }
- } while ((u <= 0) || (u > total) || (cin.fail()));
- //Prints details of the car user wants to update
- cout << endl;
- cout << "------------------Current Car #" << u << "------------------" << endl;
- c[u - 1].display();
- cout << "-------------------------------------------" << endl;
- cout << endl;
- //Manufacturer
- cout << "Please enter the manufacturer of the car: ";
- cin.clear();
- cin.ignore(1000, '\n');
- getline(cin, manufacturer);
- c[u - 1].setManufacturer(manufacturer);
- //Type
- c[u - 1].setType(funcType());
- //SeatNumber
- string ynError;
- do {
- cout << "Do you know what is the number of seats?(y/n)\n";
- cin >> option;
- ynError = checker2(option);
- } while ((ynError == "a"));
- if ((option == "y") || (option == "Y")) {
- do {
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Please enter number of seats: \n";
- cin >> seat;
- if ((cin.fail()) || (seat <= 0)) {
- cout << endl;
- cout << "Invalid input. Please key in a positive integer." << endl; //print error message
- }
- } while ((cin.fail()) || (seat <= 0)); //condition to loop and allow user to re-enter
- c[total].setSeat(seat);
- } else {
- c[total].setSeat(1); //seats number is set to 1 if it is unknown(default)
- }
- //Car Plate Number
- char cpnError; //cpnError is used to check for special characters and certain alphabets
- cout << "Please enter the car plate number(e.g.: PNM1234): \n";
- cin.ignore(1, '\n');
- getline(cin, carPlateNo);
- cpnError = checker(carPlateNo);
- if (cpnError == 'a') { //if carPlateNo have any special characters or certain alphabets
- do {
- cout << "Please enter the car plate number(e.g.: PNM1234): \n";
- getline(cin, carPlateNo);
- cpnError = checker(carPlateNo);
- } while (cpnError == 'a'); //will loop and allow user to re-enter if special characters/certain alphabets was entered
- }
- c[u - 1].setCarPlateNo(carPlateNo);
- //Model
- cout << "Please enter the model of the car: \n";
- getline(cin, model);
- c[u - 1].setModel(model);
- cout << "-------------------------------------------" << endl;
- cout << "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- main();
- }
- //OPTION 4: Delete a car of user's choice
- void deleteRecord() {
- string ynError;
- int d; //d is number of the car that user wants to delete;
- cout << "------------------=DELETE=------------------" << endl;
- do {
- //user selects which car they want to delete
- cout << "Enter 1 to delete the first car that was created,2 for the second, etc." << endl;
- cout << "Please enter which car you want to delete: ";
- cin >> d;
- if ((d <= 0) || (d > total) || (cin.fail())) {
- system("clear");
- cout << "Invalid input. You only have " << total << " car(s) in the car record." << endl <<
- "Please enter a number within range." << endl;
- cout << endl;
- }
- } while ((d <= 0) || (d > total) || (cin.fail()));
- cout << "------------------Car #" << d << "------------------" << endl;
- c[d - 1].display();
- cout << "-------------------------------------------" << endl;
- cout << endl;
- do {
- cout << "Are you sure you want to delete this car?(y/n)" << endl;
- cin.clear();
- cin.ignore();
- cin >> option;
- ynError = checker2(option);
- } while (ynError == "a");
- if ((option == "y") || (option == "Y")) {
- for (int i = d - 1; i < SIZE - 1; i++) {
- c[i] = c[d]; //replaces a car's details with details of the next car starting from the car that the user chose to delete
- d++;
- }
- c[SIZE - 1] = Car(); //give the last car(car #80) default values
- myMinus();
- }
- cout << "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- main();
- }
- //OPTION 5: Creating a .txt file(To save car record)
- void textFile() {
- //declare variables
- string fileName, ma, ty, cpn, mo;
- int se;
- cout << "------------------=Create Txt File=------------------" << endl;
- //Create file with the file name user entered
- cout << "Enter file name: ";
- cin >> fileName;
- cout << "Your .txt file can be found in the same folder where your .cpp file is saved." << endl;
- fileName = fileName + ".txt";
- //output to .txt file
- ofstream file(fileName.c_str());
- if (file.is_open()) {
- //output all the cars in car record
- for (int i = 0; i < total; i++) {
- file << "------------------Car #" << i + 1 << "------------------" << endl;
- ma = c[i].getManufacturer(); //get the manufacturer value of the car stored in the array
- file << "Manufacturer: " << ma << endl;
- ty = c[i].getType();
- file << "Type: " << ty << endl;
- se = c[i].getSeat();
- file << "Seat: " << se << endl;
- cpn = c[i].getCarPlateNo();
- file << "Car Plate Number: " << cpn << endl;
- mo = c[i].getModel();
- file << "Model: " << mo << endl;
- file << "-------------------------------------------" << endl;
- }
- file.close(); //close file
- }
- cin.get();
- cout << "-----------------------------------------------------" << endl;
- cout << "Press enter to continue . . .";
- cin.clear();
- cin.ignore(1000, '\n');
- system("CLS");
- main();
- }
- int main() {
- cout << "===============WELCOME TO CAR MANAGEMENT SYSTEM===============" << endl;
- cout << "Please enter your selection" << endl <<
- "1. Create new car" << endl <<
- "2. View car(s)" << endl <<
- "3. Update car" << endl <<
- "4. Delete " << endl <<
- "5. Create txt file" << endl <<
- "6. Exit" << endl;
- cout << "Choice: ";
- cin >> choice;
- cin.clear();
- cin.ignore(1, '\n');
- system("clear");
- switch (choice) {
- case 1:
- if (total == 80) {
- cout << "You have reached the maximum number of cars that can be created" << endl;
- }
- addRecord();
- break;
- case 2:
- displayRecord();
- break;
- case 3:
- //print error messege to user when there is no cars in record
- if (total == 0) {
- cout << "You have no created cars available." << endl;
- main();
- }
- updateRecord();
- break;
- case 4:
- if (total == 0) {
- cout << "You have no created cars available." << endl;
- main();
- }
- deleteRecord();
- break;
- case 5:
- textFile();
- break;
- case 6:
- exit(1); //behaves like return 1 and ends the program
- break;
- default:
- cout << choice << " is not valid choice" << endl;
- main();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement