Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created Feb 9, 2022
Embed
What would you like to do?
public class CatalogProduct
{
public CatalogProduct()
{
}
public CatalogProduct(string sku, string name, string description, decimal price, decimal cost)
{
Sku = sku;
Name = name;
Description = description;
Price = price;
Cost = cost;
}
public string Sku { get; private set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; private set; }
public decimal Cost { get; private set; }
public int QuantityOnHand { get; private set; }
public bool ForSale { get; private set; }
public bool FreeShipping { get; private set; }
public void SetAvailable()
{
if (CanSetAvailable())
{
ForSale = true;
}
}
public bool CanSetAvailable()
{
return ForSale == false && QuantityOnHand > 0;
}
public string ValidationError()
{
return CanSetAvailable()
? null
: "Product is must be unavailable and Quantity greater than 0";
}
public bool CanSetUnavailable()
{
return ForSale;
}
public void InventoryAdjustment(int adjustment)
{
QuantityOnHand += adjustment;
if (QuantityOnHand <= 0)
{
UnavailableForSale();
}
}
public void IncreasePrice(decimal newPrice)
{
if (newPrice < Price)
{
throw new InvalidOperationException("New price must be greater than current price.");
}
Price = newPrice;
}
public void DecreasePrice(decimal newPrice)
{
if (newPrice > Price)
{
throw new InvalidOperationException("New price must be less than current price.");
}
Price = newPrice;
if (Price <= 0)
{
UnavailableForSale();
}
}
public void UnavailableForSale()
{
ForSale = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment