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 HW_5_4
- {
- class Program
- {
- static void Main(string[] args)
- {
- Product skirt = new Product("Юбка", 10, 2);
- Product belt = new Product("Ремень", 15.5, 3);
- Product socks = new Product("Носки", 1.4, 10);
- Product armani = new Product("Сережки", 95, 10);
- Product[] stock = { skirt, belt, socks, armani };
- Seller seller = new Seller(stock);
- Buyer buyer = new Buyer(100);
- Console.WriteLine("Добро пожаловать в магазин.\n" +
- "Выберите дальнейшее действие\n" +
- "1. Просмотреть весь ассортимент\n" +
- "2. Купить товар\n" +
- "3. Просмотреть свои вещи и баланс\n" +
- "4. Выход");
- while (true)
- {
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case "1":
- seller.WriteAllProducts();
- break;
- case "2":
- seller.WriteAllProducts();
- Console.Write("Какой товар хотите купить? Введите название: ");
- string desiredName = Console.ReadLine();
- Console.Write("Сколько?");
- int desiredAmount = Convert.ToInt32(Console.ReadLine());
- if (buyer.Balance >= seller.GetPurchasePrice(desiredName, desiredAmount))
- {
- if (seller.isEnough(desiredName, desiredAmount))
- {
- buyer.Purchase(seller.Sell(desiredName, desiredAmount));
- seller.DeleteItem(desiredName, desiredAmount);
- }
- else
- {
- Console.WriteLine("Извините, недостаточно товара на складе.");
- }
- }
- else
- {
- Console.WriteLine("Извините, у вас недостаточно средств.");
- }
- break;
- case "3":
- buyer.WriteAllProperty();
- break;
- case "4":
- Environment.Exit(0);
- break;
- }
- }
- }
- }
- class Owner
- {
- protected Product[] _products { get; set; }
- public Owner(Product[] products)
- {
- _products = products;
- }
- public Owner()
- {
- _products = new Product[0];
- }
- public void AddItem(Product product)
- {
- bool alreadyExist = false;
- for (int i = 0; i < _products.Length; i++)
- {
- if (product.Name == _products[i].Name)
- {
- _products[i].increaseAmount(product.Amount);
- alreadyExist = true;
- }
- }
- if (!alreadyExist)
- {
- Product[] tempList = new Product[_products.Length];
- for (int j = 0; j < _products.Length; j++)
- {
- tempList[j] = _products[j];
- }
- _products = new Product[_products.Length + 1];
- for (int i = 0; i < _products.Length - 1; i++)
- {
- _products[i] = tempList[i];
- }
- _products[_products.Length - 1] = product;
- }
- }
- public void DeleteItem(String name, int amount)
- {
- for (int i = 0; i < _products.Length; i++)
- {
- if (name == _products[i].Name)
- {
- _products[i].decreaseAmount(amount);
- }
- }
- }
- }
- class Seller : Owner
- {
- public Seller(Product[] products) : base(products)
- {
- }
- public void WriteAllProducts()
- {
- Console.WriteLine("В магазине в наличии:");
- for (int i = 0; i < _products.Length; i++)
- {
- if (_products[i].Amount != 0)
- {
- Console.WriteLine($"{_products[i].Name} {_products[i].Price} руб. {_products[i].Amount} шт.");
- }
- }
- }
- public Product Sell(string name, int amount)
- {
- string newName = "";
- double price = 0;
- for (int i = 0; i < _products.Length; i++)
- {
- if (name == _products[i].Name)
- {
- newName = _products[i].Name;
- price = _products[i].Price;
- }
- }
- Product sell = new Product(newName, price, amount);
- return sell;
- }
- public double GetPurchasePrice(string name, int amount)
- {
- double purchasePrice = 0;
- for (int i = 0; i < _products.Length; i++)
- {
- if (name == _products[i].Name)
- {
- purchasePrice = _products[i].Price * amount;
- }
- }
- return purchasePrice;
- }
- public bool isEnough(string name, int amount)
- {
- bool isEnough = false;
- for (int i = 0; i < _products.Length; i++)
- {
- if (name == _products[i].Name)
- {
- if (_products[i].Amount >= amount)
- {
- isEnough = true;
- }
- }
- }
- return isEnough;
- }
- }
- class Buyer : Owner
- {
- private double _balance;
- public double Balance
- {
- get
- {
- return _balance;
- }
- set
- {
- if (value >= 0)
- {
- _balance = value;
- }
- }
- }
- public Buyer(double balance)
- {
- _balance = balance;
- _products = new Product[0];
- }
- public void Purchase(Product product)
- {
- AddItem(product);
- _balance -= product.Price;
- }
- public void WriteAllProperty()
- {
- Console.WriteLine("У вас есть: ");
- for (int i = 0; i < _products.Length; i++)
- {
- Console.WriteLine($"{_products[i].Name} {_products[i].Amount} шт.");
- }
- Console.WriteLine($"Ваш баланс {_balance} руб.");
- }
- }
- class Product
- {
- public string Name { get; private set; }
- public double Price { get; set; }
- private int _amount;
- public int Amount
- {
- get
- {
- return _amount;
- }
- private set
- {
- if (value >= 0)
- {
- _amount = value;
- }
- }
- }
- public Product(string name, double price, int amount)
- {
- Name = name;
- Price = price;
- _amount = amount;
- }
- public void increaseAmount(int addAmount)
- {
- _amount += addAmount;
- }
- public void decreaseAmount(int deductAmount)
- {
- _amount -= deductAmount;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement