-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoneyConv.cs
398 lines (345 loc) · 12.1 KB
/
MoneyConv.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Final_Project
{
public partial class MoneyConv : Form
{
DateTime begin, end;
public static string dir = @".\files\";
public static string path = dir + "MoneyConv.txt";
public static string[] header;
public MoneyConv()
{
InitializeComponent();
}
private void MoneyConv_Load(object sender, EventArgs e)
{
rdBtFromCAD.Select();
rdBtToCAD.Select();
begin = DateTime.Now;
if (!Directory.Exists(path))
{ Directory.CreateDirectory(dir); }
}
private void bt_Conv_Click(object sender, EventArgs e)
{
Double read_amount;
Int16 read_currency;
ConvertM money;
Regex rx = new Regex(@"^([0-9]){1,}((\.)([0-9]){1,2})?$");
string read_Box;
read_Box = txBox_CurrIN.Text;
if(!rx.IsMatch(read_Box))
{
MessageBox.Show("Input only numbers!" +
"\nInput must have a maximum of 2 decimals." +
"\nThe decimal separator must be a '.' .",
"Wrong format!!!",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
txBox_CurrIN.Text = "0";
txBox_CurrIN.Focus();
}
else
{
if (rdBtFromCAD.Checked)
{ read_currency = 1; }
else if (rdBtFromUSD.Checked)
{ read_currency = 2; }
else if (rdBtFromEUR.Checked)
{ read_currency = 3; }
else if (rdBtFromGBP.Checked)
{ read_currency = 4; }
else
{ read_currency = 5; }
read_amount = Convert.ToDouble(txBox_CurrIN.Text);
money = new ConvertM(read_amount, read_currency);
if (rdBtToCAD.Checked)
{ money.To_CAD(); }
else if (rdBtToUSD.Checked)
{ money.To_USD(); }
else if (rdBtToEUR.Checked)
{ money.To_EUR(); }
else if (rdBtToGBP.Checked)
{ money.To_GBP(); }
else
{ money.To_BRL(); }
txBox_CurrOUT.Text = money.M_out.Amount.ToString();
SaveToFile(money);
}
}
private void bt_Exit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you want\nto quit the application\nMoney Exchange?", "Exit ?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
end = DateTime.Now;
TimeSpan timeInForm = end - begin;
MessageBox.Show("You have been using this application for: " +
timeInForm.ToString("mm") + " minutes and " +
timeInForm.ToString("ss") + " seconds.");
this.Close();
}
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void rdBtFromCAD_CheckedChanged(object sender, EventArgs e)
{
txBox_CurrOUT.Text = "";
}
private void rdBtFromUSD_CheckedChanged(object sender, EventArgs e)
{
txBox_CurrOUT.Text = "";
}
private void rdBtFromEUR_CheckedChanged(object sender, EventArgs e)
{
txBox_CurrOUT.Text = "";
}
private void rdBtFromGBP_CheckedChanged(object sender, EventArgs e)
{
txBox_CurrOUT.Text = "";
}
private void rdBtFromBRL_CheckedChanged(object sender, EventArgs e)
{
txBox_CurrOUT.Text = "";
}
private void rdBtToCAD_CheckedChanged(object sender, EventArgs e)
{
txBox_CurrOUT.Text = "";
}
private void rdBtToUSD_CheckedChanged(object sender, EventArgs e)
{
txBox_CurrOUT.Text = "";
}
private void rdBtToEUR_CheckedChanged(object sender, EventArgs e)
{
txBox_CurrOUT.Text = "";
}
private void rdBtToGBP_CheckedChanged(object sender, EventArgs e)
{
txBox_CurrOUT.Text = "";
}
private void rdBtToBRL_CheckedChanged(object sender, EventArgs e)
{
txBox_CurrOUT.Text = "";
}
private void bt_ReadFile_Click(object sender, EventArgs e)
{
header = new string[2];
header[0] = "Conversion (From/To)";
header[1] = "Date and Time";
DisplayFile obj = new DisplayFile(path,header);
obj.ShowDialog();
}
private void txBox_CurrIN_TextChanged(object sender, EventArgs e)
{
txBox_CurrOUT.Text = "";
}
private void SaveToFile(ConvertM money)
{
// opening/creating file to write
DateTime date_now = DateTime.Now;
FileStream file = new FileStream(path, FileMode.Append, FileAccess.Write);
StreamWriter new_line = new StreamWriter(file);
new_line.Write(money.M_in.Amount + " " + money.M_in.CurrencyString() + " = " + money.M_out.Amount + " " + money.M_out.CurrencyString() + "; ");
new_line.WriteLine(date_now.ToString("yyyy/MM/dd HH:mm:ss"));
new_line.Close();
file.Close();
}
}
internal class Money
{
private Double amount;
private Int16 currency;
public Double Amount
{
set { amount = value; }
get { return amount; }
}
public Int16 Currency
{
set { currency = value; }
get { return currency; }
}
public Money()
{
Amount = 0;
Currency = 0;
}
public Money(Double amount, Int16 currency)
{
Amount = amount;
Currency = currency;
}
public string CurrencyString()
{
string curr;
switch (Currency)
{
case 1:
curr="CAD";
break;
case 2:
curr = "USD";
break;
case 3:
curr = "EUR";
break;
case 4:
curr = "GBP";
break;
case 5:
curr = "BRL";
break;
default:
curr = "NO CURRENCY";
break;
}
return curr;
}
}
internal class ConvertM
{
private Money m_in, m_out;
public Money M_in
{
set { m_in = value; }
get { return m_in; }
}
public Money M_out
{
set { m_out = value; }
get { return m_out; }
}
public ConvertM()
{
M_in = new Money(0,0);
M_out = new Money(0,0);
}
public ConvertM(Double amount, Int16 currency)
{
M_in = new Money(amount, currency);
M_out = new Money(0, 0);
}
public void To_CAD ()
{
M_out.Currency = 1; //currency 1 = CAD
switch (M_in.Currency)
{
case 1: //Money In is in CAD
M_out = M_in;
break;
case 2: //Money In is in USD
M_out.Amount = M_in.Amount * 1.3799; //100 USD = 137.99 CAD 15/03/2023
break;
case 3: //Money In is in EUR
M_out.Amount = M_in.Amount * 1.4553; //100 EUR = 145.53 CAD 15/03/2023
break;
case 4: //Money In is in GBP
M_out.Amount = M_in.Amount * 1.6585; //100 GBP = 165.85 CAD 15/03/2023
break;
case 5: //Money In is in BRL
M_out.Amount = M_in.Amount * 0.2591; //100 BRL = 25.91 CAD 15/03/2023
break;
}
}
public void To_USD()
{
M_out.Currency = 2; //currency 2 = USD
switch (M_in.Currency)
{
case 1: //Money In is in CAD
M_out.Amount = M_in.Amount * 0.7244; //100 CAD = 72.44 USD 15/03/2023
break;
case 2: //Money In is in USD
M_out = M_in;
break;
case 3: //Money In is in EUR
M_out.Amount = M_in.Amount * 1.0542; //100 EUR = 105.42 USD 15/03/2023
break;
case 4: //Money In is in GBP
M_out.Amount = M_in.Amount * 1.2018; //100 GBP = 120.18 USD 15/03/2023
break;
case 5: //Money In is in BRL
M_out.Amount = M_in.Amount * 0.1879; //100 BRL = 18.79 USD 15/03/2023
break;
}
}
public void To_EUR()
{
M_out.Currency = 3; //currency 3 = EUR
switch (M_in.Currency)
{
case 1: //Money In is in CAD
M_out.Amount = M_in.Amount * 0.6871; //100 CAD = 68.71 EUR 15/03/2023
break;
case 2: //Money In is in USD
M_out.Amount = M_in.Amount * 0.9485; //100 USD = 94.85 EUR 15/03/2023
break;
case 3: //Money In is in EUR
M_out = M_in;
break;
case 4: //Money In is in GBP
M_out.Amount = M_in.Amount * 1.14; //100 GBP = 114.00 EUR 15/03/2023
break;
case 5: //Money In is in BRL
M_out.Amount = M_in.Amount * 0.1782; //100 BRL = 17.82 EUR 15/03/2023
break;
}
}
public void To_GBP()
{
M_out.Currency = 4; //currency 4 = GBP
switch (M_in.Currency)
{
case 1: //Money In is in CAD
M_out.Amount = M_in.Amount * 0.6024; //100 CAD = 60.24 GBP 15/03/2023
break;
case 2: //Money In is in USD
M_out.Amount = M_in.Amount * 0.8317; //100 USD = 83.17 GBP 15/03/2023
break;
case 3: //Money In is in EUR
M_out.Amount = M_in.Amount * 0.8764; //100 EUR = 87.64 GBP 15/03/2023
break;
case 4: //Money In is in GBP
M_out = M_in;
break;
case 5: //Money In is in BRL
M_out.Amount = M_in.Amount * 0.1563; //100 BRL = 15.63 GBP 15/03/2023
break;
}
}
public void To_BRL()
{
M_out.Currency = 5; //currency 5 = BRL
switch (M_in.Currency)
{
case 1: //Money In is in CAD
M_out.Amount = M_in.Amount * 3.8541; //100 CAD = 385.41 BRL 15/03/2023
break;
case 2: //Money In is in USD
M_out.Amount = M_in.Amount * 5.3192; //100 USD = 531.92 BRL 15/03/2023
break;
case 3: //Money In is in EUR
M_out.Amount = M_in.Amount * 5.6064; //100 EUR = 560.64 BRL 15/03/2023
break;
case 4: //Money In is in GBP
M_out.Amount = M_in.Amount * 6.3956; //100 GBP = 639.56 BRL 15/03/2023
break;
case 5: //Money In is in BRL
M_out = M_in;
break;
}
}
}
}