首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提示用户提供就业信息。

提示用户提供就业信息。
EN

Code Review用户
提问于 2015-01-17 17:29:23
回答 1查看 2.2K关注 0票数 1

我创建了一个简单的程序,在这个程序中,用户会被提示提供有关他们就业的各种细节。在我的Employee.java文件中,我有所有的方法。

Employee.java:

代码语言:javascript
复制
import javax.swing.JOptionPane;

public class Employee {

    private static String name;
    private static String jobTitle;
    private static int age;
    private static int salary;
    private static int taxAmount;
    private static int salaryPostTax;

    public static int getSalary() {
        try {
            String inputSalary = JOptionPane.showInputDialog("Enter your salary:");
            salary = Integer.parseInt(inputSalary);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "ERROR! That's NOT a numeric value.");
        }
        return salary;
    }

    public static String getName() {
        name = JOptionPane.showInputDialog("Enter your name:");
        return name;
    }

    public static String getJobTitle() {
        jobTitle = JOptionPane.showInputDialog("Enter your Job Title:");
        return jobTitle;
    }

    public static int getAge() {
        try {
            String inputAge = JOptionPane.showInputDialog("Enter your age:");
            age = Integer.parseInt(inputAge);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "ERROR! That's NOT a numeric value.");
        }
        return age;
    }

    public static int calcTaxAmount () {

        if (salary < 10_000)
            taxAmount = 0;

        if ((salary >= 10_000) && (salary < 30_000))
            taxAmount = 28;

        if ((salary >= 30_000) && (salary < 80_000))
            taxAmount = 38;

        if (salary >= 80_000)
            taxAmount = 48;

        return taxAmount;
    }

    public static int calcSalary () {
        salaryPostTax = salary - taxAmount;

        return salaryPostTax;
    }

    public static void showPayment() {
        if (inputValid() == true) {

            JOptionPane.showMessageDialog(null, "Pay to the order of " + name + " (" + jobTitle +
                    ") the sum of £"+ salaryPostTax);
        } else {
            JOptionPane.showMessageDialog(null, "ERROR! Please fill in ALL the required fields correclty.");
        }
    }

    public static boolean inputValid() {
        if (
                (name != null) &&
                (jobTitle != null) &&
                (!(salary <= 0)) &&
                (!(salaryPostTax <= 0)) &&
                (!(age <= 0))) {
            return true;

        } else {
            return false;
        }
    }
}

我有一个使用类的文件(UseIt.java)。不过,我的问题是:这是否是一个好的编程实践:

1.将所有的方法放在一个方法中,只需调用一个方法,而不是单独调用它们,这就变成了累人的?

Employee.java:(示例1)

代码语言:javascript
复制
//Collate ALL Employee details into ONE method
public static void getAllDetails() {
    Employee.getName();
    Employee.getAge();
    Employee.getJobTitle();
    Employee.getSalary();
    Employee.calcTaxAmount();
    Employee.calcSalary();
    Employee.showPayment();
}

UseIt.java:(示例1)

代码语言:javascript
复制
public class UseIt {

    static Employee foo = new Employee();

    public static void main(String[] args) {
        foo.getAllDetails(); //Calling ONE method, rather than all of them.
    }
}

2.将所有方法放入构造函数中,以便方法在创建实例?

时自动运行。

Employee.java (例2)

代码语言:javascript
复制
//Collate ALL Employee details into the constructor
public Employee() {
    Employee.getName();
    Employee.getAge();
    Employee.getJobTitle();
    Employee.getSalary();
    Employee.calcTaxAmount();
    Employee.calcSalary();
    Employee.showPayment();
}

UseIt.java (例2)

代码语言:javascript
复制
public class UseIt {

    public static void main(String[] args) {
        Employee foo = new Employee();
    }
}
EN

回答 1

Code Review用户

回答已采纳

发布于 2015-01-17 17:50:42

所以请原谅任何编译错误。

作为一个新手,有很多东西要学,我首先要了解固体原理,同时也要注意Java程序员犯的十大错误

您要做的第一件事是避开启动程序的static main(String[] args)方法。您不想使用静态方法。因此,您需要实例化您的系统(MySystem),以便您处于对象的世界中。

你想把你的责任分开。

总是喜欢不变性。

这里我有一个不可变的Employee对象(一旦实例化就不能更改它)。从我们的DetailsRetriever将从用户获得输入的主要方法,这使用DetailsValidator来确保输入是正确的。如果所有细节都正确,则Factory将创建employee对象。

代码语言:javascript
复制
public class UseIt {

    public static void main(String[] args) {
        MySystem foo = new MySystem(new DetailsRetriever(new DetailsValidator()), new EmployeeFactory())
        foo.start();
    }
}
代码语言:javascript
复制
public class MySystem {

   private final DetailsRetreiver detailsRetriever;
   private final EmployeeFactory employeeFactory;

   public MySystem(DetailsRetreiver detailsRetriever, EmployeeFactory employeeFactory) {
         this.detailsRetriever = detailsRetriever;
         this.employeeFactory = employeeFactory;
   }

   public void start() {
         String name = detailsRetriever.retrieveName();
         String jobTitle = detailsRetriever.retrieveJobTitle();
         ..etc

         Employee employee = employeeFactory.create(name, jobTitle, ..etc);
   }

}
代码语言:javascript
复制
public class Employee {

    private final String name;
    private final String jobTitle;
    private final int age;
    private final int salary;

    public Employee(String name, String jobTitle, int age, int salary) {
        this.name = name;
        this.jobTitle = jobTitle;
        this.age = age;
        this.salary = salary;
    }

   public int calcTaxAmount() {
    int calculatedTax;
    if (salary < 10_000)
        calculatedTax = 0;

    if ((salary >= 10_000) && (salary < 30_000))
        calculatedTax = 28;

    if ((salary >= 30_000) && (salary < 80_000))
        calculatedTax = 38;

    if (salary >= 80_000)
        calculatedTax = 48;

    return taxAmount;
}

   public int calcSalary() {
    int calculatedSalaryPostTax = salary - calcTaxAmount();

    return calculatedSalaryPostTax;
   }

}
代码语言:javascript
复制
public class DetailsRetreiver {

    private final DetailsValidator detailsValidator;

    public DetailsRetreiver(DetailsValidator detailsValidator) {
      this.detailsValidator - detailsValidator;
    }

    public String getName() {
        String name = JOptionPane.showInputDialog("Enter your name:");
        if(detailsValidator.validateName(name)) {
          return name;
        } else {
           return getName(); // or throw an exception
        }
    }

    // etc ...

}
代码语言:javascript
复制
public class DetailsValidator {

     public boolean validateName(String input) {
          // add more validation as you need
          return !"".equals(input);
     }

     // etc
}
代码语言:javascript
复制
public class EmployeeFactory {

      public Employee create(String name, String jobTitle, int age, ... etc) {
           return new Employee(name, jobTitle, age .. etc); 
      }

}
票数 5
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/77901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档