-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathSaleLog.cs
43 lines (37 loc) · 928 Bytes
/
SaleLog.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
using System.Collections.Generic;
namespace Synchronization
{
public class SaleLog
{
private readonly object _sync = new object();
private decimal _total;
private readonly List<string> _saleDetails = new List<string>();
public decimal Total
{
get
{
lock (_sync)
{
return _total;
}
}
}
public void AddSale(string item, decimal price)
{
string details = $"{item} sold at {price}";
lock (_sync)
{
_total += price;
_saleDetails.Add(details);
}
}
public string[] GetDetails(out decimal total)
{
lock (_sync)
{
total = _total;
return _saleDetails.ToArray();
}
}
}
}