Last active
August 20, 2024 18:54
-
-
Save Olsney/0360294d3a07f458fecaf205c0bfd42a to your computer and use it in GitHub Desktop.
NapilnikQuest2
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 NapilnikQuest2; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Good iPhone12 = new Good("IPhone 12"); | |
Good iPhone11 = new Good("IPhone 11"); | |
Warehouse warehouse = new Warehouse(); | |
Shop shop = new Shop(warehouse); | |
warehouse.Delive(iPhone12, 10); | |
warehouse.Delive(iPhone11, 1); | |
//Вывод всех товаров на складе с их остатком | |
//Попросили вывод - я его дописал | |
warehouse.ShowInfo(); | |
Cart cart = shop.Cart(); | |
cart.Add(iPhone12, 4); | |
cart.Add(iPhone11, 3); //при такой ситуации возникает ошибка так, как нет нужного количества товара на складе | |
//Вывод всех товаров в корзине | |
//Попросили вывод - я его дописал | |
cart.ShowInfo(); | |
Console.WriteLine(cart.Order().Paylink); | |
cart.Add(iPhone12, 9); //Ошибка, после заказа со склада убираются заказанные товары | |
} | |
} | |
interface IWarehouse | |
{ | |
bool CanGive(Good good, int amount); | |
void Remove(Good good, int amount); | |
} | |
class Cart | |
{ | |
private readonly IWarehouse _warehouse; | |
private readonly Dictionary<Good, int> _goods; | |
public Cart(IWarehouse warehouse) | |
{ | |
ArgumentNullException.ThrowIfNull(warehouse); | |
_warehouse = warehouse; | |
_goods = new Dictionary<Good, int>(); | |
} | |
public void Add(Good good, int amount) | |
{ | |
ArgumentNullException.ThrowIfNull(good); | |
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(amount); | |
amount += _goods.GetValueOrDefault(good, 0); | |
if (_warehouse.CanGive(good, amount) == false) | |
{ | |
Console.WriteLine("Товара нет на складе"); | |
return; | |
} | |
_goods[good] = amount; | |
} | |
public Order Order() | |
{ | |
foreach (var good in _goods.Keys) | |
{ | |
if (_warehouse.CanGive(good, _goods[good]) == false) | |
throw new InvalidOperationException(); | |
} | |
foreach (var good in _goods.Keys) | |
{ | |
_warehouse.Remove(good, _goods[good]); | |
} | |
return new Order(); | |
} | |
public void ShowInfo() | |
{ | |
Console.WriteLine("В корзине:"); | |
foreach (var good in _goods) | |
{ | |
Console.WriteLine($"{good.Key.Name} - {good.Value}"); | |
} | |
} | |
} | |
class Order | |
{ | |
public Order() => | |
Paylink = CreatePayLink(); | |
public string Paylink { get; } | |
private string CreatePayLink() => | |
"unique paylink"; | |
} | |
class Shop | |
{ | |
private readonly Warehouse _warehouse; | |
public Shop(Warehouse warehouse) | |
{ | |
ArgumentNullException.ThrowIfNull(warehouse); | |
_warehouse = warehouse; | |
} | |
public Cart Cart() | |
{ | |
return new Cart(_warehouse); | |
} | |
} | |
class Warehouse : IWarehouse | |
{ | |
private readonly Dictionary<Good, int> _goods; | |
public Warehouse() | |
{ | |
_goods = new Dictionary<Good, int>(); | |
} | |
public void Delive(Good good, int amount) | |
{ | |
ArgumentNullException.ThrowIfNull(good); | |
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(amount); | |
amount += _goods.GetValueOrDefault(good, 0); | |
_goods[good] = amount; | |
} | |
public bool CanGive(Good good, int amount) | |
{ | |
if (_goods.TryGetValue(good, out int goodAmount) == false) | |
return false; | |
return goodAmount >= amount; | |
} | |
public void Remove(Good good, int amount) | |
{ | |
ArgumentNullException.ThrowIfNull(good); | |
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(amount); | |
if (CanGive(good, amount) == false) | |
throw new InvalidOperationException(); | |
_goods[good] -= amount; | |
if (_goods[good] == 0) | |
_goods.Remove(good); | |
} | |
public void ShowInfo() | |
{ | |
Console.WriteLine("На складе:"); | |
foreach (var good in _goods) | |
{ | |
Console.WriteLine($"{good.Key.Name} - {good.Value}"); | |
} | |
} | |
} | |
class Good | |
{ | |
public Good(string name) | |
{ | |
ArgumentNullException.ThrowIfNullOrEmpty(name); | |
Name = name; | |
} | |
public string Name { get; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment