Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace HW_5_4
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Product skirt = new Product("Юбка", 10, 2);
  14.             Product belt = new Product("Ремень", 15.5, 3);
  15.             Product socks = new Product("Носки", 1.4, 10);
  16.             Product armani = new Product("Сережки", 95, 10);
  17.  
  18.             Product[] stock = { skirt, belt, socks, armani };
  19.  
  20.             Seller seller = new Seller(stock);
  21.             Buyer buyer = new Buyer(100);
  22.  
  23.             Console.WriteLine("Добро пожаловать в магазин.\n" +
  24.                               "Выберите дальнейшее действие\n" +
  25.                               "1. Просмотреть весь ассортимент\n" +
  26.                               "2. Купить товар\n" +
  27.                               "3. Просмотреть свои вещи и баланс\n" +
  28.                               "4. Выход");
  29.             while (true)
  30.             {
  31.                 string userInput = Console.ReadLine();
  32.                 switch (userInput)
  33.                 {
  34.                     case "1":
  35.                         seller.WriteAllProducts();
  36.                         break;
  37.  
  38.                     case "2":
  39.                         seller.WriteAllProducts();
  40.                         Console.Write("Какой товар хотите купить? Введите название: ");
  41.                         string desiredName = Console.ReadLine();
  42.                         Console.Write("Сколько?");
  43.                         int desiredAmount = Convert.ToInt32(Console.ReadLine());
  44.                         if (buyer.Balance >= seller.GetPurchasePrice(desiredName, desiredAmount))
  45.                         {
  46.                             if (seller.isEnough(desiredName, desiredAmount))
  47.                             {
  48.                                 buyer.Purchase(seller.Sell(desiredName, desiredAmount));
  49.                                 seller.DeleteItem(desiredName, desiredAmount);
  50.                             }
  51.                             else
  52.                             {
  53.                                 Console.WriteLine("Извините, недостаточно товара на складе.");
  54.                             }
  55.                         }
  56.                         else
  57.                         {
  58.                             Console.WriteLine("Извините, у вас недостаточно средств.");
  59.                         }
  60.                         break;
  61.  
  62.                     case "3":
  63.                         buyer.WriteAllProperty();
  64.                         break;
  65.  
  66.                     case "4":
  67.                         Environment.Exit(0);
  68.                         break;
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74.  
  75.     class Owner
  76.     {
  77.         protected Product[] _products { get; set; }
  78.  
  79.         public Owner(Product[] products)
  80.         {
  81.             _products = products;
  82.         }
  83.  
  84.         public Owner()
  85.         {
  86.             _products = new Product[0];
  87.         }
  88.  
  89.         public void AddItem(Product product)
  90.         {
  91.             bool alreadyExist = false;
  92.  
  93.             for (int i = 0; i < _products.Length; i++)
  94.             {
  95.                 if (product.Name == _products[i].Name)
  96.                 {
  97.                     _products[i].increaseAmount(product.Amount);
  98.                     alreadyExist = true;
  99.                 }
  100.             }
  101.  
  102.             if (!alreadyExist)
  103.             {
  104.                 Product[] tempList = new Product[_products.Length];
  105.  
  106.                 for (int j = 0; j < _products.Length; j++)
  107.                 {
  108.                     tempList[j] = _products[j];
  109.                 }
  110.  
  111.                 _products = new Product[_products.Length + 1];
  112.  
  113.                 for (int i = 0; i < _products.Length - 1; i++)
  114.                 {
  115.                     _products[i] = tempList[i];
  116.                 }
  117.  
  118.                 _products[_products.Length - 1] = product;
  119.             }
  120.         }
  121.  
  122.         public void DeleteItem(String name, int amount)
  123.         {
  124.             for (int i = 0; i < _products.Length; i++)
  125.             {
  126.                 if (name == _products[i].Name)
  127.                 {
  128.                     _products[i].decreaseAmount(amount);
  129.                 }
  130.             }
  131.         }
  132.     }
  133.  
  134.     class Seller : Owner
  135.     {
  136.         public Seller(Product[] products) : base(products)
  137.         {
  138.         }
  139.         public void WriteAllProducts()
  140.         {
  141.             Console.WriteLine("В магазине в наличии:");
  142.             for (int i = 0; i < _products.Length; i++)
  143.             {
  144.                 if (_products[i].Amount != 0)
  145.                 {
  146.                     Console.WriteLine($"{_products[i].Name}  {_products[i].Price}  руб.  {_products[i].Amount} шт.");
  147.                 }
  148.             }
  149.         }
  150.  
  151.         public Product Sell(string name, int amount)
  152.         {
  153.             string newName = "";
  154.             double price = 0;
  155.  
  156.             for (int i = 0; i < _products.Length; i++)
  157.             {
  158.                 if (name == _products[i].Name)
  159.                 {
  160.                     newName = _products[i].Name;
  161.                     price = _products[i].Price;
  162.                 }
  163.             }
  164.  
  165.             Product sell = new Product(newName, price, amount);
  166.  
  167.             return sell;
  168.         }
  169.  
  170.         public double GetPurchasePrice(string name, int amount)
  171.         {
  172.             double purchasePrice = 0;
  173.  
  174.             for (int i = 0; i < _products.Length; i++)
  175.             {
  176.                 if (name == _products[i].Name)
  177.                 {
  178.                     purchasePrice = _products[i].Price * amount;
  179.                 }
  180.             }
  181.  
  182.             return purchasePrice;
  183.         }
  184.  
  185.         public bool isEnough(string name, int amount)
  186.         {
  187.             bool isEnough = false;
  188.  
  189.             for (int i = 0; i < _products.Length; i++)
  190.             {
  191.                 if (name == _products[i].Name)
  192.                 {
  193.                     if (_products[i].Amount >= amount)
  194.                     {
  195.                         isEnough = true;
  196.                     }
  197.                 }
  198.             }
  199.             return isEnough;
  200.         }
  201.     }
  202.  
  203.     class Buyer : Owner
  204.     {
  205.  
  206.         private double _balance;
  207.         public double Balance
  208.         {
  209.             get
  210.             {
  211.                 return _balance;
  212.             }
  213.             set
  214.             {
  215.                 if (value >= 0)
  216.                 {
  217.                     _balance = value;
  218.                 }
  219.             }
  220.         }
  221.  
  222.  
  223.         public Buyer(double balance)
  224.         {
  225.             _balance = balance;
  226.             _products = new Product[0];
  227.         }
  228.  
  229.         public void Purchase(Product product)
  230.         {
  231.             AddItem(product);
  232.             _balance -= product.Price;
  233.         }
  234.  
  235.         public void WriteAllProperty()
  236.         {
  237.             Console.WriteLine("У вас есть: ");
  238.  
  239.             for (int i = 0; i < _products.Length; i++)
  240.             {
  241.                 Console.WriteLine($"{_products[i].Name}  {_products[i].Amount} шт.");
  242.             }
  243.  
  244.             Console.WriteLine($"Ваш баланс {_balance} руб.");
  245.         }
  246.     }
  247.  
  248.  
  249.     class Product
  250.     {
  251.         public string Name { get; private set; }
  252.         public double Price { get; set; }
  253.  
  254.         private int _amount;
  255.  
  256.         public int Amount
  257.         {
  258.             get
  259.             {
  260.                 return _amount;
  261.             }
  262.             private set
  263.             {
  264.                 if (value >= 0)
  265.                 {
  266.                     _amount = value;
  267.                 }
  268.             }
  269.         }
  270.  
  271.         public Product(string name, double price, int amount)
  272.         {
  273.             Name = name;
  274.             Price = price;
  275.             _amount = amount;
  276.         }
  277.  
  278.         public void increaseAmount(int addAmount)
  279.         {
  280.             _amount += addAmount;
  281.         }
  282.  
  283.         public void decreaseAmount(int deductAmount)
  284.         {
  285.             _amount -= deductAmount;
  286.         }
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement