Repository navigation
Expand file tree
/
Copy pathOptimumChange.cs
More file actions
211 lines (186 loc) · 6.17 KB
/
Copy pathOptimumChange.cs
File metadata and controls
211 lines (186 loc) · 6.17 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections.Generic;
namespace MoneyChangeDue
{
public static class OptimumChange
{
static List<decimal> moneyDenominations;
static string countryCode;
static void Main(string[] args)
{
// Define country and currency for global usage
Console.Write("Set country code for currency (MX, US): ");
countryCode = Console.ReadLine();
moneyDenominations = MoneyDenomination.SetMoneyDenomination(countryCode);
// Validate country code
if (moneyDenominations.Count == 0)
{
Console.WriteLine("\nInvalid country code. Please make sure to enter one of the country codes between parenthesis. Press any key to exit.");
Console.ReadLine();
Environment.Exit(0);
}
BeginTransaction();
}
static void BeginTransaction()
{
decimal productPrice = 0;
decimal paidAmount = 0;
try
{
// Product price and paid amount
Console.Write("\nProduct price: ");
productPrice = decimal.Parse(Console.ReadLine());
Console.Write("Paid amount: ");
paidAmount = decimal.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine("\nOnly numeric values allowed. Please try again.");
BeginTransaction();
}
SortedList<decimal, decimal> changeCalc = CalculateChange(productPrice, paidAmount);
// Display optimum change details from sorted list
Console.WriteLine("\nChange details per denomination:");
Console.WriteLine("Denomination - Total");
foreach (decimal denomination in changeCalc.Keys)
{
Console.WriteLine("$" + denomination + " - " + changeCalc[denomination]);
}
NewTransaction();
}
/*
* CalculateChange method can be called by sending product price and paid amount as arguments.
* This method will return a sorted list with the optimum change details per denomination.
* This overloaded method is used by the main program.
*/
static SortedList<decimal, decimal> CalculateChange(decimal productPrice, decimal paidAmount)
{
SortedList<decimal, decimal> changeCalc = new SortedList<decimal, decimal>();
decimal changeDue = 0;
// Validate that amounts have only two decimals
bool productPriceIsCorrect = (productPrice * 100) == Math.Truncate(productPrice * 100);
bool paidAmountIsCorrect = (paidAmount * 100) == Math.Truncate(paidAmount * 100);
if (productPriceIsCorrect && paidAmountIsCorrect)
{
// Calculate and validate change due
changeDue = paidAmount - productPrice;
if (changeDue > 0)
{
Console.WriteLine("\nTotal change due: " + changeDue);
}
else if (changeDue == 0)
{
Console.WriteLine("\nNo change due.");
BeginTransaction();
}
else
{
Console.WriteLine("\nPaid amount is less than the product price. Please make sure to enter the correct values.");
BeginTransaction();
}
}
else
{
Console.WriteLine("\nEntered amounts have more than two decimals. Please make sure to enter the correct values.");
BeginTransaction();
}
try
{
// Save count per denomination
foreach (decimal denomination in moneyDenominations)
{
if (changeDue / denomination >= 1)
{
changeCalc.Add(denomination, Math.Truncate(changeDue / denomination));
// Update change as the remainder with respect to money denomination
changeDue %= denomination;
}
}
}
catch (DivideByZeroException)
{
Console.WriteLine("\nA money denomination cannot be equal to 0. Press any key to exit.");
Console.ReadLine();
Environment.Exit(0);
}
return changeCalc;
}
/*
* CalculateChange method can be called by sending product price, paid amount and money denominations as arguments.
* This method will return a sorted list with the optimum change details per denomination.
* This overloaded method can be used by declaring its static class.
*/
public static SortedList<decimal, decimal> CalculateChange(decimal productPrice, decimal paidAmount, List<decimal> moneyDenominations)
{
SortedList<decimal, decimal> changeCalc = new SortedList<decimal, decimal>();
decimal changeDue = 0;
try
{
// Validate that amounts have only two decimals
bool productPriceIsCorrect = (productPrice * 100) == Math.Truncate(productPrice * 100);
bool paidAmountIsCorrect = (paidAmount * 100) == Math.Truncate(paidAmount * 100);
if (productPriceIsCorrect && paidAmountIsCorrect)
{
// Calculate and validate change due
changeDue = paidAmount - productPrice;
if (changeDue <= 0)
{
return changeCalc;
}
}
else
{
return changeCalc;
}
}
catch
{
return changeCalc;
}
try
{
// Save count per denomination
foreach (decimal denomination in moneyDenominations)
{
if (changeDue / denomination >= 1)
{
changeCalc.Add(denomination, Math.Truncate(changeDue / denomination));
// Update change as the remainder with respect to money denomination
changeDue %= denomination;
}
}
}
catch (DivideByZeroException)
{
return changeCalc;
}
return changeCalc;
}
static void NewTransaction() {
try
{
// New transaction after calc
Console.Write("\nNew transaction? (Y, N) ");
char response = char.Parse(Console.ReadLine());
if (response == 'Y')
{
BeginTransaction();
}
else if (response == 'N')
{
Environment.Exit(0);
}
else
{
Console.WriteLine("\nInvalid selection, please try again.");
NewTransaction();
}
}
catch
{
Console.WriteLine("\nPlease enter a response.");
NewTransaction();
}
}
}
}