-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
60 lines (48 loc) · 1.62 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Linq;
using SwemExample.Basket;
using SwemExample.Items;
namespace SwemExample;
public class Program
{
// The abstract factory pattern is used to either create a limited or unlimited basket.
private static readonly BasketFactory BasketFactory = new UnlimitedBasketFactory();
private static void Main()
{
IBasket basket = BasketFactory.Create();
// Add items to the basket.
PopulateBasket(basket);
// Print the added items.
Console.WriteLine("After population");
PrintBasket(basket);
// Remove items until the price of all items in the basekt is below 10.
RemoveItemsUntil(basket, 10);
// Print the basket after the items were removed.
Console.WriteLine("After removal");
PrintBasket(basket);
}
private static void PopulateBasket(IBasket basket)
{
Item item1 = new SportItem(15, "Football", 1, SportItemKind.X);
basket.Add(item1);
Item item2 = new SportItem(10, "Baseball", 1, SportItemKind.Y);
basket.Add(item2);
Item item3 = new SportItem(20, "Bike", 1, SportItemKind.Z);
basket.Add(item3);
}
private static void RemoveItemsUntil(IBasket basket, int targetPrice)
{
while (basket.GetPrice() > targetPrice)
{
Item itemToRemove = basket.Items.OrderByDescending(item => item.Price).First();
basket.Remove(itemToRemove);
}
}
private static void PrintBasket(IBasket basket)
{
foreach (Item item in basket.Items)
{
Console.WriteLine(item.Name);
}
}
}