|
| 1 | +# -- exercise |
| 2 | +# incorrect |
| 3 | +class HeroI: |
| 4 | + def __init__(self, name, primary_attribute, base_damage, health): |
| 5 | + self.name = name; |
| 6 | + self.primary_attribute = primary_attribute; |
| 7 | + self.base_damage = base_damage; |
| 8 | + self.health = health; |
| 9 | + |
| 10 | + def calculate_damage(self, additional_damage): |
| 11 | + return self.base_damage + additional_damage; |
| 12 | + |
| 13 | + def display_info(self): |
| 14 | + info = f"Hero: {self.name}\n"; |
| 15 | + info += f"Primary Attribute: {self.primary_attribute}\n"; |
| 16 | + info += f"Base Damage: {self.base_damage}\n"; |
| 17 | + info += f"Health: {self.health}"; |
| 18 | + return info; |
| 19 | + |
| 20 | +hero = HeroI("Axe", "Strength", 50, 1000); |
| 21 | +print(hero.display_info()); |
| 22 | +print(f"Damage: {hero.calculate_damage(20)}"); |
| 23 | + |
| 24 | +# correct |
| 25 | +class HeroC: |
| 26 | + def __init__(self, name, primary_attribute, base_damage, health): |
| 27 | + self.name = name; |
| 28 | + self.primary_attribute = primary_attribute; |
| 29 | + self.base_damage = base_damage; |
| 30 | + self.health = health; |
| 31 | + |
| 32 | +class DamageCalculator: |
| 33 | + def calculate_damage(self, hero, additional_damage): |
| 34 | + return hero.base_damage + additional_damage; |
| 35 | + |
| 36 | +class HeroDisplay: |
| 37 | + def display_info(self, hero): |
| 38 | + info = f"Hero: {hero.name}\n" |
| 39 | + info += f"Primary Attribute: {hero.primary_attribute}\n"; |
| 40 | + info += f"Base Damage: {hero.base_damage}\n"; |
| 41 | + info += f"Health: {hero.health}"; |
| 42 | + return info; |
| 43 | + |
| 44 | +print("------------------------------------------------") |
| 45 | +hero = HeroC("Axe", "Strength", 50, 1000); |
| 46 | +damage_calculator = DamageCalculator(); |
| 47 | +hero_display = HeroDisplay(); |
| 48 | + |
| 49 | +print(hero_display.display_info(hero)) |
| 50 | +print(f"Damage: {damage_calculator.calculate_damage(hero, 20)}"); |
| 51 | + |
| 52 | +# -- extra challenge |
| 53 | +# incorrect |
| 54 | +class Library: |
| 55 | + def __init__(self): |
| 56 | + self.books = []; |
| 57 | + self.users = []; |
| 58 | + self.loans = []; |
| 59 | + |
| 60 | + def add_book(self, title, author, copies): |
| 61 | + book = {"title": title, "author": author, "copies": copies}; |
| 62 | + self.books.append(book); |
| 63 | + |
| 64 | + def add_user(self, name, user_id, email): |
| 65 | + user = {"name": name, "user_id": user_id, "email": email}; |
| 66 | + self.users.append(user); |
| 67 | + |
| 68 | + def loan_book(self, user_id, book_title): |
| 69 | + for book in self.books: |
| 70 | + if book["title"] == book_title and book["copies"] > 0: |
| 71 | + self.loans.append({"user_id": user_id, "book_title": book_title}); |
| 72 | + book["copies"] -= 1; |
| 73 | + return True; |
| 74 | + return False; |
| 75 | + |
| 76 | + def return_book(self, user_id, book_title): |
| 77 | + for loan in self.loans: |
| 78 | + if loan["user_id"] == user_id and loan["book_title"] == book_title: |
| 79 | + self.loans.remove(loan); |
| 80 | + for book in self.books: |
| 81 | + if book["title"] == book_title: |
| 82 | + book["copies"] += 1; |
| 83 | + return True; |
| 84 | + return False; |
| 85 | + |
| 86 | +library = Library() |
| 87 | +library.add_book("1996", "Ahmed Khedr", 4); |
| 88 | +library.add_user("Legion Commander", 1, "legioncommander@gmail.com"); |
| 89 | +library.loan_book(1, "1996"); |
| 90 | +library.return_book(1, "1996"); |
| 91 | + |
| 92 | + |
| 93 | +# correct |
| 94 | +class BookManager: |
| 95 | + def __init__(self): |
| 96 | + self.books = []; |
| 97 | + |
| 98 | + def add_book(self, title, author, copies): |
| 99 | + book = {"title": title, "author": author, "copies": copies}; |
| 100 | + self.books.append(book); |
| 101 | + |
| 102 | + def update_copies(self, book_title, copies): |
| 103 | + for book in self.books: |
| 104 | + if book["title"] == book_title: |
| 105 | + book["copies"] += copies; |
| 106 | + return True; |
| 107 | + return False; |
| 108 | + |
| 109 | + def get_book(self, book_title): |
| 110 | + for book in self.books: |
| 111 | + if book["title"] == book_title: |
| 112 | + return book; |
| 113 | + return None; |
| 114 | + |
| 115 | +class UserManager: |
| 116 | + def __init__(self): |
| 117 | + self.users = []; |
| 118 | + |
| 119 | + def add_user(self, name, user_id, email): |
| 120 | + user = {"name": name, "user_id": user_id, "email": email}; |
| 121 | + self.users.append(user); |
| 122 | + |
| 123 | + def get_user(self, user_id): |
| 124 | + for user in self.users: |
| 125 | + if user["user_id"] == user_id: |
| 126 | + return user; |
| 127 | + return None; |
| 128 | + |
| 129 | +class LoanManager: |
| 130 | + def __init__(self, book_manager): |
| 131 | + self.loans = []; |
| 132 | + self.book_manager = book_manager; |
| 133 | + |
| 134 | + def loan_book(self, user_id, book_title): |
| 135 | + book = self.book_manager.get_book(book_title); |
| 136 | + if book and book["copies"] > 0: |
| 137 | + self.loans.append({"user_id": user_id, "book_title": book_title}); |
| 138 | + self.book_manager.update_copies(book_title, -1); |
| 139 | + return True; |
| 140 | + return False; |
| 141 | + |
| 142 | + def return_book(self, user_id, book_title): |
| 143 | + for loan in self.loans: |
| 144 | + if loan["user_id"] == user_id and loan["book_title"] == book_title: |
| 145 | + self.loans.remove(loan); |
| 146 | + self.book_manager.update_copies(book_title, 1); |
| 147 | + return True; |
| 148 | + return False; |
| 149 | + |
| 150 | +# Uso correcto |
| 151 | +book_manager = BookManager(); |
| 152 | +user_manager = UserManager(); |
| 153 | +loan_manager = LoanManager(book_manager); |
| 154 | + |
| 155 | +book_manager.add_book("1996", "Ahmed Khedr", 4); |
| 156 | +user_manager.add_user("Legion Commander", 1, "legioncommander@gmail.com"); |
| 157 | +loan_manager.loan_book(1, "1996"); |
| 158 | +loan_manager.return_book(1, "1996"); |
| 159 | + |
0 commit comments