目录
Arrays类
数组中存储对象时 排序
在要比较的类中public class 类名 implements Comparable <类名>
Comparator
JDK8新特性:Lambda表达式
Lambda表达式的省略写法
方法引用
静态方法引用
实例方法引用
特定类型的方法引用
构造器引用
医院挂号系统
Arrays类
操作数组的工具类
数组中存储对象时 排序
在要比较的类中public class 类名 implements Comparable <类名>
Comparator
JDK8新特性:Lambda表达式
用于简化匿名内部类的代码表示[只能简化函数式接口的匿名内部类]
函数式接口:有且仅有一个抽象方法的接口[可能有@FunctionalInterface的注解]
另一个例子
、
Lambda表达式的省略写法
方法引用
进一步简化Lambda表达式
静态方法引用
类名::静态方法
如果某Lambda表达式里只调用一个静态方法,并且前后参数的形式一致,就可以用静态方法引用
实例方法引用
对象名::实例方法
如果某Lambda表达式里只调用一个实例方法,并且前后参数的形式一致,就可以用实例方法引用
特定类型的方法引用
类型::方法
如果某Lambda表达式里只调用一个实例方法,并且前面参数列表中的第一个参数是作为方法的主调,后面的所有参数都是作为该实例方法的入参的,则此时就可以用特定类型的方法引用。
构造器引用
类名::new
如果某Lambda表达式里只是在创建对象,并且前后参数情况一致,就可以用构造器引用
医院挂号系统
HosipitalManager
package cn.ptz.frame;import cn.ptz.bean.Appointment;
import cn.ptz.bean.Department;
import cn.ptz.bean.Doctor;
import cn.ptz.bean.Schedule;
import jdk.swing.interop.SwingInterOpUtils;import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.UUID;public class HospitalManager {private ArrayList<Department> allDepartments = new ArrayList<>();private ArrayList<Appointment> appointments = new ArrayList<>();private Scanner sc = new Scanner(System.in);public void start() {while (true) {System.out.println("----医院管理系统");System.out.println("1、科室管理-添加科室");System.out.println("2、科室管理-删除科室");System.out.println("3、科室管理-修改科室");System.out.println("4、医生管理-录入医生");System.out.println("5、医生管理-医生坐诊设置");System.out.println("6、医生管理-展示医生坐诊情况");System.out.println("7、医生管理-挂号预约");System.out.println("8、搜索某医生预约情况");System.out.println("请输入操作命令");Scanner sc = new Scanner(System.in);switch (sc.next()) {case "1":addDepartment();break;case "4":addDoctor();break;case "5":setDoctorJob();break;case "6":showAllDoctorInfos();break;case "7":break;case "8":break;default:}}}private void showAllDoctorInfos() {System.out.println("----全部医生坐镇信息如下");for (int i = 0; i < allDepartments.size(); i++) {Department department = allDepartments.get(i);System.out.println((i + 1) + "," + department.getName());System.out.println("-----------------------");ArrayList<Doctor> doctors = department.getDoctors();for (int j = 0; j < doctors.size(); j++) {Doctor doctor = doctors.get(j);System.out.println(doctor.getName() + "的坐诊信息");ArrayList<Schedule> schedules = doctor.getSchedules();updateSchedules(schedules);for (int k = 0; k < schedules.size(); k++) {Schedule schedule = schedules.get(k);System.out.println(schedule.getToday());if (!schedule.isUpdate()){System.out.println("未排班");continue;}if (schedule.isMorning()){System.out.println("上午" + schedule.getMstart() + "---" + schedule.getMend());System.out.println("预约总人数:" + schedule.getmTotalNumber());}else {System.out.println("上午休息");}if (schedule.isAfternoon()){// 同上午}}}}}private void setDoctorJob() {System.out.println("-------医生坐诊时间");Department department = getDepartmentByUser();if (department == null){return;}ArrayList<Doctor> doctors = department.getDoctors();if (doctors.size() == 0){System.out.println("当前无医生");return;}while (true){System.out.println("医生列表如下:");for (int i = 0; i < doctors.size(); i++) {Doctor doctor = doctors.get(i);System.out.println((i + 1) + "、" + doctor.getName());}System.out.println("请输入");int command = sc.nextInt();if (command < 1 || command > doctors.size()){System.out.println("输入有误");continue;}Doctor doctor = doctors.get(command - 1);ArrayList<Schedule> schedules = doctor.getSchedules();updateSchedules(schedules);for (int i = 0; i < schedules.size(); i++) {Schedule schedule = schedules.get(i);updateDoctorSchedule(schedule);}}}private void updateDoctorSchedule(Schedule schedule) {LocalDate today = schedule.getToday();System.out.println(today + "的安排如下");if (!schedule.isUpdate()) {System.out.println("未排班");} else {System.out.println("上午");if (schedule.isMorning()) {System.out.println();} else {System.out.println("休息");}System.out.println("下午");if (schedule.isAfternoon()) {System.out.println();} else {System.out.println("休息");}}System.out.println("是否修改 y/n");String rs = sc.next();if ("y".equals(rs)) {schedule.setUpdate(true);System.out.println("上午是否上班 y/n");String rs2 = sc.next();if ("y".equals(rs2)){schedule.setMorning(true);System.out.println("上午上班的开始时间和结束时间是");String start = sc.next();String end = sc.next();System.out.println("可预约人数是");int number = sc.nextInt();schedule .setMstart(LocalTime.parse(start));schedule .setMend(LocalTime.parse(end));schedule.setmTotalNumber(number);}else{schedule.setMorning(false);}System.out.println("下午是否上班 y/n");// 同上午}}private void updateSchedules(ArrayList<Schedule> schedules) {if (schedules.size() == 0){// 第一次排班for (int i = 0; i <= 6; i++) {Schedule schedule = new Schedule();LocalDate now = LocalDate.now();schedule.setToday(now.plusDays(i));schedules.add(schedule);}return;}// 去除过期的时间for (int i = 0; i < schedules.size(); i++) {Schedule schedule = schedules.get(i);LocalDate now = LocalDate.now();LocalDate current = schedule.getToday();if (current.equals(now)){break;}if (current.isBefore(now)){schedules.remove(schedule);i--;}}// 补全当前和未来6天LocalDate last = schedules.get(schedules.size() - 1).getToday();int size = schedules.size();for (int i = 0; i < 7 - size; i++) {Schedule schedule = new Schedule();schedule.setToday(last.plusDays(i + 1));schedules.add(schedule);}}private Department getDepartmentByUser() {if (allDepartments.size() == 0) {System.out.println("当前无科室");return null;}while (true) {System.out.println("选择科室");for (int i = 0; i < allDepartments.size(); i++) {Department department = allDepartments.get(i);System.out.println((i + 1) + "、" + department.getName());}System.out.println("请输入");int command = sc.nextInt();if (command < 1 || command > allDepartments.size()) {System.out.println("输入有误");continue;}return allDepartments.get(command - 1);}}private void addDoctor() {System.out.println("-------添加医生");Doctor doctor = new Doctor();Department department = getDepartmentByUser();doctor.setDepartmentName(department.getName());doctor.setDoctorId(UUID.randomUUID().toString());System.out.println("姓名");String name = sc.next();doctor.setName(name);// 省略其他信息的录入 性别、年龄、特长..System.out.println("入职时间(yyyy-MM-dd)");String joinDateString = sc.next();LocalDate joinDate = LocalDate.parse(joinDateString);doctor.setJoinDate(joinDate);department.getDoctors().add(doctor);System.out.println("录入成功");}private void addDepartment() {System.out.println("------添加科室");OUT:while (true) {System.out.println("请输入科室名称");String name = sc.next();for (int i = 0; i < allDepartments.size(); i++) {Department department = allDepartments.get(i);if (department.getName().equals(name)) continue OUT;}Department department = new Department();department.setName(name);allDepartments.add(department);System.out.println("添加成功");break;}}
}