Advertisement
JohnJuly

Homework43

May 6th, 2025
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Homework43_
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Trader trader = new Trader();
  11.             Salesman salesman = new Salesman(500);
  12.             Customer customer = new Customer(50);
  13.  
  14.             bool isWork = true;
  15.             string userInput;
  16.  
  17.             while (isWork)
  18.             {
  19.                 Console.WriteLine($"Деньги продавца: ${salesman.Money} \nВаши деньги: ${customer.Money}\n");
  20.                 Console.WriteLine("1 - Продавец показывает его товар.");
  21.                 Console.WriteLine("2 - Купить товар.");
  22.                 Console.WriteLine("3 - Показать преобретенные у продавца товары.");
  23.                 Console.WriteLine("4 - Выход");
  24.                 userInput = Console.ReadLine();
  25.  
  26.                 switch (userInput)
  27.                 {
  28.                     case "1":
  29.                         salesman.ShowInventory();
  30.                         break;
  31.  
  32.                     case "2":
  33.                         trader.ToBargain(customer, salesman);
  34.                         break;
  35.  
  36.                     case "3":
  37.                         customer.ShowInventory();
  38.                         break;
  39.  
  40.                     case "4":
  41.                         isWork = false;
  42.                         break;
  43.  
  44.                     default:
  45.                         Console.WriteLine("Неверная команда!");
  46.                         break;
  47.                 }
  48.  
  49.                 Console.ReadKey();
  50.                 Console.Clear();
  51.             }
  52.         }
  53.     }
  54.  
  55.     class Trader
  56.     {
  57.         protected List<Product> Inventory = new List<Product>();
  58.  
  59.         public int Money { get; protected set; }
  60.  
  61.         public void ToBargain(Customer customer, Salesman salesman)
  62.         {
  63.             salesman.ShowInventory();
  64.  
  65.             if (salesman.CheckAvailableProduct(out Product product))
  66.             {
  67.                 customer.AddToInventory(product);
  68.                 salesman.RemoveFromInventory(product);
  69.                 Console.WriteLine("Вы купили {0}", product.Name);
  70.             }
  71.             else
  72.             {
  73.                 Console.WriteLine("Товар не найден!.");
  74.             }
  75.         }
  76.  
  77.         public virtual void ShowInventory()
  78.         {
  79.             foreach (Product product in Inventory)
  80.             {
  81.                 Console.WriteLine(product.Name);
  82.             }
  83.         }
  84.  
  85.         protected void AddToInventory(Product product)
  86.         {
  87.             GivePayment(product);
  88.             Inventory.Add(product);
  89.         }
  90.  
  91.         protected void RemoveFromInventory(Product product)
  92.         {
  93.             AcceptPayment(product);
  94.             Inventory.Remove(product);
  95.         }
  96.  
  97.         protected void AcceptPayment(Product product)
  98.         {
  99.             Money += product.Cost;
  100.         }
  101.  
  102.         protected void GivePayment(Product product)
  103.         {
  104.             Money -= product.Cost;
  105.         }
  106.     }
  107.  
  108.     class Customer : Trader
  109.     {
  110.         public Customer(int money)
  111.         {
  112.             Money = money;
  113.         }
  114.     }
  115.  
  116.     class Salesman : Trader
  117.     {
  118.         public Salesman(int money)
  119.         {
  120.             Money = money;
  121.             Inventory.Add(new Product("Мясо", 15));
  122.             Inventory.Add(new Product("Молоко", 8));
  123.             Inventory.Add(new Product("Рис", 4));
  124.             Inventory.Add(new Product("Вода", 1));
  125.             Inventory.Add(new Product("Хлеб", 2));
  126.             Inventory.Add(new Product("Масло", 10));
  127.         }
  128.  
  129.         public bool CheckAvailableProduct(out Product product)
  130.         {
  131.             Console.Write("Введите наименование товара: ");
  132.             string userInput = Console.ReadLine();
  133.  
  134.             foreach (Product productInInventory in Inventory)
  135.             {
  136.                 if (productInInventory.Name.ToLower() == userInput.ToLower())
  137.                 {
  138.                     product = productInInventory;
  139.                     return true;
  140.                 }
  141.             }
  142.  
  143.             product = null;
  144.             return false;
  145.         }
  146.  
  147.         public override void ShowInventory()
  148.         {
  149.             foreach (Product product in Inventory)
  150.             {
  151.                 Console.WriteLine(product.Name + " - $" + product.Cost);
  152.             }
  153.         }
  154.     }
  155.  
  156.     class Product
  157.     {
  158.         public string Name { get; private set; }
  159.  
  160.         public int Cost { get; private set; }
  161.  
  162.         public Product(string name, int cost)
  163.         {
  164.             Name = name;
  165.             Cost = cost;
  166.         }
  167.     }
  168. }
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement