Skip to content

Commit 315a979

Browse files
committed
Added Inheritance - Exercise and Encapsulation - Lab
1 parent ab185c5 commit 315a979

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+354
-0
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from project.person import Person
2+
3+
4+
class Child(Person):
5+
6+
def __init__(self, name, age):
7+
super().__init__(name, age)
8+
9+
10+
# person = Person("Peter", 25)
11+
# child = Child("Peter Junior", 5)
12+
# print(person.name)
13+
# print(person.age)
14+
# print(child.__class__.__bases__[0].__name__)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Person:
2+
3+
def __init__(self, name, age):
4+
self.name = name
5+
self.age = age
6+
Binary file not shown.

OOP/3.Inheritance/Inheritance - Exercise/02. Zoo/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Animal:
2+
3+
def __init__(self, name):
4+
self.name = name
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from project.mammal import Mammal
2+
3+
4+
class Bear(Mammal):
5+
pass
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from project.mammal import Mammal
2+
3+
4+
class Gorilla(Mammal):
5+
pass
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from project.reptile import Reptile
2+
3+
4+
class Lizard(Reptile):
5+
pass
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from project.animal import Animal
2+
3+
4+
class Mammal(Animal):
5+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from project.animal import Animal
2+
3+
4+
class Reptile(Animal):
5+
pass
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from project.reptile import Reptile
2+
3+
4+
class Snake(Reptile):
5+
pass
6+
Binary file not shown.

OOP/3.Inheritance/Inheritance - Exercise/03. Players and Monsters/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from project.dark_knight import DarkKnight
2+
3+
4+
class BladeKnight(DarkKnight):
5+
def __str__(self):
6+
return f"{self.username} of type {BladeKnight.__name__} has level {self.level}"
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from project.knight import Knight
2+
3+
4+
class DarkKnight(Knight):
5+
def __str__(self):
6+
return f"{self.username} of type {DarkKnight.__name__} has level {self.level}"
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from project.wizard import Wizard
2+
3+
4+
class DarkWizard(Wizard):
5+
def __str__(self):
6+
return f"{self.username} of type {DarkWizard.__name__} has level {self.level}"
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from project.hero import Hero
2+
3+
4+
class Elf(Hero):
5+
def __str__(self):
6+
return f"{self.username} of type {Elf.__name__} has level {self.level}"
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Hero:
2+
3+
def __init__(self, username, level):
4+
self.username = username
5+
self.level = level
6+
7+
def __str__(self):
8+
return f"{self.username} of type {Hero.__name__} has level {self.level}"
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from project.hero import Hero
2+
3+
4+
class Knight(Hero):
5+
def __str__(self):
6+
return f"{self.username} of type {Knight.__name__} has level {self.level}"
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from project.elf import Elf
2+
3+
4+
class MuseElf(Elf):
5+
def __str__(self):
6+
return f"{self.username} of type {MuseElf.__name__} has level {self.level}"
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from project.dark_wizard import DarkWizard
2+
3+
4+
class SoulMaster(DarkWizard):
5+
def __str__(self):
6+
return f"{self.username} of type {SoulMaster.__name__} has level {self.level}"
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from project.hero import Hero
2+
3+
4+
class Wizard(Hero):
5+
def __str__(self):
6+
return f"{self.username} of type {Wizard.__name__} has level {self.level}"
7+
Binary file not shown.

OOP/3.Inheritance/Inheritance - Exercise/04. Need for Speed/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from project.vehicle import Vehicle
2+
3+
4+
class Car(Vehicle):
5+
DEFAULT_FUEL_CONSUMPTION = 3
6+
fuel_consumption = 3
7+
8+
def drive(self, kilometers):
9+
if self.fuel - (kilometers * Car.fuel_consumption) >= 0:
10+
self.fuel -= (kilometers * Car.fuel_consumption)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from project.motorcycle import Motorcycle
2+
3+
4+
class CrossMotorcycle(Motorcycle):
5+
def drive(self, kilometers):
6+
if self.fuel - (kilometers * CrossMotorcycle.fuel_consumption) >= 0:
7+
self.fuel -= (kilometers * CrossMotorcycle.fuel_consumption)
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from project.car import Car
2+
3+
4+
class FamilyCar(Car):
5+
def drive(self, kilometers):
6+
if self.fuel - (kilometers * FamilyCar.fuel_consumption) >= 0:
7+
self.fuel -= (kilometers * FamilyCar.fuel_consumption)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from project.vehicle import Vehicle
2+
3+
4+
class Motorcycle(Vehicle):
5+
def drive(self, kilometers):
6+
if self.fuel - (kilometers * Motorcycle.fuel_consumption) >= 0:
7+
self.fuel -= (kilometers * Motorcycle.fuel_consumption)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from project.motorcycle import Motorcycle
2+
3+
4+
class RaceMotorcycle(Motorcycle):
5+
DEFAULT_FUEL_CONSUMPTION = 8
6+
fuel_consumption = 8
7+
8+
def drive(self, kilometers):
9+
if self.fuel - (kilometers * RaceMotorcycle.fuel_consumption) >= 0:
10+
self.fuel -= (kilometers * RaceMotorcycle.fuel_consumption)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from project.car import Car
2+
3+
4+
class SportCar(Car):
5+
DEFAULT_FUEL_CONSUMPTION = 10
6+
fuel_consumption = 10
7+
8+
def drive(self, kilometers):
9+
if self.fuel - (kilometers * SportCar.fuel_consumption) >= 0:
10+
self.fuel -= (kilometers * SportCar.fuel_consumption)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Vehicle:
2+
DEFAULT_FUEL_CONSUMPTION = 1.25
3+
fuel_consumption = 1.25
4+
5+
def __init__(self, fuel, horse_power):
6+
self.fuel = fuel
7+
self.horse_power = horse_power
8+
9+
def drive(self, kilometers):
10+
if self.fuel - (kilometers * Vehicle.fuel_consumption) >= 0:
11+
self.fuel -= (kilometers * Vehicle.fuel_consumption)
12+
Binary file not shown.

OOP/3.Inheritance/Inheritance - Exercise/05. Shop/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from project.product import Product
2+
3+
4+
class Drink(Product):
5+
6+
def __init__(self, name):
7+
self.name = name
8+
self.quantity = 10
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from project.product import Product
2+
3+
4+
class Food(Product):
5+
6+
def __init__(self, name):
7+
self.name = name
8+
self.quantity = 15
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Product:
2+
3+
def __init__(self, name, quantity):
4+
self.name = name
5+
self.quantity = quantity
6+
7+
def decrease(self, quantity):
8+
if self.quantity - quantity >= 0:
9+
self.quantity -= quantity
10+
11+
def increase(self, quantity):
12+
self.quantity += quantity
13+
14+
def __repr__(self):
15+
return self.name
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from project.product import Product
2+
3+
4+
class ProductRepository:
5+
products = []
6+
7+
def add(self, product: Product):
8+
self.products.append(product)
9+
10+
def find(self, product: Product):
11+
for x in self.products:
12+
if x.__class__.__name__ == product.__class__.__name__:
13+
return x
14+
15+
def remove(self, product_name):
16+
for pos, x in enumerate(self.products):
17+
if x.name == product_name:
18+
del self.products[pos]
19+
20+
def __repr__(self):
21+
show = ""
22+
for x in self.products:
23+
if x.name not in show:
24+
show += f"{x.name}: {x.quantity}\n"
25+
return show.rstrip()
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Person:
2+
3+
def __init__(self, name, age):
4+
self.__name = name
5+
self.__age = age
6+
7+
def get_name(self):
8+
return self.__name
9+
10+
def get_age(self):
11+
return self.__age
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Mammal:
2+
__kingdom = "animals"
3+
4+
def __init__(self, name, type, sound):
5+
self.name = name
6+
self.type = type
7+
self.sound = sound
8+
9+
def make_sound(self):
10+
return f"{self.name} makes {self.sound}"
11+
12+
def get_kingdom(self):
13+
return self.__kingdom
14+
15+
def info(self):
16+
return f"{self.name} is of type {self.type}"
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Profile:
2+
3+
def __init__(self, username, password):
4+
self.username = username
5+
self.password = password
6+
7+
@property
8+
def username(self):
9+
return self.__username
10+
11+
@property
12+
def password(self):
13+
return self.__password
14+
15+
@username.setter
16+
def username(self, value):
17+
if 5 <= len(value) <= 15:
18+
self.__username = value
19+
else:
20+
raise ValueError("The username must be between 5 and 15 characters.")
21+
22+
@password.setter
23+
def password(self, value):
24+
if len(value) >= 8 and value != value.lower() and [x for x in value if x.isdigit()]:
25+
self.__password = value
26+
else:
27+
raise ValueError("The password must be 8 or more characters with at least 1 digit and 1 uppercase letter.")
28+
29+
def __str__(self):
30+
return f"You have a profile with username: \"{self.__username}\" and password: {'*' * len(self.__password)}"
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class EmailValidator:
2+
3+
def __init__(self, min_length, mails, domains):
4+
self.min_length = min_length
5+
self.mails = mails
6+
self.domains = domains
7+
8+
def __is_name_valid(self, name):
9+
return len(name) >= self.min_length
10+
11+
def __is_mail_valid(self, mail):
12+
return mail in self.mails
13+
14+
def __is_domain_valid(self, domain):
15+
return domain in self.domains
16+
17+
def validate(self, email):
18+
_username = email.split("@")[0]
19+
_mail = email.split("@")[1].split(".")[0]
20+
_domain = email.split(".")[-1]
21+
22+
valid_name = self.__is_name_valid(_username)
23+
valid_mail = self.__is_mail_valid(_mail)
24+
valid_domain = self.__is_domain_valid(_domain)
25+
26+
return valid_name and valid_mail and valid_domain
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Account:
2+
3+
def __init__(self, id, balance, pin):
4+
self.__id = id
5+
self.balance = balance
6+
self.__pin = pin
7+
8+
def get_id(self, pin):
9+
if pin == self.__pin:
10+
return self.__id
11+
return "Wrong pin"
12+
13+
def change_pin(self, old_pin, new_pin):
14+
if old_pin == self.__pin:
15+
self.__pin = new_pin
16+
return "Pin changed"
17+
return "Wrong pin"
18+

0 commit comments

Comments
 (0)