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 CSLight15
- {
- class Program
- {
- static void Main(string[] args)
- {
- DealParty player = new DealParty(100);
- DealParty seller = new DealParty(10000);
- seller.AddProduct(new Product("Компьютер", 300, "Япония"));
- seller.AddProduct(new Product("Мышь", 20, "США"));
- seller.AddProduct(new Product("Клавиатура", 25, "Франция"));
- while (true)
- {
- Console.WriteLine("1) Показать все товары продавца");
- Console.WriteLine("2) Показать все вещи игрока");
- Console.WriteLine("3) Купить товар");
- switch (Console.ReadLine())
- {
- case "1":
- Console.Clear();
- seller.ShowProductList();
- break;
- case "2":
- Console.Clear();
- player.ShowProductList();
- Console.WriteLine($"Оставшиеся деньги: {player.Money}$");
- break;
- case "3":
- Console.Clear();
- seller.ShowProductList();
- Console.Write("Введите название товара: ");
- switch (player.Buy(Console.ReadLine(), seller))
- {
- case 1:
- Console.WriteLine("Покупка прошла успешно!");
- break;
- case 0:
- Console.WriteLine("У вас недостаточно средств");
- break;
- case -1:
- Console.WriteLine("Такого товара здесь нет");
- break;
- }
- break;
- }
- }
- }
- class DealParty
- {
- private Product[] _products = new Product[0];
- public int Money { get; private set; }
- public DealParty(int money = 0)
- {
- if (money >= 0)
- Money = money;
- }
- public int Buy(string productName, DealParty seller)
- {
- for (int i = 0; i < seller._products.Length; i++)
- {
- if (seller._products[i].Name.ToLower() == productName.ToLower())
- {
- if (Money >= seller._products[i].Price)
- {
- AddProduct(seller._products[i]);
- seller.RemoveProduct(i);
- return 1;
- }
- else
- {
- return 0;
- }
- }
- }
- return -1;
- }
- public void AddProduct(Product product)
- {
- Money -= product.Price;
- Product[] temp = new Product[_products.Length + 1];
- for (int i = 0; i < _products.Length; i++)
- {
- temp[i] = _products[i];
- }
- temp[temp.Length - 1] = product;
- _products = temp;
- }
- public void RemoveProduct(int productNumber)
- {
- Money += _products[productNumber].Price;
- Product[] temp = new Product[_products.Length - 1];
- for (int i = 0, j = 0; i < temp.Length; i++, j++)
- {
- if (productNumber == i && productNumber != _products.Length - 1)
- {
- j++;
- }
- temp[i] = _products[j];
- }
- _products = temp;
- }
- public void ShowProductList()
- {
- for (int i = 0; i < _products.Length; i++)
- {
- _products[i].ShowProduct();
- }
- }
- }
- class Product
- {
- public string Name { get; private set; }
- public int Price { get; private set; }
- public string ProducingCountry { get; private set; }
- public Product (string name, int price, string country)
- {
- Name = name;
- Price = price;
- ProducingCountry = country;
- }
- public void ShowProduct()
- {
- Console.WriteLine($"Название: {Name}\nЦена: {Price}$\nСтрана-производитель: {ProducingCountry}\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement