"JAVA language program design"
"JAVA语言程序设计"
"JAVA language program design"
***Part I***
It is assumed that "today" is excluded from "within the next 7 days".
It is assumed that birthday is stored as Date type with value of 0 for hours,
minutes and seconds.
It is assumed that a meeting can be hold right after previous one finished.
In other words, Meeting A, with finishing time of 10:00:00,
and Meeting B, with a starting time of 10:00:00,
are not considered as overlapped.
It is assumed that meeting with a duration of 0 seconds is valid and can cause
Overlapping.
It is assumed that this schedule system may have further update with more types of
events.
It is assumed that every type of events will have at least a label with String
type and a time with Date type.
It is assumed that different types of events should be handled separately.
***Part II***
See attached image.
***Part III***
ScheduleImpl.java
import java.text.SimpleDateFormat
import java.util.ArrayList
import java.util.Calendar
import java.util.Date
public class ScheduleImpl implements Schedule {
private ArrayList<Meeting> meetings
private ArrayList<Birthday> birthdays
public ScheduleImpl () {
meetings = new ArrayList<Meeting>()
birthdays = new ArrayList<Birthday>()
}
@Override
public void add(Event item) {
}
@Override
public void add(int type, Event item) {
}
@Override
public void get(String label) {
}
@Override
public void get(int type, int position) {
}
@Override
public void update(String label, Event item) {
}
@Override
public void update(int type, String label, Event item) {
}
@Override
public void remove(String label) {
}
@Override
public void remove(int type, String label) {
}
@Override
public void sort() {
}
@Override
public String birthdays() {
StringBuilder result = new StringBuilder()
result.append("List of birthdays in next 7 days:")
for (int i = 0 i < birthdays.size() i++) {
Date b = birthdays.get(i).getDOB() // Birthday
Calendar t = Calendar.getInstance() // Today
Calendar n = Calendar.getInstance()
n.add(Calendar.WEEK_OF_YEAR, 1) // Next 7 days
if (b.compareTo(t.getTime()) > 0 && b.compareTo(n.getTime()) < 0) {
result.append(birthdays.get(i).toString())
}
}
return result.toString()
}
@Override
public boolean overlapping() {
for (int i = 0 i < meetings.size() - 1 i++) {
for (int j = i + 1 j < meetings.size() j++) {
Date s1 = meetings.get(i).getStartTime()
Date s2 = meetings.get(j).getStartTime()
Date f1 = meetings.get(i).getFinishTime()
if (s1.compareTo(s1) <= 0 && f1.compareTo(s2) > 0) {
return true
}
}
}
return false
}
}
interface Schedule {
public static final String DATE_FORMAT = "EEE dd-MM-yyyy"
public static final String TIME_FORMAT = "HH:mm:ss"
public static final int MEETING = 0
public static final int BIRTHDAY = 1
void add(Event item)
void add(int type, Event item)
void get(String label)
void get(int type, int position)
void update(String label, Event item)
void update(int type, String label, Event item)
void remove(String label)
void remove(int type, String label)
void sort()
String birthdays()
boolean overlapping()
}
class Event {
protected static final SimpleDateFormat DATE_FORMATTER =
new SimpleDateFormat(Schedule.DATE_FORMAT)
protected static final SimpleDateFormat TIME_FORMATTER =
new SimpleDateFormat(Schedule.TIME_FORMAT)
private String label
private Date Date
protected String getLabel() {
return label
}
protected void setLabel(String label) {
this.label = label
}
protected Date getDate() {
return Date
}
protected void setDate(Date date) {
this.Date = date
}
}
class Birthday extends Event {
public Birthday (String name, Date dateOfBirth) {
setLabel(name)
Calendar dob = Calendar.getInstance()
dob.setTime(dateOfBirth)
dob.set(Calendar.HOUR, 0)
dob.set(Calendar.MINUTE, 0)
dob.set(Calendar.SECOND, 0)
setDate(dob.getTime())
}
public String getName() {
return getLabel()
}
public Date getDOB() {
return getDate()
}
@Override
public String toString() {
StringBuilder result = new StringBuilder()
result.append(getName())
for (int i = getName().length() / 8 i < 4 i++) {
result.append("\t")
}
result.append("- ")
result.append(DATE_FORMATTER.format(getDOB()))
return result.toString()
}
}
class Meeting extends Event {
private Date finishTime
private String location
public Meeting (String description, Date startTime, Date finishTime,
String location) {
setLabel(description)
setDate(startTime)
this.finishTime = finishTime
this.location = location
}
public String getDescription() {
return getLabel()
}
public void setDescription(String description) {
setLabel(description)
}
public Date getStartTime() {
return getDate()
}
public void setStartTime(Date startTime) {
setDate(startTime)
}
public Date getFinishTime() {
return finishTime
}
public void setFinishTime(Date finishTime) {
this.finishTime = finishTime
}
public String getLocation() {
return location
}
public void setLocation(String location) {
this.location = location
}
@Override
public String toString() {
StringBuilder result = new StringBuilder()
result.append(getDescription())
result.append(":\n")
result.append(DATE_FORMATTER.format(getStartTime()))
result.append(" from ")
result.append(TIME_FORMATTER.format(getStartTime()))
result.append(" to ")
result.append(TIME_FORMATTER.format(getFinishTime()))
result.append("\nAt: ")
result.append(getLocation())
return result.toString()
}
}
***Part IV***
Test case 1: separated meetings
– expected return : False
e.g. A. 09:00 – 10:00 B. 11:00 – 12:00
Test case 2: consequent meetings
– expected return : False
e.g. A. 09:00 – 10:00 B. 10:00 – 11:00
Test case 3: partially overlapped meetings
– expected return : True
e.g. A. 09:00 – 10:00 B. 10:30 – 11:30
Test case 4: partially overlapped meetings (same start time)
– expected return : True
e.g. A. 09:00 – 10:00 B. 09:00 – 09:30
Test case 5: entirely overlapped meetings
– expected return : True
e.g. A. 09:00 – 12:00 B. 10:00 – 11:00
Test case 6: entirely overlapped meetings (same start time)
– expected return : True
e.g. A. 09:00 – 12:00 B. 09:00 – 10:00
Test case 7: exactly overlapped meetings
– expected return : True
e.g. A. 09:00 – 10:00 B. 09:00 – 10:00
***Part V***
All running times listed below are for average case.
running time of methods:
birthdays() O(n)
overlapping() O(n^2)
Recommendations on improvement:
Using sorted data (sort birthdays by date of birth, sort meetings by start time)
which takes O(n log n) to O(n^2) to sort (only needed once).
After sorting and implementing modified CRUD functions,
although create/delete functions will take O(log n) instead of O(1) to finish,
read/update functions will take O(log n) instead of O(n) to finish.
Additionally, birthdays() will take O(log n) instead of O(n) and
overlapping() will take O(n) instead of O(n^2).
In conclusion, applying these recommended improvements would significantly boost on overall performance, especially while dealing with large size datasets.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)