Skip to content

Commit 289133e

Browse files
committed
added an exercise for an object oriented mailroom.
1 parent 479f992 commit 289133e

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
.. _exercise_mailroom_oo:
2+
3+
********
4+
Mailroom
5+
********
6+
7+
Making Mailroom Object Oriented
8+
9+
A complete program
10+
==================
11+
12+
It was quite resonable to build the simple MailRoom program using a
13+
single module, a simple data structure, and functions that manipulate
14+
that data structure.
15+
16+
But if one were to expand the program with additional functionality, it
17+
would start to get a bit unwieldy and hard to maintain.
18+
19+
So it's a pretty good candidate for an object-oriented approach.
20+
21+
Goal:
22+
-----
23+
24+
Refactor the mailroom program usined a couple classes to help organise the code.
25+
26+
27+
28+
29+
The program
30+
-----------
31+
32+
Write a small command-line script called ``mailroom.py``. This script should be executable. The script should accomplish the following goals:
33+
34+
* It should have a data structure that holds a list of your donors and a
35+
history of the amounts they have donated. This structure should be populated
36+
at first with at least five donors, with between 1 and 3 donations each
37+
38+
* The script should prompt the user (you) to choose from a menu of 3 actions:
39+
'Send a Thank You' or 'Create a Report' or 'quit')
40+
41+
Sending a Thank You
42+
-------------------
43+
44+
* If the user (you) selects 'Send a Thank You', prompt for a Full Name.
45+
46+
* If the user types 'list', show them a list of the donor names and re-prompt
47+
* If the user types a name not in the list, add that name to the data structure and use it.
48+
* If the user types a name in the list, use it.
49+
* Once a name has been selected, prompt for a donation amount.
50+
* Turn the amount into a number -- it is OK at this point for the program to crash if someone types a bogus amount.
51+
* Once an amount has been given, add that amount to the donation history of
52+
the selected user.
53+
* Finally, use string formatting to compose an email thanking the donor for
54+
their generous donation. Print the email to the terminal and return to the
55+
original prompt.
56+
57+
**It is fine to forget new donors once the script quits running.**
58+
59+
Creating a Report
60+
------------------
61+
62+
* If the user (you) selected 'Create a Report' print a list of your donors,
63+
sorted by total historical donation amount.
64+
65+
- Include Donor Name, total donated, number of donations and average donation amount as values in each row. You do not need to print out all their donations, just the summary info.
66+
- Using string formatting, format the output rows as nicely as possible. The end result should be tabular (values in each column should align with those above and below)
67+
- After printing this report, return to the original prompt.
68+
69+
* At any point, the user should be able to quit their current task and return
70+
to the original prompt.
71+
72+
* From the original prompt, the user should be able to quit the script cleanly
73+
74+
75+
Your report should look somethign like this::
76+
77+
Donor Name | Total Given | Num Gifts | Average Gift
78+
------------------------------------------------------------------
79+
William Gates, III $ 653784.49 2 $ 326892.24
80+
Mark Zuckerberg $ 16396.10 3 $ 5465.37
81+
Jeff Bezos $ 877.33 1 $ 877.33
82+
Paul Allen $ 708.42 3 $ 236.14
83+
84+
Guidelines
85+
----------
86+
87+
First, factor your script into separate functions. Each of the above
88+
tasks can be accomplished by a series of steps. Write discreet functions
89+
that accomplish individual steps and call them.
90+
91+
Second, use loops to control the logical flow of your program. Interactive
92+
programs are a classic use-case for the ``while`` loop.
93+
94+
Of course, ``input()`` will be useful here.
95+
96+
Put the functions you write into the script at the top.
97+
98+
Put your main interaction into an ``if __name__ == '__main__'`` block.
99+
100+
Finally, use only functions and the basic Python data types you've learned
101+
about so far. There is no need to go any farther than that for this assignment.
102+
103+
Submission
104+
----------
105+
106+
As always, put the new file in your student directory in a ``session03``
107+
directory, and add it to your clone early. Make frequent commits with
108+
good, clear messages about what you are doing and why.
109+
110+
When you are done, push your changes and make a pull request.
111+
112+
.. _exercise_mailroom_plus:
113+
114+
Adding dicts...
115+
---------------
116+
117+
118+
For the next week (after Session04)
119+
120+
You should have been able to do all that with the basic data types:
121+
122+
numbers, strings, lists and tuples.
123+
124+
But once you've learned about dictionaries (Session04) you may be able to re-write it a bit more simply and efficiently.
125+
126+
* Update mailroom from last week to:
127+
128+
- Use dicts where appropriate
129+
- Write a full set of letters to everyone to individual files on disk
130+
- See if you can use a dict to switch between the users selections
131+
- Try to use a dict and the .format() method to do the letter as one
132+
big template -- rather than building up a big string in parts.
133+
134+
Example:
135+
136+
.. code-block:: ipython
137+
138+
In [3]: d
139+
Out[3]: {'first_name': 'Chris', 'last_name': 'Barker'}
140+
141+
142+
In [5]: "My name is {first_name} {last_name}".format(**d)
143+
Out[5]: 'My name is Chris Barker'
144+
145+
Don't worry too much about the "**" -- we'll get into the details later, but for now, it means, more or less -- pass this whole dict in as a bunch of keyword arguments.
146+
147+
148+
.. _exercise_mailroom_exeptions:
149+
150+
Adding Exceptions
151+
-----------------
152+
153+
**After Session05:**
154+
155+
* Exceptions:
156+
157+
Now that you've learned about Exception handling, you can update your code to handle errors better -- like when a user inputs bad data.
158+
159+
* Comprehensions:
160+
161+
Can you use comprehensions to clean up your code a bit?
162+
163+
* Tests
164+
165+
Add some tests..
166+
167+

0 commit comments

Comments
 (0)