forked from learn-co-curriculum/python-p3-freebie-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
208 lines (184 loc) · 6.65 KB
/
Copy pathcli.py
File metadata and controls
208 lines (184 loc) · 6.65 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
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
#!/usr/bin/env python3
from helpers import (
get_session,
list_companies,
list_devs,
list_freebies,
find_company_by_id,
find_company_by_name,
find_dev_by_id,
find_dev_by_name,
find_freebie_by_id,
create_company,
create_dev,
give_freebie,
transfer_freebie,
delete_freebie,
company_summary,
dev_summary,
freebie_summary,
)
MENU = """
Choose an option:
1) List companies
2) List devs
3) List freebies
4) Create company
5) Create dev
6) Give freebie (company -> dev)
7) Transfer freebie (dev -> dev)
8) Check if dev received a specific item
9) Oldest company
10) Delete a freebie
0) Exit
"""
def input_int(prompt, allow_blank=False):
s = input(prompt).strip()
if s == "" and allow_blank:
return None
try:
return int(s)
except ValueError:
print("Please enter a valid integer.")
return input_int(prompt, allow_blank=allow_blank)
def main():
session = get_session()
try:
while True:
print(MENU)
choice = input("Enter choice: ").strip()
if choice == "1":
comps = list_companies(session)
if not comps:
print("No companies found.")
for c in comps:
print(company_summary(c))
elif choice == "2":
devs = list_devs(session)
if not devs:
print("No devs found.")
for d in devs:
print(dev_summary(d))
elif choice == "3":
frees = list_freebies(session)
if not frees:
print("No freebies found.")
for f in frees:
print(freebie_summary(f))
elif choice == "4":
name = input("Company name: ").strip()
if name == "":
print("Name required.")
continue
fy = input("Founding year (blank if unknown): ").strip()
fy_val = int(fy) if fy != "" else None
c = create_company(session, name, fy_val)
print("Created:", company_summary(c))
elif choice == "5":
name = input("Dev name: ").strip()
if name == "":
print("Name required.")
continue
d = create_dev(session, name)
print("Created:", dev_summary(d))
elif choice == "6":
comps = list_companies(session)
for c in comps:
print(company_summary(c))
comp_id = input_int("Company ID: ")
company = find_company_by_id(session, comp_id)
if not company:
print("Company not found.")
continue
devs = list_devs(session)
for d in devs:
print(dev_summary(d))
dev_id = input_int("Dev ID: ")
dev = find_dev_by_id(session, dev_id)
if not dev:
print("Dev not found.")
continue
item = input("Item name: ").strip()
if item == "":
print("Item name required.")
continue
value = input_int("Value (integer): ")
fb = give_freebie(session, company, dev, item, value)
print("Created freebie:", freebie_summary(fb))
elif choice == "7":
devs = list_devs(session)
for d in devs:
print(dev_summary(d))
from_id = input_int("Giver Dev ID: ")
from_dev = find_dev_by_id(session, from_id)
if not from_dev:
print("Dev not found.")
continue
owned = from_dev.freebies
if not owned:
print(f"{from_dev.name} has no freebies to give.")
continue
for f in owned:
print(freebie_summary(f))
freebie_id = input_int("Freebie ID to transfer: ")
freebie = find_freebie_by_id(session, freebie_id)
if not freebie or freebie not in owned:
print("That freebie is not owned by the giver.")
continue
for d in devs:
print(dev_summary(d))
to_id = input_int("Recipient Dev ID: ")
to_dev = find_dev_by_id(session, to_id)
if not to_dev:
print("Recipient not found.")
continue
ok = transfer_freebie(session, from_dev, to_dev, freebie)
if ok:
print("Transfer successful.")
else:
print("Transfer failed.")
elif choice == "8":
devs = list_devs(session)
for d in devs:
print(dev_summary(d))
dev_id = input_int("Dev ID: ")
dev = find_dev_by_id(session, dev_id)
if not dev:
print("Dev not found.")
continue
item = input("Item name to check: ").strip()
if dev.received_one(item):
print(f"Yes — {dev.name} has received '{item}'.")
else:
print(f"No — {dev.name} has not received '{item}'.")
elif choice == "9":
from models import Company
oldest = Company.oldest_company(session)
if oldest:
print("Oldest company:", company_summary(oldest))
else:
print("No companies in DB.")
elif choice == "10":
frees = list_freebies(session)
for f in frees:
print(freebie_summary(f))
fid = input_int("Freebie ID to delete: ")
fb = find_freebie_by_id(session, fid)
if not fb:
print("No such freebie.")
continue
confirm = input(f"Delete {freebie_summary(fb)}? (y/N): ").strip().lower()
if confirm == "y":
delete_freebie(session, fb)
print("Deleted.")
else:
print("Canceled.")
elif choice == "0":
print("Goodbye!")
break
else:
print("Unknown option — choose a number from the menu.")
finally:
session.close()
if __name__ == "__main__":
main()