Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <sstream>
- #include <string>
- using namespace std;
- // Enum for menu options
- enum MenuOption {
- PRINT_UNSORTED_INVENTORY = 1,
- PRINT_INVALID_RECORDS = 2,
- QUIT_PROGRAM = 3
- };
- // Class for handling car records
- class Cars {
- public:
- string CarID;
- string CarModel;
- string Manufacturer;
- int Amount_on_Hand;
- bool isValidRecord() {
- return CarID.length() == 17 && CarModel.length() == 17 && Manufacturer.length() == 17 && Amount_on_Hand >= 0;
- }
- };
- // Function declarations
- void read_file(Cars cars[], Cars validCars[], Cars errorCars[]);
- void print_record(Cars cars[]);
- void print_line(Cars cars[]);
- void save_error(Cars errorCars[]);
- void save_valid(Cars validCars[]);
- void menu(Cars cars[], Cars errorCars[]);
- //Main Function use functions to read file, print file, and save invalid and valid records
- int main() {
- Cars cars[100];
- Cars errorCars[100];
- Cars validCars[100];
- read_file(cars, validCars, errorCars);
- menu(cars, errorCars);
- return 0;
- }
- // Function definitions
- void print_table_header() {
- cout << "CarID CarModel Manufacturer Amount_on_Hand" << endl;
- }
- void print_record_table(Cars cars[]) {
- for (int i = 0; i < 10; i++) {
- cout << left << setw(17) << cars[i].CarID << " ";
- cout << setw(17) << cars[i].CarModel << " ";
- cout << setw(17) << cars[i].Manufacturer << " ";
- cout << right << setw(10) << fixed << setprecision(2) << cars[i].Amount_on_Hand << endl;
- }
- }
- void save_table_header(ofstream& file) {
- file << "CarID CarModel Manufacturer Amount_on_Hand" << endl;
- }
- void save_record_table(ofstream& file, Cars cars[]) {
- for (int i = 0; i < 10; i++) {
- file << left << setw(17) << cars[i].CarID << " ";
- file << setw(17) << cars[i].CarModel << " ";
- file << setw(17) << cars[i].Manufacturer << " ";
- file << right << setw(10) << fixed << setprecision(2) << cars[i].Amount_on_Hand << endl;
- }
- }
- void read_file(Cars cars[], Cars validCars[], Cars errorCars[]) {
- // Function to read from input file
- ifstream inputFile("./inputfile.txt");
- string line;
- int carIndex = 0;
- int validIndex = 0;
- int errorIndex = 0;
- while (getline(inputFile, line)) {
- stringstream ss(line);
- string itemID, itemName, manufacturer;
- int quantity;
- if (getline(ss, itemID, ',') && getline(ss, itemName, ',') && getline(ss, manufacturer, ',') && ss >> quantity) {
- Cars car;
- car.CarID = itemID;
- car.CarModel = itemName;
- car.Manufacturer = manufacturer;
- car.Amount_on_Hand = quantity;
- if (car.isValidRecord()) {
- validCars[validIndex++] = car;
- }
- else {
- errorCars[errorIndex++] = car;
- }
- cars[carIndex++] = car; // Add the car record to the cars array
- }
- }
- inputFile.close();
- }
- void print_record(Cars cars[]) {
- // Function to print records
- for (int i = 0; i < 10; i++) {
- cout << cars[i].CarID << " " << cars[i].CarModel << " " << cars[i].Manufacturer << " " << cars[i].Amount_on_Hand << endl;
- }
- }
- void print_line(Cars cars[]) {
- // Function to print a single record
- for (int i = 0; i < 10; i++) {
- cout << cars[i].CarID << " " << cars[i].CarModel << " " << cars[i].Manufacturer << " " << cars[i].Amount_on_Hand << endl;
- }
- }
- void save_error(Cars errorCars[]) {
- // Function to save error records to a file
- ofstream errorFile("error.txt");
- for (int i = 0; i < 10; i++) {
- if (errorCars[i].CarID != "") {
- errorFile << errorCars[i].CarID << " " << errorCars[i].CarModel << " " << errorCars[i].Manufacturer << " " << errorCars[i].Amount_on_Hand << endl;
- }
- }
- errorFile.close();
- }
- void save_valid(Cars validCars[]) {
- // Function to save valid records to a file
- ofstream validFile("valid.txt");
- {
- for (int i = 0; i < 10; i++) {
- // Save validCars entries to valid.txt
- }
- }
- }
- void menu(Cars cars[], Cars errorCars[]) {
- // Function to display a menu using switch statement
- MenuOption choice;
- do {
- cout << "Menu:\n1. Print all items in the inventory unsorted\n2. Print invalid records from the error file\n3. Quit the program\n";
- cout << "Enter your choice: ";
- int menuChoice;
- cin >> menuChoice;
- choice = static_cast<MenuOption>(menuChoice);
- switch (choice) {
- case PRINT_UNSORTED_INVENTORY:
- print_record(cars);
- break;
- case PRINT_INVALID_RECORDS:
- print_record(errorCars);
- break;
- case QUIT_PROGRAM:
- cout << "Exiting the program.\n";
- return;
- default:
- cout << "Invalid choice. Please try again.\n";
- }
- } while (true);
- }
Advertisement
Add Comment
Please, Sign In to add comment