-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
75 lines (64 loc) · 2.5 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Threading.Tasks;
using CookieJar.Factory;
namespace CookieJar
{
/// <summary>
/// Here's where all the action happens. Cats, CookieMonster and good ol' Grandma.
/// </summary>
public static class MainClass
{
/// <summary>
/// Let's go, bake some cookies!
/// </summary>
/// <param name="args">Who needs arguments.</param>
public static void Main(string[] args)
{
// someone baked some cookies already. Very kind.
Kitchen.GrabCookieJar().StoreCookies(Kitchen.BakeCookiesInOven(Enum.CookieType.DoubleChocolate, 7));
var curiousCat = Task.Run(async () =>
{
int count = 0;
while (count++ < 10)
{
// the cat sleeps for two seconds (max), before it tries to steal a cookie.
await Task.Delay(new Random().Next(2000));
var jar = Kitchen.GrabCookieJar();
var cookie = jar.TakeCookies(1);
if (cookie.Count == 1)
{
Console.WriteLine("Cat: I am happy! :) om nom");
}
else
{
Console.WriteLine("Cat: I am so sad, no cookie!");
}
}
});
var theCookieMonster = Task.Run(async () =>
{
// we do not know exactly what the monster does, and when it will do it.
await Task.Delay(new Random().Next(7000));
// but when it awakes, it is hungry.
var cookies = Kitchen.GrabCookieJar().TakeCookies(new Random().Next(1, 8));
if (cookies.Count == 0)
{
Console.WriteLine("Monster: RAWWRRRRR!");
}
else
{
Console.WriteLine("Monster: Omnomnom.");
}
});
var goodOlGrandMa = Task.Run(async () =>
{
// grand ma is slow
await Task.Delay(8000);
var cookies = Kitchen.BakeCookiesInOven(Enum.CookieType.TripleChocolate, 10);
Kitchen.GrabCookieJar().StoreCookies(cookies);
Console.WriteLine("Grandma: I was so kind and refilled the cookies.");
});
Task.WhenAll(new[] { curiousCat, theCookieMonster, goodOlGrandMa }).GetAwaiter().GetResult();
}
}
}