Advertisement
ALEXANDAR_GEORGIEV

oop_2

Jun 9th, 2023 (edited)
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. # Cup
  2. def say_hello():
  3.     return
  4.  
  5.  
  6. class Cup:
  7.     def __init__(self, size, quantity):
  8.         self.size = size
  9.         self.quantity = quantity
  10.  
  11.     def fill(self, quantity) -> None:
  12.         if self.quantity + quantity <= self.size:
  13.             self.quantity += quantity
  14.  
  15.  
  16.     def status(self) -> int:
  17.         return self.size - self.quantity
  18.  
  19.  
  20. # Employee
  21. class Employee:
  22.     MONTHS = 12
  23.  
  24.     def __init__(self, employee_id, first_name, last_name, salary):
  25.         self.id = employee_id
  26.         self.first_name = first_name
  27.         self.last_name = last_name
  28.         self. salary = salary
  29.  
  30.     def get_full_name(self) -> str:
  31.         return f"{self.first_name} {self.last_name}"
  32.  
  33.     def get_annual_salary(self) -> float:
  34.         return self.salary * Employee.MONTHS    # MONTHS не принадлежи на инит, а на класа
  35.  
  36.     def get_raise_salary(self, amount: float) -> float:
  37.         self.salary += amount
  38.  
  39.         return self.salary
  40.  
  41. employee = Employee(744423129, "John", "Smith", 1000)
  42.  
  43. #flower
  44. class Flower:
  45.     def __init__(self, name: str, water_requirements):
  46.         self.name = name
  47.         self.water_requirements = water_requirements
  48.         self.is_happy = False
  49.  
  50.     def water(self, quantity) -> None:
  51.         if quantity >= self.water_requirements:
  52.             self.is_happy = True
  53.  
  54.     def status(self) -> str:
  55.  
  56.         return f"{self.name} is {'' if self.is_happy else 'not '}happy"
  57.  
  58. # hero
  59. class Hero:
  60.     def __init__(self, name: str, health: int):
  61.         self.name = name
  62.         self.health = health
  63.  
  64.     def defend(self, damage: int) -> None or str:   # Ще върне или None или string
  65.         self.health -= damage
  66.  
  67.         if self.health <= 0:
  68.             self.health = 0
  69.             return f"{self.name} was defeated"
  70.  
  71.     def heal(self, amount: int) -> None: # -> None описва че връща None
  72.         self.health += amount
  73.  
  74. hero = Hero("Peter", 100 )
  75. print(hero.defend(50))
  76. hero.heal(50)
  77.  
  78. # Programer
  79. class Programmer:
  80.     def __init__(self, name, language, skills):
  81.         self.name = name
  82.         self.language = language
  83.         self.skills = skills
  84.  
  85.     def watch_course(self, course_name, language, skills_earned):
  86.         if self.language != language:
  87.             return f"{self.name} does not know {language}."
  88.  
  89.         self.skills += skills_earned
  90.  
  91.         return f"{self.name} watched {course_name}"
  92.  
  93.     def change_language(self, new_language, skills_needed):
  94.         if self.skills >= skills_needed:
  95.             if self.language != new_language:
  96.                 previous_language = self.language
  97.                 self.language = new_language
  98.  
  99.                 return f"{self.name} switched from {previous_language} to {new_language}"
  100.  
  101.             return f"{self.name} already knows {new_language}"
  102.  
  103.         return f"{self.name} needs {skills_needed - self.skills} more skills"
  104.  
  105. # Shop
  106. from typing import List
  107.  
  108.  
  109. class Shop:
  110.     def __init__(self, name, items: List[str]):
  111.         self.name = name
  112.         self.items = items
  113.  
  114.     def get_items_count(self):
  115.         return len(self.items)
  116.  
  117.  
  118. shop = Shop("My Shop", ["Apples", "Bananas", "Cucumbers"])
  119. billa = Shop("Bolla", ["Billa stuff", "anything"])
  120.  
  121. print(shop.items)
  122. print(billa.items)
  123. print(shop.get_items_count())
  124. print(billa.get_items_count())
  125.  
  126. # Steam_user
  127. from typing import List
  128.  
  129. class SteamUser:
  130.     def __init__(self, username, games: List[str]):
  131.         self.username = username
  132.         self.games = games
  133.         self.played_hours: int = 0
  134.  
  135.     def play(self, game, hours):
  136.         if game not in self.games:
  137.             return f"{game} is not in library"
  138.  
  139.         self.played_hours += hours
  140.  
  141.         return f"{self.username} is playing {game}"
  142.  
  143.     def buy_game(self, game) -> str:
  144.         if game in self.games:
  145.             return f"{game} is already in your library"
  146.  
  147.         self.games.append(game)
  148.  
  149.         return f"{self.username} bought {game}"
  150.  
  151.     def status(self):
  152.         return f"{self.username} has {len(self.games)} games. Total play time: {self.played_hours}"
  153.  
  154.  
  155.  
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement