SaNik74

Shop

Jul 11th, 2024 (edited)
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.82 KB | None | 0 0
  1. using System;
  2. using System.Data.SqlTypes;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace Shop
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Shop shop = new Shop();
  12.             Seller seller = new Seller();
  13.             Buyer buyer = new Buyer();
  14.  
  15.             shop.Work(seller, buyer);
  16.         }
  17.     }
  18.  
  19.     class Shop
  20.     {
  21.         public void Work(Seller seller, Buyer buyer)
  22.         {
  23.             const int CommandBuyProduct = 1;
  24.             const int CommandShowBag = 2;
  25.             const int CommandExit = 3;
  26.  
  27.             bool isWorking = true;
  28.  
  29.             while (isWorking)
  30.             {
  31.                 Console.WriteLine("Список товаров в магазине:\n");
  32.  
  33.                 seller.ShowWarehouse();
  34.                 Console.WriteLine();
  35.                 seller.ShowMoney(buyer, seller);
  36.  
  37.                 Console.WriteLine($"\nЕсли хотите купить товар введите - {CommandBuyProduct}\n" +
  38.                     $"Если хотите посмотреть, что в сумке у покупателя введите - {CommandShowBag}\n" +
  39.                     $"Если хотите выйти из магазина введите - {CommandExit}\n\n");
  40.  
  41.                 int userInput = ReadInt();
  42.  
  43.                 if (userInput == CommandBuyProduct)
  44.                 {
  45.                     SelectionOfProducts(seller, buyer);
  46.                 }
  47.                 else if (userInput == CommandExit)
  48.                 {
  49.                     isWorking = false;
  50.                 }
  51.                 else if (userInput == CommandShowBag)
  52.                 {
  53.                     buyer.ShowBag();
  54.                 }
  55.                 else
  56.                 {
  57.                     Console.WriteLine("Вы ввели не верную команду.");
  58.                 }
  59.  
  60.                 Console.ReadKey();
  61.                 Console.Clear();
  62.             }
  63.         }
  64.  
  65.         private void SelectionOfProducts(Seller seller, Buyer buyer)
  66.         {
  67.             Console.WriteLine("Введите название товара:");
  68.             string productName = Console.ReadLine();
  69.  
  70.             Console.WriteLine("Введите количество товара:");
  71.             int value = ReadInt();
  72.  
  73.             seller.Sell(buyer, productName, value);
  74.         }
  75.  
  76.         private int ReadInt()
  77.         {
  78.             int number;
  79.  
  80.             while (int.TryParse(Console.ReadLine(), out number) != true)
  81.             {
  82.                 Console.WriteLine("Вы ввели не корректные данные");
  83.                 Console.ReadKey();
  84.             }
  85.  
  86.             return number;
  87.         }
  88.     }
  89.  
  90.     class Owner
  91.     {
  92.         protected List<Cell> Warehouse = new List<Cell>();
  93.         protected List<Cell> Bag = new List<Cell>();
  94.  
  95.         protected bool IsContains(List<Cell> content, string product, out int index)
  96.         {
  97.             for (int i = 0; i < content.Count; i++)
  98.             {
  99.                 if (content[i].Product.Name.Contains(product))
  100.                 {
  101.                     index = i;
  102.                     return true;
  103.                 }
  104.             }
  105.  
  106.             index = 0;
  107.             return false;
  108.         }
  109.  
  110.         public void ShowMoney(Buyer buyer, Seller seller)
  111.         {
  112.             Console.WriteLine($"{seller.SellerMoney} - денег у продавца.\n{buyer.BuyerMoney} - денег у покупателя.");
  113.         }
  114.     }
  115.  
  116.     class Buyer : Owner
  117.     {
  118.         public Buyer(int money = 1000)
  119.         {
  120.             BuyerMoney = money;
  121.         }
  122.  
  123.         public int BuyerMoney { get; private set; }
  124.  
  125.         public void WithdrawMoney(int productPrice, int value)
  126.         {
  127.             BuyerMoney -= productPrice * value;
  128.         }
  129.         public void AddQuantity(int quantityReduseProducts, int index)
  130.         {
  131.             Bag[index].AddQuantity(quantityReduseProducts);
  132.         }
  133.  
  134.         public void AddNewProductInBag(int value, int price, string productName)
  135.         {
  136.             Bag.Add(new Cell(value, new Product(productName, price)));
  137.         }
  138.  
  139.         public void ShowBag()
  140.         {
  141.             foreach (var item in Bag)
  142.             {
  143.                 Console.WriteLine($"{item.Product.Name}     {item.Quantity} - шт. Цена - {item.Product.Price}");
  144.             }
  145.         }
  146.     }
  147.  
  148.     class Seller : Owner
  149.     {
  150.  
  151.         public Seller(int money = 100000)
  152.         {
  153.             SellerMoney = money;
  154.  
  155.             Warehouse.Add(new Cell(10, new Product("Колбаса", 182)));
  156.             Warehouse.Add(new Cell(20, new Product("Хлеб", 54)));
  157.             Warehouse.Add(new Cell(15, new Product("Сыр", 287)));
  158.             Warehouse.Add(new Cell(5, new Product("Масло", 216)));
  159.             Warehouse.Add(new Cell(11, new Product("Молоко", 93)));
  160.         }
  161.  
  162.         public int SellerMoney { get; private set; }
  163.  
  164.         public void Sell(Buyer buyer, string productName, int value)
  165.         {
  166.             int productPrice;
  167.  
  168.             if (buyer.BuyerMoney < 0)
  169.             {
  170.                 Console.WriteLine("У покупателя недостаточно денег.");
  171.                 Console.ReadKey();
  172.                 return;
  173.             }
  174.  
  175.             if (IsContains(Warehouse, productName, out int index))
  176.             {
  177.                 productPrice = Warehouse[index].Product.Price;
  178.  
  179.                 if (buyer.BuyerMoney < productPrice * value)
  180.                 {
  181.                     Console.WriteLine("У покупателя не достаточно денег.");
  182.                     Console.ReadKey();
  183.                     return;
  184.                 }
  185.  
  186.                 ReduseQuantity(value, index);
  187.                 TopUpMoney(productPrice, value);
  188.                 buyer.WithdrawMoney(productPrice, value);
  189.             }
  190.             else
  191.             {
  192.                 return;
  193.             }
  194.  
  195.             if (IsContains(Bag, productName, out index))
  196.             {
  197.                 buyer.AddQuantity(value, index);
  198.             }
  199.             else
  200.             {
  201.                 buyer.AddNewProductInBag(value, productPrice, productName);
  202.             }
  203.         }
  204.  
  205.         public void TopUpMoney(int productPrice, int value)
  206.         {
  207.             SellerMoney += productPrice * value;
  208.         }
  209.  
  210.         public void ReduseQuantity(int quantityReduseProduct, int index)
  211.         {
  212.             Warehouse[index].ReduseQuantity(quantityReduseProduct);
  213.         }
  214.  
  215.         public void ShowWarehouse()
  216.         {
  217.             foreach (var item in Warehouse)
  218.             {
  219.                 Console.WriteLine($"{item.Product.Name}     {item.Quantity} - шт. Цена - {item.Product.Price}");
  220.             }
  221.         }
  222.     }
  223.  
  224.     class Product
  225.     {
  226.         public Product(string name, int price)
  227.         {
  228.             Name = name;
  229.             Price = price;
  230.         }
  231.  
  232.         public string Name { get; private set; }
  233.         public int Price { get; private set; }
  234.     }
  235.  
  236.     class Cell
  237.     {
  238.         public Cell(int quantity, Product product)
  239.         {
  240.             Quantity = quantity;
  241.             Product = product;
  242.         }
  243.  
  244.         public int Quantity { get; private set; }
  245.         public Product Product { get; private set; }
  246.  
  247.         public void ReduseQuantity(int quantityReduseProducts)
  248.         {
  249.             if (Quantity >= quantityReduseProducts)
  250.             {
  251.                 for (int i = quantityReduseProducts; i > 0; i--)
  252.                 {
  253.                     Quantity--;
  254.                 }
  255.             }
  256.             else
  257.             {
  258.                 Quantity = 0;
  259.             }
  260.         }
  261.  
  262.         public void AddQuantity(int quantityReduseProducts)
  263.         {
  264.             for (int i = 1; i == quantityReduseProducts; i++)
  265.             {
  266.                 Quantity++;
  267.             }
  268.         }
  269.     }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment