我只需要一些代码帮助。我有3个类,Activity,DaySchedule和DayScheduleFrontEnd。在DaySchedule类中,有一个名为addActivity的方法:
public void AddActivity(Activity aActivity)
{
//Check if activity array is full
if(activities[activities.length-1] != null)
{
System.out.println("The activities database is full");
return;
}
//Find the first empty space
for(int i=0;i<activities.length;i++)
{
if(activities[i] == null)
{
activities[i] = aActivity;
break;
}
}
this.SortActivitiesByTime();
}
我需要帮助的是一项活动不能与另一项活动发生冲突。如果一个活动的开始和结束不能与另一个活动同时落下,则活动会发生冲突。如果发生这种情况,则应通知用户并且不添加活动。此方法最终应该根据活动的开始时间对活动进行排序。因此,例如,如果我有活动:BreakFast StartHour:8 EndHour:9然后活动:早餐第2部分StartHour:8 EndHour:10它会打印出来表示存在冲突。
活动类的访问者
答案 0 :(得分:0)
首先在Activity类中创建一个名为hasConflict(Activity other)
的函数。在功能检查中是否存在时间冲突。我不完全同意你如何在活动中留出时间。您应该保留两个名为Date
和start
的{{1}}并使用end
查看两个活动之间是否存在冲突。它应该是一个简单的getTime()
。
其次,将您的if statement
数组更改为activities
。在保留LinkedList<Activity>
MAX_SIZE
并进行检查
第三次使用database is full
对您的活动进行排序。有两种方法可以调用该函数。
Collections.sort()
课程实施可比较内容,并将Activity
活动与功能中的provided
活动进行比较this
并为此功能提供该实例完成上述所有操作后,您可以将功能编写到
Comparator
list.size()
,检查数据库中是否有空格
有一些比排序更好的方法,就像在迭代活动以检查冲突时第一次添加活动来纠正索引一样,但现在这样做。
答案 1 :(得分:0)
下面是使用集合的版本,同时使用TreeSet保持元素的总量,顺序和唯一性。我没有包含get / set方法,但它应该是直截了当的。
import java.util.Comparator;
import java.util.Objects;
import java.util.TreeSet;
public class Activity {
public long startHour;
public long endHour;
public static final int MAX_SIZE = 10;
public static TreeSet<Activity> activities = new TreeSet<>(new Comparator<Activity>() {
@Override
public int compare(Activity o1, Activity o2) {
return Long.compare(o1.startHour, o2.startHour);
}
});
public boolean intersect(Activity activity) {
return (activity.startHour >= this.startHour && activity.startHour < this.endHour)
|| (this.startHour >= activity.startHour && this.startHour < activity.endHour);
}
@Override
public String toString() {
return "start: " + startHour + ", end: " + endHour;
}
@Override
public int hashCode(){
return Objects.hash(startHour, endHour);
}
@Override
public boolean equals(Object object) {
if (this == object) return true;
Activity other = (Activity) object;
return (startHour == other.startHour) && (endHour == other.endHour);
}
public static void addActivity(Activity aActivity)
{
if (activities.size() >= MAX_SIZE) {
System.out.println("The activities database is full");
return;
}
for (Activity a : activities) {
if (a.intersect(aActivity)) {
System.out.println(String.format("Activity %s intersect with %s", a, aActivity));
return;
}
}
activities.add(aActivity);
}
public static void main(String[] args) {
Activity a1 = new Activity();
a1.startHour = 0;
a1.endHour = 5;
Activity b1 = new Activity();
b1.startHour = 3;
b1.endHour = 7;
Activity b2 = new Activity();
b2.startHour = 5;
b2.endHour = 9;
Activity b3 = new Activity();
b3.startHour = 1;
b3.endHour = 2;
addActivity(a1);
addActivity(b1);
addActivity(b1);
addActivity(b2);
addActivity(b3);
System.out.println(activities);
}
}
答案 2 :(得分:0)
我相信你并没有以最好的方式做到这一点。您在数组的末尾插入一个元素,然后对整个事物进行排序。但是,您从一个空数组开始,然后添加活动。您应该找到可以插入元素的位置,并将其他元素移到右侧。
public void AddActivity(Activity aActivity)
{
//Check if activity array is full
if(activities[activities.length-1] != null)
{
System.out.println("The activities database is full");
return;
}
//Find the desired location and the first empty space
int desiredLocation = -1;
int firstEmptySpace = -1
for(int i=0;(i<activities.length) && (firstEmptySpace == -1);i++)
{
if ((desiredLocation == -1) && (aActivity.getEndHour() > activities[i].getStartHour()))
{
if (aActivity.getStartHour() < activities[i].getEndHour())
{
//Intersection :(
return;
}
else
{
desiredLocation = i;
}
}
if(activities[i] == null)
{
firstEmptySpace = i;
}
}
//shift things to the right
for (int i = firstEmptySpace; i > desiredLocation; i--)
{
activities[i] = activities[i - 1];
}
//Override previous value at desired location
activities[desiredLocation] = aActivity;
}