Skip to content

Commit 0cfdb39

Browse files
committed
Added Classes and Objects - Exercise
1 parent a3f5019 commit 0cfdb39

File tree

25 files changed

+488
-0
lines changed

25 files changed

+488
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Vet:
2+
animals = []
3+
space = 5
4+
5+
def __init__(self, name):
6+
self.name = name
7+
self.animals = []
8+
9+
def register_animal(self, animal_name):
10+
if Vet.space:
11+
Vet.animals.append(animal_name), self.animals.append(animal_name)
12+
Vet.space -= 1
13+
return f"{animal_name} registered in the clinic"
14+
return "Not enough space"
15+
16+
def unregister_animal(self, animal_name):
17+
if animal_name in Vet.animals:
18+
Vet.animals.remove(animal_name), self.animals.remove(animal_name)
19+
Vet.space += 1
20+
return f"{animal_name} unregistered successfully"
21+
return f"{animal_name} not in the clinic"
22+
23+
def info(self):
24+
return f"{self.name} has {len(self.animals)} animals. {Vet.space} space left in clinic"
25+
26+
27+
# peter = Vet("Peter")
28+
# george = Vet("George")
29+
# print(peter.register_animal("Tom"))
30+
# print(george.register_animal("Cory"))
31+
# print(peter.register_animal("Fishy"))
32+
# print(peter.register_animal("Bobby"))
33+
# print(george.register_animal("Kay"))
34+
# print(george.unregister_animal("Cory"))
35+
# print(peter.register_animal("Silky"))
36+
# print(peter.unregister_animal("Molly"))
37+
# print(peter.unregister_animal("Tom"))
38+
# print(peter.info())
39+
# print(george.info())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Time:
2+
max_hours = 23
3+
max_minutes = 59
4+
max_seconds = 59
5+
6+
def __init__(self, hours, minutes, seconds):
7+
self.hours = hours
8+
self.minutes = minutes
9+
self.seconds = seconds
10+
11+
def set_time(self, hours, minutes, seconds):
12+
self.hours = hours
13+
self.minutes = minutes
14+
self.seconds = seconds
15+
16+
def get_time(self):
17+
return f"{self.hours:02d}:{self.minutes:02d}:{self.seconds:02d}"
18+
19+
def next_second(self):
20+
self.seconds += 1
21+
if self.seconds > Time.max_seconds:
22+
self.seconds = 0
23+
self.minutes += 1
24+
if self.minutes > Time.max_minutes:
25+
self.minutes = 0
26+
self.hours += 1
27+
if self.hours > Time.max_hours:
28+
self.hours = 0
29+
30+
return self.get_time()
31+
32+
33+
# # TEST 1
34+
# time = Time(9, 30, 59)
35+
# print(time.next_second()) # 09:31:00
36+
#
37+
# # TEST 2
38+
# time = Time(10, 59, 59)
39+
# print(time.next_second()) # 11:00:00
40+
#
41+
# # TEST 3
42+
# time = Time(23, 59, 59)
43+
# print(time.next_second()) # 00:00:00
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Account:
2+
3+
def __init__(self, id, name, balance=0):
4+
self.id = id
5+
self.name = name
6+
self.balance = balance
7+
8+
def credit(self, amount):
9+
self.balance += amount
10+
return self.balance
11+
12+
def debit(self, amount):
13+
if amount <= self.balance:
14+
self.balance -= amount
15+
return self.balance
16+
return "Amount exceeded balance"
17+
18+
def info(self):
19+
return f"User {self.name} with account {self.id} has {self.balance} balance"
20+
21+
22+
# account = Account(1234, "George", 1000)
23+
# print(account.credit(500))
24+
# print(account.debit(1500))
25+
# print(account.info()) 1500
26+
# #
27+
# #
28+
# account = Account(5411256, "Peter")
29+
# print(account.debit(500))
30+
# print(account.credit(1000))
31+
# print(account.debit(500))
32+
# print(account.info())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class PizzaDelivery:
2+
3+
def __init__(self, name, price, ingredients):
4+
self.name = name
5+
self.price = price
6+
self.ingredients = ingredients
7+
self.ordered = False
8+
9+
def add_extra(self, ingredient, quantity, price_per_quantity):
10+
if not self.ordered:
11+
if ingredient in self.ingredients.keys():
12+
self.ingredients[ingredient] += quantity
13+
else:
14+
self.ingredients[ingredient] = quantity
15+
16+
self.price += (price_per_quantity * quantity)
17+
18+
else:
19+
return f"Pizza {self.name} already prepared, and we can't make any changes!"
20+
21+
def remove_ingredient(self, ingredient, quantity, price_per_quantity):
22+
if not self.ordered:
23+
if ingredient not in self.ingredients.keys():
24+
return f"Wrong ingredient selected! We do not use {ingredient} in {self.name}!"
25+
26+
elif self.ingredients[ingredient] - quantity < 0:
27+
return f"Please check again the desired quantity of {ingredient}!"
28+
29+
else:
30+
self.ingredients[ingredient] -= quantity
31+
self.price -= (price_per_quantity * quantity)
32+
33+
return f"Pizza {self.name} already prepared, and we can't make any changes!"
34+
35+
def make_order(self):
36+
self.ordered = True
37+
return f"You've ordered pizza {self.name} prepared with " \
38+
f"{', '.join([f'{ingredient}: {quantity}' for ingredient, quantity in self.ingredients.items()])} " \
39+
f"and the price will be {self.price}lv."
40+
41+
42+
# margarita = PizzaDelivery('Margarita', 11, {'cheese': 2, 'tomatoes': 1})
43+
# margarita.add_extra('mozzarella', 1, 0.5)
44+
# margarita.add_extra('cheese', 1, 1)
45+
# margarita.remove_ingredient('cheese', 1, 1)
46+
# print(margarita.remove_ingredient('bacon', 1, 2.5))
47+
# print(margarita.remove_ingredient('tomatoes', 2, 0.5))
48+
# margarita.remove_ingredient('cheese', 2, 1)
49+
# print(margarita.make_order())
50+
# print(margarita.add_extra('cheese', 1, 1))

OOP/2.Classes and Objects/Classes and Objects - Exercise/05. To-do List/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from project.task import Task
2+
3+
4+
class Section:
5+
6+
def __init__(self, name):
7+
self.name = name
8+
self.tasks = {}
9+
10+
def add_task(self, new_task: Task):
11+
if new_task.name not in self.tasks.keys():
12+
self.tasks[new_task.name] = new_task
13+
return f"Task {new_task.details()} is added to the section"
14+
return f"Task is already in the section {self.name}"
15+
16+
def complete_task(self, task_name):
17+
if task_name in self.tasks.keys():
18+
self.tasks[task_name].completed = True
19+
return f"Completed task {task_name}"
20+
return f"Could not find task with the name {task_name}"
21+
22+
def clean_section(self):
23+
deleted_tasks_count = 0
24+
task_keys = self.tasks.copy().keys()
25+
for task_key in task_keys:
26+
if self.tasks[task_key].completed:
27+
deleted_tasks_count += 1
28+
del self.tasks[task_key]
29+
30+
return f"Cleared {deleted_tasks_count} tasks."
31+
32+
def view_section(self):
33+
show_tasks_format = '\n'.join([task.details() for task in self.tasks.values()])
34+
return f"Section {self.name}:\n{show_tasks_format}"
35+
36+
37+
# task = Task("Make bed", "27/05/2020")
38+
# print(task.change_name("Go to University"))
39+
# print(task.change_due_date("28.05.2020"))
40+
# task.add_comment("Don't forget laptop")
41+
# print(task.edit_comment(0, "Don't forget laptop and notebook"))
42+
# print(task.details())
43+
# section = Section("Daily tasks")
44+
# print(section.add_task(task))
45+
# second_task = Task("Make bed", "27/05/2020")
46+
# section.add_task(second_task)
47+
# print(section.clean_section())
48+
# print(section.view_section())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Task:
2+
3+
def __init__(self, name, due_date):
4+
self.name = name
5+
self.due_date = due_date
6+
self.comments = []
7+
self.completed = False
8+
9+
def change_name(self, new_name):
10+
if new_name == self.name:
11+
return "Name cannot be the same."
12+
self.name = new_name
13+
return new_name
14+
15+
def change_due_date(self, new_due_date):
16+
if new_due_date == self.due_date:
17+
return "Date cannot be the same."
18+
self.due_date = new_due_date
19+
return new_due_date
20+
21+
def add_comment(self, comment):
22+
self.comments.append(comment)
23+
24+
def edit_comment(self, comment_number, new_comment):
25+
try:
26+
self.comments[comment_number] = new_comment
27+
return ', '.join(self.comments)
28+
except IndexError:
29+
return "Cannot find comment."
30+
31+
def details(self):
32+
return f"Name: {self.name} - Due Date: {self.due_date}"
33+

OOP/2.Classes and Objects/Classes and Objects - Exercise/06. Guild System/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from project.player import Player
2+
3+
4+
class Guild:
5+
6+
def __init__(self, name):
7+
self.name = name
8+
self.players = []
9+
10+
def assign_player(self, player: Player):
11+
if player.guild == "Unaffiliated":
12+
self.players.append(player)
13+
player.guild = self.name
14+
return f"Welcome player {player.name} to the guild {self.name}"
15+
16+
elif player.guild == self.name:
17+
return f"Player {player.name} is already in the guild."
18+
19+
else:
20+
return f"Player {player.name} is in another guild."
21+
22+
def kick_player(self, player_name):
23+
player = [name for name in self.players if name.name == player_name]
24+
25+
if player:
26+
player[0].guild = "Unaffiliated"
27+
self.players.remove(player[0])
28+
return f"Player {player_name} has been removed from the guild."
29+
30+
return f"Player {player_name} is not in the guild."
31+
32+
def guild_info(self):
33+
players = '\n'.join([player.player_info() for player in self.players])
34+
return f"Guild: {self.name}\n{players}"
35+
36+
37+
# player = Player("George", 50, 100)
38+
# print(player.add_skill("Shield Break", 20))
39+
# print(player.player_info())
40+
# guild = Guild("UGT")
41+
# print(guild.assign_player(player))
42+
# print(guild.guild_info())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Player:
2+
3+
def __init__(self, name, hp, mp):
4+
self.name = name
5+
self.hp = hp
6+
self.mp = mp
7+
self.skills = {}
8+
self.guild = "Unaffiliated"
9+
10+
def add_skill(self, skill, mana):
11+
if skill not in self.skills.keys():
12+
self.skills[skill] = mana
13+
return f"Skill {skill} added to the collection of the player {self.name}"
14+
return "Skill already added"
15+
16+
def player_info(self):
17+
skills = '\n'.join([f"==={skill} - {mana}" for skill, mana in self.skills.items()])
18+
return f"Name: {self.name}\nGuild: {self.guild}\nHP: {self.hp}\nMP: {self.mp}\n{skills}"
19+

OOP/2.Classes and Objects/Classes and Objects - Exercise/07. Spoopify/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from project.song import Song
2+
3+
4+
class Album:
5+
6+
def __init__(self, name, *songs):
7+
self.name = name
8+
self.published = False
9+
self.songs = list(songs)
10+
11+
def add_song(self, song: Song):
12+
if song.single:
13+
return f"Cannot add {song.name}. It's a single"
14+
15+
elif self.published:
16+
return "Cannot add songs. Album is published."
17+
18+
elif song not in self.songs:
19+
self.songs.append(song)
20+
return f"Song {song.name} has been added to the album {self.name}."
21+
22+
else:
23+
return "Song is already in the album."
24+
25+
def remove_song(self, song_name):
26+
if song_name not in [song.name for song in self.songs]:
27+
return "Song is not in the album."
28+
29+
elif self.published:
30+
return "Cannot remove songs. Album is published."
31+
32+
self.songs = [song for song in self.songs if song.name != song_name]
33+
return f"Removed song {song_name} from album {self.name}."
34+
35+
def publish(self):
36+
if not self.published:
37+
self.published = True
38+
return f"Album {self.name} has been published."
39+
return f"Album {self.name} is already published."
40+
41+
def details(self):
42+
songs = '\n'.join([f"== {song.get_info()}" for song in self.songs])
43+
return f"Album {self.name}\n{songs}"

0 commit comments

Comments
 (0)