我在java上有这个任务我不确定我是否正确这样做

时间:2017-04-22 15:55:39

标签: java

Foo Corporation需要一个计划来计算每小时员工的工资。

美国劳工部要求员工在一周内工作的40小时以上的任何时间都能获得一半的工资。

例如,如果一名员工工作45小时,他们可以获得5小时的加班费,是基本工资的1.5倍。

马萨诸塞州要求每小时雇员每小时至少支付8.00美元。

Foo Corp要求员工一周内工作时间不得超过60小时。

员工获得报酬(工作小时数)×(基本工资),每小时最多40小时。对于超过40的每小时,他们得到加班=(基本工资)×1.5。基本工资不得低于最低工资(每小时8.00美元)。如果是,则打印错误。如果小时数大于60,则打印错误消息。

创建一个名为FooCorporation的新类。

编写一种方法,将基本工资和工时作为参数,并打印总工资或错误。编写一个主方法,为每个员工调用此方法:

工作基本工资小时员工1 $ 7.50 35员工2 $ 8.20 47员工3 $ 10.00 73

您必须以此格式从文本文件中读取基本工资和小时数。

Employee, basePay, hours
1, 8, 40,
2, 9, 33,
3, 10, 70

这是我的代码:

package foocorporation;

public class FooCorporation {
    double minimumWage = 8.0;
    int maxHours = 60;
    float x;
    int y;


    public static void main(String[] args) {
        salaryCalculation(x,y, minimumWage, maxHours);
        salaryCalculation(x,y, minimumWage, maxHours);
        salaryCalculation(x,y, minimumWage, maxHours);

   }
     public static void salaryCalculation
    (double basePay, int hoursWorked, double minimumWage, int maxHours){
        double totalSalary = 0;
        if ((basePay < minimumWage) || (hoursWorked > maxHours)){
            System.out.println("Error!");
        }
        else {  
            if (hoursWorked > 40){
                totalSalary = basePay * 40 + 1.5*basePay*(hoursWorked - 40);
            }
            else {
                totalSalary = basePay * hoursWorked;
            }
            System.out.println("Your total salary is " + totalSalary);
        }
    }

}

2 个答案:

答案 0 :(得分:0)

看看这个

<强>更新  如果您想按照稍后的要求从用户那里获取输入

public class FooCorporation {
    static final double minimumWage = 8.0; // static means that can be accessed without creating an object of the class and instantiating it
    static final int maxHours = 60; // final means constant = cannot be changed after declared
    static double basePay=0; // initialize them to zero, at least you know your program won't throw null pointer exception if forgot to set their 
                             //values before invoking the method
    static int hoursWorked=0;


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        for (int i=1; i<=3; i++, System.out.println()){
           System.out.println("For Employee Number: " + i);
           System.out.println("Enter Base Pay:");
           basePay = in.nextDouble();
           System.out.println("Enter Hours Worked:");
           hoursWorked = in.nextInt();
           salaryCalculation(); 
       }
   }
     public static void salaryCalculation(){
        double totalSalary = 0;
        if ((basePay < minimumWage) || (hoursWorked > maxHours)){
            System.out.println("Error!");
        }
        else {   

        if (hoursWorked > 40){
                    totalSalary = basePay * 40 + 1.5*basePay*(hoursWorked - 40);
                }
                else {
                    totalSalary = basePay * hoursWorked;
                }
                System.out.println("Your total salary is " + totalSalary);
            }
        }

    }

结果将用于您的输入,例如:

For Employee Number: 1
Enter Base Pay:
7.5
Enter Hours Worked:
35
Error!

For Employee Number: 2
Enter Base Pay:
8.2
Enter Hours Worked:
47
Your total salary is 414.1

For Employee Number: 3
Enter Base Pay:
10
Enter Hours Worked:
37
Your total salary is 370.0

答案 1 :(得分:-1)

package foocorporation;

public class Foocorporation {
    final static double minimumWage = 8.0;
    final static int maxHours = 60;   
public static void main(String[] args) {
    float x;
    int y;
    x=7.5f;
    y=35;
    salaryCalculation(x,y);
    x=8.2f;
    y=47;
    salaryCalculation(x,y);
    x=10.0f;
    y=73;
    salaryCalculation(x,y);
   }
public static void salaryCalculation(double basePay, int hoursWorked){
    double totalSalary = 0;
    if ((basePay < minimumWage) || (hoursWorked > maxHours)){
    System.out.println("Error!");
}
    else {  
        if (hoursWorked > 40){
        totalSalary = basePay * 40 + 1.5*basePay*(hoursWorked - 40);
        }
        else {
        totalSalary = basePay * hoursWorked;
        }
        System.out.println("Your total salary is " + totalSalary);
    }
}
}
相关问题