-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuelCalculator.py
81 lines (70 loc) · 2.67 KB
/
fuelCalculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class fuelCalculator(object):
def __init__(self):
self.__date = "mm/dd/yyyy" #initialize date
self.__pricegas = 0 #initialize price of gas
self.__gallonsgas = 0 #initialize gallons of gas
self.__miles = 0 #initialize miles
self.__pricepg = 0 #initialize price per gallon
@property #date property
def date(self):
return self.__date
@date.setter#date setter
def date(self, date):
self.__date = date
return self.__date
@property #price of gas property
def pricegas(self):
return self.__pricegas
@pricegas.setter
def pricegas(self, price):
self.__pricegas = float(price)
return self.__pricegas
@property
def pricepgi(self):
return float(self.__pricepg)
@property #gallons of gas
def gallonsgas(self):
return self.__gallonsgas
@gallonsgas.setter
def gallonsgas(self, gallons):
self.__gallonsgas = float(gallons)
return self.__gallonsgas
@property #property for setting miles (int)
def miles(self):
return self.__miles
@miles.setter
def miles(self, dist):
self.__miles = int(miles)
return self.__miles
def pricepg(self, price, gallons):
price = float(price)
gallons = float(gallons)
self.__pricepg = abs(price / gallons)
self.__pricepg = round(self.__pricepg, 2)
return self.__pricepg
def setMilesRange(self, before, after): #range method for calculating miles (int)
before = int(before)
after = int(after)
self.__miles = abs(after - before)
return self.__miles
def mpg(self): #calculate MPG, assuming miles and gallons properties are already set
mpgR = round(self.__miles / self.__gallonsgas, 2) #miles per gallon equates to > miles / gallons
return ("{}".format(mpgR))
def status(self): #status method
try:
print("On {date}, you filled up {gallonsgas} gallons of gas, with a gas mileage of {mpg}".format(date = self.date, gallonsgas = self.gallonsgas, mpg = self.mpg()))
print("Gas on {date} was approximately {price} dollars per gallon.".format(date = self.date, price = self.pricegas))
except: #incase any properties are missing an error will be thrown and handled.
print("Missing required component(s). >> date, gallons of gas, price of gas, mpg")
def main():
#testing the fuelCalculator class.
f = fuelCalculator()
f.date = "10/11/2016"
f.pricegas = 2.15
f.gallonsgas = 6
f.setMilesRange(1,188)
f.pricepg(2.15, 5)
print(f.mpg())
f.status()
if __name__ == "__main__":
main()