|
| 1 | +from dataclasses import dataclass |
| 2 | +from enum import Enum |
| 3 | +from typing import List |
| 4 | + |
| 5 | +class OperatingSystem(Enum): |
| 6 | + MACOS = "macOS" |
| 7 | + ARCH = "Arch Linux" |
| 8 | + UBUNTU = "Ubuntu" |
| 9 | + |
| 10 | +@dataclass(frozen=True) |
| 11 | +class Person: |
| 12 | + name: str |
| 13 | + age: int |
| 14 | + preferred_operating_system: OperatingSystem |
| 15 | + |
| 16 | + |
| 17 | +@dataclass(frozen=True) |
| 18 | +class Laptop: |
| 19 | + id: int |
| 20 | + manufacturer: str |
| 21 | + model: str |
| 22 | + screen_size_in_inches: float |
| 23 | + operating_system: OperatingSystem |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: |
| 29 | + possible_laptops = [] |
| 30 | + for laptop in laptops: |
| 31 | + if laptop.operating_system == person.preferred_operating_system: |
| 32 | + possible_laptops.append(laptop) |
| 33 | + return possible_laptops |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +laptops = [ |
| 39 | + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), |
| 40 | + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), |
| 41 | + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), |
| 42 | + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), |
| 43 | +] |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +namePerson = input("enter your personal name: ") |
| 48 | +try: |
| 49 | + agePerson = input("enter your age: ") |
| 50 | +except ValueError: |
| 51 | + print("Invalid age. Age must be a number.") |
| 52 | + exit() |
| 53 | +try: |
| 54 | + preferredOS = input("enter your preferred operating system: ") |
| 55 | +except KeyError: |
| 56 | + print("Invalid operating system entered. Please choose from: MACOS, ARCH, UBUNTU.") |
| 57 | + exit() |
| 58 | + |
| 59 | +person=Person(name=namePerson, age=int(agePerson), preferred_operating_system=OperatingSystem[preferredOS]) |
| 60 | +possible_laptops = find_possible_laptops(laptops, person) |
| 61 | +print(f"Possible laptops for {person.name}: {possible_laptops}") |
| 62 | + |
| 63 | + |
| 64 | + |
0 commit comments