Tuesday, November 27, 2018

Mileage class and Card Class

Mileage.py

class Mileage:
    def __init__(self, miles, gallons):
        self.miles=miles
        self.gallons=gallons
        self.pricePerGallon = 0.0
    
    def getMiles(self):
        return self.miles

    def getGallons(self):
        return self.gallons
    
    def setPricePerGallon(self,price):
        self.pricePerGallon=price
    
    def getPricePerGallon(self):
        return self.pricePerGallon
    
    def calculateMPG(self):
        return self.miles/self.gallons
    
    def calculatePricePerMile(self):
        result=0.0
        if self.pricePerGallon != 0:
            cost=self.pricePerGallon * self.gallons
            result=cost/self.miles
        return result

    def __str__(self):
        return str(self.miles) + ' miles ' + str(self.gallons) + ' gallons'

Display.py

from mileage import Mileage

class Display:
    def __init__(self):
        self.miles=self.getMiles()
        self.gallons=self.getGallons()
        self.mileage=Mileage(self.miles, self.gallons)

    def getMiles(self):
        self.miles = float(input("Enter the total miles: "))
        return self.miles

    def getGallons(self):
        self.gallons=float(input("Enter the total gallons "))
        return self.gallons
    
    def getMPG(self):
        #self.miles=self.getMiles()
        #self.gallons=self.getGallons()
        #self.mileage = Mileage(self.miles, self.gallons)
        self.mpg=self.mileage.calculateMPG()
    
    def getPricePerMile(self):
        price=eval(input("enter the price per gallon "))
        self.mileage.setPricePerGallon(price)
        self.ppm=self.mileage.calculatePricePerMile()
    
    def displayanswers(self):
        self.getPricePerMile()
        self.getMPG()
        print(" the mileage is ", self.mpg)
        print(" The price per miles is", self.ppm)


def main():
    display = Display()
    display.displayanswers()
        
main()

Card.py

#Card
class Card:
    def __init__(self, rank, suit):
        self.rank=rank
        self.suit=suit
        self.value=0
       
        
    def getRank(self):
        return self.rank

    def getSuit(self):
        return self.suit

    def getValue(self):
        if self.rank > 10:
            self.value=10
        else:
            self.value=self.rank
        return self.value

    def setSuit(self):
        self.su=""
        if self.suit =="d":
            self.su="diamonds"
        elif self.suit=="h":
            self.su="hearts"
        elif self.suit=="s":
            self.su="spades"
        else:
            self.su ="clubs"
        return self.su

    
    
    def __str__(self):
        
        if self.rank >1 and self.rank< 11:
            self.name=str(self.rank) + " of " + self.setSuit()
        if self.rank==1:
            self.name="the ace of " + self.setSuit()
        if self.rank==11:
            self.name="the jack of " + self.setSuit()
        if self.rank==12:
             self.name="the queen of " + self.setSuit()
        if self.rank==13:
             self.name="the king of " + self.setSuit()
        return self.name

def main():
    card=Card(9,"s")
    print(card)

main()

No comments:

Post a Comment