Advertisement
Vapio

task30

May 4th, 2021 (edited)
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Program
  5. {
  6.     public static void Main()
  7.     {
  8.         Shop shop = new Shop(400.0f);
  9.         bool isTrading = true;
  10.         string choose;
  11.  
  12.         while (isTrading)
  13.         {
  14.             Console.WriteLine("\nChoose an option : ");
  15.             Console.WriteLine("0. Show items of Trader.");
  16.             Console.WriteLine("1. Show items of Player.");
  17.             Console.WriteLine("2. Trade an item.");
  18.             Console.WriteLine("3. Exit.");
  19.             choose = Console.ReadLine();
  20.  
  21.             switch (choose)
  22.             {
  23.                 case "0":
  24.                     shop.ShowTraderItems();
  25.                     break;
  26.                 case "1":
  27.                     shop.ShowPlayerItems();
  28.                     break;
  29.                 case "2":
  30.                     shop.TryTrade();
  31.                     break;
  32.                 case "3":
  33.                     isTrading = false;
  34.                     break;
  35.                 default:
  36.                     break;
  37.             }
  38.         }
  39.     }
  40. }
  41.  
  42. public class Shop
  43. {
  44.     private Player _player;
  45.     private Trader _trader;
  46.  
  47.     public Shop(float wallet = 50.0f)
  48.     {
  49.         _player = new Player(wallet);
  50.         _trader = new Trader();
  51.         CreateItems();
  52.     }
  53.  
  54.     public void TryTrade()
  55.     {
  56.         if (_trader.ItemsCount != 0)
  57.         {
  58.             int id = GetId();
  59.             Item item = _trader.GetItem(id);
  60.  
  61.             if (_player.Wallet - item.Prize > 0)
  62.             {
  63.                 _trader.RemoveItem(id);
  64.                 _player.AddItem(item);
  65.                 Console.WriteLine("Item sold");
  66.             }
  67.             else
  68.             {
  69.                 Console.WriteLine("You cann't buy this item");
  70.             }
  71.             return;
  72.         }
  73.  
  74.         Console.WriteLine("\nAll items sold out.");
  75.     }
  76.  
  77.     public void ShowPlayerItems()
  78.     {
  79.         _player.ShowList();
  80.     }
  81.  
  82.     public void ShowTraderItems()
  83.     {
  84.         _trader.ShowList();
  85.     }
  86.  
  87.     private void CreateItems()
  88.     {
  89.         Random random = new Random();
  90.         float coinsPerRubel = 100;
  91.         int prizeMinimum = 50;
  92.         int prizeMaximum = 10000;
  93.         int itemsAmountDefault = 7;
  94.         string[] names =
  95.         {
  96.         "Axe", "Sword", "Shield", "Bow"
  97.         };
  98.  
  99.         float prize;
  100.         string name;
  101.         Item item;
  102.  
  103.         for (int i = 0; i < itemsAmountDefault; ++i)
  104.         {
  105.             prize = random.Next(prizeMinimum, prizeMaximum) / coinsPerRubel;
  106.             name = names[random.Next(0, names.Length)];
  107.             item = new Item(name, prize);
  108.             _trader.AddItem(item);
  109.         }
  110.     }
  111.  
  112.     private int GetId()
  113.     {
  114.         int id = 0;
  115.         bool isParse = false;
  116.         bool isInRange = false;
  117.         do
  118.         {
  119.             Console.WriteLine("\nInput id of item:");
  120.             isParse = int.TryParse(Console.ReadLine(), out id);
  121.             isInRange = _trader.SearchElement(id);
  122.  
  123.             if (isParse != true)
  124.                 Console.WriteLine("Cannot read input number.");
  125.             if (isInRange != true)
  126.                 Console.WriteLine("Id number is out of index.");
  127.         }
  128.         while (isParse != true || isInRange != true);
  129.  
  130.         return id;
  131.     }
  132. }
  133.  
  134. public class Item
  135. {
  136.     public string Name { get; private set; }
  137.     public float Prize { get; private set; }
  138.  
  139.     public Item(string name, float prize)
  140.     {
  141.         Name = name;
  142.         Prize = prize;
  143.     }
  144.  
  145.     public Item(Item item)
  146.     {
  147.         Name = item.Name;
  148.         Prize = item.Prize;
  149.     }
  150.  
  151.     public override string ToString()
  152.     {
  153.         return String.Format("{0} - {1:0.00}", Name, Prize);
  154.     }
  155. }
  156.  
  157. public class Person
  158. {
  159.     protected List<Item> Items;
  160.    
  161.     public int ItemsCount => Items.Count;
  162.  
  163.     public float Wallet { get; protected set; }
  164.  
  165.     protected Person()
  166.     {
  167.         Items = new List<Item>();
  168.     }
  169.  
  170.     public bool SearchElement(int index)
  171.     {
  172.         return index >= 0 && index < Items.Count;
  173.     }
  174.  
  175.     public Item GetItem(int index)
  176.     {
  177.         return Items[index];
  178.     }
  179.  
  180.     public void RemoveItem(int index)
  181.     {
  182.         if (SearchElement(index))
  183.         {
  184.             IncreaseWallet(Items[index].Prize);
  185.             Items.RemoveAt(index);
  186.         }
  187.     }
  188.  
  189.     public virtual void ShowList()
  190.     {
  191.         Console.WriteLine();
  192.         for (int i = 0; i < Items.Count; ++i)
  193.             Console.WriteLine($"{i} - {Items[i]}");
  194.     }
  195.  
  196.     public void DecreaseWallet(float prize)
  197.     {
  198.         Wallet -= prize;
  199.     }
  200.  
  201.     public void IncreaseWallet(float prize)
  202.     {
  203.         Wallet += prize;
  204.     }
  205. }
  206.  
  207. public class Player : Person
  208. {
  209.     public Player(float wallet = 50.0f) : base()
  210.     {
  211.         Wallet = wallet;
  212.     }
  213.  
  214.     public override void ShowList()
  215.     {
  216.         Console.WriteLine($"\nYour wallet is : {Wallet:0.00}");
  217.         base.ShowList();
  218.     }
  219.  
  220.     public void AddItem(Item item)
  221.     {
  222.         DecreaseWallet(item.Prize);
  223.         Items.Add(item);
  224.     }
  225.  
  226.     public override string ToString()
  227.     {
  228.         return "I'm player";
  229.     }
  230. }
  231.  
  232. public class Trader : Person
  233. {
  234.     public Trader() : base() { }
  235.  
  236.     public void AddItem(Item item)
  237.     {
  238.         Items.Add(item);
  239.     }
  240.  
  241.     public override string ToString()
  242.     {
  243.         return "I'm trader";
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement