请给出实现python实验室管理系统
时间: 2023-12-16 21:04:30 浏览: 144
实验室管理系统的设计与实现
以下是一个简单的Python实验室管理系统的实现:
1. 首先,我们需要定义一个实验室类,包含实验室的名称、容量和使用状态:
```python
class Lab:
def __init__(self, name, capacity):
self.name = name
self.capacity = capacity
self.is_used = False
```
2. 接下来,我们需要定义一个实验室管理系统类,包含实验室列表、学生列表和预约列表:
```python
class LabManagementSystem:
def __init__(self):
self.labs = []
self.students = []
self.reservations = []
```
3. 然后,我们需要定义一些方法来添加实验室、学生和预约:
```python
def add_lab(self, lab):
self.labs.append(lab)
def add_student(self, student):
self.students.append(student)
def add_reservation(self, reservation):
self.reservations.append(reservation)
```
4. 接下来,我们需要实现一些方法来显示实验室、学生和预约的列表:
```python
def show_labs(self):
for lab in self.labs:
print(f"{lab.name} ({lab.capacity} seats) - {'Used' if lab.is_used else 'Available'}")
def show_students(self):
for student in self.students:
print(f"{student.name} ({student.id})")
def show_reservations(self):
for reservation in self.reservations:
print(f"{reservation.student.name} ({reservation.lab.name}) - {reservation.date} ({reservation.time})")
```
5. 最后,我们需要实现一些方法来预约实验室:
```python
def reserve_lab(self, student, lab, date, time):
for reservation in self.reservations:
if reservation.lab == lab and reservation.date == date and reservation.time == time:
print("Sorry, this lab is already reserved at this time.")
return
if lab.is_used:
print("Sorry, this lab is already in use.")
return
lab.is_used = True
reservation = Reservation(student, lab, date, time)
self.add_reservation(reservation)
print("Reservation successful.")
class Student:
def __init__(self, name, id):
self.name = name
self.id = id
class Reservation:
def __init__(self, student, lab, date, time):
self.student = student
self.lab = lab
self.date = date
self.time = time
```
完整代码如下:
```python
class Lab:
def __init__(self, name, capacity):
self.name = name
self.capacity = capacity
self.is_used = False
class Student:
def __init__(self, name, id):
self.name = name
self.id = id
class Reservation:
def __init__(self, student, lab, date, time):
self.student = student
self.lab = lab
self.date = date
self.time = time
class LabManagementSystem:
def __init__(self):
self.labs = []
self.students = []
self.reservations = []
def add_lab(self, lab):
self.labs.append(lab)
def add_student(self, student):
self.students.append(student)
def add_reservation(self, reservation):
self.reservations.append(reservation)
def show_labs(self):
for lab in self.labs:
print(f"{lab.name} ({lab.capacity} seats) - {'Used' if lab.is_used else 'Available'}")
def show_students(self):
for student in self.students:
print(f"{student.name} ({student.id})")
def show_reservations(self):
for reservation in self.reservations:
print(f"{reservation.student.name} ({reservation.lab.name}) - {reservation.date} ({reservation.time})")
def reserve_lab(self, student, lab, date, time):
for reservation in self.reservations:
if reservation.lab == lab and reservation.date == date and reservation.time == time:
print("Sorry, this lab is already reserved at this time.")
return
if lab.is_used:
print("Sorry, this lab is already in use.")
return
lab.is_used = True
reservation = Reservation(student, lab, date, time)
self.add_reservation(reservation)
print("Reservation successful.")
system = LabManagementSystem()
# add some labs
system.add_lab(Lab("Lab 1", 20))
system.add_lab(Lab("Lab 2", 30))
system.add_lab(Lab("Lab 3", 40))
# add some students
system.add_student(Student("Alice", "A001"))
system.add_student(Student("Bob", "B002"))
system.add_student(Student("Charlie", "C003"))
# reserve some labs
system.reserve_lab(system.students[0], system.labs[0], "2022-01-01", "09:00")
system.reserve_lab(system.students[1], system.labs[1], "2022-01-01", "10:00")
system.reserve_lab(system.students[2], system.labs[2], "2022-01-01", "11:00")
# show lab, student and reservation lists
system.show_labs()
system.show_students()
system.show_reservations()
```
以上代码可实现一个简单的实验室管理系统,具有添加实验室、学生和预约、显示实验室、学生和预约列表、预约实验室等功能。
阅读全文