java - 为什么我的代码不能正确打印类(class)

标签 java arraylist

代码如下:

驱动程序:

package myschool;

import java.util.ArrayList;
import java.util.Scanner;

public class MySchool {


public static void main(String[] args) {

    ArrayList<Student> listStudent = new ArrayList<>();
    ArrayList<Course> listCourse = new ArrayList<>();
    Student s = new Student();
    Course c = new Course();

    boolean continueLoop = true;
    Scanner userInput = new Scanner(System.in);
    Scanner courseAddInput = new Scanner(System.in);
    int option;

    do{
    try {  
    System.out.println(" What would you like to do?");
    System.out.println(" 1) Add a student");
    System.out.println(" 2) View students");
    System.out.println(" 3) Remove a student");
    System.out.println(" 4) Exit");
    System.out.print("--> ");
    option = userInput.nextInt();

    switch( option ){
            case 1:
                Scanner inputs = new Scanner(System.in);
                String fName, lName;
                int sID;
                double sGPA;

                System.out.print(" First Name:");
                fName = inputs.nextLine();
                s.setStudentFirstName( fName );

                System.out.print(" Last Name:");
                lName = inputs.nextLine();
                s.setStudentLastName( lName );

                System.out.print(" ID Number:");
                sID = inputs.nextInt();
                s.setStudentID( sID );

                System.out.print(" GPA:");
                sGPA = inputs.nextDouble();
                s.setStudentGPA( sGPA );

                String cName, instructor, cBeginTime, cEndTime, cDay;
                int i = 0, cID, cCred;
                boolean continueAdd = true;
                Scanner input = new Scanner(System.in);
                Scanner input2 = new Scanner(System.in);
                String addCourse;

                do{
                System.out.println("Would you like to add a course? Y/N");
                addCourse = input2.nextLine();

                if( "N".equals(addCourse)|| "n".equals(addCourse))
                    continueAdd = false;

                if(continueAdd){  
                System.out.print(" CourseName:");
                cName = input.nextLine();
                c.setCourseName( cName );

                System.out.print(" Instructor:");
                instructor = input.nextLine();
                c.setCourseInstructor( instructor );

                System.out.print(" CourseID:");
                cID = input.nextInt();
                c.setCourseID( cID );

                System.out.print(" CourseCredit:");
                cCred = input.nextInt();
                c.setCourseCred( cCred );

                /*System.out.print(" StartTime:");
                cBeginTime = input.nextLine();
                c.setCourseBeginTime(cBeginTime);

                System.out.print(" EndTime:");
                cEndTime = input2.nextLine();
                c.setCourseEndTime(cEndTime);*/

                listCourse.add( new Course( c.getCourseName(), c.getCourseInstructor(), c.getCourseCred(), c.getCourseBeginTime(), c.getCourseEndTime(), c.getCourseID() ));
                }
                }while( continueAdd );
                listStudent.add( new Student( s.getStudentFirstName(),s.getStudentLastName(), s.getStudentID(), s.getStudentGPA(), listCourse));
                break;

            case 2:
                if(!listStudent.isEmpty()){
                    for(Student l:listStudent) {
                        System.out.println(l);
                        for(Course n:listCourse) {
                            System.out.println(n);
                        }
                        System.out.println();
                    }


                }else
                System.out.println("There are no students to view\n");

               break;

            case 3:
                Scanner removeChoice = new Scanner(System.in);

                try {
                    if(!listStudent.isEmpty()){
                    int j = 0;
                    System.out.println("Which student do you want to remove?");

                        for(Student l:listStudent) {
                            System.out.print(j+1 + ")");
                            System.out.println(l);
                            j++;
                        }
                        int remove = removeChoice.nextInt();
                            listStudent.remove( remove - 1 );
                        System.out.println("Student has been removed\n");
                    }else
                        System.out.println("There are no students to remove\n");

                } catch (Exception e) {
                    System.out.println("There are no students to remove\n");
                }

                break;

            case 4:
                continueLoop = false;
                break;
    }
    } catch (Exception e) {
        System.out.println("That is not a valid option!!!");
        continueLoop = false;
    }

    }while( continueLoop );

}
}


///////////Student Class///////////


package myschool;

import java.util.ArrayList;


public class Student {

String studentFirstName, studentLastName;
int studentID;
double studentGPA;
ArrayList<Course> listCourse = new ArrayList<>();

Student(){}

public Student(String studentFirstName, String studentLastName, int studentID, double studentGPA, ArrayList courseList) {
    this.studentFirstName = studentFirstName;
    this.studentLastName = studentLastName;
    this.studentID = studentID;
    this.studentGPA = studentGPA;
    this.listCourse = courseList;
}

public void setListCourse(ArrayList<Course> listCourse) {
    this.listCourse = listCourse;
}

public ArrayList<Course> getListCourse() {
    return listCourse;
}

public void setStudentFirstName(String studentFirstName) {
    this.studentFirstName = studentFirstName;
}

public String getStudentFirstName() {
    return studentFirstName;
}

public void setStudentLastName(String studentLastName) {
    this.studentLastName = studentLastName;
}

public String getStudentLastName() {
    return studentLastName;
}

public void setStudentID(int studentID) {
    this.studentID = studentID;
}

public int getStudentID() {
    return studentID;
}

public void setStudentGPA(double studentGPA) {
    this.studentGPA = studentGPA;
}

public double getStudentGPA() {
    return studentGPA;
}  

@Override
public String toString() {
    return ("FirstName:" + this.getStudentFirstName() +
                " LastName:" + this.getStudentLastName() +
                " ID:" + this.getStudentID() +
                    " GPA:" + this.getStudentGPA());
   }
} 

///////////Course Class//////////////

package myschool;

public class Course {

private String courseName, courseInstructor, courseBeginTime, courseEndTime;
int courseID, courseCred;

Course(){}

public Course(String courseName, String courseInstructor, int courseCred, String courseBeginTime, String courseEndTime, int courseID) {
    this.courseName = courseName;
    this.courseInstructor = courseInstructor;
    this.courseCred = courseCred;
    this.courseBeginTime = courseBeginTime;
    this.courseEndTime = courseEndTime;
    this.courseID = courseID;
}

public void setCourseName(String courseName) {
    this.courseName = courseName;
}

public String getCourseName() {
    return courseName;
}

public void setCourseInstructor(String courseInstructor) {
    this.courseInstructor = courseInstructor;
}

public String getCourseInstructor() {
    return courseInstructor;
}

public void setCourseCred( int courseCred) {
    this.courseCred = courseCred;
}

public int getCourseCred() {
    return courseCred;
}

public String getCourseBeginTime() {
    return courseBeginTime;
}

public void setCourseBeginTime(String courseBeginTime) {
    this.courseBeginTime = courseBeginTime;
}

public void setCourseEndTime(String courseEndTime) {
    this.courseEndTime = courseEndTime;
}

public String getCourseEndTime() {
    return courseEndTime;
}

public void setCourseID(int courseID) {
    this.courseID = courseID;
}

public int getCourseID() {
    return courseID;
}

@Override
public String toString() {
    return( "Course Name:" + this.getCourseName() 
            + " Course ID:" + this.getCourseBeginTime() 
            + " Instructor:" + this.getCourseInstructor()
            + " Credit:" + this.getCourseCred()
            + " Begin Time:" + this.getCourseBeginTime()
            + " End Time:" + this.getCourseEndTime());
  }
}   

这是运行时发生的事情

 What would you like to do?
 1) Add a student
 2) View students
 3) Remove a student
 4) Exit
--> 1
 First Name:Mike
 Last Name:Smith
 ID Number:2345
 GPA:4
Would you like to add a course? Y/N
y
 CourseName:MAth
 Instructor:get
 CourseID:123
 CourseCredit:3
Would you like to add a course? Y/N
n
 What would you like to do?
 1) Add a student
 2) View students
 3) Remove a student
 4) Exit
--> 1
 First Name:Sarah
 Last Name:Smoith
 ID Number:42342
 GPA:3
Would you like to add a course? Y/N
y
 CourseName:Science
 Instructor:tra;l
 CourseID:345
 CourseCredit:4
Would you like to add a course? Y/N
n
 What would you like to do?
 1) Add a student
 2) View students
 3) Remove a student
 4) Exit
--> 2
FirstName:Mike LastName:Smith ID:2345 GPA:4.0
Course Name:MAth Course ID:null Instructor:get Credit:3 Begin Time:null End Time:null
Course Name:Science Course ID:null Instructor:tra;l Credit:4 Begin Time:null End Time:null

FirstName:Sarah LastName:Smoith ID:42342 GPA:3.0
Course Name:MAth Course ID:null Instructor:get Credit:3 Begin Time:null End Time:null
Course Name:Science Course ID:null Instructor:tra;l Credit:4 Begin Time:null End Time:null

最佳答案

你的toString方法有问题,检查注释:

public String toString() {
    return( "Course Name:" + this.getCourseName() 
            + " Course ID:" + this.getCourseBeginTime()  //  getCourseID should be used here
            + " Instructor:" + this.getCourseInstructor()
            + " Credit:" + this.getCourseCred()
            + " Begin Time:" + this.getCourseBeginTime()
            + " End Time:" + this.getCourseEndTime());
  }

因此,适当更新所有 getter 调用:

关于java - 为什么我的代码不能正确打印类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22651552/

相关文章:

kotlin - 类型不匹配Kotlin和getStringArrayListExtra

java - 如何将ArrayList限制为15个?

arrays - Flutter - 在同一指令中创建列表和 'addAll'

java - 如何以正确的方式将java对象写入firebase?

java - 简化条件逻辑以提高可读性

java - 如何使用返回所有文档的 spring-data-solr 构建 FacetQuery (* :*) from solr index

Java - 尝试搜索 ArrayList 时不执行 For 循环

Java - 从第一个父级开始的组路径列表

java - 数组的 Jackson-CSV 架构

java - 2 ArrayList中的常见对象