以下是一个简单的图书馆志愿者值班、换班、调班、请假的Python代码示例:

import csv

class Volunteer:
    def __init__(self, name, id):
        self.name = name
        self.id = id

class Shift:
    def __init__(self, date, time, volunteer):
        self.date = date
        self.time = time
        self.volunteer = volunteer

class LibrarySchedule:
    def __init__(self):
        self.volunteers = []
        self.shifts = []
        self.load_volunteers()
        self.load_shifts()

    def load_volunteers(self):
        with open('volunteers.csv', 'r') as f:
            reader = csv.reader(f)
            for row in reader:
                volunteer = Volunteer(row[0], row[1])
                self.volunteers.append(volunteer)

    def load_shifts(self):
        with open('shifts.csv', 'r') as f:
            reader = csv.reader(f)
            for row in reader:
                volunteer = self.find_volunteer(row[2])
                shift = Shift(row[0], row[1], volunteer)
                self.shifts.append(shift)

    def find_volunteer(self, id):
        for volunteer in self.volunteers:
            if volunteer.id == id:
                return volunteer
        return None

    def display_schedule(self):
        print('{:<10}{:<10}{:<10}'.format('Date', 'Time', 'Volunteer'))
        for shift in self.shifts:
            print('{:<10}{:<10}{:<10}'.format(shift.date, shift.time, shift.volunteer.name))

    def request_swap_shift(self, volunteer1, volunteer2, date, time):
        shift1 = self.find_shift(volunteer1, date, time)
        shift2 = self.find_shift(volunteer2, date, time)
        if shift1 is None or shift2 is None:
            print('Error: One or both of the shifts do not exist')
            return
        if shift1.volunteer != volunteer1 or shift2.volunteer != volunteer2:
            print('Error: One or both of the shifts do not belong to the specified volunteers')
            return
        shift1.volunteer = volunteer2
        shift2.volunteer = volunteer1
        print('Shifts successfully swapped')

    def request_change_shift(self, volunteer, date, time):
        shift = self.find_shift(volunteer, date, time)
        if shift is None:
            print('Error: Shift does not exist')
            return
        new_volunteer_id = input('Enter the ID of the new volunteer: ')
        new_volunteer = self.find_volunteer(new_volunteer_id)
        if new_volunteer is None:
            print('Error: Volunteer does not exist')
            return
        shift.volunteer = new_volunteer
        print('Shift successfully changed')

    def request_leave(self, volunteer, date, time):
        shift = self.find_shift(volunteer, date, time)
        if shift is None:
            print('Error: Shift does not exist')
            return
        shift.volunteer = None
        print('Leave request submitted')

    def find_shift(self, volunteer, date, time):
        for shift in self.shifts:
            if shift.volunteer == volunteer and shift.date == date and shift.time == time:
                return shift
        return None

    def save_shifts(self):
        with open('shifts.csv', 'w') as f:
            writer = csv.writer(f)
            for shift in self.shifts:
                writer.writerow([shift.date, shift.time, shift.volunteer.id])

schedule = LibrarySchedule()

while True:
    print('1. Display schedule')
    print('2. Request to swap shifts')
    print('3. Request to change shift')
    print('4. Request leave')
    print('5. Save schedule')
    print('6. Quit')
    choice = input('Enter your choice: ')

    if choice == '1':
        schedule.display_schedule()

    elif choice == '2':
        volunteer1_id = input('Enter the ID of the first volunteer: ')
        volunteer1 = schedule.find_volunteer(volunteer1_id)
        if volunteer1 is None:
            print('Error: Volunteer does not exist')
            continue
        volunteer2_id = input('Enter the ID of the second volunteer: ')
        volunteer2 = schedule.find_volunteer(volunteer2_id)
        if volunteer2 is None:
            print('Error: Volunteer does not exist')
            continue
        date = input('Enter the date of the shift (YYYY-MM-DD): ')
        time = input('Enter the time of the shift (HH:MM): ')
        schedule.request_swap_shift(volunteer1, volunteer2, date, time)

    elif choice == '3':
        volunteer_id = input('Enter your ID: ')
        volunteer = schedule.find_volunteer(volunteer_id)
        if volunteer is None:
            print('Error: Volunteer does not exist')
            continue
        date = input('Enter the date of the shift (YYYY-MM-DD): ')
        time = input('Enter the time of the shift (HH:MM): ')
        schedule.request_change_shift(volunteer, date, time)

    elif choice == '4':
        volunteer_id = input('Enter your ID: ')
        volunteer = schedule.find_volunteer(volunteer_id)
        if volunteer is None:
            print('Error: Volunteer does not exist')
            continue
        date = input('Enter the date of the shift (YYYY-MM-DD): ')
        time = input('Enter the time of the shift (HH:MM): ')
        schedule.request_leave(volunteer, date, time)

    elif choice == '5':
        schedule.save_shifts()

    elif choice == '6':
        break

    else:
        print('Error: Invalid choice')

这个代码使用了CSV文件来存储志愿者列表和值班表。您需要在同一个目录下创建两个CSV文件:volunteers.csvshifts.csvvolunteers.csv应该包含两列,第一列是志愿者的姓名,第二列是志愿者的ID。shifts.csv应该包含三列,第一列是日期,第二列是时间,第三列是值班志愿者的ID。代码会自动加载这两个文件并创建相应的志愿者和班次对象。您可以通过调用display_schedule()方法来显示当前的值班表。

代码还提供了三个方法来请求交换班次,更改班次和请假。这些方法需要输入志愿者的ID、日期和时间。如果请求成功,班次表将被更新,如果请求失败,则会输出错误消息。

最后,代码还提供了方法来将更新后的班次表保存回CSV文件中。您可以选择在退出程序之前保存班次表。


原文地址: https://www.cveoy.top/t/topic/b7zX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录