如何在不声明大量对象名称的情况下添加新的Employee

时间:2017-10-06 11:04:22

标签: java eclipse oop java-ee

我在工作中添加和显示不同类型的员工(差异部门)以及不同的工资和薪酬方面做了一个小小的任务。好处,使用OOP方法。我不确定我的代码在代码重用和代码方面是否正确。如果我真的符合OOP编码方法......到目前为止,我已经显示了每个类型/部门的1名员工,我将它们作为班级名称。(参见下面的代码)。我的问题是,如果我添加一个新员工,我将再次声明另一个类型为Employee的对象。如果会有很多员工,我会有很多对象。我如何减轻这一点,并且我可以知道我的OOP编码方法到目前为止是否正确?太非常了!这是我的代码:

        //this is my parent class which implements an interface...
        public abstract class Employees implements ICompensation{
        private String fname;
        private String lname;
        private char gender;
        private String address;
        private double salary;

        public String getfname(){
            return this.fname;
        }

        public void setfname(String fname){
            this.fname = fname;
        }

        public String getLname() {
            return lname;
        }

        public void setLname(String lname) {
            this.lname = lname;
        }

        public char getGender() {
            return gender;
        }

        public void setGender(char gender) {
            this.gender = gender;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public double getSalary() {
            return salary;
        }

        public void setSalary(double salary) {
            this.salary = salary;
        }
        }

    // this is a Developer type of employee

        public class Developer extends Employees{

        public Developer(String fname,String lname, char gender,String address, double salary){
            setfname(fname);
            setLname(lname);
            setGender(gender);
            setAddress(address);
            setSalary(salary);
        }

        @Override
        public double calculateSalary() {
            double salary = getSalary();
            salary += 10;
            return salary;
        }

        @Override
        public void print() {
            System.out.println(this.getClass());
            System.out.println(this.getfname());
            System.out.println(this.getLname());
            System.out.println(this.getGender());
            System.out.println(this.getAddress());
            System.out.println(this.calculateSalary());}}

    //this is QA type of employee

        public class QA extends Employees{

        public QA(String fname,String lname,char gender,String address,double salary) {
            setfname(fname);
            setLname(lname);
            setGender(gender);
            setAddress(address);
            setSalary(salary);
        }

        @Override
        public double calculateSalary() {
            double salary = getSalary();
            salary = salary + 20;
            return salary;

        }

        @Override
        public void print() {
            System.out.println(this.getClass());
            System.out.println(this.getfname());
            System.out.println(this.getLname());
            System.out.println(this.getGender());
            System.out.println(this.getAddress());
            System.out.println(this.calculateSalary()); 
        }
        }

我还有另外两个班级是BA&管理器类,但我不会在这里包含,因为它只是与其他派生类具有相同的内容。

    //so here is my Interface
        public interface ICompensation {
        double calculateSalary();
        void print();
        }

    //and here is my main method.
    import java.util.*;

public class main {
    public static void main(String[] args){
        Employees dev = new Developer("Janel","Logrono",'M',"Alabang",491);
        Employees qa = new QA("juan","Sir",'M',"Taguig",1240);
        Employees ba = new BA("pedro","Lyn",'F',"Taguig",1150);
        Employees manager = new Manager("sebastian","rods",'M',"USA",555399);

        ArrayList<Employees> ls = new ArrayList<>();    
        ls.add(dev);
        ls.add(qa);
        ls.add(ba);
        ls.add(manager);

        for(Employees e : ls){
            e.print();
            System.out.println();
        }
    }
}

如何添加另一名员工而不宣布大量对象,并且我可以知道我的OOP编码方法到目前为止是否正确,我认为这里有很多冗余代码,如何减少它? THX!

1 个答案:

答案 0 :(得分:-1)

首先,您不应该在bean类中实现ICompensation。 Bean类只包含getter,setter和amp;构造函数。您需要创建另一个将实现ICompensation的类。在那里,您将编写用于计算和其他方法的代码。

在数据库中,您可以添加另一列“角色”,该列将定义员工角色。通过这种方法,您无需创建额外的方法,如QA,Developer,Manager。

请查看以下链接。在这里,他们尝试使用MVC模型开发登录页面。您可以忽略jsp页面并专注于控制器和bean。 https://krazytech.com/programs/a-login-application-in-java-using-model-view-controllermvc-design-pattern