You need to enable JavaScript to run this app. Java医护预约系统如何为doctor类添加预约创建功能
优惠活动
大模型
产品
解决方案
定价
更多

Java医护预约系统如何为doctor类添加预约创建功能

医护预约登记功能实现方案

先说明两处可优化的基础问题:

  • Java类名建议遵循大驼峰命名规范,原有doctor类改为Doctor,record类改为Record
  • 原record类的printInfo方法中存在单词拼写错误Treatement,可修正为Treatment
  • 原doctor类中定义的LocalDate date、LocalTime time成员变量无实际作用,可直接删除

1. 修改Doctor类,绑定专属预约列表

核心实现逻辑:在Doctor类内部新增ArrayList<Record>类型的私有成员变量,专门存储当前医护人员的所有预约记录,同时新增预约添加、时间冲突校验、预约查询相关方法。修改后的代码如下:

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;

/**
* 医护人员类
* @author (your name)
* @version (a version number or a date)
*/
public class Doctor
{
  // 基础属性
  private String name;
  private String profession;
  private String office;
  // 新增:当前医护人员专属预约列表
  private ArrayList<Record> appointments;
 
  /**
   * 构造方法
   */
  public Doctor(String name,String profession,String office)
  {
      this.name = name;
      this.profession = profession;
      this.office = office;
      // 初始化预约列表
      this.appointments = new ArrayList<>();
  }

  public String getName()
  {
      return name;
  }

  public String getProfession()
  {
      return profession;
  }

  public String getOffice()
  {
      return office;
  }
  
  // 新增:添加预约,自带时间冲突校验,添加成功返回true,冲突返回false
  public boolean addAppointment(Record newAppointment) {
      LocalDate newDate = newAppointment.getDate();
      LocalTime newStart = newAppointment.getStartTime();
      LocalTime newEnd = newAppointment.getEndTime();
      
      // 遍历已有预约判断时间冲突
      for (Record existAppo : appointments) {
          // 仅需校验同一天的预约
          if (existAppo.getDate().equals(newDate)) {
              LocalTime existStart = existAppo.getStartTime();
              LocalTime existEnd = existAppo.getEndTime();
              // 判断时间是否重叠:新预约开始时间在已有预约区间内 或 新预约结束时间在已有预约区间内
              if ((newStart.isAfter(existStart) && newStart.isBefore(existEnd)) 
                  || (newEnd.isAfter(existStart) && newEnd.isBefore(existEnd))
                  || newStart.equals(existStart)) {
                  // 时间冲突,添加失败
                  return false;
              }
          }
      }
      // 无冲突,添加预约
      appointments.add(newAppointment);
      return true;
  }
  
  // 新增:获取当前医护人员所有预约
  public ArrayList<Record> getAllAppointments() {
      return appointments;
  }
  
  // 新增:打印所有预约信息
  public void printAllAppointments() {
      System.out.println(name + "的预约记录如下:");
      for (Record appo : appointments) {
          System.out.println(appo.printInfo());
      }
  }

  public String printInfo()
  {
      return "name: " + name +"; profession: "+profession + "; office: "+office;
  }
}

2. 功能使用示例

你可以直接通过Doctor类的实例调用addAppointment方法添加专属预约,使用示例代码如下:

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;

public class TestAppointment {
    public static void main(String[] args) {
        // 你原有存储所有医护人员的列表
        ArrayList<Doctor> hold = new ArrayList<>();
        
        // 创建医护人员对象并加入列表
        Doctor drLi = new Doctor("李华", "外科", "外科2诊室");
        hold.add(drLi);
        
        // 创建预约记录
        Record appo1 = new Record("伤口换药", LocalDate.of(2024, 6, 1), LocalTime.of(10, 30), LocalTime.of(10, 0));
        Record appo2 = new Record("术后复查", LocalDate.of(2024, 6, 1), LocalTime.of(11, 30), LocalTime.of(11, 0));
        
        // 给李华添加预约
        drLi.addAppointment(appo1);
        drLi.addAppointment(appo2);
        
        // 打印医护人员信息和对应预约
        System.out.println(drLi.printInfo());
        drLi.printAllAppointments();
    }
}

如果你不需要时间冲突校验,直接删除addAppointment方法里的冲突判断逻辑,直接执行appointments.add(newAppointment)返回true即可。

内容的提问来源于stack exchange,提问作者Casper Josiah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 17:15:05