Create a class to manage donations of a charity

Class to manage donations of a charity

Python Programming Challenge

A Python programming challenge to get familiar with classes and methods (OOP). Create a class to manage donations of a charity. The program  begins with an empty dictionary. Add functions inside the class to run specific actions at the user’s request. Ask users (“input()” method) what task they would like to complete. The Python program should keep asking the user for tasks until the user decides to quit. The tasks included in the program will be the following:

  1. Add donator: Creates a new entry in a dictionary called “yourdonation”. The user will input the donator’s name.
  2. Add donations: Adds additional donations to an existing donator.
  3. Calculate max: Shows the greatest donation received
  4. Quit: Ends the program.

Expected output

python program to practice class

Steps

      1. Define a class called "Donation".
      2. Inside the class, define an empty dictionary called "yourdonation" to store the donators and their donations.
      3. Define a method called "add_donator" that takes the donator’s name as input and creates a new entry in the "yourdonation" with an empty list as the value.
      4. Define a method called "add_donation" that takes the donator’s name and the amount of the donation as input. This method should check if the donator exists in the "yourdonation", and if so, append the donation to the corresponding list of donations.
      5. Define a method called "calculate_max" that iterates over the "yourdonation" dictionary and prints the greatest donation for each donator.
      6. Define a method called "quit_program" that ends the program.
      7. Define a method called "run" that continuously prompts the user for tasks until they choose to quit. Inside this method, use the "input()" function to ask the user for the task they would like to complete and call the corresponding methods accordingly.
      8. Create an instance of the "Donation" class and call the "run()" method to start the program.

Source code


class Donation:
    def __init__(self):
        self.donationbook = {}

    def add_donator(self):
        name = input("Enter the donator's name: ")
        donations = input("Enter the donations (separated by commas): ").split(",")
        self.donationbook[name] = list(map(int, donations))
        print("Donator added successfully!")

    def add_donations(self):
        name = input("Enter the donator's name: ")
        if name in self.donationbook:
            donations = input("Enter additional donations (separated by commas): ").split(",")
            self.donationbook[name].extend(list(map(int, donations)))
            print("Donations added successfully!")
        else:
            print("Donator not found!")

    def view_donationbook(self):
        if self.donationbook:
            for donator, donations in self.donationbook.items():
                print(f"{donator}: {donations}")
        else:
            print("Donationbook is empty!")

    def calculate_max(self):
        if self.donationbook:
            for donator, donations in self.donationbook.items():
                max_donation = max(donations)
                print(f"{donator}: {max_donation}")
        else:
            print("Donationbook is empty!")

    def run(self):
        while True:
            print("\n--- Donationbook Menu ---")
            print("1. Add donator")
            print("2. Add donations")
            print("3. View donationbook")
            print("4. Calculate max")
            print("5. Quit")

            choice = input("Enter your choice: ")

            if choice == "1":
                self.add_donator()
            elif choice == "2":
                self.add_donations()
            elif choice == "3":
                self.view_donationbook()
            elif choice == "4":
                self.calculate_max()
            elif choice == "5":
                print("Program ended.")
                break
            else:
                print("Invalid choice. Please try again.")


# Example usage:
donation = Donation()
donation.run()

Related Challenges

Try more Python challenges focused on OOP to boost your coding skills.

Tags:

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for Practice
Register New Account
Shopping cart