Question: Add menu options to retrieve a car record by a car by Id or Model name a user should be able to search by ID

Add menu options to retrieve a car record

by a car by Id or Model name a user should be able to search by ID or by item name create only one submenu option for searching the search should not be case sensitive (for example, a user may enter sTRolleR or STROLLER); store items in all lower/upper case to expedite the search write one search function that can search by ID and name and goes through the array of structs/objects only once allow partial search; must return all matching items (for example, a user can enter "roll" to search for a "stroller"; return "rolls", "stroller"); see C++ find() function show the entire record if found or an appropriate message if not by a price range user should be able to enter the target price and the range; for example target price: 5000 range %: 20 20% of 5000 is 1000, display all valid car records with a price at least $4000 (5000-1000) and no more than $6000 (5000+20%)

both IDs below should be valid but 12ABmP34Z is not found when testing code

12ABMP34Z Fusion_5 20 17000.00 12ABmP34Z Fusion_5 20 17000.00

My code:

#include #include #include #include #include using namespace std;

const int MAX_ID_LENGTH = 9;

// structure containing carID, model, quantity and price typedef struct { string carID; string model; string quantity; string price; } CarRecord;

// Function Prototypes bool isValidCarRecord(CarRecord car); void getData(CarRecord inventory[100], int *size); void printCarInventory(CarRecord inventory[100], int size, bool valid); int printMenu(); void printInvalidCarRecords(string fileName, CarRecord inventory[100], int size, bool valid); void invalidCarRecords(string fileName, CarRecord inventory[100], int size); void swap(CarRecord *xp, CarRecord *yp); void sort(CarRecord inventory[100], int size, int sortBy, bool valid); void searchBy_carID_model(CarRecord inventory[100], int size); void searchBy_price(CarRecord inventory[100], int size);

int main() { bool valid; // array of structure CarRecord CarRecord inventory[100]; int size = 0; int choice = 1; // reading file and storing in array getData(inventory, &size); // continue untill user chooses to exit while (choice != 8) { choice = printMenu();

switch (choice) { case 1: // for case 1 print inventory printCarInventory(inventory, size, valid); break; case 2: // for case 2 print invalid inventory printInvalidCarRecords("error.txt", inventory, size, valid); break; case 3: // for input 2 search inventory by carID searchBy_carID_model(inventory, size); break; case 4: // sort inventory by carID sort(inventory, size, 0, valid); break; case 5: // sort inventory by model sort(inventory, size, 1, valid); break; case 6: // sort inventory by quantity sort(inventory, size, 2, valid); break; case 7: // sort inventory by price sort(inventory, size, 3, valid); break; case 8: cout << "Goodbye"; break; default: // printing message for invalid input cout << "Invalid Input. Please try again. "; break; } } }

// Function to check if a car record is valid bool isValidCarRecord(CarRecord car) { string errorMessages = "Not a Valide Record "; if (car.carID.length() != MAX_ID_LENGTH) { return false; } if (!isdigit(car.carID[0]) || !isdigit(car.carID[1])) { return false; } for (int i = 2; i < 6; i++) { car.carID[i] = toupper(car.carID[i]); if (!isalpha(car.carID[i])) { return false; } if (car.carID[i] < 'A' || car.carID[i] > 'S' || car.carID[i] == 'O') { return false; } } for (int i = 6; i < MAX_ID_LENGTH; i++) { car.carID[i] = toupper(car.carID[i]); if (!isalnum(car.carID[i])) { return false; } } return true; }

// reading file void getData(CarRecord inventory[100], int *size) { ifstream file("carInventory.txt"); ofstream errorFile("error.txt"); if (file.is_open()) { string line; while (getline(file, line)) { size_t pos = 0; string token; CarRecord temp; pos = line.find(" "); token = line.substr(0, pos); temp.carID = token; if (!isValidCarRecord(temp)) { errorFile << line << " "; continue; } line.erase(0, pos + 1); pos = line.find(" "); token = line.substr(0, pos); temp.model = token; line.erase(0, pos + 1); pos = line.find(" "); token = line.substr(0, pos); temp.quantity = token; line.erase(0, pos + 1); temp.price = line; inventory[*size] = temp; *size = *size + 1; } file.close(); errorFile.close(); } else { cout << "Cannot open file. "; EXIT_SUCCESS; } }

// printing inventory void printCarInventory(CarRecord inventory[100], int size, bool valid) { if (valid == false) { cout << left << setw(10) << "Car ID" << setw(20) << "Model" << right << setw(10) << "Quantity" << setw(10) << "Price" << " " << left << setw(9) << setfill('-') << "-" << setw(20) << setfill('-') << "-" << right << setw(10) << setfill('-') << "-" << setw(10) << setfill('-') << "-" << " " << setfill(' ');

for (int i = 0; i < size; i++) { cout << left << setw(9) << inventory[i].carID << "\t" << setw(15) << inventory[i].model << "\t" << right << setw(10) << inventory[i].quantity << "\t" << fixed << setprecision(2) << setw(10) << inventory[i].price << " " << setprecision(6); }

cout << left << setw(9) << setfill('-') << "-" << setw(20) << setfill('-') << "-" << right << setw(10) << setfill('-') << "-" << setw(10) << setfill('-') << "-" << " " << setfill(' '); } }

int printMenu() { // choice variable int choice = 8; // showing Menu cout << " Please choose : " "1) Print Inventory " "2) print invalid records from an error file " "3) Search By Car ID or Model " "4) Sort By Car ID " "5) Sort By Model " "6) Sort By Quantity " "7) Sort By Price " "8) Exit. " ; // taking choice cin >> choice; // take choice again for invalid input

return choice; }

void printInvalidCarRecords(string fileName, CarRecord inventory[100], int size, bool valid) { ifstream errorFile("error.txt"); if (errorFile.is_open()) { string line; while (getline(errorFile, line)) { cout << " Invalid" << " " << "-----------------------------------" << " " << line << " "; } }

errorFile.close(); }

void invalidCarRecords(string fileName, CarRecord inventory[100], int size) { ofstream fout; fout.open("error.txt", ios::app); for (int i = 0; i < size; i++) { fout << inventory[i].carID << "\t" << inventory[i].model << "\t" << inventory[i].quantity << "\t" << inventory[i].price << " "; }

fout.close(); }

// swap two structures void swap(CarRecord *xp, CarRecord *yp) { CarRecord temp = *xp; *xp = *yp; *yp = temp; }

// sort using bubble sort. void sort(CarRecord inventory[100], int size, int sortBy, bool valid) { int i, j; bool condition;

for (i = 0; i < size - 1; i++) { for (j = 0; j < size - i - 1; j++) { // checking condition if (sortBy == 0) { condition = inventory[j].carID > inventory[j + 1].carID; } else if (sortBy == 1) { condition = inventory[j].model > inventory[j + 1].model; } else if (sortBy == 2) { condition = inventory[j].quantity > inventory[j + 1].quantity; } else if (sortBy == 3) { condition = inventory[j].price > inventory[j + 1].price; }

if (condition) { // if condition is true swap swap(&inventory[j], &inventory[j + 1]); } } } printCarInventory(inventory, size, valid); }

// Searching By carID void searchBy_carID_model(CarRecord inventory[100], int size) { string input; cout << " Please give carID or model : "; cin >> input; //bool isValidCarRecord(CarRecord car); cout << " " <

cout << " " <

// Searching By Price void searchBy_price(CarRecord inventory[100], int size){

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!