Skip to content

Commit 3188beb

Browse files
committed
copy of mailroom from session03
1 parent 48fd3e0 commit 3188beb

1 file changed

Lines changed: 258 additions & 0 deletions

File tree

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
#!/usr/bin/env python
2+
3+
""" Program to manage donations. """
4+
5+
# Define the static donor list
6+
donors = [
7+
{ "last_name": "Smith", "first_name": "Joe",
8+
"donations": [
9+
{ "amount": 10000.00 }, { "amount": 2100.00 }, { "amount": 2330.20 } ] },
10+
{ "last_name": "Gates, III", "first_name": "William",
11+
"donations": [
12+
{ "amount": 326892.00 }, { "amount": 326892.49 } ] },
13+
{ "last_name": "Zuckerberg", "first_name": "Mark",
14+
"donations": [
15+
{ "amount": 5000.00 }, { "amount": 5000.00 }, { "amount": 6396.10 } ] },
16+
{ "last_name": "Bezos", "first_name": "Jeff",
17+
"donations": [
18+
{ "amount": 877.33 } ] },
19+
{ "last_name": "Allen", "first_name": "Paul",
20+
"donations": [
21+
{ "amount": 10.00 }, { "amount": 600.00 }, { "amount": 98.42} ] },
22+
{ "last_name": "Adams", "first_name": "Georgia",
23+
"donations": [
24+
{ "amount": 200 }, { "amount": 500 }, { "amount": 200000 }, { "amount": 700 } ] }
25+
]
26+
27+
28+
def print_lines(lines=2):
29+
""" Print variable number of linefeeds for clarity. """
30+
31+
for i in range(lines):
32+
print("")
33+
34+
35+
def print_report():
36+
""" Print formatted list of all donors. """
37+
38+
print_lines()
39+
40+
# print report header
41+
# index name total gvn #gifts avg gift
42+
print("{0:5} | {1:20} | {2:14} | {3:9} | {4:12}".format(
43+
"ID","Donor Name","Total Given","Num Gifts","Average Gift"))
44+
print("-"*72)
45+
46+
# enumerate so we have the list index handy
47+
for donor_index, donor in enumerate(donors):
48+
49+
num_donations=len(donor["donations"])
50+
51+
total_donations = 0
52+
for donation in donor["donations"]:
53+
total_donations += donation["amount"]
54+
55+
average_donation = total_donations / num_donations
56+
57+
# index name total gvn #gifts avg gift
58+
print("{0:05d} {1:20} ${2:14,.2f} {3:9.0f} ${4:12,.2f}".format(
59+
donor_index,donor["first_name"]+" "+donor["last_name"],
60+
total_donations,num_donations,average_donation))
61+
62+
63+
def parse_name(full_name):
64+
""" Convert string name into constituent parts. """
65+
66+
# capture suffix, if present. we assume it will follow a comma
67+
try:
68+
suffix=full_name.split(",")[1].strip()
69+
except:
70+
suffix=""
71+
72+
# name with suffix removed
73+
informal_name=full_name.split(",")[0].strip()
74+
# we assume the last name is one word minus the suffix
75+
last_name=informal_name.split()[-1].strip()
76+
# we assume the first name(s) is everything to the left of the last name
77+
first_name=" ".join(informal_name.split()[0:-1])
78+
79+
return { "full_name": full_name.title(), "informal_name": informal_name.title(),
80+
"suffix": suffix.upper(), "last_name": last_name.title(), "first_name": first_name.title() }
81+
82+
83+
def get_donation_amount(informal_name):
84+
""" Get a donation. """
85+
86+
menu = "\n"
87+
menu += "GIFT ENTRY (ammount)\n"
88+
menu += "-----------------------\n"
89+
menu += "\n"
90+
menu += "Enter the numeric amount {} has donated or\n"
91+
menu += "(q)uit to return to cancel the donation and\n"
92+
menu += "return to the previous menu.\n"
93+
menu += "\n"
94+
95+
amount=0
96+
while amount==0:
97+
98+
print_lines()
99+
100+
print(menu.format(informal_name))
101+
selection=input("Donation amount or (q)uit: ")
102+
103+
# let them bail if they want
104+
if selection.lower().strip() in ["q", "quit"]:
105+
return None
106+
107+
# protect against non numeric input
108+
try:
109+
amount=float(selection)
110+
return amount
111+
except:
112+
print_lines()
113+
print("The value must be numeric. Please try again or (q)uit.")
114+
115+
116+
def print_thank_you(donor_id,hint):
117+
""" Print a thank you letter. """
118+
119+
letter = "\n"
120+
letter += "Dearest {} {},\n"
121+
letter += "\n"
122+
letter += "We are are grateful for the generious donation on the behalf of\n"
123+
letter += "the {} family.\n"
124+
letter += "\n"
125+
letter += "It is through the donations of {} patrions like yourself that\n"
126+
letter += "allows us to continue to support the community.\n"
127+
letter += "\n"
128+
letter += "Sincerely,\n"
129+
letter += "\n"
130+
letter += "Tux Humboldt\n"
131+
letter += "Shark Loss Prevention Institue\n"
132+
133+
print_lines()
134+
print("-"*80)
135+
136+
print(letter.format(
137+
donors[donor_id]["first_name"],donors[donor_id]["last_name"],
138+
donors[donor_id]["last_name"],
139+
hint))
140+
141+
print("-"*80)
142+
print_lines()
143+
144+
145+
def thank_you_entry():
146+
""" Enter new donation and send thank you. """
147+
148+
menu = "\n"
149+
menu += "DONATION ENTRY (Donor Name)\n"
150+
menu += "---------------------------\n"
151+
menu += "\n"
152+
menu += "Enter the full name (first, last) of the donor\n"
153+
menu += "for whom you would like to enter a donation,\n"
154+
menu += "(l)ist to see a list of the existing donors, or\n"
155+
menu += "(q)uit to return to the previous menu.\n"
156+
menu += "\n"
157+
158+
while True:
159+
160+
print_lines()
161+
162+
print(menu)
163+
selection=input("Donor Name, (l)ist or (q)uit: ")
164+
165+
# check for a quit directive
166+
if selection.lower().strip() in ["q", "quit"]:
167+
return
168+
169+
# check for a list directive
170+
if selection.lower().strip() in ["l", "list"]:
171+
print_report()
172+
continue
173+
174+
# parse the string into name components
175+
current_donor=parse_name(selection)
176+
177+
# determine if the provided name matches an existing record
178+
donor_id=None
179+
for donor_index, existing_donor in enumerate(donors):
180+
# assemble full name from first_name, last_name
181+
existing_full_name = existing_donor["first_name"]+" "+existing_donor["last_name"]
182+
# if we get a match, store the index of the donor record
183+
if existing_full_name.lower().strip() == current_donor["full_name"].lower().strip():
184+
donor_id=donor_index
185+
186+
# prompt for new donation, cancel if None returned
187+
new_donation=get_donation_amount(current_donor["informal_name"])
188+
if new_donation is None:
189+
print_lines()
190+
print("Donation cancelled!")
191+
return
192+
193+
# donor_id will be None if it didn't match an existing entry
194+
195+
# add donation to an existing donor
196+
if donor_id is not None:
197+
donors[donor_id]["donations"].append({ "amount": new_donation})
198+
hint="returning"
199+
#print("existing donor:",donors[donor_id])
200+
201+
# add a new donor
202+
else:
203+
donors.append( { "last_name": ", ".join(filter(None,[current_donor["last_name"], current_donor["suffix"]])),
204+
"first_name": current_donor["first_name"],
205+
"donations": [ { "amount": new_donation} ] } )
206+
# set the donor_id to the new donor
207+
donor_id=len(donors)-1
208+
hint="new"
209+
#print(donors[donor_id])
210+
211+
# thank the donor for the new donation
212+
print_thank_you(donor_id,hint)
213+
214+
215+
def main():
216+
""" Main menu / input loop. """
217+
218+
219+
menu = "\n"
220+
menu += "DONATION WIZARD MAIN MENU\n"
221+
menu += "-------------------------\n"
222+
menu += "\n"
223+
menu += "Select from the following:\n"
224+
menu += "\n"
225+
menu += "(P)rint Donor Report\n"
226+
menu += "(E)nter Donation\n"
227+
menu += "(Q)uit\n"
228+
menu += "\n"
229+
230+
selection=None
231+
while selection not in ["0", "quit", "q"]:
232+
233+
print_lines()
234+
235+
print(menu)
236+
selection=str(input("(p)rint, (e)nter, (q)uit: ")).lower().strip()
237+
238+
if selection in ["1", "p", "print"]:
239+
print_report()
240+
241+
# accept either send or enter
242+
if selection in ["2", "s", "send", "e", "enter"]:
243+
thank_you_entry()
244+
245+
if selection in ["d", "debug"]:
246+
print_lines()
247+
print("Donors:", donors)
248+
249+
print_lines()
250+
print("Thank you for using Donation Wizard!")
251+
print_lines()
252+
253+
254+
# call the main input loop
255+
if __name__ == "__main__":
256+
main()
257+
258+

0 commit comments

Comments
 (0)