浏览 592 次
|
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2008-06-20
Create a "Diary" class that has a collection of appointments, and has a method that returns the first available slot after the current time of a given duration.
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2008-06-21
我怎么感觉更像作业呢?
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Helper {
public static Calendar getCalendarFromString(String str) {
Date date = null;
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
date = formatter.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
}
import java.util.Calendar;
public class Appointment {
private Calendar startTime;
private Calendar endTime;
private String content;
Appointment(String startTimeStr, String endTimeStr, String content) {
Calendar startTime = Helper.getCalendarFromString(startTimeStr);
Calendar endTime = Helper.getCalendarFromString(endTimeStr);
if (startTime.compareTo(endTime) >= 0) {
throw new RuntimeException(
"error: StartTime should before EndTime!");
}
this.startTime = startTime;
this.endTime = endTime;
this.content = content;
}
public Calendar getStartTime() {
return startTime;
}
public Calendar getEndTime() {
return endTime;
}
public String getContent() {
return content;
}
public String toString() {
return "StartTime:" + startTime.getTime() + "\nEndTime:"
+ endTime.getTime() + "\nContent:" + content;
}
}
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
public class Diary {
private List<Appointment> appointmentList = new ArrayList<Appointment>();
private Calendar start;
private Calendar end;
Diary(String day) {
this.start = Helper.getCalendarFromString(day + "000000");
this.end = Helper.getCalendarFromString(day + "240000");
}
public void add(Appointment appointment) {
int index = -1;
if (appointment.getStartTime().before(this.start)
|| appointment.getEndTime().after(this.end)) {
throw new RuntimeException("the day of the appointment should be:"
+ start.getTime() + " which Diary specified");
}
int size = appointmentList.size();
if (size == 0) {
index = 0;
} else {
for (int i = 0; i < size; i++) {
Calendar startTimeExist = appointmentList.get(i).getStartTime();
Calendar endTimeExist = appointmentList.get(i).getEndTime();
Calendar startTime = appointment.getStartTime();
Calendar endTime = appointment.getEndTime();
if (endTime.compareTo(startTimeExist) <= 0) {
index = i;
break;
} else if (startTime.compareTo(endTimeExist) < 0) {
throw new RuntimeException(
"error: time has already occupied");
} else {
}
}
index = index == -1 ? size : index;
}
this.appointmentList.add(index, appointment);
}
public Appointment getComingAppointment() {
Appointment result = null;
for (int i = 0; i < this.appointmentList.size(); i++) {
Appointment appointment = appointmentList.get(i);
if (new GregorianCalendar().compareTo(appointment.getStartTime()) < 0) {
result = appointment;
break;
}
}
return result;
}
public static void main(String[] args) {
Diary diary = new Diary("20080620");
Appointment app1 = new Appointment("20080620010000", "20080620020000",
"item1");
Appointment app2 = new Appointment("20080620130000", "20080620140000",
"item2");
Appointment app3 = new Appointment("20080620150000", "20080620160000",
"item3");
Appointment app4 = new Appointment("20080620160000", "20080620170000",
"item4");
Appointment app5 = new Appointment("20080620180000", "20080620190000",
"item5");
Appointment app6 = new Appointment("20080620230000", "20080620240000",
"item6");
diary.add(app6);
diary.add(app5);
diary.add(app2);
diary.add(app3);
diary.add(app1);
diary.add(app4);
Appointment result = diary.getComingAppointment();
System.out.println(result);
}
}
|
|
| 返回顶楼 | |
|
最后更新时间:2008-06-23
就这句话a method that returns the first available slot after the current time of a given duration.举个例子:
你现在有个1个小时的会议要安排,这个method应该返回一个空余的时间段给此会议。 |
|
| 返回顶楼 | |



