我创建了一个简单的程序,在这个程序中,用户会被提示提供有关他们就业的各种细节。在我的Employee.java文件中,我有所有的方法。
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)。不过,我的问题是:这是否是一个好的编程实践:
。
Employee.java:(示例1)
//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)
public class UseIt {
static Employee foo = new Employee();
public static void main(String[] args) {
foo.getAllDetails(); //Calling ONE method, rather than all of them.
}
}时自动运行。
Employee.java (例2)
//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)
public class UseIt {
public static void main(String[] args) {
Employee foo = new Employee();
}
}发布于 2015-01-17 17:50:42
所以请原谅任何编译错误。
作为一个新手,有很多东西要学,我首先要了解固体原理,同时也要注意Java程序员犯的十大错误。
您要做的第一件事是避开启动程序的static main(String[] args)方法。您不想使用静态方法。因此,您需要实例化您的系统(MySystem),以便您处于对象的世界中。
你想把你的责任分开。
总是喜欢不变性。
这里我有一个不可变的Employee对象(一旦实例化就不能更改它)。从我们的DetailsRetriever将从用户获得输入的主要方法,这使用DetailsValidator来确保输入是正确的。如果所有细节都正确,则Factory将创建employee对象。
public class UseIt {
public static void main(String[] args) {
MySystem foo = new MySystem(new DetailsRetriever(new DetailsValidator()), new EmployeeFactory())
foo.start();
}
}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);
}
}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;
}
}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 ...
}public class DetailsValidator {
public boolean validateName(String input) {
// add more validation as you need
return !"".equals(input);
}
// etc
}public class EmployeeFactory {
public Employee create(String name, String jobTitle, int age, ... etc) {
return new Employee(name, jobTitle, age .. etc);
}
}https://codereview.stackexchange.com/questions/77901
复制相似问题