Skip to content

Commit fac2da6

Browse files
committed
Added Polymorphism and Abstraction - Exercise and SOLID - Lab
1 parent 7c214f9 commit fac2da6

File tree

29 files changed

+790
-0
lines changed

29 files changed

+790
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Vehicle(ABC):
5+
6+
def __init__(self, fuel_quantity, fuel_consumption):
7+
self.fuel_quantity = fuel_quantity
8+
self.fuel_consumption = fuel_consumption
9+
10+
@abstractmethod
11+
def drive(self, distance):
12+
pass
13+
14+
@abstractmethod
15+
def refuel(self, fuel):
16+
pass
17+
18+
19+
class Car(Vehicle):
20+
CONDITIONER = 0.9
21+
22+
def drive(self, distance):
23+
consumption = (self.fuel_consumption + Car.CONDITIONER) * distance
24+
25+
if self.fuel_quantity >= consumption:
26+
self.fuel_quantity -= consumption
27+
28+
def refuel(self, fuel):
29+
self.fuel_quantity += fuel
30+
31+
32+
class Truck(Vehicle):
33+
CONDITIONER = 1.6
34+
35+
def drive(self, distance):
36+
consumption = (self.fuel_consumption + Truck.CONDITIONER) * distance
37+
38+
if self.fuel_quantity >= consumption:
39+
self.fuel_quantity -= consumption
40+
41+
def refuel(self, fuel):
42+
self.fuel_quantity += fuel * 0.95
43+
44+
45+
# car = Car(20, 5)
46+
# car.drive(3)
47+
# print(car.fuel_quantity)
48+
# car.refuel(10)
49+
# print(car.fuel_quantity)
50+
#
51+
# print()
52+
#
53+
# truck = Truck(100, 15)
54+
# truck.drive(5)
55+
# print(truck.fuel_quantity)
56+
# truck.refuel(50)
57+
# print(truck.fuel_quantity)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Person:
2+
def __init__(self, name, surname):
3+
self.surname = surname
4+
self.name = name
5+
6+
def __add__(self, obj):
7+
return Person(self.name, obj.surname)
8+
9+
def __repr__(self):
10+
return f"{self.name} {self.surname}"
11+
12+
13+
class Group:
14+
def __init__(self, name, people):
15+
self.people = people
16+
self.name = name
17+
18+
def __len__(self):
19+
return len(self.people)
20+
21+
def __add__(self, obj):
22+
return Group(f"{self.name} {obj.name}", self.people + obj.people)
23+
24+
def __getitem__(self, index):
25+
return f"Person {index}: {str(self.people[index])}"
26+
27+
def __repr__(self):
28+
return f"Group {self.name} with members {', '.join(str(x) for x in self.people)}"
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class Account:
2+
3+
def __init__(self, owner, amount=0):
4+
self.owner = owner
5+
self.amount = amount
6+
self._transactions = []
7+
8+
@property
9+
def balance(self):
10+
return sum(self._transactions) + self.amount
11+
12+
def handle_transaction(self, transaction_amount):
13+
if self.balance + transaction_amount < 0:
14+
raise ValueError("sorry cannot go in debt!")
15+
16+
self._transactions.append(transaction_amount)
17+
18+
return f"New balance: {self.balance}"
19+
20+
def add_transaction(self, amount):
21+
if not isinstance(amount, int):
22+
raise ValueError("please use int for amount")
23+
24+
return self.handle_transaction(amount)
25+
26+
def __str__(self):
27+
return f"Account of {self.owner} with starting amount: {self.amount}"
28+
29+
def __repr__(self):
30+
return f"Account({self.owner}, {self.amount})"
31+
32+
def __len__(self):
33+
return len(self._transactions)
34+
35+
def __getitem__(self, index):
36+
return self._transactions[index]
37+
38+
def __reversed__(self):
39+
return self._transactions[::-1]
40+
41+
def __gt__(self, other):
42+
return self.balance > other.balance
43+
44+
def __lt__(self, other):
45+
return self.balance < other.balance
46+
47+
def __ge__(self, other):
48+
return self.balance >= other.balance
49+
50+
def __le__(self, other):
51+
return self.balance <= other.balance
52+
53+
def __eq__(self, other):
54+
return self.balance == other.balance
55+
56+
def __ne__(self, other):
57+
return self.balance != other.balance
58+
59+
def __add__(self, other):
60+
new_account = Account(f"{self.owner}&{other.owner}", self.amount + other.amount)
61+
new_account._transactions = self._transactions + other._transactions
62+
63+
return new_account
64+

OOP/6.Polymorphism and Abstraction/Polymorphism and Abstraction - Exercise/04. Wild Farm/project/animals/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Animal(ABC):
5+
6+
def __init__(self, name, weight, food_eaten=0):
7+
self.name = name
8+
self.weight = weight
9+
self.food_eaten = food_eaten
10+
11+
@property
12+
@abstractmethod
13+
def food_that_eats(self):
14+
pass
15+
16+
@property
17+
@abstractmethod
18+
def gained_weight(self):
19+
pass
20+
21+
@staticmethod
22+
@abstractmethod
23+
def make_sound():
24+
pass
25+
26+
def feed(self, food):
27+
if type(food) not in self.food_that_eats:
28+
return f"{self.__class__.__name__} does not eat {food.__class__.__name__}!"
29+
30+
self.weight += food.quantity * self.gained_weight
31+
self.food_eaten += food.quantity
32+
33+
class Bird(Animal, ABC):
34+
35+
def __init__(self, name, weight, wing_size):
36+
super().__init__(name, weight)
37+
self.wing_size = wing_size
38+
39+
def __repr__(self):
40+
return f"{self.__class__.__name__} [{self.name}, {self.wing_size}, {self.weight}, {self.food_eaten}]"
41+
42+
class Mammal(Animal, ABC):
43+
44+
def __init__(self, name, weight, living_region):
45+
super().__init__(name, weight)
46+
self.living_region = living_region
47+
48+
def __repr__(self):
49+
return f"{self.__class__.__name__} [{self.name}, {self.weight}, {self.living_region}, {self.food_eaten}]"
50+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from project.animals.animal import Bird
2+
from project.food import Meat, Fruit, Vegetable, Seed
3+
4+
5+
class Owl(Bird):
6+
7+
@staticmethod
8+
def make_sound():
9+
return "Hoot Hoot"
10+
11+
@property
12+
def gained_weight(self):
13+
return 0.25
14+
15+
@property
16+
def food_that_eats(self):
17+
return [Meat]
18+
19+
class Hen(Bird):
20+
21+
@staticmethod
22+
def make_sound():
23+
return "Cluck"
24+
25+
@property
26+
def gained_weight(self):
27+
return 0.35
28+
29+
@property
30+
def food_that_eats(self):
31+
return [Meat, Fruit, Vegetable, Seed]
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from project.animals.animal import Mammal
2+
from project.food import Fruit, Vegetable, Meat
3+
4+
5+
class Mouse(Mammal):
6+
7+
@staticmethod
8+
def make_sound():
9+
return "Squeak"
10+
11+
@property
12+
def gained_weight(self):
13+
return 0.10
14+
15+
@property
16+
def food_that_eats(self):
17+
return [Fruit, Vegetable]
18+
19+
20+
class Dog(Mammal):
21+
22+
@staticmethod
23+
def make_sound():
24+
return "Woof!"
25+
26+
@property
27+
def gained_weight(self):
28+
return 0.40
29+
30+
@property
31+
def food_that_eats(self):
32+
return [Meat]
33+
34+
35+
class Cat(Mammal):
36+
37+
@staticmethod
38+
def make_sound():
39+
return "Meow"
40+
41+
@property
42+
def gained_weight(self):
43+
return 0.30
44+
45+
@property
46+
def food_that_eats(self):
47+
return [Meat, Vegetable]
48+
49+
50+
class Tiger(Mammal):
51+
52+
@staticmethod
53+
def make_sound():
54+
return "ROAR!!!"
55+
56+
@property
57+
def gained_weight(self):
58+
return 1.00
59+
60+
@property
61+
def food_that_eats(self):
62+
return [Meat]
63+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Food(ABC):
5+
6+
@abstractmethod
7+
def __init__(self, quantity):
8+
self.quantity = quantity
9+
10+
11+
class Vegetable(Food):
12+
13+
def __init__(self, quantity):
14+
super().__init__(quantity)
15+
16+
17+
class Fruit(Food):
18+
19+
def __init__(self, quantity):
20+
super().__init__(quantity)
21+
22+
23+
class Meat(Food):
24+
25+
def __init__(self, quantity):
26+
super().__init__(quantity)
27+
28+
29+
class Seed(Food):
30+
31+
def __init__(self, quantity):
32+
super().__init__(quantity)
33+

OOP/6.Polymorphism and Abstraction/Polymorphism and Abstraction - Exercise/05. Animals/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Animal(ABC):
5+
6+
def __init__(self, name, age, gender):
7+
self.name = name
8+
self.age = age
9+
self.gender = gender
10+
11+
@staticmethod
12+
@abstractmethod
13+
def make_sound():
14+
pass
15+
16+
@abstractmethod
17+
def __repr__(self):
18+
pass
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from project.animal import Animal
2+
3+
4+
class Cat(Animal):
5+
6+
def __init__(self, name, age, gender):
7+
super().__init__(name, age, gender)
8+
9+
@staticmethod
10+
def make_sound():
11+
return "Meow meow!"
12+
13+
def __repr__(self):
14+
return f"This is {self.name}. {self.name} is a {self.age} year old {self.gender} {self.__class__.__name__}"
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from project.animal import Animal
2+
3+
4+
class Dog(Animal):
5+
6+
def __init__(self, name, age, gender):
7+
super().__init__(name, age, gender)
8+
9+
@staticmethod
10+
def make_sound():
11+
return "Woof!"
12+
13+
def __repr__(self):
14+
return f"This is {self.name}. {self.name} is a {self.age} year old {self.gender} {self.__class__.__name__}"
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from project.cat import Cat
2+
3+
4+
class Kitten(Cat):
5+
6+
def __init__(self, name, age, gender="Female"):
7+
super().__init__(name, age, gender)
8+
9+
@staticmethod
10+
def make_sound():
11+
return "Meow"
12+

0 commit comments

Comments
 (0)