Skip to content

Instantly share code, notes, and snippets.

@CraftyFella
Last active November 16, 2021 20:33
Show Gist options
  • Save CraftyFella/cee8a51168ab9ab12cdc4709226e8fce to your computer and use it in GitHub Desktop.
Save CraftyFella/cee8a51168ab9ab12cdc4709226e8fce to your computer and use it in GitHub Desktop.
try and write chsarp in an fsharp style
using System;
// Trying to replicate https://github.com/ijrussell/FSharpForTheMasses/blob/main/IntroductionToFSharp/4-demo-ddd-3.fsx
namespace csharpchallenge
{
record EligibleRegisteredCustomer(string Name, string Email) : Customer;
record RegisteredCustomer(string CustomerName, string? Email) : Customer;
record UnregisteredCustomer(string Name) : Customer;
abstract record Customer;
class Program
{
static decimal CalculateOrderTotal(Customer customer, decimal spend)
{
var discount =
customer switch
{
EligibleRegisteredCustomer _ when spend >= 100M => spend * 0.1m,
_ => 0m
};
return spend - discount;
}
static void Main(string[] args)
{
var john = new EligibleRegisteredCustomer("John", "john@test.org");
var mary = new EligibleRegisteredCustomer("Mary", "mary@test.org");
var richard = new RegisteredCustomer("Richard", null);
var sarah = new UnregisteredCustomer("Sarah");
var assertJohn = CalculateOrderTotal(john, 100.0m) == 90.0m;
var assertMary = CalculateOrderTotal(mary, 99.0m) == 99.0m;
var assertRichard = CalculateOrderTotal(richard, 100.0M) == 100.0m;
var assertSarah = CalculateOrderTotal(sarah, 100.0M) == 100.0m;
Console.WriteLine(assertJohn);
Console.WriteLine(assertMary);
Console.WriteLine(assertRichard);
Console.WriteLine(assertSarah);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment