Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <tchar.h>
- #include <string>
- #include <sstream>
- using namespace std;
- class Shop
- {
- public:
- string shopName, productName;
- float quantityGoods = 0, pricePerKilogram = 0;
- Shop(){}
- Shop(string s, string pro, float q, float pri) : shopName(s), productName(pro), quantityGoods(q), pricePerKilogram(pri)
- {}
- static void Write(const string path, vector<Shop> operations) {
- ofstream out(path, ios::ate);
- if (out.is_open())
- {
- for (int i = 0; i < operations.size(); i++)
- {
- out << operations[i].shopName << " " << operations[i].productName << " " << operations[i].quantityGoods << " " << operations[i].pricePerKilogram << endl;
- }
- }
- out.close();
- }
- static void Read(const string path, vector<Shop> operations) {
- string shopName, productName;
- float quantityGoods, pricePerKilogram;
- ifstream in(path); // окрываем файл для чтения
- if (in.is_open())
- {
- while (in >> shopName >> productName >> quantityGoods >> pricePerKilogram)
- {
- operations.push_back(Shop(shopName, productName, quantityGoods, pricePerKilogram));
- }
- }
- in.close();
- for (int i = 0; i < operations.size(); i++)
- {
- cout << "\tНазва магазину: " << operations[i].shopName << "\tНазва товару: " << operations[i].productName << "\tКiлькiсть товарiв: " << operations[i].quantityGoods << "\tЦiна за кiлограм: " << operations[i].pricePerKilogram << endl;
- }
- }
- static void Write(const string path, Shop operations) {
- ofstream out(path, ios::app);
- if (out.is_open())
- {
- out << operations.shopName << " " << operations.productName << " " << operations.quantityGoods << " " << operations.pricePerKilogram << endl;
- }
- out.close();
- }
- };
- vector<Shop> Product() {
- vector<Shop> operations = {
- Shop("Shop1", "Good1", 120, 3),
- Shop("Shop2", "Good2", 134, 43),
- Shop("Shop3", "Good3", 176, 2),
- Shop("Shop4", "Good4", 156, 5)
- };
- return operations;
- }
- void Add(const string path, Shop operations) {
- cout << "Назва магазину: ";
- cin >> operations.shopName;
- cout << "Назва товару: ";
- cin >> operations.productName;
- cout << "Кiлькiсть товарiв: ";
- cin >> operations.quantityGoods;
- cout << "Цiна за кiлограм: ";
- cin >> operations.pricePerKilogram;
- Shop::Write(path, operations);
- }
- void Menu() {
- cout << " 1. Додати товар.\n 2. Переглянути товари.\n 0. Вийти.\n";
- cout << ">> ";
- }
- int main() {
- setlocale(0, "");
- const string path = "shop.txt";
- Shop shop;
- uint16_t in = -1;
- Shop::Write(path, Product());
- vector<Shop> new_operations;
- do {
- Menu();
- cin >> in;
- switch (in)
- {
- case 1: { Add(path, shop); } break;
- case 2: { Shop::Read(path, new_operations); } break;
- default:
- break;
- }
- } while (in != 0);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment