Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use C++ code that is applicable to an intro class. The last attachment is

ID: 3918491 • Letter: P

Question

Please use C++ code that is applicable to an intro class. The last attachment is the input.txt.

Write an application that shows the following menu 1) Load Product Records 2) Quit Enter Choice Load Product Records will load all records from a file (provided with the assignment, attached here). Once loaded, the products will stay in memory and should not be reloaded When loading the records, assign each product a unique random product ID (numbers only) of length 6. You must ensure the product ID is unique for each product. Product records will be name, price, quantity. For example Fuzzy Dice 12.95 23 Name can be any number of words and punctuation with a maximum length of 32 . Price will be of the form x.xx with no other characters (numbers and one decimal point only) . Quantity will be a count of the product on hand Once the records are loaded and IDs assigned to each product, display the following menu 1) Display All Product Records by ID 2) Display All Product Records by Name 3) Enter Product ID to Display Single Record 4) Enter Product Name to Display Single Record 5) Display High and Low Inventory Items 6) Save Products 7) Quit Enter Choice Each item will have the following functionality: Display All Product Records by ID . o Products will be displayed on screen in ascending order of ID o Display each entire record properly formatted with labels, like this: Product ID: 123456 Product: Fuzzy Dice Price: $12.95 Quantity: 23

Explanation / Answer

#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
#include<string.h>

using namespace std;

#define SIZE 100000

#define MIN 100000

#define MAX 999999

struct item{
   int id;
   string name;
   double price;
   int qty;

};

int main(){

    item data[SIZE];
   
    srand(time(NULL));
    cout << "1.Load Product Records ";
    cout << "2.Quit ";
    cout << "Enter choice:";
    int n;
    int id = MIN + rand() % (MAX - MIN +1);
    string line;
    getline(cin,line);
    n = atoi(line.c_str());
    if (n == 2)
       return 0;
    else if (n == 1){
        ifstream fin("input.txt");
        if (!fin){
           cout << "Error opening file ";
           return 0;
        }
        int count = 0;
        int max = 0;
        int min = MIN;
        int maxi = 0;
        int mini = 0;
        while(getline(fin,data[count].name)){
            data[count].name = data[count].name.substr(0,data[count].name.length()-1);
            data[count].id = MIN + rand() % ((MAX +1) - MIN);
            getline(fin,line);
            data[count].price = atof(line.c_str());
            getline(fin,line);
           
            data[count].qty = atof(line.c_str());
            if (data[count].qty > max){
               max = data[count].qty;
               maxi = count;
            }
            if (data[count].qty < min){
               min = data[count].qty;
               mini = count;
            }
            id++;
            count++;
        }
        fin.close();
        while(true){
            cout << "1.Display All Product Records by ID ";
            cout << "2.Display All Product Records by name ";
            cout << "3.Enter Product ID to Display Single Record ";
            cout << "4.Enter Product Name to Display Single Record ";
            cout << "5.Display High and Low Inventory Items ";
            cout << "6.Save Products ";
            cout << "7.Quit ";
            cout << "Enter choice:";
            int ch;
            getline(cin,line);
            ch = atoi(line.c_str());
            if (ch == 1){
               for (int i = 0; i<count; i++){
                   for (int j = i+1; j<count; j++){
                       if (data[i].id > data[j].id){
                          string tempn = data[i].name;
                          double tempp = data[i].price;
                          int tempq = data[i].qty;
                          int tempi = data[i].id;
                          data[i].id = data[j].id;
                          data[i].name = data[j].name;
                          data[i].price = data[j].price;
                          data[i].qty = data[j].qty;
                          data[j],id = tempi;
                          data[j].name = tempn;
                          data[j].price = tempp;
                          data[j].qty = tempq;
                       }
                   }
               }
               for (int i = 0; i<count; i++){
                   cout << "Product ID: " << data[i].id << endl;
                   cout << "Product: " << data[i].name << endl;
                   cout << "Price: $" << data[i].price << endl;
                   cout << "Quantity: " << data[i].qty << endl;
               }
            }
            else if (ch == 2){
               for (int i = 0; i<count; i++){
                   for (int j = i+1; j<count; j++){
                       string name1 = "";
                       string name2 = "";
                       for (int k = 0; k<data[i].name.length(); k++){
                           name1 = name1 + (char)tolower(data[i].name[k]);
                       }
                       for (int k = 0; k<data[j].name.length(); k++){
                           name2 = name2 + (char)tolower(data[j].name[k]);
                       }
                       if (name1 > name2){
                          string tempn = data[i].name;
                          double tempp = data[i].price;
                          int tempq = data[i].qty;
                          int tempi = data[i].id;
                          data[i].id = data[j].id;
                          data[i].name = data[j].name;
                          data[i].price = data[j].price;
                          data[i].qty = data[j].qty;
                          data[j],id = tempi;
                          data[j].name = tempn;
                          data[j].price = tempp;
                          data[j].qty = tempq;
                       }
                   }
               }
               for (int i = 0; i<count; i++){
                   cout << "Product ID: " << data[i].id << endl;
                   cout << "Product: " << data[i].name << endl;
                   cout << "Price: $" << data[i].price << endl;
                   cout << "Quantity: " << data[i].qty << endl;
               }
            }
            else if (ch == 3){
               cout << "Enter Product ID:";
               getline(cin,line);
               int id1 = atoi(line.c_str());
              
               int found = 0;
               for (int i = 0; i<count; i++){
                   if (data[i].id == id1){
                      cout << "Product ID: " << data[i].id << endl;
                      cout << "Product: " << data[i].name << endl;
                      cout << "Price: $" << data[i].price << endl;
                      cout << "Quantity: " << data[i].qty << endl;
                      found == 1;
                      break;
                   }
               }
               if (found == 0)
                  cout << "It does not exist ";
            }
            else if (ch == 4){
               cout << "Enter Product Name:";
               getline(cin,line);
               for (int k = 0; k<line.length(); k++)
                   line[k] = (char)tolower(line[k]);
               int found = 0;
               for (int i = 0; i<count; i++){
                  
                   string name1 = data[i].name;
                   for (int k = 0; k<data[i].name.length(); k++){
                       name1[k] = tolower(name1[k]);
                   }
                   if (name1 == line){
                      cout << "Product ID: " << data[i].id << endl;
                      cout << "Product: " << data[i].name << endl;
                      cout << "Price: $" << data[i].price << endl;
                      cout << "Quantity: " << data[i].qty << endl;
                      found == 1;
                      break;
                   }
               }
               if (found == 0)
                  cout << "It does not exist ";
            }
            else if (ch == 5){
               cout << data[mini].id << ":" << min << endl;
               cout << data[maxi].id << ":" << max << endl;
            }
            else if (ch == 6){
                 ofstream fout("out.txt");
                 for (int i = 0; i<count; i++){
                     fout << data[i].name << endl;
                     fout << data[i].price << endl;
                     fout << data[i].qty << endl;
                 }
                 fout.close();
            }
            else if (ch == 7){
                 break;
            }
            else {
                cout << "Invalid choice ";
            }
        }

    }
    return 0;
}