Advertisement
dmaisano

Inventory Program

Dec 4th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cctype>
  4. #include <cstdio>
  5. using namespace std;
  6.  
  7. struct Inventory {
  8.   string description;
  9.   int quantity;
  10.   double wholesaleCost;
  11.   double retailCost;
  12.   string dateAdded;
  13. };
  14.  
  15. void prompt();
  16. void addItem(fstream &);
  17. void viewItem(fstream &);
  18. void modifyItem(fstream &);
  19. void validate(int *);
  20. void validate(double *);
  21. string getDate();
  22. string validateDate(string);
  23.  
  24. int main() {
  25.   char userChoice;
  26.   fstream invFile("inventory.txt", ios::in | ios::out | ios::app);
  27.  
  28.   if (invFile.fail()) // creates file if a file does not already exist
  29.   {
  30.     invFile.open("inventory.txt");
  31.     invFile.close();
  32.   }
  33.  
  34.   do {
  35.     prompt();
  36.     cin >> userChoice;
  37.  
  38.     while(!isdigit(userChoice)) {
  39.       cout << "Enter a digit only.\nEnter choice: ";
  40.       cin >> userChoice;
  41.     }
  42.  
  43.     switch(userChoice) {
  44.       case '1':
  45.         addItem(invFile);
  46.         break;
  47.       case '2':
  48.         viewItem(invFile);
  49.         break;
  50.       case '3':
  51.         modifyItem(invFile);
  52.         break;
  53.     }
  54.   } while(userChoice != '4');
  55.  
  56.   invFile.close();
  57.  
  58.   return 0;
  59. }
  60.  
  61. void prompt() {
  62.   cout << "Choose from the following.\n";
  63.   cout << "1. Add new record(s) to the file.\n";
  64.   cout << "2. Display any record(s) in the file.\n";
  65.   cout << "3. Change any record(s) in the file.\n";
  66.   cout << "4. Exit\n";
  67.   cout << "Enter choice: ";
  68. }
  69.  
  70. void addItem(fstream& file) {
  71.   Inventory item;
  72.   string date;
  73.  
  74.   cout << "Enter the description of the item: ";
  75.   cin >> item.description;
  76.   file << "Description: " << item.description << endl;
  77.   string itemDesc = item.description;
  78.  
  79.   cout << "Enter the quantity of " << itemDesc << ": ";
  80.   cin >> item.quantity;
  81.   validate(&item.quantity);
  82.   file << "Quantity: " << item.quantity << endl;
  83.  
  84.   cout << "Enter the Wholesale Cost of " << itemDesc << ": $";
  85.   cin >> item.wholesaleCost;
  86.   validate(&item.wholesaleCost);
  87.   file << "Wholesale Cost $" << item.wholesaleCost << endl;
  88.  
  89.   cout << "Enter the Retail Cost of " << itemDesc << ": $";
  90.   cin >> item.retailCost;
  91.   validate(&item.retailCost);
  92.   file << "Retail Cost $" << item.retailCost << endl;
  93.  
  94.   item.dateAdded = getDate();
  95.   file << "Date added: " << item.dateAdded << "\n\n";
  96. }
  97.  
  98. void viewItem(fstream& file) {
  99.   file.close();
  100.   file.open("inventory.txt");
  101.   // closing and re-opening the file helps ensure that the file is being read from the beginning
  102.   string desc, line;
  103.  
  104.   cout << "Enter the name of the item you want to display from the record(s): ";
  105.   cin >> desc,
  106.   desc = "Description: " + desc;
  107.   getline(file, line); // gets the first line of the file
  108.  
  109.   while(true) {
  110.     if(line == desc) { // if the matching item is found, it will display the 5 lines of corresponding info on that item
  111.       for(int i = 0; i < 6; i++) {
  112.         cout << line << endl;
  113.         getline(file, line);
  114.       }
  115.     }
  116.  
  117.     getline(file, line); // gets the next line of the file
  118.  
  119.     if(file.eof()) { // conditional to break out of the loop once the end of the file is reached
  120.       break;
  121.     }
  122.   }
  123. }
  124.  
  125. void modifyItem(fstream& file) {
  126.   fstream temp("temp.txt", ios::out);
  127.   string desc, line;
  128.  
  129.   cout << "Enter the name of the item you want to modify from the record(s): ";
  130.   cin >> desc,
  131.   desc = "Description: " + desc;
  132.  
  133.   getline(file, line);
  134.  
  135.   while(line != desc) {
  136.     temp << line << endl;
  137.     getline(file, line);
  138.   }
  139.  
  140.   if(line == desc) {
  141.     addItem(temp);
  142.   }
  143.  
  144.   while(true) {
  145.     if(line == desc) {
  146.       for(int i = 0; i < 6; i++) {
  147.         getline(file, line);
  148.       }
  149.     }
  150.  
  151.     temp << line << endl;
  152.     getline(file, line);
  153.  
  154.     if(file.eof()) {
  155.       break;
  156.     }
  157.   }
  158.  
  159.   file.close();
  160.   temp.close();
  161.  
  162.   remove("inventory.txt");
  163.   rename("temp.txt", "inventory.txt");
  164.   if( remove( "temp.txt" ) != 0 )
  165.     perror( "Error deleting file" );
  166.   else
  167.     puts( "File successfully deleted" );
  168. }
  169.  
  170. string getDate() {
  171.   string date;
  172.  
  173.   cout << "Enter the date that the item was added to the inventory (mm/dd/yyyy): ";
  174.   cin >> date;
  175.   date = validateDate(date);
  176.  
  177.   return date;
  178. }
  179.  
  180. string validateDate(string date) {
  181.   for(int i = 0; i < 11; i++) {
  182.     if(i == 0 || i == 1 || i == 3 || i == 4 || i == 6 || i == 7 || i == 8 || i == 9) {
  183.       if(!(isdigit(date[i]))) {
  184.         cout << "Invalid date format!\n";
  185.         getDate();
  186.       }
  187.     }
  188.  
  189.     else if(i == 2 || i == 5) {
  190.       if(date[i] != '/') {
  191.         cout << "Invalid date format!\n";
  192.         getDate();
  193.       }
  194.     }
  195.   }
  196.  
  197.   return date;
  198. }
  199.  
  200. void validate(int *num) {
  201.   while(*num < 0) {
  202.     cout << "No negative numbers allowed.\nPlease enter a non-negative value: ";
  203.     cin >> *num;
  204.   }
  205. }
  206.  
  207. // validate function that takes a double as a parameter
  208. void validate(double *num) {
  209.   while(*num < 0) {
  210.     cout << "No negative numbers allowed.\nPlease enter a non-negative value: ";
  211.     cin >> *num;
  212.   }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement