Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Homework43_
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Trader trader = new Trader();
- Salesman salesman = new Salesman(500);
- Customer customer = new Customer(50);
- bool isWork = true;
- string userInput;
- while (isWork)
- {
- Console.WriteLine($"Деньги продавца: ${salesman.Money} \nВаши деньги: ${customer.Money}\n");
- Console.WriteLine("1 - Продавец показывает его товар.");
- Console.WriteLine("2 - Купить товар.");
- Console.WriteLine("3 - Показать преобретенные у продавца товары.");
- Console.WriteLine("4 - Выход");
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case "1":
- salesman.ShowInventory();
- break;
- case "2":
- trader.ToBargain(customer, salesman);
- break;
- case "3":
- customer.ShowInventory();
- break;
- case "4":
- isWork = false;
- break;
- default:
- Console.WriteLine("Неверная команда!");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- class Trader
- {
- protected List<Product> Inventory = new List<Product>();
- public int Money { get; protected set; }
- public void ToBargain(Customer customer, Salesman salesman)
- {
- salesman.ShowInventory();
- if (salesman.CheckAvailableProduct(out Product product))
- {
- customer.AddToInventory(product);
- salesman.RemoveFromInventory(product);
- Console.WriteLine("Вы купили {0}", product.Name);
- }
- else
- {
- Console.WriteLine("Товар не найден!.");
- }
- }
- public virtual void ShowInventory()
- {
- foreach (Product product in Inventory)
- {
- Console.WriteLine(product.Name);
- }
- }
- protected void AddToInventory(Product product)
- {
- GivePayment(product);
- Inventory.Add(product);
- }
- protected void RemoveFromInventory(Product product)
- {
- AcceptPayment(product);
- Inventory.Remove(product);
- }
- protected void AcceptPayment(Product product)
- {
- Money += product.Cost;
- }
- protected void GivePayment(Product product)
- {
- Money -= product.Cost;
- }
- }
- class Customer : Trader
- {
- public Customer(int money)
- {
- Money = money;
- }
- }
- class Salesman : Trader
- {
- public Salesman(int money)
- {
- Money = money;
- Inventory.Add(new Product("Мясо", 15));
- Inventory.Add(new Product("Молоко", 8));
- Inventory.Add(new Product("Рис", 4));
- Inventory.Add(new Product("Вода", 1));
- Inventory.Add(new Product("Хлеб", 2));
- Inventory.Add(new Product("Масло", 10));
- }
- public bool CheckAvailableProduct(out Product product)
- {
- Console.Write("Введите наименование товара: ");
- string userInput = Console.ReadLine();
- foreach (Product productInInventory in Inventory)
- {
- if (productInInventory.Name.ToLower() == userInput.ToLower())
- {
- product = productInInventory;
- return true;
- }
- }
- product = null;
- return false;
- }
- public override void ShowInventory()
- {
- foreach (Product product in Inventory)
- {
- Console.WriteLine(product.Name + " - $" + product.Cost);
- }
- }
- }
- class Product
- {
- public string Name { get; private set; }
- public int Cost { get; private set; }
- public Product(string name, int cost)
- {
- Name = name;
- Cost = cost;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement