【问题标题】:Employee Salary Calculation员工工资计算
【发布时间】:2019-09-22 07:46:33
【问题描述】:

任务说明

我有这个问题陈述: 使用以下私有成员变量创建一个 Employee 类。

int employeeId
String employeeName
double salary
double netSalary

在 Employee 类中包含适当的 getter 和 setter 方法。在 Employee 类中编写以下方法: public void calculateNetSalary(int pfpercentage) - 此方法应以 PF 百分比作为参数。从工资中扣除 PF 金额并设置 netSalary。

创建一个 Main 类,该类具有调用该方法以获取输入并打印详细信息的 main 方法,如示例中所示。

也写一个方法:

public static Employee getEmployeeDetails() - 获取员工详细信息 - id、姓名和薪水,并返回员工对象。

public static int getPFPercentage() - 获取 PF 百分比并返回相同

在main方法中调用上述两个方法,然后调用Employee类中的calculateNetSalary方法,打印输出如下图。

示例输入 1:

输入 ID: 101 输入名字: 维韦克 输入工资: 20000 输入 PF 百分比: 7

样本输出 1:

ID:101

姓名:维韦克

工资:20000.0

净工资:18600.0


我做了什么

我在 Employee.java 中编写了 getter & setters 方法和 calculateNetSalary() 方法。我被困在 Main.java 中应该写什么以及如何写

Employee.java

public class Employee{

    private int employeeId;
    private String employeeName;
    private double salary;
    private double netSalary;

    //setters
    public void setEmployeeId(int employeeId){
        this.employeeId=employeeId;
    }
    public void setEmployeeName(String employeeName){
        this.employeeName=employeeName;
    }
    public void setSalary(double salary){
        this.salary=salary;
    }
    public void netSalary(double netSalary){
        this.netSalary=netSalary;
    }

    //getters
    public int getEmployeeId(){
        return employeeId;
    }
    public String getEmployeeName(){
        return employeeName;
    }
    public double getSalary(){
        return salary;
    }
    public double getNetSalary(){
        return netSalary;
    }

    public void calculateNetSalary(int pfpercentage){

        pfamount=salary*pfpercentage;
        netSalary=salary-pfamount;
    }
}

Main.java

import java.util.Scanner;
public class Main{
    public staic void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Employee emp = new Employee();

        System.out.println("Enter Id:"+setEmployeeId(sc.nextInt()))
        System.out.println("Enter Name:"+setEmployeeName(sc.next()));
        System.out.println("Enter salary:"+setSalary(sc.nextDouble()));

        System.out.println("Enter PF percentage:");
        double pfpercentage = sc.nextDouble();

        public static Employee getEmployeeDetails(){


        }

        public static int getPFPercentage(){

        }

    }
}

我无法完成 Main.java,因为我不确定要写什么以及如何写。

【问题讨论】:

  • 你是说你不能计算百分比吗?请澄清你被困在什么地方。
  • 其他方法中不能有方法。所以从删除这两个空方法开始。 setEmployeeId()setEmployeeName()setSalary() 是 Employee 类的方法,因此需要在员工上调用它们:emp.setEmployeeId(...)。不要尝试打印某些内容、获取输入并将员工 ID 设置在一行中。这是三个单独的指令,应该在三个单独的行上:1. 打印一条消息,2. 读取员工 ID,3. 将员工的 ID 设置为您在步骤 2 中读取的内容。

标签: java oop


【解决方案1】:

这应该是您的代码:

 public void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Employee emp = new Employee();

        emp.setEmployeeId(sc.nextInt());
        emp.setEmployeeName(sc.next()) ;
        emp.setSalary(sc.nextDouble());

        System.out.println("Enter PF percentage:");
        double pfpercentage = sc.nextDouble();
        emp.calculateNetSalary(pfpercentage);
        System.out.println("Salay is " + emp.getNetSalary());
    }

另外请注意,您还没有定义 pfamount 的类型:

 public void calculateNetSalary(double pfpercentage){

        double pfamount=salary*pfpercentage;
        netSalary=salary-pfamount;
    }

您不能在 main() 方法中定义其他方法。 您可以在其中调用其他方法(随心所欲)。

【讨论】:

    【解决方案2】:

    您的代码中有几个问题。首先查看Employee.java,有几个问题:

    1. 设置netSalary 的方法被声明为public void netSalary(double netSalary),而应该是public void SetNetSalary(double netSalary)
    2. public void calculateNetSalary(int pfpercentage) 中的计算看起来不正确。如果您要传入一个双精度数(即 2 代表 2%),那么您需要将其除以 100 以将数字转换为百分比。
    3. 如果要使用变量,则需要声明它们(namelty pfamount 需要声明为 double,然后才能对其进行赋值)。
    4. 您可能需要一个public String toString() 方法来打印您的员工对象。

    所以你最终会得到这样的结果:

    public class Employee{
    
      private int employeeId;
      private String employeeName;
      private double salary;
      private double netSalary;
    
    //setters
      public void setEmployeeId(int employeeId){
          this.employeeId=employeeId;
      }
    
      public void setEmployeeName(String employeeName){
          this.employeeName=employeeName;
      }
    
      public void setSalary(double salary){
          this.salary=salary;
      }
    
      public void setNetSalary(double netSalary){
          this.netSalary=netSalary;
      }
    
      //getters
      public int getEmployeeId(){
          return employeeId;
      }
    
      public String getEmployeeName(){
          return employeeName;
      }
    
      public double getSalary(){
          return salary;
      }
    
      public double getNetSalary(){
          return netSalary;
      }
    
      public void calculateNetSalary(double pfpercentage) {
          double pfamount = salary * (pfpercentage / 100);
          netSalary = salary - pfamount;
      }
    
      @Override
      public String toString() {
          String output = new StringBuffer()
                  .append("Id: ").append(employeeId)
                  .append(System.lineSeparator()).append("Name: ").append(employeeName)
                  .append(System.lineSeparator()).append("Salary: ").append(salary)
                  .append(System.lineSeparator()).append("Net Salary: ").append(netSalary).toString();
          return output;
      }
    }
    

    您的 Main.java 也存在一些问题:

    1. 您不能在方法中声明方法。
    2. 这不是实现java.util.Scanner ... System.out.println("Enter Id:"+setEmployeeId(sc.nextInt())) 的正确方法。本质上,Scanner 部分需要与 System.out 分开(请记住,System.out 所做的唯一事情就是打印出文本,它不会等待 Scanner 的输入)。
    3. 如果我正确理解了您的问题,您需要将您的一些逻辑从您的主要方法中移出到getEmployeeDetails()getPFPercentage()

    所以你最终会得到这样的结果:

    import java.util.Scanner;
    
    public class EmployeeSalaryCalculation {
    
    private Scanner scanner;
    
      public EmployeeSalaryCalculation() {
        scanner = new Scanner(System.in);
      }
    
      public Employee getEmployeeDetails() {
          Employee employee = new Employee();
          System.out.println("Enter Id:");
          employee.setEmployeeId(scanner.nextInt());
          System.out.println("Enter Name:");
          employee.setEmployeeName(scanner.next());
          System.out.println("Enter salary:");
          employee.setSalary(scanner.nextDouble());
          return employee;
      }
    
      public double getPFPercentage(){
          System.out.println("Enter PF percentage:");
          return scanner.nextDouble();
      }
    
      public static void main(String[] args) {
          EmployeeSalaryCalculation employeeSalaryCalculation = new EmployeeSalaryCalculation();
          Employee employee = employeeSalaryCalculation.getEmployeeDetails();
          employee.calculateNetSalary(employeeSalaryCalculation.getPFPercentage());
          System.out.println(employee.toString());
      }
    }
    

    【讨论】:

      【解决方案3】:

      你已经有了一个好的开始。但是,有一些语法错误,例如忘记用分号结束某些语句。此外,据我所知,Java 不支持嵌套方法,因此您不应该在 main 方法中包含 getEmployeeDetails()getPFPercentage() 方法。我已重新排列代码以更正此问题。

      我所做的其他更改是对您的 Employee 类,尤其是 calculateNetSalary 方法。 pfPercentage 在乘以 salary 之前除以 100。此外,实例变量netSalary 在使用适当的setter 方法计算后被设置为局部变量netSalarynetSalary 方法也已重命名为 setNetSalary,因为它更能描述该方法的作用。

      除了根据规范完成Main 类之外,我没有对您的代码进行任何其他更改。如果代码的任何其他部分需要澄清,您可以对此发表评论。

      Main.java

      import java.util.Scanner;
      
      public class Main {
      
          private static Scanner scanner = new Scanner(System.in);
      
          public static void main(String[] args){
              Employee newEmployee = getEmployeeDetails();
              double pfPercentage = getPFPercentage();
      
              System.out.println();
              System.out.println("Confirm employee details: ");
              System.out.println("Id : " + newEmployee.getEmployeeId());
              System.out.println("Name : " + newEmployee.getEmployeeName());
              System.out.println("Salary : " + newEmployee.getSalary());
      
              newEmployee.calculateNetSalary(pfPercentage);
              System.out.println("Net Salary : " + newEmployee.getNetSalary());
          }
      
          /**
           * Gets the details of a new employee from user input
           * @return the new {@link Employee}
           */
          public static Employee getEmployeeDetails() {
              Employee employee = new Employee();
              System.out.println("Enter Id: ");
              employee.setEmployeeId(scanner.nextInt());
              System.out.println("Enter Name: ");
              employee.setEmployeeName(scanner.next());
              System.out.println("Enter salary: ");
              employee.setSalary(scanner.nextDouble());
              return employee;
          }
      
          /**
           * Gets the PF percentage from user input
           * @return the PF percentage
           */
          public static double getPFPercentage(){
              System.out.println("Enter PF percentage:");
              double pfPercentage = scanner.nextDouble();
              return pfPercentage;
          }
      
      }
      

      Employee.java

      public class Employee{
      
          private int employeeId;
          private String employeeName;
          private double salary;
          private double netSalary;
      
          // Setters
          public void setEmployeeId(int employeeId){
              this.employeeId = employeeId;
          }
      
          public void setEmployeeName(String employeeName){
              this.employeeName = employeeName;
          }
      
          public void setSalary(double salary){
              this.salary = salary;
          }
      
          private void setNetSalary(double netSalary){
              this.netSalary = netSalary;
          }
      
          // Getters
          public int getEmployeeId(){
              return employeeId;
          }
      
          public String getEmployeeName(){
              return employeeName;
          }
      
          public double getSalary(){
              return salary;
          }
      
          public double getNetSalary(){
              return netSalary;
          }
      
          public void calculateNetSalary (double pfPercentage){
              double pfAmount = salary * (pfPercentage / 100);
              double netSalary = salary - pfAmount;
              this.setNetSalary(netSalary);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 2016-08-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多