员工 – 净工资

huangapple 未分类评论43阅读模式
英文:

Employee - net salary

问题

public class EmployeeTest {

    @Test
    public void testCalNetPayWithPfUnderFivePercent() {
        Employee employee = new Employee(1, "John", 50000, 3);
        double netPay = employee.calNetPay();
        assertEquals(50000.0, netPay, 0.001);
    }

    @Test
    public void testCalNetPayWithPfOverFivePercent() {
        Employee employee = new Employee(2, "Jane", 60000, 7);
        double netPay = employee.calNetPay();
        assertEquals(55800.0, netPay, 0.001);
    }
}

Note: This code assumes you have imported the necessary JUnit and assertion classes.

英文:

Write JUnit test class named "EmployeeTest" to test the method calNetPay in the given Employee Class

Employee.java

public class Employee {

private int eid;

private String name;

private double salary;

private double pfper;

public Employee(int eid, String name, double salary, double pfper) {

	super();

	this.eid = eid;

	this.name = name;

	this.salary = salary;

	this.pfper = pfper;

}

public double calNetPay(){

	if(pfper <= 5){

		return salary;

	} else {

		return salary-salary*(pfper/100);

	}

}

}

please tell me to do this question I tried some way but that do not fulfill the requirements.

答案1

得分: 0

**EmployeeTest**

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class EmployeeTest {
    Employee emp = null;
    
    @Test
    public void testCalNetPay() {
        emp = new Employee(101,"xyz",5000.0,3);
        double netPay = emp.calNetPay();
        assertEquals(5000.0,netPay,0.0);
        
        emp = new Employee(102,"abc",1000.0,6);
        netPay = emp.calNetPay();
        assertEquals(940.0,netPay,0.0);
    }
}

**EmployeeMain**

import java.util.*;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;


public class EmployeeMain {
    public static void main (String[] args) {
        Result result = JUnitCore.runClasses(EmployeeTest.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
        System.out.println(result.wasSuccessful());
    }
}
英文:

EmployeeTest

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class EmployeeTest {
    Employee emp = null;
    
    @Test
    public void testCalNetPay() {
        emp = new Employee(101,"xyz",5000.0,3);
        double netPay = emp.calNetPay();
        assertEquals(5000.0,netPay,0.0);
        
        emp = new Employee(102,"abc",1000.0,6);
        netPay = emp.calNetPay();
        assertEquals(940.0,netPay,0.0);
    }
}

EmployeeMain

import java.util.*;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;


public class EmployeeMain {
    public static void main (String[] args) {
        Result result = JUnitCore.runClasses(EmployeeTest.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
        System.out.println(result.wasSuccessful());
    }
}

This should work, hope it helps.

huangapple
  • 本文由 发表于 2020年4月5日 12:49:48
  • 转载请务必保留本文链接:https://java.coder-hub.com/61038213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定