Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Program
- {
- public static void Main()
- {
- Shop shop = new Shop(400.0f);
- bool isTrading = true;
- string choose;
- while (isTrading)
- {
- Console.WriteLine("\nChoose an option : ");
- Console.WriteLine("0. Show items of Trader.");
- Console.WriteLine("1. Show items of Player.");
- Console.WriteLine("2. Trade an item.");
- Console.WriteLine("3. Exit.");
- choose = Console.ReadLine();
- switch (choose)
- {
- case "0":
- shop.ShowTraderItems();
- break;
- case "1":
- shop.ShowPlayerItems();
- break;
- case "2":
- shop.TryTrade();
- break;
- case "3":
- isTrading = false;
- break;
- default:
- break;
- }
- }
- }
- }
- public class Shop
- {
- private Player _player;
- private Trader _trader;
- public Shop(float wallet = 50.0f)
- {
- _player = new Player(wallet);
- _trader = new Trader();
- CreateItems();
- }
- public void TryTrade()
- {
- if (_trader.ItemsCount != 0)
- {
- int id = GetId();
- Item item = _trader.GetItem(id);
- if (_player.Wallet - item.Prize > 0)
- {
- _trader.RemoveItem(id);
- _player.AddItem(item);
- Console.WriteLine("Item sold");
- }
- else
- {
- Console.WriteLine("You cann't buy this item");
- }
- return;
- }
- Console.WriteLine("\nAll items sold out.");
- }
- public void ShowPlayerItems()
- {
- _player.ShowList();
- }
- public void ShowTraderItems()
- {
- _trader.ShowList();
- }
- private void CreateItems()
- {
- Random random = new Random();
- float coinsPerRubel = 100;
- int prizeMinimum = 50;
- int prizeMaximum = 10000;
- int itemsAmountDefault = 7;
- string[] names =
- {
- "Axe", "Sword", "Shield", "Bow"
- };
- float prize;
- string name;
- Item item;
- for (int i = 0; i < itemsAmountDefault; ++i)
- {
- prize = random.Next(prizeMinimum, prizeMaximum) / coinsPerRubel;
- name = names[random.Next(0, names.Length)];
- item = new Item(name, prize);
- _trader.AddItem(item);
- }
- }
- private int GetId()
- {
- int id = 0;
- bool isParse = false;
- bool isInRange = false;
- do
- {
- Console.WriteLine("\nInput id of item:");
- isParse = int.TryParse(Console.ReadLine(), out id);
- isInRange = _trader.SearchElement(id);
- if (isParse != true)
- Console.WriteLine("Cannot read input number.");
- if (isInRange != true)
- Console.WriteLine("Id number is out of index.");
- }
- while (isParse != true || isInRange != true);
- return id;
- }
- }
- public class Item
- {
- public string Name { get; private set; }
- public float Prize { get; private set; }
- public Item(string name, float prize)
- {
- Name = name;
- Prize = prize;
- }
- public Item(Item item)
- {
- Name = item.Name;
- Prize = item.Prize;
- }
- public override string ToString()
- {
- return String.Format("{0} - {1:0.00}", Name, Prize);
- }
- }
- public class Person
- {
- protected List<Item> Items;
- public int ItemsCount => Items.Count;
- public float Wallet { get; protected set; }
- protected Person()
- {
- Items = new List<Item>();
- }
- public bool SearchElement(int index)
- {
- return index >= 0 && index < Items.Count;
- }
- public Item GetItem(int index)
- {
- return Items[index];
- }
- public void RemoveItem(int index)
- {
- if (SearchElement(index))
- {
- IncreaseWallet(Items[index].Prize);
- Items.RemoveAt(index);
- }
- }
- public virtual void ShowList()
- {
- Console.WriteLine();
- for (int i = 0; i < Items.Count; ++i)
- Console.WriteLine($"{i} - {Items[i]}");
- }
- public void DecreaseWallet(float prize)
- {
- Wallet -= prize;
- }
- public void IncreaseWallet(float prize)
- {
- Wallet += prize;
- }
- }
- public class Player : Person
- {
- public Player(float wallet = 50.0f) : base()
- {
- Wallet = wallet;
- }
- public override void ShowList()
- {
- Console.WriteLine($"\nYour wallet is : {Wallet:0.00}");
- base.ShowList();
- }
- public void AddItem(Item item)
- {
- DecreaseWallet(item.Prize);
- Items.Add(item);
- }
- public override string ToString()
- {
- return "I'm player";
- }
- }
- public class Trader : Person
- {
- public Trader() : base() { }
- public void AddItem(Item item)
- {
- Items.Add(item);
- }
- public override string ToString()
- {
- return "I'm trader";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement