2016-10-14 17 views
0

[UMLダイアグラム]を作成するときに、[1]クラス戻り値の型メソッド

valid XHTML

私は来週中間試験のために勉強していると私は私の教授からいくつかの特定の例を練習しています。しかし、私はクラス戻り型のメソッドでいくつかの問題を抱えています。

念のためにUML図が添付されています。

私が理解しようとしているのは、ジョブクラスのgetPersonメソッドです。私はすべての従業員を格納するためにジョブクラスの配列リストが必要とは思わない。私はすでにCompanyクラスに配列リストを持っているからです。また、戻り値の型はEmployeeクラスです。このクラスの戻り値の型を使用して人の情報を取得する方法がわかりません。ジョブ・クラスで

私の問題

  1. 公務員getPerson(){}
  2. 公共ブールisVacant(){}ジョブ・クラスに
  3. はまた、あなたはgetVacantJobs、getFilledJobs、およびgetAllJobs方法を確認する気になりそれらが正しく構築されていれば?

私はすべての保存されたジョブを表示するためにイテレータを使用しました。

---------------------------従業員クラス----------------- ------------

public class Employee { 
    private String name; 
    private int id; 

    public Employee(int id, String name) { 
     this.name = name; 
     this.id =id; 

    } 

    public final String getName() { 
     return name; 
    } 

    public final void setName(String name) { 
     this.name = name; 
    } 

    public final int getId() { 
     return id; 
    } 

    public final void setId(int id) { 
     this.id = id; 
    } 

    @Override 
    public String toString() { 
     return "Employee [name=" + name + ", id=" + id + "]"; 
    } 

} 

----------------------------ジョブクラス - -------------------------------------

public class Job { 

    private String description; 

    private int id; 
    private double maxSalary; 

    public Job(int id, double maxSalary, String description) { 
     this.description = description; 
     this.id = id; 
     this.maxSalary = maxSalary; 
    } 

    public Job(int id, double maxSalary, String description, Employee e1) { 
     this.description = description; 
     this.id = id; 
     this.maxSalary = maxSalary; 



    } 

    @Override 
    public String toString() { 

     return "Job [description=" + description + ", id=" + id 
       + ", maxSalary=" + maxSalary + "]"; 
    } 

    public final String getDescription() { 
     return description; 
    } 

    public final void setDescription(String description) { 
     this.description = description; 
    } 

    public final double getMaxSalary() { 
     return maxSalary; 
    } 

    public final void setMaxSalary(double maxSalary) { 
     this.maxSalary = maxSalary; 
    } 

    public final int getId() { 
     return id; 
    } 

    public Employee getPerson() { 
     retrun 
    } 

    public final void setPerson(Employee person) { 
     this.id = person.getId(); 

    } 
} 

------ --------------------会社のクラス---------------------------

import java.util.ArrayList; 
import java.util.Iterator; 

public class Company { 

    static ArrayList list = new ArrayList(); 
    Iterator itr = list.iterator(); 


    private String name; 

    public Company(String name) { 
     this.name = name; 
    } 

    public Company() { 
     // TODO Auto-generated constructor stub 
    } 

    public static void addJob(Job j1) { 
     list.add(j1); 
    } 

    public void removeJob(int id) { 
     list.remove(id); 

    } 


    public ArrayList<Job> getVacantJobs() { 
     while (itr.hasNext()) { 
      if ((itr == null)) { 
       System.out.println(itr); 
      } 
     } 
     return null; 

    } 

    public ArrayList<Job> getFilledJobs() { 
     while (itr.hasNext()) { 
      if (!(itr == null)) { 
       System.out.println(itr); 
      } 
     } 
     return null; 
    } 

    public ArrayList<Job> getAllJobs() { 
     while (itr.hasNext()) { 
      System.out.println(itr.next()); 
     } 
     return null; 
    } 

} 
+1

あなたが「私の問題」と言うとき:1.ジョブクラスの「public Employee getPerson(){}」は、あなたの問題が何処にあるのかを説明していません。問題を本当に説明していないので、あなたの質問は話題にはなりません。 –

+0

私が見る1つの問題は、あなたが 'Employee'と' Job'に 'id'属性を混同していることです。 UMLダイアグラムは、 'Employee.id'は文字列でありintではないことを示しています。したがって、あなたのメソッド 'Job.setPerson()'はあまり意味がありません。 – Turamarth

+0

あなたのアドバイスありがとう、私はそれを念頭に置いておきます。 –

答えて

1

フィールドを追加personJobクラス。

public class Job { 

    // ..... 

    private Employee person; 

    public Employee getPerson() { 
     return person; 
    } 

    public final void setPerson(Employee person) { 
     this.person = person; 
    } 

    public boolean isVacant() { 
     return person == null; 
    } 
} 

そしてCompanyクラスにjobsフィールドを追加します。

public class Company { 

    // static ArrayList list = new ArrayList(); // You don't need this 
    // Iterator itr = list.iterator(); // You don't need this. 

    // ..... 

    private ArrayList<Job> jobs = new ArrayList<>(); 

    public ArrayList<Job> getVacantJobs() { 
     ArrayList<Job> result = new ArrayList<>(); 
     for (Job job : jobs) 
      if (job.isVacant()) 
       result.add(job); 
     return result; 
    } 

    public ArrayList<Job> getFilledJobs() { 
     ArrayList<Job> result = new ArrayList<>(); 
     for (Job job : jobs) 
      if (!job.isVacant()) 
       result.add(job); 
     return result; 
    } 

    public ArrayList<Job> getAllJobs() { 
     ArrayList<Job> result = new ArrayList<>(); 
     for (Job job : jobs) 
      result.add(job); 
     return result; 
    } 
} 
+0

これをすべて入力していただきありがとうございます。あなたはこのウルム図を理解しているように思えます。私の最後の質問はJobクラスで、nullをretrurnするisvacantメソッドがあります。 arraylistに少なくとも1人の従業員が保管されているかどうかをチェックする方法ですか?またはジョブ配列で空の場合は? –

+0

'isVacant()'は、ジョブが人に割り当てられているかどうかを返します。 nullを返すことはありません。 – saka1029

関連する問題