Advertisement
pooliamory

r3wt4

Dec 12th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <windows.h>
  6. #include <map>
  7. #include <vector>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. vector<string> *names = new vector<string>();
  13. map<int, int> costMap;
  14. map<int, int> countMap;
  15. int countNumbers = 0;
  16.  
  17. void getMenu() {
  18.     cout << "команды: " << endl;
  19.     cout << "1) поиск товара по номеру " << endl;
  20.     cout << "2) поиск товаров по наименованию " << endl;
  21.     cout << "3) покупка товара " << endl;
  22.     cout << "4) изменение цены товара " << endl;
  23.     cout << "5) добавление нового товара " << endl;
  24.     cout << "0) завершение работы " << endl;
  25. }
  26.  
  27.  
  28. class Data {
  29. protected:
  30.     int number;
  31.     int cost;
  32.     int countx;
  33.     string word;
  34.     string nameOfPosition;
  35. public:
  36.     void inputData() {
  37.         ifstream file;
  38.         file.open("shopdatabase.txt");
  39.         while (file >> word) {
  40.             if (word == "Номер:") {
  41.                 file >> number;
  42.                 countNumbers++;
  43.             }
  44.             if (word == "Наименование:") {
  45.                 file >> word;
  46.                 names->push_back(word);
  47.             }
  48.             if (word == "Цена:") {
  49.                 file >> cost;
  50.                 costMap.insert(pair<int, int>(number, cost));
  51.             }
  52.             if (word == "Количество:") {
  53.                 file >> countx;
  54.                 countMap.insert(pair<int, int>(number, countx));
  55.             }
  56.         }
  57.         file.close();
  58.     }
  59. };
  60.  
  61. class search : public Data {
  62. public:
  63.     void searchNumber() {
  64.         cout << "номер товара: ";
  65.         cin >> number;
  66.         auto it = names->begin();
  67.         it += number - 1;
  68.         if (number <= countNumbers) {
  69.             cout << "Номер: " << number << endl;
  70.             cout << "Наименование: " << *it << endl;
  71.             cout << "Цена: " << costMap.at(number) << endl;
  72.             cout << "Количество: " << countMap.at(number) << endl;
  73.         }
  74.         else {
  75.             cout << "товара с таким номером не существует" << endl;
  76.         }
  77.     }
  78.  
  79.     void searchAllName() {
  80.         number = 0;
  81.         cout << "Поиск: ";
  82.         cin >> word;
  83.         for (auto it = names->begin(); it != names->end(); it++) {
  84.             countx = 0;
  85.             number++;
  86.             nameOfPosition = *it;
  87.             for (int i = 0; i < word.size(); i++) {
  88.                 if (word[i] == nameOfPosition[i]) {
  89.                     countx++;
  90.                 }
  91.             }
  92.             if (countx == word.size()) {
  93.                 cout << "Номер: " << number << endl;
  94.                 cout << "Наименование: " << *it << endl;
  95.                 cout << "Цена: " << costMap.at(number) << endl;
  96.                 cout << "Количество: " << countMap.at(number) << endl;
  97.             }
  98.             else {
  99.                 cout << "Нет совпадений" << endl;
  100.             }
  101.         }
  102.     }
  103. };
  104.  
  105. class input : public Data {
  106. public:
  107.     void buy() {
  108.         cout << "номер покупаемого товара: ";
  109.         cin >> number;
  110.         countMap.at(number)--;
  111.     }
  112.     void inputKol() {
  113.         cout << "номер товара: ";
  114.         cin >> number;
  115.         cout << "новая цена товара: ";
  116.         cin >> costMap.at(number);
  117.     }
  118.     void inputNew() {
  119.         countNumbers++;
  120.         cout << "наименование нового товара: ";
  121.         cin >> word;
  122.         names->push_back(word);
  123.         cout << "количество нового товара: ";
  124.         cin >> countx;
  125.         cout << "цена нового товара: ";
  126.         cin >> cost;
  127.         countMap.insert(pair<int, int>(countNumbers, countx));
  128.         costMap.insert(pair<int, int>(countNumbers, cost));
  129.     }
  130. };
  131.  
  132. template <typename t>
  133. void update(t & file) {
  134.     auto it = names->begin();
  135.     for (int n = 1; n <= countNumbers; n++) {
  136.         file << "№ " << n << endl;
  137.         file << "Наименование: " << *it << endl;
  138.         file << "Количество: " << countMap.at(n) << endl;
  139.         file << "Цена: " << costMap.at(n) << endl;
  140.         it++;
  141.     }
  142. }
  143.  
  144. search s;
  145. input in;
  146.  
  147. void instructionsHandler() {
  148.     int instruction;
  149.     while (true) {
  150.         cout << endl << "Ввдедите команду: ";
  151.         cin >> instruction;
  152.         if (instruction < 0 || instruction > 5) {
  153.             cout << "нет такой команды" << endl;
  154.             continue;
  155.         }
  156.         if (instruction == 1) {
  157.             s.searchNumber();
  158.             continue;
  159.         }
  160.         if (instruction == 2) {
  161.             s.searchAllName();
  162.             continue;
  163.         }
  164.         if (instruction == 3) {
  165.             in.buy();
  166.             continue;
  167.         }
  168.         if (instruction == 4) {
  169.             in.inputKol();
  170.             continue;
  171.         }
  172.         if (instruction == 5) {
  173.             in.inputNew();
  174.             continue;
  175.         }
  176.         if (instruction == 0) {
  177.             break;
  178.         }
  179.     }
  180. }
  181.  
  182. int main(int argc, char** argv) {
  183.     SetConsoleCP(1251);
  184.     SetConsoleOutputCP(1251);
  185.  
  186.     in.inputData();
  187.     getMenu();
  188.     instructionsHandler();
  189.  
  190.     ofstream file;
  191.     file.open("shopdatabase.txt");
  192.     update<ofstream>(file);
  193.     file.close();
  194.     names->clear();
  195.     delete names;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement