In: Computer Science
Classes and Objects
Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for the Services class is computed as numberOfHours times ratePerHour, and for the Supplies class the cost will be numberOfItems times pricePerItem. Each class should have a function __str__() that will return all the required information.
Write a main() program that applies Python list to store at least two objects of each class mentioned above. Implement all available functions to demonstrate your understanding of applying those methods in your program. Make up your own data when creating each object and print out each object information accordingly. Please submit your UML diagram. Your code is expected to be commented and user-friendly.
I have implemented the Sales, Service, and main() per the given description.
Please find the following Code Screenshot, Output, and Code.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT
1.CODE SCREENSHOT :
2.OUTPUT :
3.CODE :
class Services:
#construtor to declare that initlize numberOfHours and ratePerHour
def __init__(self, numberOfHours:int, ratePerHour:float):
self._numberOfHours = numberOfHours # private
self._ratePerHour = ratePerHour # private
#setter method for numberOfHours and ratePerHour
def setNumberOfHours(self,numberOfHours:int):
self._numberOfHours = numberOfHours
def setRatePerHour(self,ratePerHour:float):
self._ratePerHour=ratePerHour
#getter method for numberOfHours and ratePerHour
def getNumberOfHours(self):
return self._numberOfHours
def getRatePerHour(self):
return self._ratePerHour
#method to calcuate the total cost
def calculateSales(self):
return self.getNumberOfHours()*self.getRatePerHour();
#a to string method to return all the information in string format
def __str__(self):
return "Service \nNumber of Hours : "+str(self.getNumberOfHours())+"\nRate Per Hour : "+str(self.getRatePerHour())+"\nCost Accrued : "+str(self.calculateSales());
class Supplies:
#Constructor to declare and intilize numberOfItems and pricePerItem
def __init__(self, numberOfItems :int, pricePerItem :float):
self._numberOfItems = numberOfItems # private
self._pricePerItem = pricePerItem # private
#setter method for numberOfItems and pricePerItem
def setNumberOfItems (self,numberOfHours:int):
self._numberOfItems = numberOfHours
def setPricePerItem (self,pricePerItem :float):
self._pricePerItem =pricePerItem
#getter method for numberOfItems and pricePerItem
def getNumberOfItems (self):
return self._numberOfItems
def getPricePerItem(self):
return self._pricePerItem
#method to calcuate the total cost
def calculateSales(self):
return self.getPricePerItem()*self.getNumberOfItems();
#a to string method to return all the information in string format
def __str__(self):
return "Supplies \nNumber of Hours : "+str(self.getNumberOfItems())+"\nRate Per Hour : "+str(self.getPricePerItem())+"\nCost Accrued : "+str(self.calculateSales());
def main():
#Creat a empty list
lst=[]
#create a service class object
Ser=Services(16,2.2)
#add the abject to list
lst.append(Ser)
#Create a Supplies class object
sup=Supplies(18,3.2)
#add that to list
lst.append(sup)
#Print the elements in the list
#please note the __str()__ will call all the getter and calculateSales methods
for i in lst:
print(i)
main()
UML calss Diagram: