Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.20 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 CSLight15
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             DealParty player = new DealParty(100);
  14.             DealParty seller = new DealParty(10000);
  15.  
  16.             seller.AddProduct(new Product("Компьютер", 300, "Япония"));
  17.             seller.AddProduct(new Product("Мышь", 20, "США"));
  18.             seller.AddProduct(new Product("Клавиатура", 25, "Франция"));
  19.  
  20.             while (true)
  21.             {
  22.                 Console.WriteLine("1) Показать все товары продавца");
  23.                 Console.WriteLine("2) Показать все вещи игрока");
  24.                 Console.WriteLine("3) Купить товар");
  25.                
  26.                 switch (Console.ReadLine())
  27.                 {
  28.                     case "1":
  29.  
  30.                         Console.Clear();
  31.  
  32.                         seller.ShowProductList();
  33.                         break;
  34.  
  35.                     case "2":
  36.  
  37.                         Console.Clear();
  38.                        
  39.                         player.ShowProductList();
  40.                         Console.WriteLine($"Оставшиеся деньги: {player.Money}$");
  41.  
  42.                         break;
  43.  
  44.                     case "3":
  45.  
  46.                         Console.Clear();
  47.  
  48.                         seller.ShowProductList();
  49.  
  50.                         Console.Write("Введите название товара: ");
  51.                         switch (player.Buy(Console.ReadLine(), seller))
  52.                         {
  53.                             case 1:
  54.  
  55.                                 Console.WriteLine("Покупка прошла успешно!");
  56.                                 break;
  57.  
  58.                             case 0:
  59.  
  60.                                 Console.WriteLine("У вас недостаточно средств");
  61.                                 break;
  62.  
  63.                             case -1:
  64.  
  65.                                 Console.WriteLine("Такого товара здесь нет");
  66.                                 break;
  67.                         }
  68.                         break;
  69.                 }
  70.             }
  71.         }
  72.  
  73.         class DealParty
  74.         {
  75.             private Product[] _products = new Product[0];
  76.             public int Money { get; private set; }
  77.  
  78.             public DealParty(int money = 0)
  79.             {
  80.                 if (money >= 0)
  81.                     Money = money;
  82.             }
  83.  
  84.             public int Buy(string productName, DealParty seller)
  85.             {
  86.                 for (int i = 0; i < seller._products.Length; i++)
  87.                 {
  88.                     if (seller._products[i].Name.ToLower() == productName.ToLower())
  89.                     {
  90.                         if (Money >= seller._products[i].Price)
  91.                         {
  92.                             AddProduct(seller._products[i]);
  93.  
  94.                             seller.RemoveProduct(i);
  95.  
  96.                             return 1;
  97.                         }
  98.                         else
  99.                         {
  100.                             return 0;
  101.                         }
  102.                     }
  103.                 }
  104.                 return -1;
  105.             }
  106.  
  107.             public void AddProduct(Product product)
  108.             {
  109.                 Money -= product.Price;
  110.  
  111.                 Product[] temp = new Product[_products.Length + 1];
  112.  
  113.                 for (int i = 0; i < _products.Length; i++)
  114.                 {
  115.                     temp[i] = _products[i];
  116.                 }
  117.  
  118.                 temp[temp.Length - 1] = product;
  119.  
  120.                 _products = temp;
  121.             }
  122.  
  123.             public void RemoveProduct(int productNumber)
  124.             {
  125.                 Money += _products[productNumber].Price;
  126.  
  127.                 Product[] temp = new Product[_products.Length - 1];
  128.  
  129.                 for (int i = 0, j = 0; i < temp.Length; i++, j++)
  130.                 {
  131.                     if (productNumber == i && productNumber != _products.Length - 1)
  132.                     {
  133.                         j++;
  134.                     }
  135.                     temp[i] = _products[j];
  136.                 }
  137.  
  138.                 _products = temp;
  139.             }
  140.  
  141.             public void ShowProductList()
  142.             {
  143.                 for (int i = 0; i < _products.Length; i++)
  144.                 {
  145.                     _products[i].ShowProduct();
  146.                 }
  147.             }
  148.         }
  149.  
  150.         class Product
  151.         {
  152.             public string Name { get; private set; }
  153.             public int Price { get; private set; }
  154.             public string ProducingCountry { get; private set; }
  155.  
  156.             public Product (string name, int price, string country)
  157.             {
  158.                 Name = name;
  159.                 Price = price;
  160.                 ProducingCountry = country;
  161.             }
  162.  
  163.             public void ShowProduct()
  164.             {
  165.                 Console.WriteLine($"Название: {Name}\nЦена: {Price}$\nСтрана-производитель: {ProducingCountry}\n");
  166.             }
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement