Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 员工工资计算 任务描述_Java_Oop - Fatal编程技术网

Java 员工工资计算 任务描述

Java 员工工资计算 任务描述,java,oop,Java,Oop,我有一个问题陈述: 使用以下私有成员变量创建类Employee int employeeId String employeeName double salary double netSalary 在Employee类中包含适当的getter和setters方法。在Employee类中编写以下方法: public void calculatenetralary(int pfpercentage)-此方法应将PF percentage作为参数。从工资中扣除PF金额并设置netSalary 创建一个

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

int employeeId
String employeeName
double salary
double netSalary
在Employee类中包含适当的getter和setters方法。在Employee类中编写以下方法: public void calculatenetralary(int pfpercentage)-此方法应将PF percentage作为参数。从工资中扣除PF金额并设置netSalary

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

还可以编写一个方法:

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

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

在main方法中,调用上述两个方法,然后在Employee类中调用calculateNetSalary方法并打印输出,如下所示

示例输入1:

输入Id: 101 输入名称: 维韦克 输入工资: 20000 输入PF百分比: 七,

样本输出1:

身份证号码: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,因为我不确定写什么和如何写。

这应该是您的代码:

 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()方法内定义其他方法。
您可以在其中调用其他方法(根据您的喜好)。

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

  • 设置
    netSalary
    的方法声明为
    public void netSalary(double netSalary)
    ,而应该是
    public void SetNetSalary(double netSalary)
  • public void calculate(int pfpercentage)
    中的计算看起来不正确。如果你想通过一个双精度测试(即2代表2%),那么你需要将其除以100,将数字转换为百分比
  • 如果要使用变量,则需要声明变量(namelty
    pfamount
    需要声明为
    double
    ,然后才能为其赋值)
  • 您可能需要一个
    公共字符串toString()
    方法来打印employee对象
  • 所以你会得到这样的结果:

    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;
      }
    }
    
    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());
      }
    }
    
    您的Main.java也有几个问题:

  • 不能在方法中声明方法
  • 这不是实现
    java.util.Scanner
    的正确方法
    System.out.println(“输入Id:+setEmployeeId(sc.nextInt()))
    。基本上,扫描仪部分需要与
    系统.out
    分离(请记住
    系统.out
    的唯一作用是打印文本,它不等待扫描仪的输入)
  • 如果我正确理解了您的问题,那么您需要将一些逻辑从main方法中移到
    getEmployeeDetails()
    getPFPercentage()
  • 所以你会得到这样的结果:

    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;
      }
    }
    
    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());
      }
    }
    

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

    我对
    Employee
    类所做的其他更改,特别是
    calculateNetSalary
    方法
    pfPercentage
    除以100,然后再乘以
    工资
    。此外,使用适当的setter方法计算后,实例变量
    netSalary
    被设置为局部变量
    netSalary
    。方法
    netSalary
    也被重命名为
    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;
        }
    
    }
    
    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);
        }
    }
    
    Employee.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;
        }
    
    }
    
    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);
        }
    }
    

    你是说你不能计算百分比吗?请澄清您所坚持的是什么。您不能在其他方法中包含方法。因此,首先删除这两个空方法
    setEmployeeId()
    setEmployeeName()
    setAlary()
    是Employee类的方法,因此需要对Employee调用它们:
    emp.setEmployeeId(…)
    。不要试图打印内容,获取输入,并在一行中设置员工ID。这是三个单独的说明,应该放在三行上:1。打印一条消息,2。读取员工ID,3。将员工的ID设置为您在步骤2中读取的ID。