example of a python class

here is an example.  white space may of been lost.
 
"""This is the policy class.  It has attributes policy number and name class
Policy:

      def __init__(self,policyNumber,name):

            self.__policyNumber=policyNumber

            self.__name=name

#The accessor methods. def getPolicyNumber(self):

            return self.__policyNumber

      def getName(self):

            return self.__name

#This is the str method which returns the formatted objects.      def
__str__(self):

            return 'The name is: '+self.getName()+' The policy number is:
'+str(self.getPolicyNumber())

 

"""This is the auto policy class which inherits policy number and name
attributes from the policy class.  It also has as attributes, model, year,
value.class Auto(Policy):

      def __init__(self,policyNumber,name,model,year,value):

            Policy.__init__(self,policyNumber,name)

            self.__model=model

            self.__year=year

            self.__value=value

#this is the start of the accessor methods.     def getModel(self):

            return self.__model

      def getYear(self):

            return self.__year

      def getValue(self):

            return self.__value

      def __str__(self):

#This is the string method.         return str(Policy.__str__(self))+'\nThe
model is: '+str(self.getModel())+'\nThe year is:
'+str(self.getYear())+'\nThe value is: '+str(self.getValue())

"""This is the car class which inherits attributes from the auto class as
well.class Car(Auto):

      def __init__(self,policyNumber,name,model,year,value,airbag):

            Auto.__init__(self,policyNumber,name,model,year,value)

            self.__airbag=airbag

#this is the accessor that returns if the car has an airbag.      def
hasAirbag(self):

            return self.__airbag

#Method that calculates the discount if off the premium if the car has an
airbag.   def getPremium(self):

            if self.hasAirbag():

                  return self.getValue()*.1*.98

            else:

                  return self.getValue()*.1

#the string method for formatting.  def __str__(self):

            return Auto.__str__(self)+'\nAirbag:'+str(self.hasAirbag())

"""This is the truck class which inherits attributes from the auto
class.class Truck(Auto):

      def __init__(self,policyNumber,name,model,year,value,weight):

            Auto.__init__(self,policyNumber,name,model,year,value)

            self.__weight=weight

#Accessor methods for the weight of the truck.  def getWeight(self):

            return self.__weight

#This method checks the weight of the truck.  If the truck is over 80,000lbs
there is a premium added to the price of the policy. def getPremium(self):

            if self.getWeight()>80000:

                  return self.getValue()*.1*1.05

            else:

                  return self.getValue()*.1

The string method for the truck class.    def __str__(self):

            return Auto.__str__(self)+'\nThe weight is:
'+str(self.getWeight())

"""This is the home class.  It inherits attributes from the policy class.
class Home(Policy):

      def __init__(self,policyNumber,name,address,value,sqFoot,floodZone):

            Policy.__init__(self,policyNumber,name)

            self.__address=address

            self.__value=value

            self.__sqFoot=sqFoot

            self.__floodZone=floodZone

#This is the accessor methods.      def getAddress(self):

            return self.__address

      def getValue(self):

            return self.__value

      def getSqFoot(self):

            return self.__sqFoot

      def getPremium(self):

#This method returns the premium.  If the home is in a floodzone then the
price of the policy will go up.       if self.__floodZone:

                  return self.getValue()*.03*1.01

            else:

                  return self.getValue()*.03

#this is the string method used for formatting. def __str__(self):

            return Policy.__str__(self)+'\nAddress:
'+str(self.getAddress())+'\nValue: '+str(self.getValue())+'\nSqFoot:
'+str(self.getSqFoot())+'\nFloodZone: '+str(self.__floodZone)

"""This is the agent class.  It holds the policy objects. class Agent:

      def __init__(self,name):

            self.__name=name

            self.__policies=[]

#creates a policy object and adds it to the agent class.    def
addPolicy(self,newPolicy):

            self.__policies.append(newPolicy)

#This method displays the policies that the agent is holding.     def
displayPolicies(self):

            for i in self.__policies:

                  print i

#this is the string method for the agent class. def __str__(self):

            result=self.__name

            for i in self.__policies:

                  result=result+'\n'+str(i)

            return result

 

#Testing of the various objects begins here.

 

ed=Agent('Ed')

first=Car(1000,"Joe",'Audi',2000,50000,True)

second=Truck(1001,'Jim','ford',2009,60000,100000)

third=Car(1002,"Jack",'BMW',2001,30000,False)

fourth=Truck(1003,'John','Mack',2007,100000,60000)

fifth=Home(1004,'Jeff','300 Summit St',500000,5000,True)

sixth=Home(1005,'Jill','301 Summit St',1000000,10000,False)

ed.addPolicy(first)

ed.addPolicy(second)

ed.addPolicy(third)

ed.addPolicy(fourth)

ed.addPolicy(fifth)

ed.addPolicy(sixth)

 

#print ed

print

print first.getPremium()

print second.getPremium()

print third.getPremium()

print fourth.getPremium()

print fifth.getPremium()

print sixth.getPremium()

 

GIF image

Other related posts:

  • » example of a python class - edward