Advertisement
yargee

Homework 5-6-1

Mar 30th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp19
  5. {
  6.     class Player
  7.     {
  8.         public int Money { get; set; } = 200;
  9.         public List<Product> Bag { get; private set; }                
  10.         public void MakePurchase(Shop shop)
  11.         {
  12.             bool alreadyExist = false;
  13.             Bag = new List<Product>();
  14.             while (true)
  15.             {
  16.                 Console.WriteLine($"У вас {Money} золота. Введите номер товара, который хотели бы приобрести.");
  17.                 int index = Convert.ToInt32(Console.ReadLine()) - 1;
  18.                 if ((Money >= shop.Assortment[index].Price) && (shop.Assortment[index].CountInShop >= 1))
  19.                 {
  20.                     Money -= shop.Assortment[index].Price;
  21.                     shop.Assortment[index].CountInShop--;
  22.                     shop.Assortment[index].CountInBag++;
  23.                     if (Bag.Count == 0)
  24.                     {
  25.                         Bag.Add(shop.Assortment[index]);
  26.                     }
  27.                     else
  28.                     {
  29.                         for (int i = 0; i < Bag.Count; i++)
  30.                         {
  31.                             if (shop.Assortment[index].ProductName == Bag[i].ProductName)
  32.                             {
  33.                                 alreadyExist = true;
  34.                                 break;
  35.                             }
  36.                             else
  37.                             {
  38.                                 alreadyExist = false;
  39.                                 continue;
  40.                             }
  41.                         }
  42.                         if (!alreadyExist)
  43.                         {
  44.                             Bag.Add(shop.Assortment[index]);
  45.                         }
  46.                     }
  47.                     shop.ShowProduct();
  48.                     Console.WriteLine($"Приобретен товар {shop.Assortment[index].ProductName}.");
  49.                     ShowBag();
  50.                 }
  51.                 else
  52.                 {
  53.                     Console.WriteLine("Покупка невозможна! Недостаточно средств, либо товар закончился.");
  54.                 }
  55.             }
  56.         }
  57.         public void ShowBag()
  58.         {
  59.             {
  60.                 Console.WriteLine("У вас в сумке следующий товар:");
  61.                 for (int i = 0; i < Bag.Count; i++)
  62.                 {
  63.                     Console.WriteLine($"{ i + 1}){ Bag[i].ProductName}. {Bag[i].CountInBag} шт.");
  64.                 }
  65.                 Console.WriteLine();
  66.             }
  67.         }
  68.     }
  69.     class Shop
  70.     {
  71.         public List<Product> Assortment { get;private set; }
  72.         public void MakeAssortment()
  73.         {
  74.             Assortment = new List<Product>();
  75.             Random random = new Random();
  76.             Product butcherSword = new Weapon("Меч палача", 30, 0);
  77.             Product sniperBow = new Weapon("Лук снайпера", 60, 0);
  78.             Product frostRing = new Jewelry("Кольцо мороза", 40, 0);
  79.             Product emeraldNecklace = new Jewelry("Изумрудное ожерелье", 60, 0);
  80.             Product hastePotion = new Potions("Зелье ускорения", 15, 0);
  81.             Product healingPotion = new Potions("Зелье исцеления", 10, 0);
  82.             Assortment.Add(butcherSword);
  83.             Assortment.Add(sniperBow);
  84.             Assortment.Add(frostRing);
  85.             Assortment.Add(emeraldNecklace);
  86.             Assortment.Add(hastePotion);
  87.             Assortment.Add(healingPotion);
  88.  
  89.             foreach (var product in Assortment)
  90.             {
  91.                 product.CountInShop = random.Next(2, 21);
  92.             }            
  93.         }
  94.  
  95.         public void ShowProduct()
  96.         {
  97.             Console.Clear();
  98.             Console.SetCursorPosition(0, 0);
  99.             Console.WriteLine("Сейчас у нас представлен следующий товар:");
  100.             for (int i = 0; i < Assortment.Count; i++)
  101.             {
  102.                 Console.WriteLine($"{ i + 1}){ Assortment[i].ProductName} стоимостью {Assortment[i].Price} золотых. В наличии {Assortment[i].CountInShop}");
  103.             }
  104.             Console.SetCursorPosition(0, 10);
  105.         }
  106.     }
  107.     class Product
  108.     {
  109.         public string ProductName { get; private set; }
  110.         public int Price { get; private set; }
  111.         public int CountInShop { get; set; }
  112.         public int CountInBag { get; set; }
  113.  
  114.         public Product(string name, int price, int count)
  115.         {
  116.             ProductName = name;
  117.             Price = price;
  118.             CountInShop = count;
  119.             CountInBag = 0;
  120.         }
  121.     }
  122.     class Weapon : Product
  123.     {
  124.         public Weapon(string name, int price, int count) : base(name, price, count) { }
  125.     }
  126.     class Jewelry : Product
  127.     {
  128.         public Jewelry(string name, int price, int count) : base(name, price, count) { }
  129.     }
  130.     class Potions : Product
  131.     {
  132.         public Potions(string name, int price, int count) : base(name, price, count) { }
  133.     }
  134.  
  135.     class Program
  136.     {
  137.         static void Main(string[] args)
  138.         {
  139.             Shop shop = new Shop();
  140.             Player player = new Player();
  141.                        
  142.             shop.MakeAssortment();
  143.             shop.ShowProduct();
  144.             player.MakePurchase(shop);
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement