Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace magazine
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool isWork = true;
- Trader trader = new Trader();
- Player player = new Player(1000);
- while (isWork)
- {
- player.ShowBag();
- Console.WriteLine("\n[1] - Показать список товаров;\n" +
- "[2] - Купить товар;\n" +
- "[3] - Выйти из магазина");
- Console.Write("Ввод: ");
- switch (Console.ReadLine())
- {
- case "1":
- trader.ShowGoods();
- break;
- case "2":
- trader.Trade(player);
- break;
- case "3":
- isWork = false;
- break;
- }
- }
- }
- }
- class Trader
- {
- private int _cashbox;
- private List<Goods> _goods = new List<Goods>();
- public Trader()
- {
- Random rand = new Random();
- for (int i = 0; i < 15; i++)
- {
- _goods.Add(new Goods(rand.Next(0, 1000), rand.Next(0, 1000)));
- }
- }
- public void Trade(Player player)
- {
- Console.Write("Выберите товар из списка: ");
- if (int.TryParse(Console.ReadLine(), out int goodsNumber)
- && (goodsNumber >= 0 && goodsNumber < _goods.Count))
- {
- --goodsNumber;
- if (player.CheckMoney(_goods[goodsNumber]))
- {
- Console.WriteLine("Оплачиваем товар...");
- _cashbox += player.ToPay(_goods[goodsNumber]);
- _goods.RemoveAt(goodsNumber);
- }
- else
- {
- Console.WriteLine("У вас нет денег на товар!");
- return;
- }
- }
- else
- {
- Console.WriteLine("Товара нет в списке!");
- return;
- }
- }
- public void ShowGoods()
- {
- Console.WriteLine("\nСписок товаров: ");
- for (int i = 0; i < _goods.Count; i++)
- {
- Console.WriteLine($"[№{i + 1}]: #{_goods[i].ProductCode} - ${_goods[i].Price}");
- }
- }
- }
- class Goods
- {
- public int ProductCode { get; private set; }
- public int Price { get; private set; }
- public Goods(int productCode, int price)
- {
- ProductCode = productCode;
- Price = price;
- }
- }
- class Player
- {
- private int _money;
- private List<Goods> _shopingBag = new List<Goods>();
- public Player(int money)
- {
- _money = money;
- }
- public bool CheckMoney(Goods goods)
- {
- return _money >= goods.Price;
- }
- public int ToPay(Goods goods)
- {
- _money -= goods.Price;
- _shopingBag.Add(goods);
- return goods.Price;
- }
- public void ShowBag()
- {
- Console.WriteLine($"\nКошелек: ${_money}");
- if (_shopingBag.Count != 0)
- {
- Console.WriteLine("Корзина: ");
- foreach (var goods in _shopingBag)
- {
- Console.WriteLine($"Товар с номером: {goods.ProductCode}");
- }
- }
- else
- {
- Console.WriteLine("Корзина пуста!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement