-
Notifications
You must be signed in to change notification settings - Fork 0
/
bw.erl
283 lines (251 loc) · 11.8 KB
/
bw.erl
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
-module(bw).
-export([init/0]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NOTIZEN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% TransaktionsID noch nicht in der TranListe!!!! ? evtl als Übergabeparameter
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Verwendbare Workerfunktionen
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% konto_anlegen(PID)
% kontostand_abfragen(PID, Kontonr)
% konto_sperren(PID, Kontonr)
% konto_entsperren(PID, Kontonr)
% geld_einzahlen(PID, Kontonr, Betrag)
% konto_loeschen(PID, Kontonr)
% geld_auszahlen(PID, Kontonr, Betrag)
% geld_ueberweisen(PID, ZielKontonr, Kontonr, Betrag)
% dispokredit_beantragen(PID, Kontonr)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Konteneinträge
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% {KONTONUMMER,
% {sperrvermerk, false/true},
% {vermoegen, VERMÖGEN},
% {maxDispo, DISPOHÖHE},
% {dispoZins, DISPOZINS},
% {transaktionsliste, TRANSAKTIONSLISTE}
% }
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Transaktionsliste
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ATOME FÜR DIE TYPIFIZIERUNG DER EINTRÄGE
%
% erstellung
% erfragung
% sperrung
% entsperrung
% einzahlung
% loeschung
% auszahlung
% ueberweisung
%
% AUFBAU EINER TRASAKTION
%
% {TRANSAKTIONSID, TYP, {zeit, DATUM, UHRZEIT}, {notizen, NOTIZEN}, {wer, KONTONUMMER}, {wert, BETRAG}}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
init() ->
receive
[konto_anlegen, ClientPId] -> konto_anlegen(ClientPId);
[konto_loeschen, ClientPId, Kontonr] -> konto_loeschen(ClientPId, Kontonr);
[kontostand_abfragen, ClientPId, Kontonr] -> kontostand_abfragen(ClientPId, Kontonr);
[historie, ClientPId, Kontonr] -> historie(ClientPId, Kontonr);
[geld_einzahlen, ClientPId, Kontonr, Ursprung, Betrag] -> geld_einzahlen(ClientPId, Kontonr, Ursprung, Betrag);
[geld_auszahlen, ClientPId, Kontonr, Betrag] -> geld_auszahlen(ClientPId, Kontonr, Betrag);
[geld_ueberweisen, ClientPId, ZielKontonr, KontoNr, Betrag] -> geld_ueberweisen(ClientPId, ZielKontonr, KontoNr, Betrag);
[geld_ueberweisen_einzahlen, ClientPId, ZielKontonr, KontoNr, Betrag] -> geld_ueberweisen_einzahlen(ClientPId, ZielKontonr, KontoNr, Betrag);
[dispokredit_beantragen, ClientPId, KontoNr] -> dispokredit_beantragen(ClientPId, KontoNr);
[konto_sperren, ClientPId, KontoNr] -> konto_sperren(ClientPId, KontoNr);
[konto_entsperren, ClientPId, KontoNr] -> konto_entsperren(ClientPId, KontoNr)
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Interne Funktionen die uns das Leben leichter machen
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
error_handling({nok, Reason}) ->
io:format("Fehler: ~p ~n", [Reason]),
dets:close(konten),
exit(normal);
error_handling({ok, konten}) ->
ok.
daten_lesen(PID, Kontonr) ->
{Response, Reason} = dets:open_file(konten, [{file, "db_konten"}, {type, set}]),
error_handling({Response, Reason}),
Konto = dets:lookup(konten, Kontonr),
case Konto of
[] -> PID ! {nok, "Konto nicht gefunden"},
error_handling({nok, "Konto nicht gefunden"});
_ -> ok
end,
dets:close(konten),
Konto
.
daten_schreiben(Konto) ->
dets:open_file(konten, [{file, "db_konten"}, {type, set}]),
dets:insert(konten, Konto),
dets:close(konten).
kontoinfo(Feld, Konto) ->
[{_ ,{sperrvermerk, Sperrvermerk},{vermoegen, Kontostand},{maxDispo, MaxDispo} ,{dispoZins, DispoZins},{transaktionsliste, Transaktionsliste}}] = Konto,
case Feld of
sperrvermerk -> Sperrvermerk;
vermoegen -> Kontostand;
maxDispo -> MaxDispo;
dispoZins -> DispoZins;
transaktionsliste -> Transaktionsliste
end.
kontochange(Feld, Wert, Konto) ->
[{Kontonr ,{sperrvermerk, Sperrvermerk},{vermoegen, Kontostand},{maxDispo, MaxDispo} ,{dispoZins, DispoZins},{transaktionsliste, Transaktionsliste}}] = Konto,
case Feld of
sperrvermerk -> AktualisiertesKonto = [{Kontonr ,{sperrvermerk, Wert},{vermoegen, Kontostand},{maxDispo, MaxDispo} ,{dispoZins, DispoZins},{transaktionsliste, Transaktionsliste}}];
vermoegen -> AktualisiertesKonto = [{Kontonr ,{sperrvermerk, Sperrvermerk},{vermoegen, Wert},{maxDispo, MaxDispo} ,{dispoZins, DispoZins},{transaktionsliste, Transaktionsliste}}];
maxDispo -> AktualisiertesKonto = [{Kontonr ,{sperrvermerk, Sperrvermerk},{vermoegen, Kontostand},{maxDispo, Wert} ,{dispoZins, DispoZins},{transaktionsliste, Transaktionsliste}}];
dispoZins -> AktualisiertesKonto = [{Kontonr ,{sperrvermerk, Sperrvermerk},{vermoegen, Kontostand},{maxDispo, MaxDispo} ,{dispoZins, Wert},{transaktionsliste, Transaktionsliste}}];
transaktionsliste -> AktualisiertesKonto = [{Kontonr ,{sperrvermerk, Sperrvermerk},{vermoegen, Kontostand},{maxDispo, MaxDispo} ,{dispoZins, DispoZins},{transaktionsliste, [Wert|Transaktionsliste]}}]
end,
daten_schreiben(AktualisiertesKonto),
AktualisiertesKonto.
kontolog(Operation, {Notizen, Wer, Betrag}, Konto) ->
kontochange(transaktionsliste, {self(), Operation, {zeit, date(), time()}, {notizen, Notizen}, {wer, Wer}, {wert, Betrag}}, Konto).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Funktionen für die Anwendung des Workers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
konto_anlegen(PID) ->
dets:open_file(konten, [{file, "db_konten"}, {type, set}]),
% Speichern der letzen Kontonummer, wenn die Tabelle nicht existiert wird sie hier erstellt
Kontonr_temp = dets:first(konten),
case Kontonr_temp of %%Konteninit
'$end_of_table' -> dets:insert(konten, {inkKontoNr, 0});
_ -> ignoreit
end,
[{inkKontoNr, HoechsteKontonr}] = dets:lookup(konten, inkKontoNr),
Kontonr = HoechsteKontonr + 1,
dets:insert(konten, {Kontonr,
{sperrvermerk, false},
{vermoegen, 0},
{maxDispo, 0},
{dispoZins, 0},
{transaktionsliste, []}}),
dets:insert(konten, {inkKontoNr, Kontonr}),
dets:close(konten),
Konto = daten_lesen(PID, Kontonr),
kontolog(konto_angelegt, {"Konto wurde erstellt", Kontonr, 0}, Konto),
PID ! {ok, Kontonr}.
kontostand_abfragen(PID, Kontonr) ->
Konto = daten_lesen(PID, Kontonr),
timer:sleep(1000),
Kontostand = kontoinfo(vermoegen, Konto),
kontolog(erfragung, {"Kontostand wurde abgefragt", Kontonr, 0}, Konto),
PID ! {ok, Kontostand}.
historie(PID, Kontonr) ->
Konto = daten_lesen(PID, Kontonr),
[{_ ,_Sperrvermerk, Kontostand, _MaxDispo ,_DispoZins ,Transaktionsliste}] = Konto,
timer:sleep(1000),
PID ! {ok, [Transaktionsliste, Kontostand]}.
konto_sperren(PID, Kontonr) ->
Konto = daten_lesen(PID, Kontonr),
timer:sleep(1000),
kontochange(sperrvermerk, true, Konto),
Konto2 = daten_lesen(PID, Kontonr),
kontolog(sperrung, {"Konto gesperrt", Kontonr, 0}, Konto2),
PID ! {ok, 'Konto wurde gesperrt'}.
konto_entsperren(PID, Kontonr) ->
Konto = daten_lesen(PID, Kontonr),
kontochange(sperrvermerk, false, Konto),
timer:sleep(1000),
Konto2 = daten_lesen(PID, Kontonr),
kontolog(entsperrung, {"Konto entsperrt", Kontonr, 0}, Konto2),
PID ! {ok, 'Konto wurde entsperrt'}.
geld_einzahlen(PID, Kontonr, Verwendungszweck, Betrag) ->
geld_einzahlen(PID, Kontonr, Kontonr, Verwendungszweck, Betrag).
%Nur für interne Verwendung für die Überweisungen
geld_einzahlen(PID, Kontonr, Ursprung, Verwendungszweck, Betrag) ->
Konto = daten_lesen(PID, Kontonr),
%timer:sleep(1000),
case kontoinfo(sperrvermerk, Konto) of
true -> PID ! {nok, "Konto gesperrt"},
kontolog(einzahlung, {"Konto gesperrt", Kontonr, 0}, Konto);
false ->Kontostand = kontoinfo(vermoegen, Konto),
NeuerKontostand = Kontostand + Betrag,
AktKonto_1 = kontochange(vermoegen, NeuerKontostand, Konto),
%TransaktionsID noch nicht in der TranListe!!!! ? evtl als Übergabeparameter
kontolog(einzahlung, {Verwendungszweck, Ursprung, Betrag}, AktKonto_1),
Kontotemp = daten_lesen(PID, Kontonr),
Vermoegen = kontoinfo(vermoegen, Kontotemp),
PID ! {ok, Vermoegen}
end
.
konto_loeschen(PID, Kontonr) ->
case kontoinfo(sperrvermerk, daten_lesen(PID, Kontonr)) of
true -> PID ! {nok, "Konto gesperrt"},
kontolog(loeschung, {"Konto gesperrt", Kontonr, 0}, daten_lesen(PID, Kontonr));
false ->dets:open_file(konten, [{file, "db_konten"}, {type, set}]),
dets:delete(konten, Kontonr),
dets:close(konten),
PID ! {ok, Kontonr}
end
.
geld_auszahlen(PID, Kontonr, Betrag) ->
Konto = daten_lesen(PID, Kontonr),
case kontoinfo(sperrvermerk, Konto) of
true -> PID ! {nok, "Konto gesperrt"},
kontolog(auszahlung, {"Konto gesperrt", Kontonr, 0}, Konto);
false ->Kontostand = kontoinfo(vermoegen, Konto),
DispoBetrag = kontoinfo(maxDispo, Konto),
NeuerKontostand = Kontostand - Betrag,
case NeuerKontostand < 0 - DispoBetrag of
true -> PID ! {nok, "Nicht genug Geld"},
kontolog(auszahlung, {"Auszahlung nicht möglich", Kontonr, 0}, daten_lesen(PID, Kontonr));
false -> AktKonto_1 = kontochange(vermoegen, NeuerKontostand, Konto),
%TransaktionsID noch nicht in der TranListe!!!! ? evtl als Übergabeparameter
kontolog(auszahlung, {"", Kontonr, Betrag}, AktKonto_1),
Kontotemp = daten_lesen(PID, Kontonr),
Vermoegen = kontoinfo(vermoegen, Kontotemp),
PID ! {ok, Vermoegen}
end
end
.
geld_ueberweisen(PID, ZielKontonr, Kontonr, Betrag) ->
case kontoinfo(sperrvermerk, daten_lesen(PID, ZielKontonr)) or kontoinfo(sperrvermerk, daten_lesen(PID, Kontonr)) of
true -> PID ! {nok, "Eins der Konten ist gesperrt"},
kontolog(ueberweisung, {"Eins der Konten ist gesperrt", Kontonr, 0}, daten_lesen(PID, Kontonr));
false ->geld_auszahlen(self(), Kontonr, Betrag),
receive
{nok, Message} -> PID ! {nok, Message};
{ok, _} -> timer:sleep(1000),
geld_ueberweisen_einzahlen(PID, ZielKontonr, Kontonr, Betrag)
end
end.
geld_ueberweisen_einzahlen(PID, ZielKontonr, Kontonr, Betrag) ->
geld_einzahlen(self(), ZielKontonr, Kontonr, Betrag),
receive
{nok, Message2} -> PID ! {nok, Message2};
{ok, _} -> PID ! {ok, 'Geld wurde ueberwiesen'}
end.
dispokredit_beantragen(PID, Kontonr) ->
Konto = daten_lesen(PID, Kontonr),
case kontoinfo(sperrvermerk, Konto) of
true -> PID ! {nok, "Konto gesperrt"},
kontolog(dispobeantragung, {"Konto gesperrt", Kontonr, 0}, daten_lesen(PID, Kontonr));
false ->
Vermoegen = kontoinfo(vermoegen, daten_lesen(PID, Kontonr)),
Dispo = Vermoegen * 0.1,
kontochange(maxDispo, Dispo, daten_lesen(PID, Kontonr)),
kontochange(dispoZins, 12, daten_lesen(PID, Kontonr)),
kontolog(dispobeantragung, {"Dispokredit mit 12 Prozent", Kontonr, Dispo}, daten_lesen(PID, Kontonr)),
PID ! {ok, Dispo}
end
.