我尝试搜索这个答案并找到了几个类似于我正在寻找的答案,但我似乎无法将对无关数据提供的建议应用于我的特定程序。
我需要使用我创建的这个工作代码(一个计算员工年度薪酬的程序)并以显示两个班级的方式进行调整(根据我的家庭作业指示)。给出的输出正是我想要的,我只需要帮助来重新组织代码,因此不仅仅是主类。这是我能在这里得到帮助的吗?
这是我的工作代码:
public class AnnualCompensation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//create salary variable and commission percentage variable
double salary = 60000;
double commissionPercentage = 0.06;
//create command output for the user to get directions to enter value
System.out.println("Enter total amount of sales for the year: ");
double value = input.nextDouble();
double totalCompensation = (value * commissionPercentage) + salary;
System.out.println("Your annual compensation is: " + "$" + totalCompensation);
}
}
提前致谢。
答案 0 :(得分:1)
Employee
和salary
作为字段的班级commissionPercentage
。 Employee
中创建一个方法,该方法将vavlue
作为输入,并计算补偿并将其返回。Employee
类的实例并调用calculateMethod。答案 1 :(得分:1)
我会用这些类构建它:
AnnualCompensationCalculator
将为您作为实用程序类进行计算,并且AnnualCompensation
主要课程,专注于请求用户输入(并会调用计算器)。答案 2 :(得分:0)
假设您可以在新类中移动逻辑。
AnnualCompensationCalculator.java
public class AnnualCompensationCalculator{
private static double commissionPercentage = 0.06;
public static double calculateCompensation(double sales ,double salary){
return((sales * commissionPercentage) + salary);
}
}
AnnualCompensation .java
public class AnnualCompensation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//create salary variable and commission percentage variable
double salary = 60000;
//create command output for the user to get directions to enter value
System.out.println("Enter total amount of sales for the year: ");
double value = input.nextDouble();
System.out.println("Your annual compensation is: " + "$" + AnnualCompensationCalculator.calculateCompensation(value,salary));
}
}
答案 3 :(得分:0)
遵循面向对象编程,我建议你创建一个新的类Employee
,它保存员工的薪水和薪酬百分比,还有一个计算薪酬的方法。
像这样:
class Employee {
private double salary;
private double commPercent;
public Employee(double salary, double commPercent) {
this.salary = salary;
this.commPercent = commPercent;
}
public double calculateCompensation(double totalSales) {
return (totalSales * commPercent) + salary;
}
/* Setters & Getters */
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getCommPercent() {
return commPercent;
}
public void setCommPercent(double commPercent) {
this.commPercent = commPercent;
}
}
然后让你的主类使用这个Employee
类来完成所有工作:
public class AnnualCompensation {
public static void main(String[] args) {
//Initialize an Employee object
Employee emp = new Employee(60000, 0.06);
//Create command output for the user to get directions to enter value
System.out.print("Enter total amount of sales for the year: ");
Scanner input = new Scanner(System.in);
double salesAmt = input.nextDouble();
//Calculate the compensation based on the user input then print it
System.out.println("Your annual compensation is: $"
+ emp.calculateCompensation(salesAmt));
}
}