-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaySchedule.java
102 lines (96 loc) · 2.14 KB
/
DaySchedule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Created by Noah Shaw
*/
public class DaySchedule {
//Instance variables
private Activity[] activities;
public static final int numOfActivities = 20;
//Constructors
public DaySchedule()
{
activities = new Activity[numOfActivities];
}
//No Accessors needed
//No Mutators needed
//Methods
public void addActivity(Activity aActivity)
{
if(activities[activities.length - 1] != null)
{
System.out.println("Sorry the schedule is full.");
return;
}
else
{
for(int i = 0; i < activities.length; i++)
{
if(activities[i] == null)
{
activities[i] = aActivity;
break;
}
else if(activities[i].getStartHour() == aActivity.getStartHour() || activities[i].getEndHour() == aActivity.getEndHour())
{
System.out.println("You already have something scheduled for that time.");
break;
}
}
}
boolean hasSwapped = true;
while(hasSwapped)
{
hasSwapped = false;
for(int j = 0; j < activities.length - 1; j++)
{
if(activities[j] == null || activities[j+1] == null)
{
break;
}
else if(activities[j].getStartHour() > activities[j+1].getStartHour())
{
//Swap
Activity temp = activities[j];
activities[j] = activities[j+1];
activities[j+1] = temp;
hasSwapped = true;
break;
}
}
}
}
public void removeActivity(Activity aActivity)
{
int rmIndex = -1; //Sentinel value
for(int i = 0; i < activities.length; i++)
{
if(activities[i] != null && activities[i].getName().equalsIgnoreCase(aActivity.getName()))
{
rmIndex = i;
break;
}
}
if(rmIndex == -1)
{
System.out.println("That activity does not exist.");
return;
}
for(int i = rmIndex; i < activities.length - 1; i++)
{
activities[i] = activities[i+1];
}
activities[activities.length - 1] = null;
}
public void printActivities()
{
System.out.println();
System.out.println("CURRENT SCHEDULE");
for(int i = 0; i < activities.length; i++)
{
if(activities[i] == null)
{
break;
}
System.out.println(activities[i]);
}
}
}