Last active
April 20, 2024 19:03
-
-
Save Dead-in-side/743de304dee2710a2e9f766bbf0a3715 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace IJunior | |
{ | |
public class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
Salesman salesman = new Salesman(new List<Product>(), 0); | |
Buyer buyer = new Buyer(new List<Product>(), 15); | |
salesman.AddProductList(); | |
salesman.ShowInfo(); | |
buyer.PayForProduct(salesman.SellProduct(buyer.ChooseProduct())); | |
buyer.ShowInfo(); | |
} | |
} | |
public abstract class Human | |
{ | |
protected List<Product> Products; | |
protected int Money; | |
public void ShowInfo() | |
{ | |
foreach (var product in Products) | |
{ | |
product.ShowInfo(); | |
} | |
Console.WriteLine("Количество денег: " + Money); | |
} | |
} | |
public class Salesman : Human | |
{ | |
public Salesman(List<Product> products, int money) | |
{ | |
Products = products; | |
Money = money; | |
} | |
public Product SellProduct(string productName) | |
{ | |
Product productForSale = Products.Find(delegate (Product product) { return product.Name == productName; }); | |
if (productForSale == null == false) | |
{ | |
Money += productForSale.Price; | |
return productForSale; | |
} | |
else | |
{ | |
Console.WriteLine("Данного товара нет на прилавке"); | |
return productForSale; | |
} | |
} | |
public void AddProductList() | |
{ | |
Console.WriteLine("Введите наименование товара: "); | |
string name = Console.ReadLine(); | |
Console.WriteLine("Выставьет цену: "); | |
if (int.TryParse(Console.ReadLine(), out int price)) | |
{ | |
Products.Add(new Product(name, price)); | |
} | |
} | |
} | |
public class Buyer : Human | |
{ | |
public Buyer(List<Product> products, int money) | |
{ | |
Products = products; | |
Money = money; | |
} | |
public string ChooseProduct() | |
{ | |
Console.WriteLine("Выберите вещь для покупки: "); | |
string productName = Console.ReadLine(); | |
return productName; | |
} | |
public void PayForProduct(Product product) | |
{ | |
if (product == null == false) | |
{ | |
Products.Add(product); | |
Money -= product.Price; | |
} | |
} | |
} | |
public class Product | |
{ | |
public Product(string name, int price) | |
{ | |
Name = name; | |
Price = price; | |
} | |
public string Name { get; private set; } | |
public int Price { get; private set; } | |
public void ShowInfo() | |
{ | |
Console.WriteLine(Name + ". Цена: " + Price); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment