2012-11-13 31 views
2

我有三個不同的課程,一個用於研究生,本科生以及普通學生類,我需要弄清楚如何從主方法中將學生添加到數組列表中。我無法弄清楚如何添加名字,因爲它是私人的,它需要保持私密。我需要創建一個程序,用於添加,刪除,列出,保存和排序創建的學生,本科生和研究生課程中的學生

package enrollmentdatabase; 

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

public class EnrollmentDataBase { 

public static void main(String[] args) { 
    Scanner input = new Scanner (System.in); 
    String option = optionChoise(input); 
    String option1 = "ADD";//ADD option 
    String option2 = "REMOVE";//REMOVE OPTION 
    String option3 = "LIST";//LIST OPTION 
    String option4 = "SAVE";//SAVE OPTION 
    String option5 = "SORT";//SORT OPTION 
    ArrayList studentList = new ArrayList(); 

}//end of main method 
public static String optionChoise(Scanner input){ 
    String opt1 = "ADD";//ADD option 
    String opt2 = "REMOVE";//REMOVE OPTION 
    String opt3 = "LIST";//LIST OPTION 
    String opt4 = "SAVE";//SAVE OPTION 
    String opt5 = "SORT";//SORT OPTION 
    System.out.println("Enter what you want to do(ADD, REMOVE, LIST, SAVE, or SORT): "); 
    String opt = input.nextLine(); 
    if((opt.compareToIgnoreCase(opt1)) !=0 || (opt.compareToIgnoreCase(opt1)) != 0 || (opt.compareToIgnoreCase(opt1)) !=0 
      || (opt.compareToIgnoreCase(opt1)) !=0 || (opt.compareToIgnoreCase(opt1)) !=0){//enter this if conditional in order to establish that the input is valid 
     System.out.println("This is not a valid input, please enter in what you want to do: "); 
     opt = input.nextLine(); 
    }//end of if conditional 
    return opt; 
}//end of option method 
public static ArrayList addList(ArrayList studentList, Scanner input){ 
    System.out.println("Enter the Student's first name: "); 
    String nameFirst= input.nextLine(); 
    Student student1 = new Student(); 
    student1.firstName = nameFirst; 
    System.out.println("Enter the Student's last name: "); 
    System.out.println("Enter the Student's UID: "); 
    System.out.println("Enter the Student's status: "); 
    System.out.println("Enter YES for having a thesis option, else NO: "); 
    System.out.println("Enter Masters for Master Studies of PHD for PHD studies: "); 
    System.out.println("Enter the name of the major professor: "); 
    System.out.println("Enter the student class standing: "); 
    System.out.println("Enter the Student's major: "); 
    System.out.println("Enter the student's overall GPA: "); 
    System.out.println("Enter the student's major GPA: "); 
}//end of addList method 
}//end of class 
+0

getter和setter。 – Max

+0

神聖......?你在發明數據庫嗎? – Juvanis

+2

@BlueBullet ..我會說「製造數據庫」..;) –

回答

1

我無法弄清楚如何,因爲這是私人加名字,

公共的getterpublic的setter方法私人數據和通過它們訪問它。

例如:

class Student { 
    private String fName; 
    public void setFname(String fName){ 
    this.fName = fName; 
    } 
    public String getFName(){ 
    return fname; 
    } 
    } 
class AnotherClass { 
public static void main(String...args){ 
student s = new Student(); 
System.out.println(s.getFName());//to access fName(which is private) in class Student. 
} 

}