The combined power of F# and C#

CSharp · Console

/Program.cs

using System.Collections.Generic;
using System.Linq;

public class Product 
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}

public class OrderLine 
{
    public Product Product { get; set; }
    public int Quantity { get; set; }
}

public abstract class Discount 
{
    public abstract decimal Apply(decimal subtotal);
}

public class Percentage : Discount {
	private decimal percentage;
	public Percentage(decimal percentage) {
		this.percentage = percentage;
	}
	public override decimal Apply(decimal subtotal) {
		return subtotal * (100M - percentage)/100M;	
	}
}
public class Fixed : Discount {
	private decimal amount;
	public Fixed(decimal amount) {
		this.amount = amount;
	}
	public override decimal Apply(decimal subtotal) {
		return amount - subtotal;
	}
}

public class Store {
	public record Order(List<OrderLine> OrderLines, Discount Discount)
	{
		public decimal CalculateTotalPrice() 
		{
			decimal subtotal = OrderLines.Sum(ol => ol.Product.Price * ol.Quantity);
			return Discount?.Apply(subtotal) ?? subtotal;
		}
	}
}

;