-
Notifications
You must be signed in to change notification settings - Fork 0
/
JZ_Repozytorium.py
297 lines (251 loc) · 10.8 KB
/
JZ_Repozytorium.py
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
from numpy.lib.function_base import kaiser
__author__ = 'laptop1'
# -*- coding: utf-8 -*-
import sqlite3
import numpy
import JZ_TworzBaze
from datetime import datetime
#
#
db_path = 'kredyty_hip.db'
##
class RepositoryException(Exception):
def __init__(self, message, *errors):
Exception.__init__(self, message)
self.errors = errors
#
class Klient():
def __init__(self, klientId, imie=None, nazwisko=None, ulica=None, nrDomu=None, nrMieszkania=None, kodPocztowy=None, miasto=None, dataUr=None, kredyty=[]):
self.klientId = klientId
self.imie = imie
self.nazwisko = nazwisko
self.ulica = ulica
self.nrDomu = nrDomu
self.nrMieszkania = nrMieszkania
self.kodPocztowy = kodPocztowy
self.miasto = miasto
self.dataUr = dataUr
self.kredyty = kredyty
def __repr__(self):
return "<Klient(kilentId='%s', imie='%s', nazwisko='%s', ulica=%s, nrDomu=%s, nrMieszkania=%s, kodPocztowy=%s, miasto='%s, data_ur='%s', kredyty='%s')>" % (
self.klientId, str(self.imie), str(self.nazwisko), str(self.ulica), str(self.nrDomu), str(self.nrMieszkania),
str(self.kodPocztowy), str(self.miasto), str(self.dataUr), str(self.kredyty)
)
class Kredyt():
def __init__(self, kredytId, kwota, oproc, klientId=None):
self.kredytId = kredytId
self.kwota = kwota
self.oproc = oproc
self.klientId = klientId
def __repr__(self):
return "<Kredyty(kredytId='%s', kwota='%s', oproc='%s', idkl='%s')>" % (
self.kredytId, str(self.kwota), str(self.oproc), str(self.klientId)
)
class Repository():
def __init__(self):
try:
self.conn = self.get_connection()
except Exception as e:
raise RepositoryException('GET CONNECTION:', *e.args)
self._complete = False
def __enter__(self):
return self
def __exit__(self, type_, value, traceback):
self.close()
def complete(self):
self._complete = True
def get_connection(self):
return sqlite3.connect(db_path)
def close(self):
if self.conn:
try:
if self._complete:
self.conn.commit()
else:
self.conn.rollback()
except Exception as e:
raise RepositoryException(*e.args)
finally:
try:
self.conn.close()
except Exception as e:
raise RepositoryException(*e.args)
class KlienciRepository(Repository):
def add(self, klient):
try:
c = self.conn.cursor()
c.execute('''INSERT INTO Klienci (klientId, imie, nazwisko, ulica, nrDomu, nrMieszkania, kodPocztowy, miasto, dataUr)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)''',
(
klient.klientId, str(klient.imie), str(klient.nazwisko), str(klient.ulica),
klient.nrDomu, klient.nrMieszkania, klient.kodPocztowy, klient.miasto, klient.dataUr
)
)
if klient.kredyty:
for kredyt in klient.kredyty:
try:
c.execute('INSERT INTO Kredyty ( kwota, oproc, klientId) VALUES(?,?,?)',
( kredyt.kwota, kredyt.oproc, klient.klientId)
)
except Exception as e:
print(e.message)
raise RepositoryException('blad dodawania kredytu: %s, Dla klienta: %s' %
(str(kredyt), str(klient.klientId))
)
except Exception as e:
raise RepositoryException('blad dodawania klienta %s: %s' % (str(klient), e.message))
def delete(self, klient):
try:
c = self.conn.cursor()
c.execute('DELETE FROM Kredyty WHERE klientId=?', (klient.klientId,))
c.execute('DELETE FROM Klienci WHERE klientId=?', (klient.klientId,))
except Exception as e:
raise RepositoryException('blad usuwania klienta %s' % str(klient))
def getKlientById(self, klientId):
try:
c = self.conn.cursor()
c.execute("SELECT * FROM Klienci WHERE klientId=?", (klientId,))
kl_row = c.fetchone()
klient = Klient(klientId=klientId)
if kl_row == None:
klient=None
else:
klient.imie = kl_row[1]
klient.nazwisko = kl_row[2]
klient.ulica = kl_row[3]
klient.nrDomu = kl_row[4]
klient.nrMieszkania = kl_row[5]
klient.kodPocztowy = kl_row[6]
klient.miasto = kl_row[7]
klient.dataUr = kl_row[8]
c.execute("SELECT * FROM Kredyty WHERE klientId=? order by kredytId", (klientId,))
kl_items_rows = c.fetchall()
items_list = []
for item_row in kl_items_rows:
item = Kredyt(kredytId=item_row[0], kwota=item_row[1], oproc=item_row[2], klientId=item_row[3])
items_list.append(item)
klient.kredyty=items_list
except Exception as e:
raise RepositoryException('blad pobierania klienta po ID: %s %s' % (str(klientId), e.message))
return klient
def update(self, klient):
try:
istniejacy = self.getKlientById(klient.klientId)
if istniejacy != None:
self.delete(istniejacy)
self.add(klient)
except Exception as e:
raise RepositoryException('Blad aktualizowania klienta %s' % str(klient))
def sredniaKwotaKredytuKlienta(self, klientId):
klient = self.getKlientById(klientId)
if klient==None or klient.kredyty==None or len(klient.kredyty)==0:
return None
kwoty = []
for kredyt in klient.kredyty:
kwoty.append(kredyt.kwota)
return numpy.mean(kwoty)
class KredytyRepository(Repository):
def sredniaKwotaWszystkichKredytow(self):
try:
c = self.conn.cursor()
c.execute("SELECT kwota FROM Kredyty")
kwoty_rows = c.fetchall()
kwoty_list = []
for row in kwoty_rows:
kwoty_list.append(row[0]),
if len(kwoty_list)==0:
return None
else:
return numpy.mean(kwoty_list)
except Exception as e:
raise RepositoryException('blad pobierania kwot kredytow po ID: %s' % e.message)
if __name__ == '__main__':
try:
JZ_TworzBaze.baza()
except Exception as e:
print("Blad tworzenia bazy %s " % e.message)
try:
with KlienciRepository() as klient_repository:
print("###### DODAWANIE KLIENTOW - START ######")
klient_repository.add(
Klient(klientId = 1, imie = "Anna", nazwisko = "Nowak", ulica = "Kolejowa", nrDomu="12a", nrMieszkania="11",
kodPocztowy="80-180", miasto = "Gdansk", dataUr = '21-04-1974',
kredyty = [
Kredyt(kredytId=None, kwota=250000, oproc=5.1 ),
Kredyt(kredytId=None, kwota=15400, oproc=14.1 ),
Kredyt(kredytId=None, kwota=2700, oproc=18.3 )
]
)
)
klient_repository.add(
Klient(klientId = 2, imie = "Tomasz", nazwisko = "Kowalski", ulica = "Piotrkowska", nrDomu="1", nrMieszkania="8b",
kodPocztowy="82-300", miasto = "Elblag", dataUr = '11-11-1982',
kredyty = [
Kredyt(kredytId=None, kwota=310000, oproc=4.14 )
]
)
)
klient_repository.add(
Klient(klientId = 3, imie = "Agata", nazwisko = "Paciaciak", ulica = "Przemyska", nrDomu="21", nrMieszkania=None,
kodPocztowy="87-100", miasto = "Torun", dataUr = '21-01-1983',
kredyty = [
Kredyt(kredytId=None, kwota=3600, oproc=24.8 ),
Kredyt(kredytId=None, kwota=2400, oproc=19.14 ),
Kredyt(kredytId=None, kwota=1800, oproc=17.83 ),
Kredyt(kredytId=None, kwota=1650, oproc=21.03 )
]
)
)
klient_repository.complete()
print("###### DODAWANIE KLIENTOW - ZAKONCZONE POMYSLNIE ######")
except RepositoryException as e:
print(e)
try:
print("");
print("###### POBIERANIE KLIENTA - START ######")
print KlienciRepository().getKlientById(1)
print("###### POBIERANIE KLIENTA - ZAKONCZONE POMYSLNIE ######")
except Exception as e:
print(e)
try:
print("");
print("###### AKTUALIZOWANIE KLIENTA - START ######")
klientId = 1
klient = KlienciRepository().getKlientById(klientId)
klient.nrDomu = "33"
klient.nrMieszkania = "8c"
klient.kredyty.append(Kredyt(kredytId=None, kwota=3721, oproc=16.9 ))
with KlienciRepository() as klienci_repository:
klienci_repository.update(klient)
klienci_repository.complete()
print("Klient po aktualizacji: %s " % KlienciRepository().getKlientById(klientId))
print("###### AKTUALIZOWANIE KLIENTA - ZAKONCZONE POMYSLNIE ######")
except Exception as e:
print(e)
try:
print("");
print("###### USUWANIE KLIENTA - START ######")
klientId = 2
klient = KlienciRepository().getKlientById(klientId)
with KlienciRepository() as klienci_repository:
klienci_repository.delete(klient)
klienci_repository.complete()
print("###### USUWANIE KLIENTA - ZAKONCZONE POMYSLNIE ######")
except Exception as e:
print(e)
try:
print("");
print("###### SREDNIA KWOTA KREDYTU KLIENTA - START ######")
klientId = 3
srednia = KlienciRepository().sredniaKwotaKredytuKlienta(klientId)
print("klientId: %d srednia kw kred: %.2f" % (klientId, srednia) )
print("###### SREDNIA KWOTA KREDYTU KLIENTA - ZAKONCZONE POMYSLNIE ######")
except Exception as e:
print(e)
try:
print("");
print("###### SREDNIA KWOTA WSZYSTKICH KREDYTOW - START ######")
print("Srednia kwota kredytow: %.2f" % KredytyRepository().sredniaKwotaWszystkichKredytow())
print("###### SREDNIA KWOTA WSZYSTKICH KREDYTOW - ZAKONCZONE POMYSLNIE ######")
except Exception as e:
print(e)