Skip to content

Commit a98dfcc

Browse files
authored
Add files via upload
1 parent 888fe7b commit a98dfcc

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

car_class2.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Car Class example
2+
# Info on classes:
3+
# https://docs.python.org/3/tutorial/classes.html
4+
5+
class Car:
6+
"""Common base class for all cars"""
7+
car_count = 0 # class variable for keeping a cars created counter
8+
9+
# The __init__method accepts arguments for the
10+
# make, model, and mileage. It initializes the data
11+
# attributes with these values and increments the car counter.
12+
def __init__(self, year, make, model, mileage):
13+
self.__year = year
14+
self.__make = make
15+
self.__model = model
16+
self.__mileage = mileage
17+
Car.car_count += 1
18+
19+
# The __str__method returns a string representation of the object.
20+
def __str__(self):
21+
return '{0:s} {1:s} {2:s} with {3:,d} miles'.format(self.__year, self.__make, self.__model, self.__mileage)
22+
23+
# The __add__method "overloards" the addition + operator for the Car class to increment the mileage.
24+
def __add__(self, miles):
25+
return self.__mileage + miles
26+
27+
28+
# The following methods are mutators (setters)
29+
# for the class's data attributes.
30+
def set_year(self, year):
31+
self.__year = year
32+
33+
def set_make(self, make):
34+
self.__make = make
35+
36+
def set_model(self, model):
37+
self.__model = model
38+
39+
def set_mileage(self, mileage):
40+
self.__mileage = mileage
41+
42+
# The following methods are the accessors (getters)
43+
# for the class's data attributes.
44+
def get_year(self):
45+
return self.__year
46+
47+
def get_make(self):
48+
return self.__make
49+
50+
def get_model(self):
51+
return self.__model
52+
53+
def get_mileage(self):
54+
return self.__mileage
55+
56+
# A method to increase the car mileage by miles driven
57+
def add_mileage(self, miles):
58+
self.__mileage += miles
59+
60+
61+
###########################################################
62+
## Test the Car class
63+
###########################################################
64+
## The main function.
65+
def main():
66+
print('Create car1 ...')
67+
car1 = Car('2019', 'Ford', 'Mustang', 1000)
68+
print('Year:{:s}'.format(car1.get_year()) )
69+
print('Make:{:s}'.format(car1.get_make()) )
70+
print('Model: {:s}'.format(car1.get_model()) )
71+
print('Mileage: {:,d}'.format(car1.get_mileage()) )
72+
print(car1)
73+
74+
print('\nAdd 500 to car1 mileage ...')
75+
car1.add_mileage(500)
76+
print(car1)
77+
78+
print('\nCreate car2 ...')
79+
car2 = Car('2018', 'Chevrolet', 'Cruze', 5000)
80+
print(car2)
81+
82+
print('\nCreate car3 ...')
83+
car3 = Car('2015', 'BMW', '3 Series', 8000)
84+
print(car3)
85+
86+
print()
87+
print('Total Car Count: {:,d}'.format(Car.car_count))
88+
print()
89+
90+
91+
# Call the main function.
92+
if __name__ == '__main__':
93+
main()
94+
95+
96+

0 commit comments

Comments
 (0)