sM是该类的一个实例,运行方法staffCanWork时,它应返回一个可以在给定日期和时间工作的人员对象的列表,但是,在运行该方法时,它会导致指针错误为空。
在静态void main中测试方法;
Date date = Date.valueOf(LocalDate.now());
List<Staff> staff = sM.staffCanWork(date, Time.valueOf(LocalTime.now()));
for(int i = 0; i < staff.size(); i++){
System.out.println(staff.get(i).getId());
}
使用hibernate的getStaff()方法使用hibernate填充带有人员对象的列表。
public List<Staff> getStaff(){
List staff =null;
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
TypedQuery query = session.getNamedQuery("getStaff");;
staff = query.getResultList();
}catch (HibernateException e){
e.printStackTrace();
}return staff;
}
staffCanWork方法,检查列表以查看员工是否可以按给定的日期和时间进行工作。
public List<Staff> staffCanWork(Date date, Time time){
List<Staff> staff = getStaff();
List<Staff> staff1 = null;
for(int i = 0; i < staff.size(); i++){
if(staff.get(i).getDoctor() == null){
if(staff.get(i).getNurse().canWork(date, time)) staff1.add(staff.get(i));
}
else {
if(staff.get(i).getDoctor().canWork(date, time)) staff1.add(staff.get(i));
}
}
System.out.println(staff1.get(0).getId() + "test");
return staff1;
}
方法可以工作
public boolean canWork(Date date, Time time){
Boolean canWork =false;
Boolean aM = false;
if(time.getTime() < 12){ aM = true;}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E");
simpleDateFormat.format(date);
if(aM = true) {
switch (simpleDateFormat.format(date)) {
case "Mon": if (getMondayAM() == true) canWork = true;
break;
case "Tue" :if (getTuesdayAM() == true) canWork = true;
break;
case "Wed" :if (getWednesdayAM() == true) canWork = true;
break;
case "Thu" :if (getThursdayAM() == true) canWork = true;
break;
case "Fri" :if (getFridayAM() == true) canWork = true;
break;
case "Sat" :if (getSaturdayAM() == true) canWork = true;
break;
case "Sun" :if (getSundayAM() == true) canWork = true;
default: return false;
}
}
switch (simpleDateFormat.format(date)) {
case "Mon": if (getMondayPM() == true) canWork = true;
break;
case "Tue" :if (getTuesdayPM() == true) canWork = true;
break;
case "Wed" :if (getWednesdayPM() == true) canWork = true;
break;
case "Thu" :if (getThursdayPM() == true) canWork = true;
break;
case "Fri" :if (getFridayPM() == true) canWork = true;
break;
case "Sat" :if (getSaturdayPM() == true) canWork = true;
break;
case "Sun" :if (getSundayPM() == true) canWork = true;
default: return false;
}
return canWork;
}