ex-oop-05-coffe-vending-mac.../CoffeeVendingMachines/Product.cs

40 lines
896 B
C#

namespace CoffeeVendingMachines;
public class Product
{
public const int FallbackPrice = 50;
private readonly int _price;
private int _stock;
public bool InStock => _stock > 0;
public string Name { get; }
public int NumberSold { get; private set; }
public int Price => _price;
public Product (string name, int price = FallbackPrice, int stock = 0)
{
Name = name;
_price = price;
_stock = stock;
if(price <= 0 || price % 5 != 0)
{
_price = FallbackPrice;
}
}
public bool AddSale()
{
if (InStock)
{
NumberSold++;
_stock--;
return true;
}
return false;
}
public override string ToString()
{
return $"{Name,-10} € {(decimal)Price/100:0.00} [{_stock} in stock | {NumberSold} sold]";
}
}