Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Sem 3 / Sem 3

.txt
Скачиваний:
0
Добавлен:
10.04.2019
Размер:
5.38 Кб
Скачать
// Sem 3.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class product
{
private:
	char* name;
	int quantity;
public:
	product() {};
	void Set(char* _name, int _quantity)
	{
		name = _name;
		quantity = _quantity;
	};
	char* GetName()
	{
		return name;
	}
	int GetQuantity()
	{
		return quantity;
	}
	void operator+(product p)
	{
		ofstream file;
		file.open("products.txt", ios::app);
		if (!file.is_open())
			cout << "File can not be opened!\n";
		else
		{
			file << p.name << "	" << p.quantity << "\n";
		}
		file.close();
	}
	void operator-(char* filename)
	{
		ifstream file(filename);
		ofstream file_temp("Temp.txt");
		if (!file.is_open())
			cout << "File can not be opened!\n";
		else
		{
			string str, str_temp;
			cin >> str;
			while (getline(file, str_temp))
			{
				if (str_temp != str)
					file_temp << str_temp;
			}
		}
		file.close();
		file_temp.close();
		remove(filename);
		rename("Temp.txt", filename);
	}
	~product() {};
};

int inic(product p [])							//ф-ция инициализации массива объектов
{
	ifstream prod("products.txt");
	char name [20], quantity;
	int i = 0;
	if (!prod.is_open())						// если файл не открыт
		cout << "File can not be opened!\n"; // сообщить об этом
	else
	{
		while (!prod.eof())
		{
			prod >> name;
			prod >> quantity;
			p[i].Set(name, quantity);
			i++;
		}
	}
	prod.close();
	return i;
}

int main()
{
	setlocale(LC_ALL, " ");
	product P[20];
	int len;
	len = inic(P);

	string name, s;								//авторизация пользователя
	cout << "Enter your username:\n";		//неавторизованному пользователю доступно только чтение пулов
	cin >> name;								//авторизованному пользователю доступны чтение и редактирование
	ifstream users("users.txt");				//пулов и списка пользователей
	int x = 0;									//список пользователей хранится в файле users.txt
	while (getline(users, s))
	{
		if (s == name)
		{
			cout << "User is found\n";
			x = 1;
			break;
		}
	}
	if (x == 0)
		cout << "User is not found\n" << "You come in as a guest\n";
	char a = 'y';
	while (a == 'y')
	{
		if (x == 1)
		{
			cout << "1. List the resources\n"
				<< "2. Edit resources list\n"
				<< "3. List users\n"
				<< "4. Exit\n";
			char c;
			cin >> c;
			switch (c)
			{
			case '1':
			{
				ifstream prod("products.txt");
				if (!prod.is_open())
					cout << "File can not be opened!\n";
				else
				{
					int i = 0;
					string str;
					while (getline(prod, str))
					{
						cout << str << endl;
					}
					cout << "----------------------------\n\n";
				}
				prod.close();
				break;
			}
			case '2':
			{
				cout << "Possible actions:\n"
					<< "+ <name resource> <quantity>	- adding a new resource\n"
					<< "- <name resource>	- deleting a resource\n";
				char act;
				cin >> act;
				if (act == '+')
				{
					ofstream file;
					file.open("products.txt", ios::app);
					if (!file.is_open())
						cout << "File can not be opened!\n";
					else
					{
						string str, str1;
						cin >> str >> str1;
						file << str << "	" << str1 << "\n";
					}
					file.close();
					inic(P);
				}
				else
				{
					if (act == '-')
					{
						ifstream file("products.txt");
						ofstream file_temp("Temp.txt");
						if (!file.is_open())
							cout << "File can not be opened!\n";
						else
						{
							string str, str_temp, quantity_temp;
							cin >> str;
							while (!file.eof())
							{
								file >> str_temp;
								file >> quantity_temp;
								if (str != str_temp)
								{
									file_temp << str_temp << " " << quantity_temp << endl;
								}
							}
						}
						file.close();
						file_temp.close();
						remove("products.txt");
						rename("Temp.txt", "products.txt");
					}
				}
				break;
			}
			case '3':
			{
				ifstream users("users.txt");
				if (!users.is_open())
					cout << "File can not be opened!\n";
				else
				{
					int i = 0;
					string str;
					while (getline(users, str))
					{
						cout << str << endl;
					}
					cout << "----------------------------\n" << endl;
				}
				users.close();
				break;
			}
			case '4':
			{
				a = 'n';
				break;
			}
			default:
				break;
			}
		}
		else
		{
			cout << "1. List the resources\n"
				<< "2. List users\n"
				<< "3. Exit\n";
			char b;
			cin >> b;
			switch (b)
			{
			case '1':
			{
				ifstream prod("products.txt");
				if (!prod.is_open())
					cout << "File can not be opened!\n";
				else
				{
					int i = 0;
					string str;
					while (getline(prod, str))
					{
						cout << str << endl;
					}
					cout << "----------------------------\n\n";
				}
				prod.close();
				break;
			}
			case '2':
			{
				ifstream users("users.txt");
				if (!users.is_open())
					cout << "File can not be opened!\n";
				else
				{
					int i = 0;
					string str;
					while (getline(users, str))
					{
						cout << str << endl;
					}
					cout << "----------------------------\n" << endl;
				}
				users.close();
				break;
			}
			case '3':
			{
				a = 'n';
				break;
			}
			default:
				break;
			}
		}
	}
    return 0;
}
Соседние файлы в папке Sem 3