Class StudentRoster

java.lang.Object
solution.StudentRoster

public class StudentRoster extends Object
Class for managing student names and ids.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new StudentRoster with empty ArrayLists for ids and names.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addStudent(String name, int id)
    Adds a student's name and id to the ArrayLists for names and ids.
    void
    changeName(Integer id, String newName)
    Changes the name of the student with the given id.
    Returns an Arraylist of ids for students with the passed-in name.
    Returns the student name that corresponds to the passed-in id.
    boolean
    Returns true if there is a student with the passed-in id.
    boolean
    Returns true if there is a student with the passed-in name.
    void
    Removes the student with the passed-in id from the Arraylist of ids and the corresponding name from the Arraylist of names.
    void
    Removes all students with the passed-in name from the Arraylist of names and their corresponding ids from the Arraylist of ids.
    Returns a String that has the id and name of each student on a separate line.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • StudentRoster

      public StudentRoster()
      Creates a new StudentRoster with empty ArrayLists for ids and names.
  • Method Details

    • addStudent

      public void addStudent(String name, int id)
      Adds a student's name and id to the ArrayLists for names and ids. The id is inserted into the Arraylist of ids such that the Arraylist is sorted in ascending order. The name should be inserted into the Arraylist of names so it has the same index as the id. If the passed-in id is already in the Arraylist of ids nothing happens. You are not allowed to, and don't need to, use any built-in sort methods.
      Parameters:
      name - Name of the student.
      id - ID of the student.
    • hasStudent

      public boolean hasStudent(Integer id)
      Returns true if there is a student with the passed-in id.
      Parameters:
      id - ID to look up in the Arraylist of ids.
      Returns:
      True if id is in the Arraylist of ids, False otherwise.
    • hasStudent

      public boolean hasStudent(String name)
      Returns true if there is a student with the passed-in name.
      Parameters:
      name - Name to look up in the Arraylist of names.
      Returns:
      True if name is in the Arraylist of names, False otherwise.
    • removeStudent

      public void removeStudent(String name)
      Removes all students with the passed-in name from the Arraylist of names and their corresponding ids from the Arraylist of ids. If the student name is not in names, nothing happens.
      Parameters:
      name - Name of student(s) to remove.
    • removeStudent

      public void removeStudent(Integer id)
      Removes the student with the passed-in id from the Arraylist of ids and the corresponding name from the Arraylist of names. If the student name is not in names, nothing happens.
      Parameters:
      id - ID of the student to remove.
    • getStudentID

      public ArrayList<Integer> getStudentID(String name)
      Returns an Arraylist of ids for students with the passed-in name. If there are no students with the passed-in name, returns an empty Arraylist.
      Parameters:
      name - Name of student(s) whose ids we want.
      Returns:
      ArrayList of ids for the student name passed-in.
    • getStudentName

      public String getStudentName(Integer id)
      Returns the student name that corresponds to the passed-in id. If the id is not in ids, returns null.
      Parameters:
      id - ID to look up in ids.
      Returns:
      Name of the student that corresponds to the id or null if there is no student with that id.
    • changeName

      public void changeName(Integer id, String newName)
      Changes the name of the student with the given id. If there is no student with the passed-in id, nothing happens.
      Parameters:
      id - ID of student whose name we want to change.
      newName - New name of the student.
    • toString

      public String toString()
      Returns a String that has the id and name of each student on a separate line. See sample output.
      Overrides:
      toString in class Object
      Returns:
      String representing the ids and names in the roster.