Login
Order Now
Support
Python Task on Object Oriented

Python Task on Object Oriented

  • 29th Jul, 2022
  • 15:12 PM
from donor_models import DonorCollection
import pickle
import sys

dc=DonorCollection()

txt = "{:<27>11} {:>11}  ${:>12}"

def add():
    name=input("Enter Name : ")
    if (dc.add(name)==0):
        print("Donor Name Already Existed")
    else:
        print("Donor Added")
    
    
def donation():
    try:
        while(True):
            name=input("Enter Name : ")
            if (name=="list"):
                l=dc.showNames()
                if(len(l)==0):
                    print("No Record Found")
                else:
                    print ("Donor Name ")
                    for i in l:
                        print(i)
            else:
                break
        while(True):
            amt=float(input("Enter Donation Amount : "))
            
            if (amt>0):
                print(dc.donation(name,amt))
                break
            else:
                print("Donation amount cannot be less than 0")
    except Exception as ex:
        print ("Exception raised",ex)
    

def report():
    
    print("\nDonor Name                | Total Given | Num Gifts | Average Gift")
    print("------------------------------------------------------------------")
    l=dc.report()
    
    if(len(l)==0):
        print("No Record Found")
    else:
        for i in range (0,len(l)):
            for j in range (0,len(l)-i-1):
                if(l[j][1]                     temp=l[j]
                    l[j]=l[j+1]
                    l[j+1]=temp
        for i in l:   
          print(txt.format(i[0],i[1],i[2],i[3]))
    
    

def search():
    name=input("Enter Donor Name : ")
    l=dc.search(name)
    if(len(l)==0):
        print("Donor Not Found")
        
    else:
        print("\nDonor Name                | Total Given | Num Gifts | Average Gift")
        print("------------------------------------------------------------------")
        print(txt.format(l[0],l[1],l[2],l[3]))

def load():
    temp=DonorCollection()
    try:
        with open('data.pkl','rb')as input:
            
            temp=pickle.load(input)
        print("Data Loaded from file")
    except Exception as ex:
        print("Exception ", ex)
        print("Data cannot be loaded")
    return temp

def save():
    with open('data.pkl','wb')as output:
        pickle.dump(dc,output,pickle.HIGHEST_PROTOCOL)
        print("Data saved in file")

    

if __name__ == '__main__':
    
    while(True):
        print("\n1.Add a donor")
        print("2.Give a donation")
        print("3.Search for donor")
        print("4.Create a report")
        print("5.Save your data")
        print("6.reload your data")
        print("7.Quit")
        choice=int(input("Enter Your Choice : "))
        if(choice==1):
            add()
        elif(choice==2):
            donation()
        elif(choice==3):
            search()    
        elif(choice==4):
            report()
        elif(choice==5):
            save()
        elif(choice==6):
            dc=load()
            
        elif(choice==7):
            sys.exit()
        else:
            print("Wrong Choice")

Share this post

assignment helpassignment helperassignment expertsassignment writing services