| 
 | 1 | +#!/usr/bin/env python  | 
 | 2 | +import sys  | 
 | 3 | + | 
 | 4 | +donors = {"John Doe": [152.33, 700], "Jane Doe": [23.19, 50, 15.99]}  | 
 | 5 | + | 
 | 6 | +def main_loop():  | 
 | 7 | +    """ Main menu to call different functions """  | 
 | 8 | +    menu_dict = {'s': thank_you,  | 
 | 9 | +                 'c': report,  | 
 | 10 | +                 'e': email_all,  | 
 | 11 | +                 'q': sys.exit}  | 
 | 12 | +    while True:  | 
 | 13 | +        print("\n========== Donation Management System Main Menu ==========")  | 
 | 14 | +        print("*                 (s) Send a Thank You                   *")  | 
 | 15 | +        print("*                 (c) Create a Report                    *")  | 
 | 16 | +        print("*                 (e) Send letters to everyone           *")  | 
 | 17 | +        print("*                 (q) Quit                               *")  | 
 | 18 | +        print("==========================================================")  | 
 | 19 | +        result = input("Please select a menu item: ")  | 
 | 20 | +        try:  | 
 | 21 | +            menu_dict[result]()  | 
 | 22 | +        except KeyError:  | 
 | 23 | +            print("\n*** Selected item not in the menu. Please try again. ***")  | 
 | 24 | + | 
 | 25 | +def email(n):  | 
 | 26 | +    """ Send donor an email with latest donation """  | 
 | 27 | +    return """  | 
 | 28 | +        Dear {},  | 
 | 29 | +
  | 
 | 30 | +        Thank you for your recent generous donation of ${}.  | 
 | 31 | +        Your support encourages our continued commitment to reaching our goal.  | 
 | 32 | +
  | 
 | 33 | +        Sincerely,  | 
 | 34 | +        The Donation Management  | 
 | 35 | +        """.format(n, donors.get(n)[-1])  | 
 | 36 | + | 
 | 37 | +def donor_list():  | 
 | 38 | +    """ List name of donors """  | 
 | 39 | +    return str(donors.keys())[11:-2]  | 
 | 40 | + | 
 | 41 | +def add_donor(name):  | 
 | 42 | +    """ Create new donor entry """  | 
 | 43 | +    donors[name] = []  | 
 | 44 | + | 
 | 45 | +def add_donation(name, amount):  | 
 | 46 | +    """ Create new donation """  | 
 | 47 | +    try:  | 
 | 48 | +        donors[name].append(float(amount))  | 
 | 49 | +        print("\nSending Thank You email...\n{}".format(email(name)))  | 
 | 50 | +    except ValueError:  | 
 | 51 | +        print("*** Wrong value format, please enter a valid number. ***")  | 
 | 52 | +        # Make sure to remove the donor if donation amount not entered correctly  | 
 | 53 | +        donors.pop(name)  | 
 | 54 | + | 
 | 55 | +def thank_you():  | 
 | 56 | +    """ Thank you function """  | 
 | 57 | +    while True:  | 
 | 58 | +        result = input("\nPlease enter full name, 'list' for current donor list, or 'q' return to the main menu: ")  | 
 | 59 | + | 
 | 60 | +        # Print donor list  | 
 | 61 | +        if result == "list":  | 
 | 62 | +            print(donor_list())  | 
 | 63 | +        # Back to the main menu  | 
 | 64 | +        elif result == "q":  | 
 | 65 | +            break  | 
 | 66 | +        # Create new donations  | 
 | 67 | +        else:  | 
 | 68 | +            # Check if donor is already in dict, if not, create a empty list for value  | 
 | 69 | +            if result not in donors:  | 
 | 70 | +                add_donor(result)  | 
 | 71 | +            amount = input("Please enter the amount of donation: ")  | 
 | 72 | +            add_donation(result, amount)  | 
 | 73 | + | 
 | 74 | +def gen_report(name):  | 
 | 75 | +    """ Generate report """  | 
 | 76 | +    total = 0.0  | 
 | 77 | +    for i in donors[name]:  | 
 | 78 | +        total += i  | 
 | 79 | +    # Number of donations  | 
 | 80 | +    don = len(donors[name])  | 
 | 81 | +    # Average donation  | 
 | 82 | +    avg = total/don  | 
 | 83 | +    # Print report  | 
 | 84 | +    return "* {: <30}{:16.2f}{: >16}{:18.2f} *".format(name, total, don, avg)  | 
 | 85 | + | 
 | 86 | +def report():  | 
 | 87 | +    """ Output report """  | 
 | 88 | +    print("\n* Donor Name                  |     Total gifted     | Donations |  Average gifted *")  | 
 | 89 | +    print("====================================================================================")  | 
 | 90 | +    for donor in donors:  | 
 | 91 | +        # Print report  | 
 | 92 | +        print(gen_report(donor))  | 
 | 93 | +    print("====================================================================================")  | 
 | 94 | + | 
 | 95 | +def email_all():  | 
 | 96 | +    """ Write a full set of letters to everyone to individual files on disk """  | 
 | 97 | +    for donor in donors:  | 
 | 98 | +        with open('{}.txt'.format(donor).replace(" ", "_"), 'w') as f_out:  | 
 | 99 | +            f_out.write(email(donor))  | 
 | 100 | +    print("\nLetters generated!")  | 
 | 101 | + | 
 | 102 | +if __name__ == '__main__':  | 
 | 103 | +    """ Main function """  | 
 | 104 | +    main_loop()  | 
0 commit comments