Vla_DOS

Untitled

Jul 25th, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <tchar.h>
  5. #include <string>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. class Shop
  11. {
  12.  
  13. public:
  14.     string shopName, productName;
  15.     float quantityGoods = 0, pricePerKilogram = 0;
  16.  
  17.     Shop(){}
  18.     Shop(string s, string pro, float q, float pri) : shopName(s), productName(pro), quantityGoods(q), pricePerKilogram(pri)
  19.     {}
  20.  
  21.  
  22.     static void Write(const string path, vector<Shop> operations) {
  23.  
  24.         ofstream out(path, ios::ate);
  25.  
  26.         if (out.is_open())
  27.         {
  28.             for (int i = 0; i < operations.size(); i++)
  29.             {
  30.                 out << operations[i].shopName << " " << operations[i].productName << " " << operations[i].quantityGoods << " " << operations[i].pricePerKilogram << endl;
  31.             }
  32.         }
  33.         out.close();
  34.     }
  35.     static void Read(const string path, vector<Shop> operations) {
  36.        
  37.         string shopName, productName;
  38.         float quantityGoods, pricePerKilogram;
  39.         ifstream in(path); // окрываем файл для чтения
  40.         if (in.is_open())
  41.         {
  42.             while (in >> shopName >> productName >> quantityGoods >> pricePerKilogram)
  43.             {
  44.                 operations.push_back(Shop(shopName, productName, quantityGoods, pricePerKilogram));
  45.             }
  46.         }
  47.         in.close();
  48.  
  49.         for (int i = 0; i < operations.size(); i++)
  50.         {
  51.             cout << "\tНазва магазину: " << operations[i].shopName << "\tНазва товару: " << operations[i].productName << "\tКiлькiсть товарiв: " << operations[i].quantityGoods << "\tЦiна за кiлограм: " << operations[i].pricePerKilogram << endl;
  52.         }
  53.     }
  54.  
  55.     static void Write(const string path, Shop operations) {
  56.         ofstream out(path, ios::app);
  57.  
  58.         if (out.is_open())
  59.         {
  60.             out << operations.shopName << " " << operations.productName << " " << operations.quantityGoods << " " << operations.pricePerKilogram << endl;
  61.         }
  62.         out.close();
  63.     }
  64. };
  65.  
  66. vector<Shop> Product() {
  67.     vector<Shop> operations = {
  68.         Shop("Shop1", "Good1", 120, 3),
  69.         Shop("Shop2", "Good2", 134, 43),
  70.         Shop("Shop3", "Good3", 176, 2),
  71.         Shop("Shop4", "Good4", 156, 5)
  72.     };
  73.     return operations;
  74. }
  75. void Add(const string path, Shop operations) {
  76.  
  77.     cout << "Назва магазину: ";
  78.     cin >> operations.shopName;
  79.  
  80.     cout << "Назва товару: ";
  81.     cin >> operations.productName;
  82.  
  83.     cout << "Кiлькiсть товарiв: ";
  84.     cin >> operations.quantityGoods;
  85.  
  86.     cout << "Цiна за кiлограм: ";
  87.     cin >> operations.pricePerKilogram;
  88.  
  89.     Shop::Write(path, operations);
  90.  
  91. }
  92. void Menu() {
  93.     cout << " 1. Додати товар.\n 2. Переглянути товари.\n 0. Вийти.\n";
  94.     cout << ">> ";
  95. }
  96.  
  97.  
  98. int main() {
  99.     setlocale(0, "");
  100.     const string path = "shop.txt";
  101.     Shop shop;
  102.     uint16_t in = -1;
  103.  
  104.     Shop::Write(path, Product());
  105.     vector<Shop> new_operations;
  106.  
  107.     do {
  108.         Menu();
  109.         cin >> in;
  110.         switch (in)
  111.         {
  112.         case 1: { Add(path, shop); } break;
  113.         case 2: { Shop::Read(path, new_operations); } break;
  114.         default:
  115.             break;
  116.         }
  117.     } while (in != 0);
  118.    
  119.     system("pause");
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment