2017-10-12 29 views
0

時,字符串不會顯示我有一個問題,即使輸入了信息,當被調用顯示時(選項6),它也不會顯示從選項6輸入的信息。我覺得它是因爲我不能顯示在我的courseModule類中添加super(courseName)。任何幫助,不勝感激,虐待留下所有相關的代碼。當調用

AppMain類

import java.util.*; 
public class AppMain { 
// ArrayList of students 
private static ArrayList<Student> studentList = new ArrayList<Student>(); 
// ArrayList of teachers 
private static ArrayList<Teacher> teacherList = new ArrayList<Teacher>(); 
// ArrayList of Modules 
private static ArrayList<CourseModule> ModuleList = new ArrayList<CourseModule>(); 


public static void main(String[] args) 
{ 
    menu(); 
} 

public static void menu() 
{ 




    int option = 0; 

    while(option != 7) { 

     Scanner in = new Scanner(System.in); 

     // Creates main menu 
     System.out.println("Choose an option."); 
     System.out.println("[1] Create student"); 
     System.out.println("[2] Display students"); 
     System.out.println("[3] Create Teacher"); 
     System.out.println("[4] Display Teachers"); 
     System.out.println("[5] Create Module"); 
     System.out.println("[6] Display Modules"); 
     System.out.println("[7] Exit"); 
     System.out.print("Option? "); 
     option = in.nextInt(); 

     // Switch case for dealing with choice 
     // Each case is braced {} so convinient variable names can be reused.. 
     // .. without fear of variables 'carrying over' 
     switch (option) { 
      case 1: { 
       String name; 
       int grade; 
       System.out.println("Enter name: "); 
       in.nextLine(); // Stops the program 'running' over the next line 
       name = in.nextLine(); 
       System.out.println("Enter grade: "); 
       grade = in.nextInt(); 
       if (grade <= 100 && grade >= 0) { 
        Student student = new Student(name, grade); 
        studentList.add(student); 
        //error message 
       } else { 
        System.out.println("Grade not in boundaries!\nUser not added \nEnter grade between 0-100."); 
       } 
       break; 
      } 
      case 2: { 
       Iterator<Student> itr = studentList.iterator(); 
       while (itr.hasNext()) { 
        Student student = itr.next(); 
        System.out.println(student.toString() + "\n"); 
       } 
       break; 
      } 
      case 3: { 
       String name; 
       System.out.println("Enter Teachers name: "); 
       in.nextLine(); 
       name = in.nextLine(); 
       Teacher teacher = new Teacher(name); 
       teacherList.add(teacher); 
       break; 
      } 
      case 4: { 
       // Iterators through the teachList ArrayList.. 
       // .. and prints each one with a line between 
       Iterator<Teacher> itr = teacherList.iterator(); 
       while (itr.hasNext()) { 
        Teacher teacher = itr.next(); 
        System.out.println(teacher.toString() + "\n"); 
       } 
       break; 
      } 
      case 5: { 

       String name; 
       String courseName; // defining module String 
       System.out.println("Enter Teachers name: "); 
       in.nextLine(); 
       name = in.nextLine(); 
       Teacher teacher = new Teacher(name); 
       teacherList.add(teacher); 
       System.out.println("Enter Module Name: "); 
       courseName = in.nextLine(); 
       CourseModule Module = new CourseModule(courseName, teacher); 
       ModuleList.add(Module); 
       break; 



      } 
      case 6: { 
       // Iterator<Teacher> itr = teacherList.iterator(); 
      // while (itr.hasNext()) { 
       //  Teacher teacher = itr.next(); 
      //  System.out.println(teacher.toString() + "\n"); 
      // } 
       Iterator<CourseModule> itr = ModuleList.iterator(); 
       while (itr.hasNext()) { 
        CourseModule Module = itr.next(); 
        System.out.println(Module.toString() + "\n"); 
       } 

而且繼承人的CourseModule類

import java.util.*; 
public class CourseModule implements Course { 

String courseName; 
Teacher teacher; 
ArrayList<Student> students = new ArrayList<Student>(); 


public CourseModule (String courseName, Teacher teacher) { 

} 

@Override 
public void assignTeacher(Teacher teacher) { 


} 

@Override 
public void enrolStudent(Student student) { 

} 

我有一種感覺它做的courseModule公共courseModule但一切我都試過不行的,任何幫助非常感謝卡住了30分鐘,發現其他地方沒有幫助

+0

這是一個家庭作業?如果是的話,請將作業標籤 – FrankK

+0

包括在我的一個Uni工作室的作業中,只是看到了你的編輯,不知道這是一件現在會做的事 –

+0

好,那麼,它不顯示「[6]顯示模塊「部分,或者外殼6下的部分:? – FrankK

回答

3

您的構造函數中沒有代碼,因此更改爲

public CourseModule (String courseName, Teacher teacher) { 
    this.courseName = courseName; 
    this.teacher = teacher; 
} 

在某個階段,你需要在其他的方法來補得

+0

剛剛添加了這個非常感謝,但現在當選項6返回時,它不給我教師名字或詛咒名稱,只是一個哈希? –

+0

如果你想打印你想要打印的東西,你需要自定義toString()方法。生病告訴你一個例子 – FrankK

+0

@FrankK不用擔心!我只是做了一個,非常感謝您的幫助 –

0
public class CourseModule implements Course { 

String courseName; 
Teacher teacher; 
ArrayList<Student> students = new ArrayList<Student>(); 


    public CourseModule (String courseName, Teacher teacher) { 
     this.courseName = courseName; 
     this.teacher = teacher; 
    } 

    @Override 
    public void assignTeacher(Teacher teacher) { 

    } 

    @Override 
    public void enrolStudent(Student student) { 

    } 

    public String toString(){ 
     // i don't know what method's are in your teacher class, so im just guessing here. 
     return courseName + " " + teacher.name; 
    } 
} 

序供您toString方法打印的實際值,你必須添加自己的toString方法。