-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerson.cs
64 lines (60 loc) · 2.01 KB
/
Person.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
namespace cookieBakerySomething
{
class Person
{
public String Name { get; private set; }
public Bakery FavoriteBakery { get; private set; }
/// <summary>
/// Creates a new person able to buy cookies
/// </summary>
/// <param name="name">Name of the person</param>
/// <param name="bakery">reference to the favorite bakery of the person</param>
public Person(String name, Bakery bakery)
{
FavoriteBakery = bakery;
Name = name;
//Creates a new thread of the lookforcookies method
Thread thread = new Thread(LookForCookies);
thread.Start();
}
/// <summary>
/// Looks for a cookie available for purchase at the favorite bakery
/// </summary>
public void LookForCookies()
{
//Sets a time for the cookiegrabbing loop
Stopwatch timer = new Stopwatch();
timer.Start();
while (true)
{
//if less than a second has passed since we last tried to grab a cookie has passed jump to next iteration
if (timer.ElapsedMilliseconds < 1000)
continue;
timer.Restart();
//Locks this thread making only it available to access the cookie count of our bakery
lock (FavoriteBakery.Lock)
{
if (FavoriteBakery.getCookiesCount() > 0)
{
FavoriteBakery.SellCookieTo(this);
}
}
}
}
/// <summary>
/// Returns a string of this object
/// </summary>
/// <returns>Name of the person object</returns>
public override String ToString()
{
return Name;
}
}
}