-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.py
93 lines (74 loc) · 3.22 KB
/
app.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
import sqlite3
import pprint
from docopt import docopt
__doc__ = """
Donation System
Usage:
app.py donate --from <from_user> --to <to_user> --amount <amount>
app.py withdraw --user <user> --amount <amount>
Options:
-f, --from The user from which bank account is donated.
-t, --to The user to which bank account is donated.
-u, --user The user wanting to withdraw money from his/her account.
-a, --amount The amount handled by the command.
Existing accounts:
* MagazinRoyale
* FynnKliemann
* Peter
* Alexandros
"""
if __name__ == '__main__':
conn = sqlite3.connect('./banking.db')
conn.execute("CREATE TABLE IF NOT EXISTS ACCOUNT (NAME TEXT PRIMARY KEY NOT NULL, BALANCE REAL NOT NULL);")
conn.execute("INSERT OR IGNORE INTO ACCOUNT (NAME, BALANCE) VALUES ('MagazinRoyale', 1000.00 );")
conn.execute("INSERT OR IGNORE INTO ACCOUNT (NAME, BALANCE) VALUES ('FynnKliemann', 1000.00 )")
conn.execute("INSERT OR IGNORE INTO ACCOUNT (NAME, BALANCE) VALUES ('Peter', 1000000.00 )")
conn.execute("INSERT OR IGNORE INTO ACCOUNT (NAME, BALANCE) VALUES ('Alexandros', 1.00 )")
conn.commit()
args = docopt(__doc__)
pprint.pprint(args)
if args["donate"]:
# user1 = 'MagazinRoyale' # this should be argument #1
# user2 = 'FynnKliemann' # this should be argument #2
# amount = 100 # this should be argument #3
from_user = args["<from_user>"]
to_user = args["<to_user>"]
amount = float(args["<amount>"])
cursor = conn.execute("SELECT * from ACCOUNT;")
print("Before donation:")
for row in cursor:
print("NAME = ", row[0], ";\tBALANCE = ", row[1])
cursor = conn.execute("SELECT BALANCE FROM ACCOUNT WHERE NAME = ?;", (from_user,))
balance1 = 0
for row in cursor:
balance1 = row[0]
conn.execute("UPDATE ACCOUNT SET BALANCE = ? WHERE NAME = ?;", ((balance1 - amount), from_user,))
cursor = conn.execute("SELECT BALANCE FROM ACCOUNT WHERE NAME = ?;", (to_user,))
balance2 = 0
for row in cursor:
balance2 = row[0]
conn.execute("UPDATE ACCOUNT SET BALANCE = ? WHERE NAME = ?;", ((balance2 + amount), to_user,))
conn.commit()
cursor = conn.execute("SELECT * from ACCOUNT;")
print("\nAfter donation:")
for row in cursor:
print("NAME = ", row[0], ";\tBALANCE = ", row[1])
conn.close()
if args["withdraw"]:
user = args["<user>"]
amount = float(args["<amount>"])
cursor = conn.execute("SELECT * from ACCOUNT;")
print("Before withdrawal:")
for row in cursor:
print("NAME = ", row[0], ";\tBALANCE = ", row[1])
cursor = conn.execute("SELECT BALANCE FROM ACCOUNT WHERE NAME = ?;", (user,))
balance = 0
for row in cursor:
balance = row[0]
conn.execute("UPDATE ACCOUNT SET BALANCE = ? WHERE NAME = ?;", ((balance - amount), user,))
conn.commit()
cursor = conn.execute("SELECT * from ACCOUNT;")
print("\nAfter withdrawal:")
for row in cursor:
print("NAME = ", row[0], ";\tBALANCE = ", row[1])
conn.close()