Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool isOpen = true;
- Product product;
- Seller seller = new Seller();
- Console.WriteLine("Добрый день. Сколько у вас монет?");
- int playerMoney = Convert.ToInt32(Console.ReadLine());
- Player player = new Player(playerMoney);
- while (isOpen)
- {
- Console.WriteLine("Выберите команду");
- Console.WriteLine("1 - Список товаров у продавца\n" + "2 - Купить товар\n" +
- "3 - Список товаров у игрока\n" + "0 - Выход из программы");
- int userInput = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine();
- Console.Clear();
- switch (userInput)
- {
- case 1:
- seller.ShowSellerInventory();
- break;
- case 2:
- seller.ShowSellerInventory();
- Console.WriteLine("Выберите номер товара, который хотите купить\n");
- int userNumber = Convert.ToInt32(Console.ReadLine()) - 1;
- product = seller.GetProduct(userNumber);
- if (player.IsEnoughMoney(product) == true)
- {
- seller.SellProduct(userNumber);
- }
- break;
- case 3:
- player.ShowPlayerProducts();
- break;
- case 0:
- isOpen = false;
- break;
- default:
- Console.WriteLine("Недопустимое значение");
- break;
- }
- }
- }
- }
- class Inventory
- {
- private Product[] _products;
- public Product[] Products
- {
- get
- {
- return _products;
- }
- }
- public Inventory()
- {
- _products = new Product[] {new Product("Печенье", 70),
- new Product("Молоко", 50), new Product("Яблоки", 150),
- new Product("Сыр", 200), new Product("Киви", 120) };
- }
- }
- class Product
- {
- public string Name { get; private set; }
- public int Price { get; private set; }
- public Product(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public void ShowInfo()
- {
- Console.WriteLine(Name + ", цена " + Price + "у.е.");
- }
- }
- class Player
- {
- private Product[] _products = new Product[0];
- private int _money;
- public Player(int money)
- {
- _money = money;
- }
- public bool IsEnoughMoney(Product product)
- {
- bool isEnoughMoney = false;
- if (_money >= product.Price)
- {
- _money -= product.Price;
- AddProduct(product);
- Console.WriteLine("Товар куплен");
- Console.WriteLine("\nУ игрока осталось " + _money + " монет\n");
- isEnoughMoney = true;
- }
- else
- {
- Console.WriteLine("У игрока недостаточно денег");
- }
- return isEnoughMoney;
- }
- public void AddProduct(Product product)
- {
- Product[] tempArray = new Product[_products.Length + 1];
- for (int i = 0; i < _products.Length; i++)
- {
- tempArray[i] = _products[i];
- }
- tempArray[tempArray.Length - 1] = product;
- _products = tempArray;
- }
- public void ShowPlayerProducts()
- {
- for (int i = 0; i < _products.Length; i++)
- {
- Console.Write(i + 1 + ") ");
- _products[i].ShowInfo();
- }
- }
- }
- class Seller
- {
- private Inventory _inventory = new Inventory();
- private Product[] _products;
- public Seller()
- {
- _products = _inventory.Products;
- }
- public Product GetProduct(int index)
- {
- return _products[index];
- }
- public void SellProduct(int index)
- {
- Product[] tempArray = new Product[_products.Length - 1];
- for (int i = 0; i < index; i++)
- {
- tempArray[i] = _products[i];
- }
- for (int i = index; i < tempArray.Length; i++)
- {
- tempArray[i] = _products[i + 1];
- }
- _products = tempArray;
- }
- public void ShowSellerInventory()
- {
- for (int i = 0; i < _products.Length; i++)
- {
- Console.Write(i + 1 + ") ");
- _products[i].ShowInfo();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement