как исправить эту программу Java - PullRequest
0 голосов
/ 01 июня 2019

Создайте класс Employee со следующими закрытыми переменными-членами.

int employeeId
String employeeName
double salary
double netSalary

Включите соответствующий метод get и setters в класс Employee.Напишите следующий метод в классе Employee:

public void calculateNetSalary(int pfpercentage) // This method should take PF percentage as argument. Deduct the PF amount from the salary and set the netSalary.  

Создайте класс Main, у которого есть метод main, который вызывает метод для получения входных данных и печатает детали, как показано в примере.

Также напишите метод:

public static Employee getEmployeeDetails() // which gets the employee details and returns the employee object.

public static int getPFPercentage() // which gets the PF percentage and returns the same

Я написал этот код.Вывод правильный, но показывает какую-то ошибку, как ее исправить?

public class Employee {
    private int employeeId;
    private String employeeName;
    private double salary;
    private double netSalary;

    public static Employee instances= new Employee();

    public int getEmployeeId() {
        return employeeId;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public double getSalary() {
        return salary;
    }

    public double getNetSalary() {
        return netSalary;
    }

    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;
    }
    public void calculateNetSalary(int pfpercentage) {
        instances.netSalary=(instances.salary)*(1-(((double)pfpercentage)/100));

    }
    public static Employee getEmployeeDetails(int id, String name,double salary) {

        instances.setEmployeeId(id);
        instances.setEmployeeName(name);
        instances.setSalary(salary);
        return instances;
    }

    public static int getPFPercentage(int pfpercentage)
    { instances.calculateNetSalary(pfpercentage);
    ' return pfpercentage;  '
    }
}'




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

        System.out.println("Enter id");
        int id=sc.nextInt();
        System.out.println("Enter name");
        sc.nextLine();
        String name=sc.nextLine();
        System.out.println("Enter salary");
        double salary=sc.nextDouble();

        Employee e1= Employee.getEmployeeDetails( id, name, salary );
        System.out.println("Enter pfpercentage");
        Employee.getPFPercentage(sc.nextInt());

        System.out.println("Id :"+e1.getEmployeeId());
        System.out.println("name :"+e1.getEmployeeName());
        System.out.println("salary :"+e1.getSalary());
        System.out.println("Net salary :"+e1.getNetSalary());
    }

}
FAIL-1:

check the availaibility of getEmployeeDetails() in main method or check wether the signature(returntype/argument/acessspecifier/methodname)of the of the method getEmployeeDetails is correct.
check the availaibility of getPFPercentage() in main method or check wether the signature(returntype/argument/acessspecifier/methodname)of the of the method getPFPercentage() is correct
FAIL-1: check the logic of calculateNetSalary
FAIL-2: check the method of getPFPercentage
FAIL-2: check the method of getEmployeeDetails

Ответы [ 2 ]

1 голос
/ 13 июня 2019
public class Employee  {
private int employeeId;
private String employeeName;
private double salary;
private double netSalary;
public int getEmployeeId() {
    return employeeId;
}
public String getEmployeeName()  {
    return employeeName;
}
public double getSalary() {
    return salary;
}
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 calculateNetSalary(int pf) {
    double temp=(pf/100.00);
    setNetSalary(salary-(salary*(temp)));
}
public void setNetSalary(double netSalary) {
    this.netSalary=netSalary;
}
public double getNetSalary() {
    return netSalary;
}

}

public class Main{
public static Employee getEmployeeDetails(){
    Scanner sc=new Scanner(System.in);
    Employee e=new Employee();
    System.out.println("Enter Id:");
    e.setEmployeeId(sc.nextInt());
    System.out.println("Enter Name:");
    e.setEmployeeName(sc.next());
    System.out.println("Enter salary:");
    e.setSalary(sc.nextDouble());
    return e;
}
public static int getPFPercentage(){
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter PF percentage:");
    int val=sc.nextInt();
    return val;
}
public static void main(String arg[]){
    Scanner sc=new Scanner(System.in);
    Employee e=getEmployeeDetails();
    int temp=getPFPercentage();
    e.calculateNetSalary(temp);
    System.out.println("Id : "+e.getEmployeeId());
    System.out.println("Name : "+e.getEmployeeName());
    System.out.println("Salary : "+e.getSalary());
    System.out.println("Net Salary : "+e.getNetSalary());
}

}

0 голосов
/ 01 июня 2019

Попробуйте этот код.

class Employee {

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

     public Employee(int employeeId, String employeeName, double salary) {
         this.employeeId = employeeId;
         this.employeeName = employeeName;
         this.salary = salary;
     }

     public int getEmployeeId() {
         return employeeId;
     }

     public String getEmployeeName() {
         return employeeName;
     }

     public double getSalary() {
         return salary;
     }

     public double getNetSalary() {
         return netSalary;
     }

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

     public void calculateNetSalary(double pfpercentage) {
         this.netSalary = this.salary * (1 - (pfpercentage / 100));
     }
} 

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter id");
    int id = sc.nextInt();
    System.out.println("Enter name");
    sc.nextLine();
    String name = sc.nextLine();
    System.out.println("Enter salary");
    double salary = sc.nextDouble();

    Employee e1 = new  Employee(id, name, salary);
    System.out.println("Enter pfpercentage");
    e1.calculateNetSalary(sc.nextDouble());

    System.out.println("Id :" + e1.getEmployeeId());
    System.out.println("name :" + e1.getEmployeeName());
    System.out.println("salary :" + e1.getSalary());
    System.out.println("Net salary :" + e1.getNetSalary());
}

В вашем коде вы объявили статический объект Employee, а также у вас есть конструктор по умолчанию. Это не хорошая практика программирования. Общая цель класса - создать несколько объектов. Если вам нужен один единственный объект, вы должны использовать шаблон проектирования Singleton.

https://www.javatpoint.com/singleton-design-pattern-in-java

public static Employee instance;

public static Employee getInstance(){
    if(instance != null){
        instance = new Employee();
    }
    return instance;
}

//private constructor
private Employee(){};

Я не понял цели этого метода. Имя метода говорит getPFPercentage, в то время как ваш параметр также pfpercentage.

public static int getPFPercentage(int pfpercentage)
{ instances.calculateNetSalary(pfpercentage);
    ' return pfpercentage;  '
}

Я думаю, что вышеупомянутый метод является излишним. Мы можем вызвать метод calcNetSalary и получить рассчитанную чистую зарплату через его получатель.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...