Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <windows.h>
- #include <map>
- #include <vector>
- using namespace std;
- vector<string> *names = new vector<string>();
- map<int, int> costMap;
- map<int, int> countMap;
- int countNumbers = 0;
- void getMenu() {
- cout << "команды: " << endl;
- cout << "1) поиск товара по номеру " << endl;
- cout << "2) поиск товаров по наименованию " << endl;
- cout << "3) покупка товара " << endl;
- cout << "4) изменение цены товара " << endl;
- cout << "5) добавление нового товара " << endl;
- cout << "0) завершение работы " << endl;
- }
- class Data {
- protected:
- int number;
- int cost;
- int countx;
- string word;
- string nameOfPosition;
- public:
- void inputData() {
- ifstream file;
- file.open("shopdatabase.txt");
- while (file >> word) {
- if (word == "Номер:") {
- file >> number;
- countNumbers++;
- }
- if (word == "Наименование:") {
- file >> word;
- names->push_back(word);
- }
- if (word == "Цена:") {
- file >> cost;
- costMap.insert(pair<int, int>(number, cost));
- }
- if (word == "Количество:") {
- file >> countx;
- countMap.insert(pair<int, int>(number, countx));
- }
- }
- file.close();
- }
- };
- class search : public Data {
- public:
- void searchNumber() {
- cout << "номер товара: ";
- cin >> number;
- auto it = names->begin();
- it += number - 1;
- if (number <= countNumbers) {
- cout << "Номер: " << number << endl;
- cout << "Наименование: " << *it << endl;
- cout << "Цена: " << costMap.at(number) << endl;
- cout << "Количество: " << countMap.at(number) << endl;
- }
- else {
- cout << "товара с таким номером не существует" << endl;
- }
- }
- void searchAllName() {
- number = 0;
- cout << "Поиск: ";
- cin >> word;
- for (auto it = names->begin(); it != names->end(); it++) {
- countx = 0;
- number++;
- nameOfPosition = *it;
- for (int i = 0; i < word.size(); i++) {
- if (word[i] == nameOfPosition[i]) {
- countx++;
- }
- }
- if (countx == word.size()) {
- cout << "Номер: " << number << endl;
- cout << "Наименование: " << *it << endl;
- cout << "Цена: " << costMap.at(number) << endl;
- cout << "Количество: " << countMap.at(number) << endl;
- }
- else {
- cout << "Нет совпадений" << endl;
- }
- }
- }
- };
- class input : public Data {
- public:
- void buy() {
- cout << "номер покупаемого товара: ";
- cin >> number;
- countMap.at(number)--;
- }
- void inputKol() {
- cout << "номер товара: ";
- cin >> number;
- cout << "новая цена товара: ";
- cin >> costMap.at(number);
- }
- void inputNew() {
- countNumbers++;
- cout << "наименование нового товара: ";
- cin >> word;
- names->push_back(word);
- cout << "количество нового товара: ";
- cin >> countx;
- cout << "цена нового товара: ";
- cin >> cost;
- countMap.insert(pair<int, int>(countNumbers, countx));
- costMap.insert(pair<int, int>(countNumbers, cost));
- }
- };
- template <typename t>
- void update(t & file) {
- auto it = names->begin();
- for (int n = 1; n <= countNumbers; n++) {
- file << "№ " << n << endl;
- file << "Наименование: " << *it << endl;
- file << "Количество: " << countMap.at(n) << endl;
- file << "Цена: " << costMap.at(n) << endl;
- it++;
- }
- }
- search s;
- input in;
- void instructionsHandler() {
- int instruction;
- while (true) {
- cout << endl << "Ввдедите команду: ";
- cin >> instruction;
- if (instruction < 0 || instruction > 5) {
- cout << "нет такой команды" << endl;
- continue;
- }
- if (instruction == 1) {
- s.searchNumber();
- continue;
- }
- if (instruction == 2) {
- s.searchAllName();
- continue;
- }
- if (instruction == 3) {
- in.buy();
- continue;
- }
- if (instruction == 4) {
- in.inputKol();
- continue;
- }
- if (instruction == 5) {
- in.inputNew();
- continue;
- }
- if (instruction == 0) {
- break;
- }
- }
- }
- int main(int argc, char** argv) {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- in.inputData();
- getMenu();
- instructionsHandler();
- ofstream file;
- file.open("shopdatabase.txt");
- update<ofstream>(file);
- file.close();
- names->clear();
- delete names;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement