Відповідь:
class Worker: def __init__(self, name, position, salary): self.name = name self.position = position self.salary = salary self.office = None def assign_office(self, office): self.office = office def __str__(self): return f"{self.name} ({self.position}) - ${self.salary}" class Office: def __init__(self, location, size): self.location = location self.size = size self.workers = [] def add_worker(self, worker): self.workers.append(worker) worker.assign_office(self) def get_workers(self): return self.workers def __str__(self): return f"{self.location} ({self.size} sq. ft.) - {len(self.workers)} workers" if __name__ == "__main__": office1 = Office("New York", 1000) office2 = Office("London", 800) worker1 = Worker("John", "Manager", 70000) worker2 = Worker("Jane", "Developer", 50000) worker3 = Worker("Jack", "Designer", 60000) office1.add_worker(worker1) office2.add_worker(worker2) office2.add_worker(worker3) offices = [office1, office2] print("Offices:") for office in offices: print(f"\\t{office}") print("\\t\\tWorkers:") for worker in office.get_workers(): print(f"\\t\\t\\t{worker}")
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Відповідь:
class Worker: def __init__(self, name, position, salary): self.name = name self.position = position self.salary = salary self.office = None def assign_office(self, office): self.office = office def __str__(self): return f"{self.name} ({self.position}) - ${self.salary}" class Office: def __init__(self, location, size): self.location = location self.size = size self.workers = [] def add_worker(self, worker): self.workers.append(worker) worker.assign_office(self) def get_workers(self): return self.workers def __str__(self): return f"{self.location} ({self.size} sq. ft.) - {len(self.workers)} workers" if __name__ == "__main__": office1 = Office("New York", 1000) office2 = Office("London", 800) worker1 = Worker("John", "Manager", 70000) worker2 = Worker("Jane", "Developer", 50000) worker3 = Worker("Jack", "Designer", 60000) office1.add_worker(worker1) office2.add_worker(worker2) office2.add_worker(worker3) offices = [office1, office2] print("Offices:") for office in offices: print(f"\\t{office}") print("\\t\\tWorkers:") for worker in office.get_workers(): print(f"\\t\\t\\t{worker}")