Nevemlaci

Untitled

Feb 18th, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.02 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. // Enum for menu options
  9. enum MenuOption {
  10.     PRINT_UNSORTED_INVENTORY = 1,
  11.     PRINT_INVALID_RECORDS = 2,
  12.     QUIT_PROGRAM = 3
  13. };
  14.  
  15. // Class for handling car records
  16. class Cars {
  17. public:
  18.     string CarID;
  19.     string CarModel;
  20.     string Manufacturer;
  21.     int Amount_on_Hand;
  22.     bool isValidRecord() {
  23.         return CarID.length() == 17 && CarModel.length() == 17 && Manufacturer.length() == 17 && Amount_on_Hand >= 0;
  24.     }
  25. };
  26.  
  27. // Function declarations
  28. void read_file(Cars cars[], Cars validCars[], Cars errorCars[]);
  29. void print_record(Cars cars[]);
  30. void print_line(Cars cars[]);
  31. void save_error(Cars errorCars[]);
  32. void save_valid(Cars validCars[]);
  33. void menu(Cars cars[], Cars errorCars[]);
  34.  
  35. //Main Function use functions to read file, print file, and save invalid and valid records
  36. int main() {
  37.  
  38.     Cars cars[100];
  39.     Cars errorCars[100];
  40.     Cars validCars[100];
  41.     read_file(cars, validCars, errorCars);
  42.     menu(cars, errorCars);
  43.     return 0;
  44. }
  45.  
  46. // Function definitions
  47. void print_table_header() {
  48.     cout << "CarID            CarModel         Manufacturer      Amount_on_Hand" << endl;
  49. }
  50.  
  51. void print_record_table(Cars cars[]) {
  52.     for (int i = 0; i < 10; i++) {
  53.         cout << left << setw(17) << cars[i].CarID << " ";
  54.         cout << setw(17) << cars[i].CarModel << " ";
  55.         cout << setw(17) << cars[i].Manufacturer << " ";
  56.         cout << right << setw(10) << fixed << setprecision(2) << cars[i].Amount_on_Hand << endl;
  57.     }
  58. }
  59.  
  60. void save_table_header(ofstream& file) {
  61.     file << "CarID            CarModel         Manufacturer      Amount_on_Hand" << endl;
  62. }
  63.  
  64. void save_record_table(ofstream& file, Cars cars[]) {
  65.     for (int i = 0; i < 10; i++) {
  66.         file << left << setw(17) << cars[i].CarID << " ";
  67.         file << setw(17) << cars[i].CarModel << " ";
  68.         file << setw(17) << cars[i].Manufacturer << " ";
  69.         file << right << setw(10) << fixed << setprecision(2) << cars[i].Amount_on_Hand << endl;
  70.     }
  71. }
  72.  
  73. void read_file(Cars cars[], Cars validCars[], Cars errorCars[]) {
  74.     // Function to read from input file
  75.     ifstream inputFile("./inputfile.txt");
  76.     string line;
  77.     int carIndex = 0;
  78.     int validIndex = 0;
  79.     int errorIndex = 0;
  80.     while (getline(inputFile, line)) {
  81.         stringstream ss(line);
  82.         string itemID, itemName, manufacturer;
  83.         int quantity;
  84.         if (getline(ss, itemID, ',') && getline(ss, itemName, ',') && getline(ss, manufacturer, ',') && ss >> quantity) {
  85.             Cars car;
  86.             car.CarID = itemID;
  87.             car.CarModel = itemName;
  88.             car.Manufacturer = manufacturer;
  89.             car.Amount_on_Hand = quantity;
  90.  
  91.             if (car.isValidRecord()) {
  92.                 validCars[validIndex++] = car;
  93.             }
  94.             else {
  95.                 errorCars[errorIndex++] = car;
  96.             }
  97.  
  98.             cars[carIndex++] = car; // Add the car record to the cars array
  99.         }
  100.     }
  101.     inputFile.close();
  102. }
  103. void print_record(Cars cars[]) {
  104.     // Function to print records
  105.     for (int i = 0; i < 10; i++) {
  106.         cout << cars[i].CarID << " " << cars[i].CarModel << " " << cars[i].Manufacturer << " " << cars[i].Amount_on_Hand << endl;
  107.     }
  108. }
  109. void print_line(Cars cars[]) {
  110.     // Function to print a single record
  111.     for (int i = 0; i < 10; i++) {
  112.         cout << cars[i].CarID << " " << cars[i].CarModel << " " << cars[i].Manufacturer << " " << cars[i].Amount_on_Hand << endl;
  113.     }
  114. }
  115. void save_error(Cars errorCars[]) {
  116.     // Function to save error records to a file
  117.     ofstream errorFile("error.txt");
  118.     for (int i = 0; i < 10; i++) {
  119.         if (errorCars[i].CarID != "") {
  120.             errorFile << errorCars[i].CarID << " " << errorCars[i].CarModel << " " << errorCars[i].Manufacturer << " " << errorCars[i].Amount_on_Hand << endl;
  121.         }
  122.     }
  123.     errorFile.close();
  124. }
  125. void save_valid(Cars validCars[]) {
  126.     // Function to save valid records to a file
  127.     ofstream validFile("valid.txt");
  128.     {
  129.         for (int i = 0; i < 10; i++) {
  130.             // Save validCars entries to valid.txt
  131.         }
  132.     }
  133. }
  134. void menu(Cars cars[], Cars errorCars[]) {
  135.     // Function to display a menu using switch statement
  136.     MenuOption choice;
  137.     do {
  138.         cout << "Menu:\n1. Print all items in the inventory unsorted\n2. Print invalid records from the error file\n3. Quit the program\n";
  139.         cout << "Enter your choice: ";
  140.         int menuChoice;
  141.         cin >> menuChoice;
  142.         choice = static_cast<MenuOption>(menuChoice);
  143.         switch (choice) {
  144.         case PRINT_UNSORTED_INVENTORY:
  145.             print_record(cars);
  146.             break;
  147.         case PRINT_INVALID_RECORDS:
  148.             print_record(errorCars);
  149.             break;
  150.         case QUIT_PROGRAM:
  151.             cout << "Exiting the program.\n";
  152.             return;
  153.         default:
  154.             cout << "Invalid choice. Please try again.\n";
  155.         }
  156.     } while (true);
  157. }
Advertisement
Add Comment
Please, Sign In to add comment