forked from SoftWriters/CashRegister
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (86 loc) · 3.26 KB
/
Program.cs
File metadata and controls
98 lines (86 loc) · 3.26 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
namespace SWCashRegister
{
class Program
{
static void Main(string[] args)
{
string line;
if (!System.IO.File.Exists("input.txt"))
{
Console.WriteLine("Could not find file input.txt.");
return;
}
try
{
using (var input = new StreamReader("input.txt"))
using (var output = new StreamWriter("output.txt"))
{
while ((line = input.ReadLine()) != null)
{
if (!ValidateLine(line))
{
Console.WriteLine($"Found invalid value: {line}");
continue;
}
var values = line.Split(',');
int price = (int)(Convert.ToDecimal(values[0]) * 100);
int amountPaid = (int)(Convert.ToDecimal(values[1]) * 100);
if (amountPaid < price)
{
Console.WriteLine($"Insufficient funds: {line}");
continue;
}
int changeAmount = amountPaid - price;
IChangeCalculator calculator = ChangeCalculatorFactory.GetChangeCalculator(price);
output.WriteLine(PrintChange(calculator.GetChange(changeAmount)));
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
}
/// <summary>
/// Returns true if the line contains two decimal values, separated by a comma
/// </summary>
/// <param name="line"></param>
/// <returns>True if line is correctly formatted</returns>
public static bool ValidateLine(string line)
{
var regex = new Regex(@"^\d+\.\d{2}\,\d+\.\d{2}$");
return regex.IsMatch(line) ;
}
/// <summary>
/// Gets a text description of change quantities.
/// </summary>
/// <param name="change">A list mapping a currency to a quantity</param>
/// <returns>Description of change.</returns>
public static string PrintChange(IList<(Currency Currency, int Quantity)> change)
{
List<string> changeText = new List<string>();
var sortedChange = change
.GroupBy(x => x.Currency)
.Select(g => new {
Currency = g.Key,
Quantity = g.Sum(x => x.Quantity)
})
.OrderByDescending(x => x.Currency.Value);
foreach (var denomination in sortedChange)
{
changeText.Add($"{denomination.Quantity} {(denomination.Quantity == 1 ? denomination.Currency.Name : denomination.Currency.Plural)}");
}
return String.Join(",", changeText);
}
}
}