-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dormitory.py
68 lines (53 loc) · 2.06 KB
/
Dormitory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Student:
def __init__(self, name, age, year):
self.name = name
self.age = age
self.year = year
self.students_room = {}
def student_room(self, name, room_number):
self.students_room[name] = room_number
print(f"{name} is in {room_number}th room")
class Room:
def __init__(self, number_of_room, capacity):
self.number_of_room = number_of_room
self.capacity = capacity
self.students_in_room = []
def has_space(self):
return len(self.students_in_room) < self.capacity
def add_student(self, student_name):
if self.has_space():
self.students_in_room.append(student_name)
print(f"{student_name} added to room {self.number_of_room}")
else:
print(f"Room {self.number_of_room} is full.")
class Dormitory:
def __init__(self):
self.all_rooms = {}
def add_room(self, room_number, capacity):
if room_number not in self.all_rooms:
self.all_rooms[room_number] = Room(room_number, capacity)
print(f"Room {room_number} added with a capacity of {capacity}.")
else:
print(f"Room {room_number} already exists.")
def assign_student(self, student_name):
for room in self.all_rooms.values():
if room.has_space():
room.add_student(student_name)
return
print(f"No available rooms for {student_name}. All rooms are full.")
def calculate_total_rooms(self):
return len(self.all_rooms)
def show_room_status(self):
for room_number, room in self.all_rooms.items():
print(f"Room {room_number}: {len(room.students_in_room)}/{room.capacity} students")
print(f"Students: {room.students_in_room}")
dorm = Dormitory()
dorm.add_room(101, 2)
dorm.add_room(102, 3)
dorm.assign_student("Alice")
dorm.assign_student("Bob")
dorm.assign_student("Charlie")
dorm.assign_student("David")
dorm.assign_student("Eve")
dorm.show_room_status()
print(f"Total rooms in the dormitory: {dorm.calculate_total_rooms()}")