/
Automat.cs
166 lines (143 loc) · 5.3 KB
/
Automat.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace Fahrkartenautomat
{
class Automat
{
private readonly Label LblBalance;
private readonly Label LblRemainingCosts;
private readonly Label Lbl10ct;
private readonly Label Lbl50ct;
private readonly Label Lbl1eu;
private readonly Label Lbl2eu;
private readonly Label Lbl5eu;
private readonly Label Lbl10eu;
private readonly Label Lbl20eu;
private float _balance;
public float Balance
{
get { return _balance; }
set
{
_balance = (float)Math.Round(value, 2);
if (_balance < 0)
_balance = 0;
LblBalance.Content = _balance.ToString("0.00") + "€";
}
}
private float _remainingCosts;
private float RemainingCosts
{
get { return _remainingCosts; }
set
{
_remainingCosts = (float)Math.Round(value, 2);
LblRemainingCosts.Content = value.ToString("0.00") + "€";
}
}
private readonly Dictionary<float, MoneyItem> _money;
private string ticketOutput;
public Automat(Label LblBalance, Label LblRemainingCosts, Label Lbl10ct, Label Lbl50ct, Label Lbl1eu, Label Lbl2eu, Label Lbl5eu, Label Lbl10eu, Label Lbl20eu)
{
this.LblBalance = LblBalance;
this.LblRemainingCosts = LblRemainingCosts;
this.Lbl10ct = Lbl10ct;
this.Lbl50ct = Lbl50ct;
this.Lbl1eu = Lbl1eu;
this.Lbl2eu = Lbl2eu;
this.Lbl5eu = Lbl5eu;
this.Lbl10eu = Lbl10eu;
this.Lbl20eu = Lbl20eu;
_money = new Dictionary<float, MoneyItem>()
{
{ 0.1f, new MoneyItem(0.1f, this.Lbl10ct) },
{ 0.5f, new MoneyItem(0.5f, this.Lbl50ct) },
{ 1.0f, new MoneyItem(1.0f, this.Lbl1eu) },
{ 2.0f, new MoneyItem(2.0f, this.Lbl2eu) },
{ 5.0f, new MoneyItem(5.0f, this.Lbl5eu) },
{ 10.0f, new MoneyItem(10.0f, this.Lbl10eu) },
{ 20.0f, new MoneyItem(20.0f, this.Lbl20eu) }
};
}
public void BuyTicketAB()
{
float cost = 2.90f;
RemainingCosts += cost;
this.ticketOutput += $"Ticket AB \t{cost.ToString("0.00")}€\n";
}
public void BuyTicketBC()
{
float cost = 3.30f;
RemainingCosts += cost;
this.ticketOutput += $"Ticket BC \t{cost.ToString("0.00")}€\n";
}
public void BuyTicketABC()
{
float cost = 3.60f;
RemainingCosts += cost;
this.ticketOutput += $"Ticket ABC\t{cost.ToString("0.00")}€\n";
}
public void AddCash(float value)
{
_money[value].Amount += 1;
CalculateBalance();
}
private void CalculateBalance()
{
float new_balance = 0;
foreach (KeyValuePair<float, MoneyItem> entry in _money)
{
new_balance += entry.Value.Amount * entry.Key;
}
Balance = new_balance;
}
public void InsertCash(float value)
{
if (RemainingCosts <= 0)
{
MessageBox.Show("No remaining costs, buy a ticket below.");
return;
}
if (Balance <= 0)
{
MessageBox.Show("Out of cash.");
return;
}
if (_money[value].Amount > 0)
{
RemainingCosts -= value;
_money[value].Amount -= 1;
CalculateBalance();
// Check if Tickets are paid off and calculate exchange
if (RemainingCosts <= 0)
{
float returnMoney = (float)Math.Round(Math.Abs(RemainingCosts), 2);
var exchange = returnMoney;
foreach (KeyValuePair<float, MoneyItem> entry in _money.Reverse())
{
returnMoney = (float)Math.Round(returnMoney, 2);
float rest = returnMoney / entry.Key;
if (rest >= 1)
{
returnMoney -= (float)Math.Floor(rest) * entry.Key;
_money[entry.Key].Amount += (int)Math.Floor(rest);
}
}
RemainingCosts = 0;
CalculateBalance();
// Output
string output = "";
output += "Ticket(s) purchased!\n";
output += "--------------------\n";
output += $"{this.ticketOutput}\n";
output += $"Exchange: \t{exchange.ToString("0.00")}€";
MessageBox.Show(output);
this.ticketOutput = "";
}
}
}
}
}