2012-06-01 52 views
0

我正在寫一個bluej程序,它由幾個獲取用戶輸入並將它們保存爲字符串數據的類組成。這些類會覆蓋其他方法,並且將顯示在名爲CollegeList的最終類中。然而,對於CollegeList類我不允許在我的任務中擴展這些子類。相反,我打算使用bluej'使用關係'並在每個循環中輸出這些類的輸入和輸出。如何才能做到這一點?這裏是我的一些代碼:java使用關係

// College List 
import java.util.*; 
import java.io.*; 
import java.util.ArrayList; 
import java.util.Scanner; 


public class CollegeList 
{ 
    //Input Reader 
    Scanner scanner = new Scanner(System.in); 
    private ArrayList<Person> people; 

    //Main Public Method 
    public CollegeList() 
    { 
     people=new ArrayList<Person>(); 
    }  

    public void main()//not allowed to extend 
    { 
     dataEntry();  
    } 

    public void getPeople(Person persons) 
    { 
     people.add(persons); 
     System.out.println(people);; 
    } 

    //*** Attempting to output preceding class Person in loop - Must be done this way 
    public void dataEntry() 
    { 
     for(Person persons: people) 
     { 
      persons.dataEntry(); 
     } 
    } 
} 

Person類:

import java.util.*; 
import java.io.*; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Person extends Student 
{ 
    // instance variables for data types 
    public ArrayList<String> firstName; 
    public ArrayList<String> lastName; 
    public ArrayList<String> streetAdress; 
    public ArrayList<String> postCode; 
    public ArrayList<String> phoneNumber; 
    Scanner scanner = new Scanner(System.in); 

    /** 
    * Constructor for objects personal details. 
    */ 
    public Person() 
    { 
     firstName = new ArrayList<String>(); 
     lastName = new ArrayList <String>(); 
     streetAdress = new ArrayList<String>(); 
     postCode = new ArrayList<String>(); 
     phoneNumber = new ArrayList<String>(); 
    }  

    /** 
    * Allows User to Enter Details into class Person and Displays it. 
    */ 
    public void dataEntry() 
    { 
     System.out.print("Enter First Name: "); 
     firstName.add(scanner.nextLine()); 
     System.out.print("Enter Last Name: "); 
     lastName.add(scanner.nextLine()); 
     System.out.print("Enter Street Adress: "); 
     streetAdress.add(scanner.nextLine()); 
     System.out.print("Enter Post Code: "); 
     postCode.add(scanner.nextLine()); 
     System.out.print("Enter Phone Number: "); 
     phoneNumber.add(scanner.nextLine()); 
     //display persons information on single line 
     System.out.println("The details are - " + 
          "Name: " + 
          firstName + "," + 
          "Surname: " + 
          lastName + "," + 
          "Street Adress: " + 
          streetAdress + "," + 
          "Post Code: " + 
          postCode + "," + 
          "Phone Number: " + 
          phoneNumber + "."); 
    } 
} 
+0

請再次解釋你正在努力完成什麼? – tgoossens

+0

當然,我最初只是擴展了頂級類來獲取它的方法內容,但我的講師似乎希望我使用for循環來獲取「person」的內容。 –

回答

2

從我從你的問題理解的是,你被要求使用組成,而不是繼承

此外,還有一些你應該糾正基本的東西:

  • Person類不應該有firstName, lastNameList應該相當有他們那樣簡單String,然後你可以創建一個Person作爲List需要。然後,您可以使用來循環,以便根據需要遍歷此列表。

  • Person不應該延伸Student,因爲從邏輯上講,每個人都不是學生。

+0

謝謝你,組成是我正在尋找。你爲我節省了很多時間:) –