This repository was archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefereeProgram.java
More file actions
160 lines (132 loc) · 4.76 KB
/
RefereeProgram.java
File metadata and controls
160 lines (132 loc) · 4.76 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.*;
/**
* Defines an object representing an array of
* RefereeClass objects
* @author Adam John Campbell Murray
*
*/
public class RefereeProgram
{
/* Instance variables */
private RefereeClass [] refereeClassArray; // array to hold referee objects
private int elementsInArray; // number of (non-null) referee objects in array
/* Class constants */
/** The maximum number of referees the program will accept */
private final int MAX_REFEREES = 12; // maximum number of referees in file
/**
* Default constructor for the RefereeProgram class.
*/
public RefereeProgram()
{
// initialise the number of elements in the referee class array
elementsInArray = 0;
// initialise the referee class array with null values
refereeClassArray = new RefereeClass[MAX_REFEREES];
for (int i = 0; i < MAX_REFEREES; i++)
refereeClassArray[i] = null;
}
/**
* Builds the referee class array from a file ordered
* lexicographically by referee ID and numerically by
* sequence number.
* @param r - RefereeClass object
*/
public void refereeClassArrayBuilder(RefereeClass r)
{
// create new RefereeClass array with space for one additional element
RefereeClass[] updatedrefereeClassArray = new RefereeClass[elementsInArray + 1];
// copy the current refereeClassArray into the new array
System.arraycopy(refereeClassArray, 0, updatedrefereeClassArray, 0, elementsInArray);
// change pointer of refereeClassArray to new array
refereeClassArray = updatedrefereeClassArray;
// add new RefereeClass object to array
refereeClassArray[elementsInArray] = r;
// increment number of elements in the array
elementsInArray++;
}
/**
* Deletes a referee class from the referee class array
* and then updates the referee class array, removing the
* null value resulting from the deletion.
* @param r - the referee class being deleted
*/
public void deleteRefereeClassFromList(RefereeClass r)
{
// set relevant position in refereeClassArray to null
refereeClassArray[getPositionInRefereeClassArray(r.getRefereeName())] = null;
// decrement number of elements in the array
elementsInArray--;
// create a new array of non-null values and
// change pointer of refereeClassArray to new array
int arraySize = 0;
RefereeClass[] refereeClassArrayTemp = new RefereeClass[MAX_REFEREES];
// add only non-null RefereeClass objects to temp array
// and increment arraySize
for (RefereeClass ref : refereeClassArray)
if (ref != null)
{
refereeClassArrayTemp[arraySize] = ref;
arraySize++;
}
// create a new RefereeClass array
RefereeClass[] updatedrefereeClassArray = new RefereeClass[arraySize];
// copy the temp array into the new array
System.arraycopy(refereeClassArrayTemp, 0, updatedrefereeClassArray, 0, arraySize);
// set array ordering by referee ID and sort
RefereeClass.setWhichOrder("ID");
Arrays.sort(updatedrefereeClassArray);
// change pointer of refereeClassArray to the new array
refereeClassArray = updatedrefereeClassArray;
}
/**
* Adds a referee class to the referee class array
* and updates the ordering of the array.
* @param r - the referee class being deleted
*/
public void addRefereeClassToList(RefereeClass r)
{
// if there are already 12 elements in the array
// throw an exception
if (elementsInArray >= MAX_REFEREES)
throw new StringIndexOutOfBoundsException();
else // otherwise add RefereeClass to the refereeClassArray
{
RefereeClass[] updatedrefereeClassArray = new RefereeClass[elementsInArray + 1];
System.arraycopy(refereeClassArray, 0, updatedrefereeClassArray, 0, elementsInArray);
refereeClassArray = updatedrefereeClassArray;
refereeClassArray[elementsInArray] = r;
RefereeClass.setWhichOrder("ID");
Arrays.sort(refereeClassArray);
elementsInArray++;
}
}
/**
* Gets the position in the referee class array of the
* RefereeClass with name equal to the name parameter.
* @param name - the name corresponding to a referee class object
* @return the position in the referee class array of referee class object
*/
public int getPositionInRefereeClassArray(String name)
{
int positionInList = 0;
for (int i = 0; i < elementsInArray; i++)
if (refereeClassArray[i] != null && refereeClassArray[i].getRefereeName().equals(name))
positionInList = i;
return positionInList;
}
/*
* Getters and setters
*/
public RefereeClass[] getrefereeClassArray()
{
return refereeClassArray;
}
public RefereeClass getRefereeClassAtX(int x)
{
return refereeClassArray[x];
}
public int getelementsInArray()
{
return elementsInArray;
}
}