Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- namespace Kursach
- {
- class Product
- {
- string name;
- double price;
- int year;
- double volume;
- public Product(string n, double p, int y, double v)
- {
- name = n;
- price = p;
- year = y;
- volume = v;
- }
- public string Name
- {
- get
- {
- return name;
- }
- set
- {
- name = value;
- }
- }
- public double Price
- {
- get
- {
- return price;
- }
- set
- {
- price = value;
- }
- }
- public int Year
- {
- get
- {
- return year;
- }
- set
- {
- year = value;
- }
- }
- public double Volume
- {
- get
- {
- return volume;
- }
- set
- {
- volume = value;
- }
- }
- public void Display()
- {
- Console.WriteLine("Название: " + name);
- Console.WriteLine("Цена: " + price);
- Console.WriteLine("Год выпуска: " + year);
- Console.WriteLine("Объем: " + volume);
- }
- }
- class Sklad
- {
- public List<Product> products;
- double EPSILON = 1e-6;
- public List<string> op = new List<string>();
- public Sklad()
- {
- products = new List<Product>();
- }
- public void AddProduct(Product p)
- {
- for (int i = 0; i < products.Count; i++) // ищем есть ли такой продукт на складе, если да то просто объем увеличиваем
- {
- if (products[i].Year == p.Year && Math.Abs(products[i].Price - p.Price) < EPSILON && products[i].Name == p.Name)
- {
- products[i].Volume += p.Volume;
- return;
- }
- }
- products.Add(p);
- }
- public bool DeleteProduct(string name, int year, double price)
- {
- for (int i = 0; i < products.Count; i++)
- {
- if (products[i].Year == year && Math.Abs(products[i].Price - price) < EPSILON && products[i].Name == name)
- {
- products.RemoveAt(i);
- return true;
- }
- }
- return false;
- }
- public void SortByYear()
- {
- products.Sort((x, y) => x.Year.CompareTo(y.Year));
- }
- public void SortByName()
- {
- products.Sort((x, y) => x.Name.CompareTo(y.Name));
- }
- public void SortByPrice()
- {
- products.Sort( (x, y) => x.Price.CompareTo(y.Price));
- }
- public void SortByVolume()
- {
- products.Sort( (x, y) => x.Volume.CompareTo(y.Volume));
- }
- public List<Product> FindByName(string name)
- {
- List<Product> ans = new List<Product>();
- for (int i = 0; i < products.Count; i++)
- {
- if (products[i].Name == name)
- ans.Add(products[i]);
- }
- return ans;
- }
- public List<Product> FindByYear(int year)
- {
- List<Product> ans = new List<Product>();
- for (int i = 0; i < products.Count; i++)
- {
- if (products[i].Year == year)
- ans.Add(products[i]);
- }
- return ans;
- }
- public List<Product> FindByPrice(double price)
- {
- List<Product> ans = new List<Product>();
- for (int i = 0; i < products.Count; i++)
- {
- if (Math.Abs(products[i].Price - price) < EPSILON)
- ans.Add(products[i]);
- }
- return ans;
- }
- public List<Product> FindByVolume(double volume)
- {
- List<Product> ans = new List<Product>();
- for (int i = 0; i < products.Count; i++)
- {
- if (Math.Abs(products[i].Volume - volume) < EPSILON)
- ans.Add(products[i]);
- }
- return ans;
- }
- public void DisplayInStock()// товары в наличии
- {
- Console.WriteLine("Информация о продуктах в наличии");
- bool wasprinted = false;
- for (int i = 0; i < products.Count; i++)
- if (products[i].Volume > 0)
- {
- products[i].Display();
- wasprinted = true;
- }
- if (wasprinted == false)
- Console.WriteLine("Продуктов в наличии нет");
- }
- public void DisplayAll() // все товары
- {
- Console.WriteLine("Информация о всех продуктах");
- if (products.Count == 0)
- Console.WriteLine("Продуктов в наличии нет");
- for (int i = 0; i < products.Count; i++)
- products[i].Display();
- }
- public void ReadFromFile(string fname)
- {
- StreamReader sr = new StreamReader(fname);
- while (!sr.EndOfStream)
- {
- string[] s = sr.ReadLine().Split();
- string name = s[0], price = s[1], year = s[2], volume = s[3];
- Product p = new Product(name, double.Parse(price), int.Parse(year), double.Parse(volume));
- AddProduct(p);
- }
- sr.Close();
- }
- public void PrintInFile(string fname)
- {
- StreamWriter sw = new StreamWriter(fname);
- for (int i = 0; i < products.Count; i++)
- {
- sw.WriteLine(products[i].Name + " " + products[i].Price + " " + products[i].Year + " " + products[i].Volume);
- }
- sw.Close();
- }
- public bool Buy(string name, double price, int year, double volume)
- {
- for (int i = 0; i < products.Count; i++)
- {
- if (products[i].Name == name && Math.Abs(products[i].Price - price) < EPSILON && products[i].Year == year && products[i].Volume >= volume)
- {
- products[i].Volume -= volume;
- return true;
- }
- }
- return false;
- }
- }
- class MainClass
- {
- public static int getCommand()
- {
- int x;
- while (true)
- {
- Console.WriteLine("Для просмотра продуктов в наличии нажмите 1");
- Console.WriteLine("Для просмотра всех продуктов нажмите 2");
- Console.WriteLine("Для добавления продукта нажмите 3");
- Console.WriteLine("Для удаления продукта нажмите 4");
- Console.WriteLine("Для поиска продукта нажмите 5");
- Console.WriteLine("Для сортировки продуктов нажмите 6");
- Console.WriteLine("Для покупки товаров нажмите 7");
- Console.WriteLine("Для выхода нажмите 0");
- try
- {
- x = int.Parse(Console.ReadLine());
- }
- catch
- {
- continue;
- }
- if (x >= 0 && x <= 7)
- break;
- }
- return x;
- }
- public static int FindParametr(string ask)
- {
- int x;
- while (true)
- {
- Console.WriteLine("Для {0} по названию нажмите 1", ask);
- Console.WriteLine("Для {0} по году выпуска нажмите 2", ask);
- Console.WriteLine("Для {0} по цене нажмите 3", ask);
- Console.WriteLine("Для {0} по объему нажмите 4", ask);
- Console.WriteLine("Для выхода нажмите 0");
- try
- {
- x = int.Parse(Console.ReadLine());
- }
- catch
- {
- continue;
- }
- if (x >= 0 && x <= 4)
- break;
- }
- return x;
- }
- public static int onemore()
- {
- int x;
- while (true)
- {
- Console.WriteLine("Если вы хотите еще что то купить нажмите 1");
- Console.WriteLine("Для выхода нажмите 0");
- try
- {
- x = int.Parse(Console.ReadLine());
- }
- catch
- {
- continue;
- }
- if (x >= 0 && x <= 1)
- break;
- }
- return x;
- }
- public static void Main(string[] args)
- {
- string fname = "input.txt";
- Sklad s = new Sklad();
- s.ReadFromFile(fname);
- Console.WriteLine("Здравствуйте!");
- int x, y;
- string name;
- double volume, price;
- int year;
- Product p;
- List<Product> prods;
- double totalprice = 0;
- while (true)
- {
- x = getCommand();
- if (x == 0)
- {
- Console.WriteLine("Спасибо за работу с нами. До свидания!");
- s.PrintInFile(fname);
- break;
- }
- if (x == 1)
- {
- s.DisplayInStock();
- }
- else if (x == 2)
- {
- s.DisplayAll();
- }
- else if (x == 3)
- {
- Console.WriteLine("Введите название продукта");
- name = Console.ReadLine();
- Console.WriteLine("Введите стоимость продукта");
- price = double.Parse(Console.ReadLine());
- Console.WriteLine("Введите год выпуска продукта");
- year = int.Parse(Console.ReadLine());
- Console.WriteLine("Введите объем продукта");
- volume = double.Parse(Console.ReadLine());
- p = new Product(name, price, year, volume);
- s.AddProduct(p);
- }
- else if (x == 4)
- {
- Console.WriteLine("Введите название продукта");
- name = Console.ReadLine();
- Console.WriteLine("Введите стоимость продукта");
- price = double.Parse(Console.ReadLine());
- Console.WriteLine("Введите год выпуска продукта");
- year = int.Parse(Console.ReadLine());
- bool deleted = s.DeleteProduct(name, year, price);
- if (deleted)
- Console.WriteLine("Удаление прошло успешно");
- else
- Console.WriteLine("Данный продукт не найден");
- }
- else if (x == 5)
- {
- y = FindParametr("поиска");
- if (y == 0)
- continue;
- if (y == 1)
- {
- Console.WriteLine("Введите название продукта");
- name = Console.ReadLine();
- prods = s.FindByName(name);
- }
- else if (y == 2)
- {
- Console.WriteLine("Введите год выпуска продукта");
- year = int.Parse(Console.ReadLine());
- prods = s.FindByYear(year);
- }
- else if (y == 3)
- {
- Console.WriteLine("Введите стоимость продукта");
- price = double.Parse(Console.ReadLine());
- prods = s.FindByPrice(price);
- }
- else
- {
- Console.WriteLine("Введите объем продукта");
- volume = double.Parse(Console.ReadLine());
- prods = s.FindByVolume(volume);
- }
- if (prods.Count == 0)
- {
- Console.WriteLine("По запрашиваему параметру продуктов не найдено");
- }
- else
- {
- Console.WriteLine("Найденные продукты");
- for (int i = 0; i < prods.Count; i++)
- prods[i].Display();
- }
- }
- else if (x == 6)
- {
- y = FindParametr("сортировки");
- if (y == 0)
- continue;
- if (y == 1)
- {
- s.SortByName();
- }
- else if (y == 2)
- {
- s.SortByYear();
- }
- else if (y == 3)
- {
- s.SortByPrice();
- }
- else
- {
- s.SortByVolume();
- }
- Console.WriteLine("Продукты после сортировки");
- s.DisplayAll();
- }
- else
- {
- totalprice = 0;
- do
- {
- Console.WriteLine("Введите название продукта");
- name = Console.ReadLine();
- Console.WriteLine("Введите стоимость продукта");
- price = double.Parse(Console.ReadLine());
- Console.WriteLine("Введите год выпуска продукта");
- year = int.Parse(Console.ReadLine());
- Console.WriteLine("Введите объем продукта");
- volume = double.Parse(Console.ReadLine());
- bool success = s.Buy(name, price, year, volume);
- if (success)
- {
- Console.WriteLine("Покупка прошла успешно");
- totalprice += volume * price;
- Console.WriteLine("Общий счет за покупку: {0}", totalprice);
- }
- else
- {
- Console.WriteLine("Данного товара в таком количестве нет на складе");
- }
- y = onemore();
- if (y == 0)
- break;
- }
- while (true);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement