Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Delegates Demo

using using DemoLibrary;\


DemoLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleUI
{
class Program
{
static ShoppingCartModel cart = new ShoppingCartModel();

static void Main(string[] args)


{
PopulateCartWithDemoData();

Console.WriteLine($"The total for the cart is {cart.GenerateTotal(SubTotalAlert, CalculateLeveledDiscount,


AlertUser):C2}");
Console.WriteLine();

decimal total = cart.GenerateTotal((subTotal) => Console.WriteLine($"The subtotal for cart 2 is {subTotal:C2}"),


(products, subTotal) => {
if (products.Count > 3)
{
return subTotal * 0.5M;
}
else
{
return subTotal;
}
},
(message) => Console.WriteLine($"Cart 2 Alert: { message }"));

Console.WriteLine($"The total for cart 2 is {total:C2}");


Console.WriteLine();

Console.WriteLine();
Console.Write("Please press any key to exit the application...");
Console.ReadKey();
}

private static void SubTotalAlert(decimal subTotal)


{
Console.WriteLine($"The subtotal is {subTotal:C2}");
}

private static void AlertUser(string message)


{
Console.WriteLine(message);
}

private static decimal CalculateLeveledDiscount(List<ProductModel> items, decimal subTotal)


{
if (subTotal > 100)
{
return subTotal * 0.80M;
}
else if (subTotal > 50)
{
return subTotal * 0.85M;
}
else if (subTotal > 10)
{
return subTotal * 0.95M;
}
else
{
return subTotal;
}
}

private static void PopulateCartWithDemoData()


{
cart.Items.Add(new ProductModel { ItemName = "Cereal", Price = 3.63M });
cart.Items.Add(new ProductModel { ItemName = "Milk", Price = 2.95M });
cart.Items.Add(new ProductModel { ItemName = "Strawberries", Price = 7.51M });
cart.Items.Add(new ProductModel { ItemName = "Blueberries", Price = 8.84M });
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoLibrary
{
public class ProductModel
{
public string ItemName { get; set; }
public decimal Price { get; set; }
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoLibrary
{
public class ShoppingCartModel
{
public delegate void MentionDiscount(decimal subTotal);

public List<ProductModel> Items { get; set; } = new List<ProductModel>();

public decimal GenerateTotal(MentionDiscount mentionSubtotal,


Func<List<ProductModel>,decimal,decimal> calculateDiscountedTotal,
Action<string> tellUserWeAreDiscounting)
{
decimal subTotal = Items.Sum(x => x.Price);

mentionSubtotal(subTotal);

tellUserWeAreDiscounting("We are applying your discount.");

decimal total = calculateDiscountedTotal(Items, subTotal);

return total;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormUI
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Dashboard());
}
}
}

You might also like