Skip to content

Commit 145d42e

Browse files
author
Kepka
committed
nowe klasy bank accounts i music albums
1 parent a59d601 commit 145d42e

File tree

6 files changed

+1630
-0
lines changed

6 files changed

+1630
-0
lines changed

OOP/accounts.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import datetime
2+
import pytz
3+
4+
5+
class Account:
6+
""" Simple account class with balance """
7+
8+
@staticmethod
9+
def _current_time():
10+
utc_time = datetime.datetime.utcnow()
11+
return pytz.utc.localize(utc_time)
12+
13+
def __init__(self, name, balance):
14+
self._name = name
15+
self._balance = balance
16+
self._transaction_list = [(Account._current_time(), balance)]
17+
print("Account created for " + self._name)
18+
19+
def deposit(self, amount):
20+
if amount > 0:
21+
self._balance += amount
22+
self.show_balance()
23+
self._transaction_list.append((Account._current_time(), amount))
24+
25+
def withdraw(self, amount):
26+
if 0 < amount <= self._balance:
27+
self._balance -= amount
28+
self._transaction_list.append((Account._current_time(), -amount))
29+
else:
30+
print("The amount must be greater that zero and no more then your amount balance")
31+
self.show_balance()
32+
33+
def show_balance(self):
34+
print(f"Balance is {self._balance}")
35+
36+
def show_transactions(self):
37+
for date, amount in self._transaction_list:
38+
if amount > 0:
39+
tran_type = "deposited"
40+
else:
41+
tran_type = "withdrawn"
42+
# amount *= -1
43+
print(f"{amount:6} {tran_type} on {date} (local time was {date.astimezone()}")
44+
45+
46+
if __name__ == '__main__':
47+
tim = Account("Tim", 0)
48+
tim.show_balance()
49+
50+
tim.deposit(1000)
51+
# tim.show_balance()
52+
tim.withdraw(500)
53+
# tim.show_balance()
54+
tim.withdraw(2000)
55+
56+
tim.show_transactions()
57+
58+
steph = Account("Steph", 800)
59+
steph.deposit(100)
60+
steph.withdraw(200)
61+
steph.show_transactions()

0 commit comments

Comments
 (0)