-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudentList.java
145 lines (132 loc) · 3.47 KB
/
StudentList.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
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
import java.util.*;
import java.io.*;
/**
* The StudentList organizes and manages a set of Students using methods. It has an ArrayList to aid in this process.
*/
public class StudentList
{
private ArrayList<Student> studentList;
/**
* Constructs an empty StudentList
*/
public StudentList()
{
studentList = new ArrayList<Student>();
}
/** Formats the StudentList when printed
* @return the formatted String of the StudentList
*/
public String toString()
{
String output = "";
for (Student e : studentList)
output += e + "\n";
return output;
}
/**
* addStudent adds a Student to the List
* @param name the Student being added
*/
public void addStudent(Student name)
{
studentList.add(name);
}
/**
* removeStudent removes a specified Student from the List if it exists and returns true.
* If the Student does not exist in the List, returns false.
* @param name the Student to search for and remove
* @return whether the Student was removed
*/
public String removeStudent(Student name)
{
boolean studentExist = studentList.remove(name);
if (studentExist == true)
return "Student successfully removed.";
else
return "Student not found.";
}
/**
* countStudent counts the number of Students in the List
* @return the number of Students in the List
*/
public int countStudent()
{
return studentList.size();
}
public Student returnStudent(Student name)
{
int studentIndex = studentList.indexOf(name);
return studentList.get(studentIndex);
}
public String searchStudent(Student findStudent)
{
int studentIndex = studentList.indexOf(findStudent);
if(studentIndex == -1)
return "Student not found.";
else
return studentList.get(studentIndex).showAllInfo();
}
public int searchStudentBelt(Student findStudent)
{
int studentIndex = studentList.indexOf(findStudent);
if(studentIndex == -1)
return -1;
else
return studentList.get(studentIndex).getBelt();
}
/**
* Sorts the List in alphabetical order
*/
public void sortByName()
{
for (int i = 1; i < studentList.size(); i++)
for (int pos = 0; pos < studentList.size() - 1; pos++)
{
if (studentList.get(pos).compareTo(studentList.get(pos+1)) > 0)
{
Student temp = studentList.get(pos);
studentList.set(pos, studentList.get(pos+1));
studentList.set(pos+1, temp);
}
}
}
/**
* Sorts the List in Belt order
*/
public void sortByBelt()
{
for (int i = 1; i < studentList.size(); i++)
for (int pos = 0; pos < studentList.size() - 1; pos++)
{
if (studentList.get(pos).compareToByBelt(studentList.get(pos+1)) > 0)
{
Student temp = studentList.get(pos);
studentList.set(pos, studentList.get(pos+1));
studentList.set(pos+1, temp);
}
}
}
/**
* Sorts the List in Age order
*/
public void sortByAge()
{
for (int i = 1; i < studentList.size(); i++)
for (int pos = 0; pos < studentList.size() - 1; pos++)
{
if (studentList.get(pos).compareToByAge(studentList.get(pos+1)) > 0)
{
Student temp = studentList.get(pos);
studentList.set(pos, studentList.get(pos+1));
studentList.set(pos+1, temp);
}
}
}
public String saveToFile()
{
String output = "";
for (Student e : studentList)
output += e.saveToFile() +"\n";
return output;
}
}