Java: Невозможно реализовать функцию поиска в моей программе - PullRequest
0 голосов
/ 29 апреля 2020

Я создаю java программу, которая похожа на офис регистратора колледжа, где администраторы добавляют классы в систему, и студенты могут добавлять или удалять эти классы из своих расписаний. Я реализовал метод поиска, чтобы увидеть, данный класс находится в системе, но я пытаюсь сделать то же самое для поиска студентов. Я думаю, что проблема лежит где-то в списке массивов, но я не могу понять, как это исправить. Вот мой код: Главный класс, который запускает всю программу (только включенный код, относящийся к проблеме):

glenAL.searchByStudent("Karen Grayson");

Класс администратора:

package registraroffice;

import java.util.ArrayList;


public class admin 
{
    //all the admin class properties
   private String adminName;
   private courses[] listOfClasses;
   private ArrayList<courses> listOfClassesAL;
   private student[] listOfStudents;
   private ArrayList<student> listOfStudentsAL;
   private boolean privateAccess;
   private int numberOfCourses;
   private int indexFilled = 0;
    //calling out a new admin using normal method
    admin(String adminName, boolean privateAccess, int numberOfCourses)
    {
        this.adminName = adminName;
        this.privateAccess = privateAccess;
        this.numberOfCourses = numberOfCourses;
        listOfClasses = new courses[this.numberOfCourses];
    }
    //calling out a new admin using AL method
    admin(String adminName, boolean privateAccess)
    {
        this.adminName = adminName;
        this.privateAccess = privateAccess;
        listOfClassesAL = new ArrayList<courses>();
    }
    //the two methods for an administrator to add a class into the system
    void addCourse(courses c)
    {
       listOfClasses[indexFilled] = c;
       indexFilled++;
    }

    void addCourseAL(courses c)
    {
        listOfClassesAL.add(c);
    }
    //admin name getter
    String getName()
    {
        return adminName;
    }
    //numberOfCourses getter
    int getNumber()
    {
        return numberOfCourses;
    }
    //printing all classes added by the specific admin can be done using one of the two methods below:
    void printAllClasses()
    {
        for(int index = 0; index < indexFilled; index++)
        {
            System.out.println("Class: " + listOfClasses[index].getCourse() + " , Category: " + listOfClasses[index].getCategory() + " , Hours: " + listOfClasses[index].getHours()
            + " , Cost: " + listOfClasses[index].getCost() + " , Seats Left: " + listOfClasses[index].getSeats());
            System.out.println();
        }
    }

  void printAllClassesAL()
  {
      int index = 0;
      while( index < listOfClassesAL.size())
      {
          System.out.println("Class: " + listOfClassesAL.get(index).getCourse() + " , Category: " + listOfClassesAL.get(index).getCategory()
          + " , Hours: " + listOfClassesAL.get(index).getHours() + " , Cost: " + listOfClassesAL.get(index).getCost() + " , Seats Left: " + listOfClassesAL.get(index).getSeats());
          System.out.println();
          index++;
      }
  }
  //removing the last admin added class
  boolean removeClassAL(courses c)
  {
      return listOfClassesAL.remove(c);
  }
  //searching for a class in the system by typing in its name
boolean searchByClassTitleAL(String className)
{               
        for(courses c : listOfClassesAL)
        {           
         if( c.getCourse().equalsIgnoreCase(className))
         {      
             System.out.println("Class " + className + " exists");
             return true; 
         }       
     }     
     System.out.println("Class " + className + " doesn't exist");
     return false;            
}
//searching for a student in the system by typing in his/her name
boolean searchByStudent(String name)
{               
        for(student a: listOfStudentsAL)
        {           
         if( a.getStudent().equalsIgnoreCase(name))
         {      
             System.out.println("Student " + name + " exists");
             return true; 
         }       
     }     
     System.out.println("Student " + name + " doesn't exist");
     return false;            
}

}

Класс курсов:

package registraroffice;


public class courses  
{
    //properties to identiy each individual class
    String className;
    String category;
    String classHours;
    double classCost;
    int seatsLeft;

    //setters and getters for each property
   void setCourse(String className)
   {
       this.className = className;
   }

   String getCourse()
   {
       return className;

   }

   void setCategory(String category)
   {
       this.category = category;
   }

   String getCategory()
   {
       return category;
   }

   void setHours(String classHours)
   {
       this.classHours = classHours;
   }

   String getHours()
   {
       return classHours;
   }

   void setCost(double classCost)
   {
       this.classCost = classCost;
   }

   double getCost()
   {
       return classCost;
   }

   void setSeats(int seatsLeft)
   {
       this.seatsLeft = seatsLeft;
   }

   int getSeats()
   {
       return seatsLeft;
   }

    Object getStudent() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

Студенческий класс:

package registraroffice;

import java.util.ArrayList;


public class student 
{
   //properties to describe each student
   String name;
   String major;
   double balanceForSemester;
   boolean privateAccess;
   courses[]classSchedule;
   ArrayList<courses> classScheduleAL;
   private student[] listOfStudents;
   private ArrayList<student> listOfStudentsAL;
   int numberOfClasses;
   int indexFilled = 0;
   //1st type of student description, used for student
   public student(String name, String major, double balanceForSemester, boolean privateAccess, int numberOfClasses)
    {
        this.name = name;
        this.major = major;
        this.balanceForSemester = balanceForSemester;
        this.privateAccess = privateAccess;
        this.numberOfClasses = numberOfClasses;
        classSchedule = new courses[this.numberOfClasses];
    }
   //2nd type of student description, used for StudentAL 
   student(String name, String major, double balanceForSemester)
    {
        this.name = name;
        this.major = major;
        this.balanceForSemester = balanceForSemester;
        classScheduleAL = new ArrayList<courses>();
    }
    //student getter
   String getStudent()
   {
       return name;
   }
   //adding a class by a student using either normal or AL method
    void addStudentClass(courses a)
    {
       classSchedule[indexFilled] = a;
       indexFilled++;
    }

    void addStudentClassAL(courses a)
    {
        classScheduleAL.add(a);
    }
    //prints out all classes added by a student using either normal or AL method
    void printAllStudentClasses()
    {
        for(int index = 0; index < indexFilled; index++)
        {
            System.out.println(classSchedule[index].getCourse());
        }
    }

  void printAllStudentClassesAL()
  {
      int index = 0;
      while( index < classScheduleAL.size())
      {
          System.out.println(classScheduleAL.get(index).getCourse());
          index++;
      }
  }
  //removes the last added class from studentAL
  boolean removeStudentClassAL(courses a)
  {
      return classScheduleAL.remove(a);
  }


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...