Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data.SqlTypes;
- using System.Runtime.InteropServices;
- namespace Shop
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Shop shop = new Shop();
- Seller seller = new Seller();
- Buyer buyer = new Buyer();
- shop.Work(seller, buyer);
- }
- }
- class Shop
- {
- public void Work(Seller seller, Buyer buyer)
- {
- const int CommandBuyProduct = 1;
- const int CommandShowBag = 2;
- const int CommandExit = 3;
- bool isWorking = true;
- while (isWorking)
- {
- Console.WriteLine("Список товаров в магазине:\n");
- seller.ShowWarehouse();
- Console.WriteLine();
- seller.ShowMoney(buyer, seller);
- Console.WriteLine($"\nЕсли хотите купить товар введите - {CommandBuyProduct}\n" +
- $"Если хотите посмотреть, что в сумке у покупателя введите - {CommandShowBag}\n" +
- $"Если хотите выйти из магазина введите - {CommandExit}\n\n");
- int userInput = ReadInt();
- if (userInput == CommandBuyProduct)
- {
- SelectionOfProducts(seller, buyer);
- }
- else if (userInput == CommandExit)
- {
- isWorking = false;
- }
- else if (userInput == CommandShowBag)
- {
- buyer.ShowBag();
- }
- else
- {
- Console.WriteLine("Вы ввели не верную команду.");
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void SelectionOfProducts(Seller seller, Buyer buyer)
- {
- Console.WriteLine("Введите название товара:");
- string productName = Console.ReadLine();
- Console.WriteLine("Введите количество товара:");
- int value = ReadInt();
- seller.Sell(buyer, productName, value);
- }
- private int ReadInt()
- {
- int number;
- while (int.TryParse(Console.ReadLine(), out number) != true)
- {
- Console.WriteLine("Вы ввели не корректные данные");
- Console.ReadKey();
- }
- return number;
- }
- }
- class Owner
- {
- protected List<Cell> Warehouse = new List<Cell>();
- protected List<Cell> Bag = new List<Cell>();
- protected bool IsContains(List<Cell> content, string product, out int index)
- {
- for (int i = 0; i < content.Count; i++)
- {
- if (content[i].Product.Name.Contains(product))
- {
- index = i;
- return true;
- }
- }
- index = 0;
- return false;
- }
- public void ShowMoney(Buyer buyer, Seller seller)
- {
- Console.WriteLine($"{seller.SellerMoney} - денег у продавца.\n{buyer.BuyerMoney} - денег у покупателя.");
- }
- }
- class Buyer : Owner
- {
- public Buyer(int money = 1000)
- {
- BuyerMoney = money;
- }
- public int BuyerMoney { get; private set; }
- public void WithdrawMoney(int productPrice, int value)
- {
- BuyerMoney -= productPrice * value;
- }
- public void AddQuantity(int quantityReduseProducts, int index)
- {
- Bag[index].AddQuantity(quantityReduseProducts);
- }
- public void AddNewProductInBag(int value, int price, string productName)
- {
- Bag.Add(new Cell(value, new Product(productName, price)));
- }
- public void ShowBag()
- {
- foreach (var item in Bag)
- {
- Console.WriteLine($"{item.Product.Name} {item.Quantity} - шт. Цена - {item.Product.Price}");
- }
- }
- }
- class Seller : Owner
- {
- public Seller(int money = 100000)
- {
- SellerMoney = money;
- Warehouse.Add(new Cell(10, new Product("Колбаса", 182)));
- Warehouse.Add(new Cell(20, new Product("Хлеб", 54)));
- Warehouse.Add(new Cell(15, new Product("Сыр", 287)));
- Warehouse.Add(new Cell(5, new Product("Масло", 216)));
- Warehouse.Add(new Cell(11, new Product("Молоко", 93)));
- }
- public int SellerMoney { get; private set; }
- public void Sell(Buyer buyer, string productName, int value)
- {
- int productPrice;
- if (buyer.BuyerMoney < 0)
- {
- Console.WriteLine("У покупателя недостаточно денег.");
- Console.ReadKey();
- return;
- }
- if (IsContains(Warehouse, productName, out int index))
- {
- productPrice = Warehouse[index].Product.Price;
- if (buyer.BuyerMoney < productPrice * value)
- {
- Console.WriteLine("У покупателя не достаточно денег.");
- Console.ReadKey();
- return;
- }
- ReduseQuantity(value, index);
- TopUpMoney(productPrice, value);
- buyer.WithdrawMoney(productPrice, value);
- }
- else
- {
- return;
- }
- if (IsContains(Bag, productName, out index))
- {
- buyer.AddQuantity(value, index);
- }
- else
- {
- buyer.AddNewProductInBag(value, productPrice, productName);
- }
- }
- public void TopUpMoney(int productPrice, int value)
- {
- SellerMoney += productPrice * value;
- }
- public void ReduseQuantity(int quantityReduseProduct, int index)
- {
- Warehouse[index].ReduseQuantity(quantityReduseProduct);
- }
- public void ShowWarehouse()
- {
- foreach (var item in Warehouse)
- {
- Console.WriteLine($"{item.Product.Name} {item.Quantity} - шт. Цена - {item.Product.Price}");
- }
- }
- }
- class Product
- {
- public Product(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- }
- class Cell
- {
- public Cell(int quantity, Product product)
- {
- Quantity = quantity;
- Product = product;
- }
- public int Quantity { get; private set; }
- public Product Product { get; private set; }
- public void ReduseQuantity(int quantityReduseProducts)
- {
- if (Quantity >= quantityReduseProducts)
- {
- for (int i = quantityReduseProducts; i > 0; i--)
- {
- Quantity--;
- }
- }
- else
- {
- Quantity = 0;
- }
- }
- public void AddQuantity(int quantityReduseProducts)
- {
- for (int i = 1; i == quantityReduseProducts; i++)
- {
- Quantity++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment