I have a student class and also a cohort class.
I need a command to find a certain student aStudent, bStudent etc
but are unsure of the right way to do it
Thanks in advance
[import java.util.*;
public class Cohort {
private static int groupSize = 15;
private HashSet aCohort;
public Cohort()
{
aCohort = new HashSet();
}
public String add(Student aStudent)
{
if (getNumberOfStudents() < groupSize)
{
if (findStudent(aStudent.getNumber()) == null)
{
if (aCohort.add(aStudent))
{
return "Success";
}
}
else
{
return "Failure - Student with the same number exists";
}
}
else
{
return "Failure - cohort is full";
}
return "Failure";
}
public String remove(int aNumber)
{
Iterator it = aCohort.iterator();
while (it.hasNext())
{
Student student = (Student)it.next();
if (aNumber == student.getNumber())
{
it.remove();
return "Success";
}
}
return "Failure";
}
public Student findStudent(int aNumber)
{
Iterator it = aCohort.iterator();
while (it.hasNext())
{
Student aStudent = (Student)it.next();
if (aNumber == aStudent.getNumber())
{
return aStudent;
}
}
return null;
}
public int getNumberOfStudents()
{
return aCohort.size();
}
public String toString()
{
String s = new String();
Iterator it = aCohort.iterator();
while (it.hasNext())
{
Student aStudent = (Student)it.next();
s = s + aStudent.toString();
if (it.hasNext())
{
s = s + "\n";
}
}
return s;
}
public static void main(String args[])
{
Cohort aCohort = new Cohort();
Student aStudent = new Student("Nathan","Leicester Uk");
System.out.println("Add student to Cohort: " + aCohort.add(aStudent));
Student bStudent = new Student("Steve","Manchester Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(bStudent));
System.out.println(aCohort.toString());
Student cStudent = new Student("John","London Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(cStudent));
Student dStudent = new Student("Peter","Newcastle Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(dStudent));
Student eStudent = new Student("Janet","Leicester Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(eStudent));
Student fStudent = new Student("Emma","Derby Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(fStudent));
Student gStudent = new Student("David","Nottingham Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(gStudent));
Student hStudent = new Student("Phillip","Coventry Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(hStudent));
Student iStudent = new Student("Harry","Portsmouth Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(iStudent));
Student jStudent = new Student("Andrew","Stoke Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(jStudent));
System.out.println(aCohort.toString());
Student kStudent = new Student("Garry","Glasgow Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(kStudent));
Student lStudent = new Student("James","London Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(lStudent));
Student mStudent = new Student("Rebecca","Swansea Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(mStudent));
Student nStudent = new Student("Jack","Luton Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(nStudent));
System.out.println(aCohort.toString());
System.out.println("Adding duplicate student to Cohort: " + aCohort.add(aStudent));
Student oStudent = new Student("Walter","Wigan Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(oStudent));
System.out.println(aCohort.toString());
System.out.println("class contains " + aCohort.getNumberOfStudents() + " students");
Student pStudent = new Student("William","Cambridge Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(pStudent));
System.out.println(aCohort.remove(2) + " Steve has been removed");
System.out.println("The cohort contains the following after a students has been removed");
System.out.println(aCohort.toString());
System.out.println(aCohort.remove(20) + " Student does not exist");
Student sStudent = new Student("Jason","Oxford Uk");
System.out.println("Add Student to Cohort: " + aCohort.add(sStudent));
System.out.println(aCohort.toString());
}
}]