创建方法时的类返回类型

时间:2016-10-14 19:47:06

标签: java oop

[UML图表] [1]

valid XHTML

我正在为下周的期中考试而学习,我正在练习我教授的一些例子。但是,我在使用类返回类型方法时遇到了一些麻烦。

我附上了UML图以防万一。

我想要了解的是Job类中的getPerson方法。我认为我不需要Job类中的数组列表来存储所有员工。因为我在公司类中已经有一个数组列表。返回类型也是Employee类,我不知道如何使用此类返回类型获取人员信息。

我的问题

  1. 公共员工getPerson(){}在Job类
  2. Job类中的公共布尔值isVacant(){}
  3. 如果正确构建了getVacantJobs,getFilledJobs和getAllJobs方法,你还要介意吗?
  4. 我使用iterator显示所有存储的作业。

    ---------------------------员工类------------------ -----------

    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 + "]";
        }
    
    }
    

    ---------------------------- Job Class ----------------- ---------------------

    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 个答案:

答案 0 :(得分:1)

将字段person添加到Job类。

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;
    }
}

并将jobs字段添加到Company类。

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;
    }
}