Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created February 9, 2022 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/bf669a9cfb799560880394606964e860 to your computer and use it in GitHub Desktop.
Save dcomartin/bf669a9cfb799560880394606964e860 to your computer and use it in GitHub Desktop.
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