diff --git a/level_1/a_user_instance.py b/level_1/a_user_instance.py index e5d0368..71a7ac9 100644 --- a/level_1/a_user_instance.py +++ b/level_1/a_user_instance.py @@ -11,8 +11,8 @@ def __init__(self, name: str, username: str, age: int, phone: str): self.username = username self.age = age self.phone = phone - - + def __str__(self): + return f'Информация о пользователе: {self.name}, {self.username}, {self.age}, {self.phone}' if __name__ == '__main__': - pass # код писать тут - + user = User("Artem", "maharadze", 19, "89162918051") + print(user) diff --git a/level_1/b_student_full_name_method.py b/level_1/b_student_full_name_method.py index 14ec439..2a59c38 100644 --- a/level_1/b_student_full_name_method.py +++ b/level_1/b_student_full_name_method.py @@ -5,7 +5,6 @@ 3. Положите результат вызова метода get_full_name в переменную и распечатайте ее. """ - class Student: def __init__(self, name: str, surname: str, faculty: str, course: int): self.name = name @@ -18,5 +17,6 @@ def get_full_name(self): if __name__ == '__main__': - pass # код писать тут + student = Student("Artem", "Nikiforov", "DevOps", 1) + print(student.get_full_name()) diff --git a/level_1/c_product_class.py b/level_1/c_product_class.py index 3952b66..8292d09 100644 --- a/level_1/c_product_class.py +++ b/level_1/c_product_class.py @@ -9,8 +9,15 @@ class Product: - pass # код писать тут + def __init__(self, name: str, description: str, price: int | str, weight: int): + self.name = name + self.description = description + self.price = price + self.weight = weight + def get_info_product(self): + return f'Информация о продукте: {self.name}, {self.description}, {self.price} р., {self.weight} гр.' if __name__ == '__main__': - pass # код писать тут + product = Product("Bananas", "They are yellow, from tropical countries, very tastye", 30, 100) + print(product.get_info_product()) diff --git a/level_1/d_bank_account_increase_balance.py b/level_1/d_bank_account_increase_balance.py index cc7a16c..cb769b9 100644 --- a/level_1/d_bank_account_increase_balance.py +++ b/level_1/d_bank_account_increase_balance.py @@ -8,15 +8,17 @@ 3. Увеличьте баланс счета у экземпляра класса с помощью метода increase_balance и снова распечатайте текущий баланс. """ - class BankAccount: def __init__(self, owner_full_name: str, balance: float): self.owner_full_name = owner_full_name self.balance = balance - + def balance_bank(self): + return self.balance def increase_balance(self, income: float): - pass # код писать тут - + self.income = income + return self.balance + self.income if __name__ == '__main__': - pass # код писать тут + bank = BankAccount("John", 10000.54) + print(bank.balance_bank()) + print(bank.increase_balance(5252.24)) diff --git a/level_1/e_bank_account_decrease_balance.py b/level_1/e_bank_account_decrease_balance.py index dfd4586..6558cd5 100644 --- a/level_1/e_bank_account_decrease_balance.py +++ b/level_1/e_bank_account_decrease_balance.py @@ -10,8 +10,28 @@ class BankAccount: - pass # код писать тут + def __init__(self, owner_full_name: str, balance: float): + self.owner_full_name = owner_full_name + self.balance = balance + + def balance_bank(self): + return self.balance + + def increase_balance(self, income: float): + self.income = income + return self.balance + self.income + + def reduce_balance(self, reduce: float): + self.reduce = reduce + current_balance = self.balance - self.reduce + if current_balance < 0: + raise ValueError('Отрицательный баланс!') + return current_balance if __name__ == '__main__': - pass # код писать тут + bank = BankAccount("John", 10000.54) + print(bank.balance_bank()) + print(bank.increase_balance(5252.24)) + print(bank.reduce_balance(3254.56)) + print(bank.reduce_balance(15524.55)) diff --git a/level_2/a_user_from_functions_to_class.py b/level_2/a_user_from_functions_to_class.py index 18a64ce..f3f1b93 100644 --- a/level_2/a_user_from_functions_to_class.py +++ b/level_2/a_user_from_functions_to_class.py @@ -4,15 +4,16 @@ Задания: 1. Создайте класс User и перенесите всю логику работы с пользователем туда. """ - - -def make_username_capitalized(username: str): - return username.capitalize() - - -def generate_short_user_description(username: str, user_id: int, name: str): - return f'User with id {user_id} has {username} username and {name} name' - - class User: - pass # код писать тут + def __init__(self, username: str, user_id: int, name: str): + self.username = username + self.user_id = user_id + self.name = name + def make_username_capitalized(self): + return self.username.capitalize() + + def generate_short_user_description(self): + return f'User with id {self.user_id} has {self.username} username and {self.name} name' +user = User("Maharadze", "@dsgdsag", "Artem") +print(user.make_username_capitalized()) +print(user.generate_short_user_description()) diff --git a/level_2/b_user_should_be_banned.py b/level_2/b_user_should_be_banned.py index 3ec9359..7d8412c 100644 --- a/level_2/b_user_should_be_banned.py +++ b/level_2/b_user_should_be_banned.py @@ -11,4 +11,13 @@ class User: - pass # код писать тут + def __init__(self, name, surname, age): + self.name = name + self.surname = surname + self.age = age + def should_be_banned(self): + if self.surname in SURNAMES_TO_BAN: + return f"Пользователь {self.surname} {self.name} забанен" + +user = User("Artem", "Vaughn", 19) +print(user.should_be_banned()) diff --git a/level_3/a_credit_bank_account.py b/level_3/a_credit_bank_account.py index b73657a..cfb9f8a 100644 --- a/level_3/a_credit_bank_account.py +++ b/level_3/a_credit_bank_account.py @@ -9,9 +9,7 @@ """ # код писать тут - - -class CreditAccount: +class BankAccount: def __init__(self, owner_full_name: str, balance: float): self.owner_full_name = owner_full_name self.balance = balance @@ -22,10 +20,21 @@ def increase_balance(self, amount: float): def decrease_balance(self, amount: float): self.balance -= amount +class CreditAccount(BankAccount): + def __init__(self, owner_full_name: str, balance: float): + super().__init__(owner_full_name, balance) + def increase_balance(self, amount: float): + super().increase_balance(amount) + return self.balance + def decrease_balance(self, amount: float): + super().decrease_balance(amount) + return self.balance def is_eligible_for_credit(self): return self.balance > 1000 if __name__ == '__main__': - pass # код писать тут - + user = CreditAccount("Artem", 10525.25) + print(user.increase_balance(1000)) + print(user.decrease_balance(5000)) + print(user.is_eligible_for_credit()) diff --git a/level_3/b_user_manage.py b/level_3/b_user_manage.py index 6417c77..fc2c2e4 100644 --- a/level_3/b_user_manage.py +++ b/level_3/b_user_manage.py @@ -22,9 +22,43 @@ def get_users(self): return self.usernames -# код писать тут +class AdminManager(UserManager): + def __init__(self): + super().__init__() + def get_users(self): + return super().get_users() + def ban_username(self, username): + if username in self.usernames: + self.usernames.remove(username) + else: + print('Такого пользователя не существует') + +class SuperAdminManager(AdminManager): + def __init__(self): + super().__init__() + def ban_all_users(self): + return self.usernames.clear() + def get_users(self): + return super().get_users() if __name__ == '__main__': - pass # код писать тут + user = UserManager() + user.add_user("anna") + user.add_user("artem") + user.add_user("ivan") + print(user.get_users()) + + admin = AdminManager() + admin.add_user('anna') + admin.add_user('gleb') + admin.add_user('ivan') + admin.ban_username('anna') + print(admin.get_users()) + manager = SuperAdminManager() + manager.add_user('anna') + manager.add_user('ivan') + manager.add_user('artem') + manager.ban_all_users() + print(manager.get_users()) diff --git a/level_3/c_text_processor.py b/level_3/c_text_processor.py index d9aed49..96233bf 100644 --- a/level_3/c_text_processor.py +++ b/level_3/c_text_processor.py @@ -20,8 +20,21 @@ def summarize(self): return f'Total text length: {len(self.text)}' -# код писать тут - +class AdvancedTextProcessor(TextProcessor): + def __init__(self, text): + super().__init__(text) + def to_upper(self): + return super().to_upper() + def summarize(self): + words = self.text.split() + count = len(words) + return f'Total text length: {len(self.text)}, total number of words in the text: {count}' if __name__ == '__main__': - pass # код писать тут + texts = TextProcessor('Total text length: 67, total number of words in the text: 10') + print(texts.to_upper()) + print(texts.summarize()) + + advance = AdvancedTextProcessor('Total text length: 67, total number of words in the text: 10') + print(advance.to_upper()) + print(advance.summarize()) diff --git a/level_3/d_alcohol_product.py b/level_3/d_alcohol_product.py index 49065b1..14871ea 100644 --- a/level_3/d_alcohol_product.py +++ b/level_3/d_alcohol_product.py @@ -1,14 +1,9 @@ -""" -У нас есть класс Product, который содержит в себе информацию о продукте. -Еще у нас есть класс AlcoholProduct, но метод is_available для него не подходит, так как -алкоголь нельзя продавать с 5 утра до 11 вечера - -Задания: +'''Задания: 1. Переопределите метод is_available в классе AlcoholProduct с использованием super() 2. is_available у AlcoholProduct должен возвращать False если сейчас часы между 5 утра и 11 вечера. Для определения текущего часа можно использовать datetime.now().hour 3. Создайте экземпляр класса AlcoholProduct и проверьте, можно ли сейчас продавать алкоголь. -""" +''' from datetime import datetime @@ -26,8 +21,20 @@ def is_available(self): class AlcoholProduct(Product): - pass # код писать тут + def __init__(self, title, price, stock_quantity): + super().__init__(title, price, stock_quantity) + def is_available(self): + super().is_available() + if 5 < datetime.now().hour < 24: + return False + else: + return f'Пейте на здоровье' if __name__ == '__main__': - pass # код писать тут + product = Product('teqila', 1000, 15) + print(product.get_discounted_price(0.75)) + print(product.is_available()) + + alco = AlcoholProduct('teqila', 1000, 15) + print(alco.is_available()) diff --git a/level_4/a_file_handler.py b/level_4/a_file_handler.py index c05ce7d..1856479 100644 --- a/level_4/a_file_handler.py +++ b/level_4/a_file_handler.py @@ -21,17 +21,37 @@ def __init__(self, filename): self.filename = filename def read(self): - with open(self.filename, 'r') as file: + with open(self.filename, 'r', encoding='utf-8') as file: return file.read() class JSONHandler(FileHandler): - pass # код писать тут + def __init__(self, filename): + super().__init__(filename) + def read(self): + with open(self.filename, 'r') as file: + return json.load(file) + + class CSVHandler(FileHandler): - pass # код писать тут + def __init__(self, filename): + super().__init__(filename) + def read(self): + with open(self.filename, 'r', encoding='utf-8') as file: + csv_reader = csv.DictReader(file) + data = [row for row in csv_reader] + return data + if __name__ == '__main__': - pass # код писать тут + hand = FileHandler('text.txt') + print(hand.read()) + + jsn = JSONHandler('recipes.json') + print(jsn.read()) + + cs = CSVHandler('user_info.csv') + print(cs.read()) diff --git a/level_4/b_food_product.py b/level_4/b_food_product.py index 993325a..50b63a2 100644 --- a/level_4/b_food_product.py +++ b/level_4/b_food_product.py @@ -26,8 +26,25 @@ def is_available(self): class FoodProduct(Product): - pass # код писать тут + def __init__(self, title, quantity, expiration_date): + super().__init__(title, quantity) + self.expiration_date = expiration_date + + def get_full_info(self): + return super().get_full_info() + + def is_available(self): + today = datetime.today() + if self.expiration_date < today.strftime('%d %m %Y'): + return False, super().is_available() + if __name__ == '__main__': - pass # код писать тут + prod = Product('milk', 15) + print(prod.get_full_info()) + print(prod.is_available()) + + food = FoodProduct('yogurt', 10, '28 03 2026') + print(food.get_full_info()) + print(food.is_available())