-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepository.py
More file actions
155 lines (134 loc) · 6.63 KB
/
Copy pathRepository.py
File metadata and controls
155 lines (134 loc) · 6.63 KB
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
import sqlite3
import atexit
from DAO import _Vaccines, _Suppliers, _Clinics, _Logistics
from DTO import *
class _Repository:
def __init__(self):
self._conn = sqlite3.connect("database.db")
self._vaccines = _Vaccines(self._conn)
self._suppliers = _Suppliers(self._conn)
self._clinics = _Clinics(self._conn)
self._logistics = _Logistics(self._conn)
def close(self):
self._conn.commit()
self._conn.close()
def create_tables(self):
cursor = self._conn.cursor()
cursor.execute("""CREATE TABLE vaccines(id INTEGER PRIMARY KEY,
date DATE NOT NULL,
supplier INTEGER REFERENCES supplier(id),
quantity INTEGER NOT NULL)
""")
cursor.execute("""CREATE TABLE suppliers(id INTEGER PRIMARY KEY,
name STRING NOT NULL,
logistic INTEGER REFERENCES logistic(id))
""")
cursor.execute("""CREATE TABLE clinics(id INTEGER PRIMARY KEY,
location STRING NOT NULL,
demand INTEGER NOT NULL,
logistic INTEGER REFERENCES logistic(id))
""")
cursor.execute("""CREATE TABLE logistics(id INTEGER PRIMARY KEY,
name STRING NOT NULL,
count_sent INTEGER NOT NULL,
count_received INTEGER NOT NULL)
""")
def fill_tables(self, init_lines, first_line):
data_arr = first_line.split(',')
data_arr = [arg.strip() for arg in data_arr]
vac_num = int(data_arr[0]) # 3
sup_num = int(data_arr[1]) # 2
cli_num = int(data_arr[2]) # 1
log_num = int(data_arr[3]) # 2
sumNum = log_num + cli_num + sup_num + vac_num # 8
currNum = sumNum
i = sumNum
for line in init_lines[sumNum - log_num:]:
args = line.split(',') #
logistic = Logistic(*args)
self._logistics.insert(logistic)
currNum = currNum-log_num
for line in init_lines[currNum - cli_num :currNum]:
args = line.split(',') #
clinic = Clinic(*args)
self._clinics.insert(clinic)
currNum = currNum-cli_num
for line in init_lines[currNum - sup_num :currNum]:
args = line.split(',') #
supplier = Supplier(*args)
self._suppliers.insert(supplier)
currNum = currNum-sup_num
for line in init_lines[currNum - vac_num :currNum]:
args = line.split(',')
vaccine = Vaccine(*args)
self._vaccines.insert(vaccine)
""" for line in reversed(init_lines):
args = line.split(',') #
if i <= vac_num: # in this case we in the line of Vaccines
vaccine = Vaccine(*args)
self._vaccines.insert(vaccine)
elif vac_num < i <= (sup_num + vac_num): # in this case we in the line of supplier
supplier = Supplier(*args)
self._suppliers.insert(supplier)
elif (sup_num + vac_num) < i <= (sumNum - log_num): # in this case we in the line of clinics
clinic = Clinic(*args)
self._clinics.insert(clinic)
else: # in this case we in the line of logNum
logistic = Logistic(*args)
self._logistics.insert(logistic)
i = i - 1
"""
def order_report(self): # ),SUM(demand),SUM(count_received)
cursor = self._conn.cursor()
cursor.execute("""SELECT SUM(quantity) FROM vaccines""")
quantity_tuple = cursor.fetchall()[0]
quantity_str = "".join(map(str, quantity_tuple))
cursor.execute("""SELECT SUM(demand) FROM clinics""")
demand_tuple = cursor.fetchall()[0]
demand_str = "".join(map(str, demand_tuple))
cursor.execute("""SELECT SUM(count_received) FROM logistics""")
count_received_tuple = cursor.fetchall()[0]
count_received_str = "".join(map(str, count_received_tuple))
cursor.execute("""SELECT SUM(count_sent) FROM logistics""")
count_sent_tuple = cursor.fetchall()[0]
count_sent_str = "".join(map(str, count_sent_tuple))
str_report = quantity_str + "," + demand_str + "," + count_received_str + "," + count_sent_str
return str_report
def received_shipment(self, args):
name = args[0]
amount = args[1]
date = args[2]
cursor = self._conn.cursor()
cursor.execute("""SELECT id FROM suppliers
WHERE name = ? """, [name])
sup_id = cursor.fetchone()[0]
cursor.execute(""" SELECT COUNT(id) FROM vaccines""")
vac_id = cursor.fetchone()[0]
vaccine = Vaccine(vac_id, date, sup_id, amount)
self._vaccines.insert(vaccine) # inserting to the Vaccines
cursor.execute("""SELECT logistic FROM suppliers
WHERE name = ? """, [name])
log_id = cursor.fetchone()[0]
self._logistics.update_count_received(log_id, amount) # update count_received
def send_shipment(self, args):
amount = args[1]
cursor = self._conn.cursor()
cursor.execute("""SELECT id FROM clinics WHERE location = ?""", [args[0]])
clinic_id = cursor.fetchone()[0]
self._clinics.update_demand(clinic_id, amount) # Updates the demand after the clinic got the vaccines
cursor.execute("""SELECT logistic FROM clinics WHERE id = ?""", [clinic_id])
logistic_id = cursor.fetchone()[0]
self._logistics.update_count_sent(logistic_id, amount) # Updates the sent count of the logistic id
cursor.execute("""SELECT * FROM vaccines""")
int_amount = int(amount)
while int_amount > 0:
curr_old_vaccines_stock = cursor.fetchone() # The tupple of the first line on the vaccines table.
curr_quantity = int(curr_old_vaccines_stock[3])
if int_amount >= curr_quantity:
int_amount = int_amount - curr_quantity
self._vaccines.delete(curr_old_vaccines_stock[0])
elif int_amount < curr_quantity:
self._vaccines.update_quantity(curr_old_vaccines_stock[0], int_amount)
int_amount = 0
repo = _Repository()
atexit.register(repo.close)