1 answer

Hello, How would I test this in Junit? Is there anything else I can do with...

Question:

Hello, How would I test this in Junit? Is there anything else I can do with my code?

import java.text.DecimalFormat;

public class PayCalculator implements Constants {
  
   private String employeeName;
   private int reportID;
   private double hourlyWage;
   private static int ID = 0;
   private static int reportIDGenerator = 1000;
   public int[] overtimes = HOURS_WORKED;

public PayCalculator(String name) {
   super(); {
   this.reportID = reportIDGenerator;
   reportIDGenerator+=10;
   this.overtimes = HOURS_WORKED;
   }
}
public PayCalculator(String name, double hourlyWage) {
   this.reportID = reportIDGenerator;
reportIDGenerator+=10;
this.employeeName = name;
this.hourlyWage = hourlyWage;
this.overtimes = HOURS_WORKED;
}

public String getEmployeeName() {
return employeeName;
}

public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

public int getReportID() {
return reportID;
}

public double getHourlyWage() {
return hourlyWage;
}

public void setHourlyWage(double hourlyWage) {
this.hourlyWage = hourlyWage;
}
  
@Override
public String toString() {
return "PayCalculator [employeeName=" + employeeName + ", reportId=" + reportID +
       ", hourlyWage=" + hourlyWage+ "]";
}
public double calculateYearlyGrossPay(){
double totalGross = 0.0;
for(int period = 1; period <= PAY_PERIODS_IN_YEAR; period++){
totalGross += calculateGrossForPeriod(period);
}
return totalGross;
}
public double calculateYearlyNetPay(){
double totalNet = 0.0;
for(int period = 1; period <= PAY_PERIODS_IN_YEAR; period++){
totalNet += calculateNetPayForPeriod(period);
}
return totalNet;
}
public double calculateNetPayForPeriod(int periodNumber){
double gross = calculateGrossForPeriod(periodNumber);
double tax = calculateTax(gross);
double netPay = gross-tax;
return netPay;
}
public double PAY_PERIODS_IN_YEAR(int periodNumber){
double gross = calculateGrossForPeriod(periodNumber);
double tax = calculateTax(gross);
double netPay = gross-tax;
return netPay;
}
public void printNetPayForAllPeriods(){
DecimalFormat df = new DecimalFormat("#.00");
System.out.println("NET PAY for all periods:\n");
for(int period =1; period <= PAY_PERIODS_IN_YEAR; period++){
System.out.println("PERIOD:"+period+" NET PAY:"+df.format(calculateNetPayForPeriod(period)));
}
}
public void increaseWageRate(double percentage){
hourlyWage =hourlyWage+ hourlyWage*(percentage/100);
}

private double calculateGrossForPeriod(int periodNumber){
double regulayPay = FULL_TIME*hourlyWage;
double overtimePay = overtimes[periodNumber-1]*(hourlyWage*OVERTIME_RATE);
double gross= regulayPay+overtimePay;
return gross;
}
private double calculateTax(double gross){
double federalTax = gross*FEDERAL_TAX_RATE;
double stateTax = gross*STATE_TAX_RATE;
return federalTax+stateTax;
}
  
}

public interface Constants {
  
   public final int[] HOURS_WORKED = {89, 80, 19, 73, 44, 99, 77, 0, 80, 70, 80, 87, 84, 82,
           80, 30, 89, 90, 100, 120, 0, 69, 99, 91, 83, 80};
  
   public final int PAY_PERIODS_IN_YEAR = 26;
   public final double FEDERAL_TAX_RATE = 0.2;
   public final double STATE_TAX_RATE = 0.09;
   public final double OVERTIME_RATE = 1.5;
   public final double FULL_TIME = 80;
  
   public final int PAY_PERIOD_1 = 0;
   public final int PAY_PERIOD_2 = 1;
   public final int PAY_PERIOD_3 = 2;
   public final int PAY_PERIOD_4 = 3;
   public final int PAY_PERIOD_5 = 4;
   public final int PAY_PERIOD_6 = 5;
   public final int PAY_PERIOD_7 = 6;
   public final int PAY_PERIOD_8 = 7;
   public final int PAY_PERIOD_9 = 8;
   public final int PAY_PERIOD_10 = 9;
   public final int PAY_PERIOD_11 = 10;
   public final int PAY_PERIOD_12 = 11;
   public final int PAY_PERIOD_13 = 12;
   public final int PAY_PERIOD_14 = 13;
   public final int PAY_PERIOD_15 = 14;
   public final int PAY_PERIOD_16 = 15;
   public final int PAY_PERIOD_17 = 16;
   public final int PAY_PERIOD_18 = 17;
   public final int PAY_PERIOD_19 = 18;
   public final int PAY_PERIOD_20 = 19;
   public final int PAY_PERIOD_21 = 20;
   public final int PAY_PERIOD_22 = 21;
   public final int PAY_PERIOD_23 = 22;
   public final int PAY_PERIOD_24 = 23;
   public final int PAY_PERIOD_25 = 24;
   public final int PAY_PERIOD_26 = 25;
}


Answers

I would share you some snapshots from one of existing project to show how junit can be applied successfully

So this is the general structure of the project and the imports you would need .

E Quick Access - a PO Java EE - shoppingbackend/src/test/java/com/homsil/shoppingbackend/test/Cooklest.java - Eclipse File Ed

You can multiple test cases like


   @Test
   public void testGetCook() {
      
       cook = cookDAO.get(3);
      
      
       assertEquals("Successfully fetched a single cook from the table!","Television",cook.getName());
      
      
   }
  
  
   @Test
   public void testUpdateCook() {
      
       cook = cookDAO.get(3);
      
       cook.setName("TV");
      
       assertEquals("Successfully updated a single cook in the table!",true,cookDAO.update(cook));
      
      
   }
  

This code is from the same file . the important line is the assertEquals for you . So if the update has been successfull then the test case pass .

74 750 /* 76 77 @Test public void testDeleteCook() { cook = cookDAO.get (3); assertEquals(Successfully deleted a single cook

After that you need to write click on the file and Run As --> Junit .

So for your example , you need to run the test case on your methods for example .

private double calculateTax(double gross){
double federalTax = gross*FEDERAL_TAX_RATE;
double stateTax = gross*STATE_TAX_RATE;
return federalTax+stateTax;
}

assertEquals("Successfully calculated test",expectedvalue,calculateTax(100.00));

@Test
   public void testUpdateCook() {
      
// Create the PayCalculator object here.
  assertEquals("Successfully calculated test",expectedvalue,obj.calculateTax(100.00));
      
      
   }

Hope this helps.

.

Similar Solved Questions

1 answer
Which system of equations below models the following problem? Your company makes and sells deck chairs....
Which system of equations below models the following problem? Your company makes and sells deck chairs. Model A take 3 square yards of canvas and 160 ounces of iron. Model B takes 4 square yards of canvas and 150 ounces of iron. You have 1,453 square yards of canvas and 62,800 ounces of iron. How ma...
2 answers
A sphere has a volume of 500/3 pi cubic centimeters
a sphere has a volume of 500/3 pi cubic centimeters. What is the total surface area, in sqaure cntimeters, of the square?A. 25 piB. 40 piC. 100 piD. 400 piPlease show your work. im very confused :(...
1 answer
Professional Development assists your career development. List five (5) examples of Professional Development activities:...
Professional Development assists your career development. List five (5) examples of Professional Development activities:...
1 answer
Apter 3 Homework Question 1 of 12 < > View Policies Current Attempt in Progress This...
apter 3 Homework Question 1 of 12 < > View Policies Current Attempt in Progress This information relates to Martinez Real Estate Agency. Oct. 1 Stockholders invest $33,400 in exchange for common stock of the corporation. Hires an administrative assistant at an annual salary of $38,880. Buys of...
1 answer
EXERCISE & Cear Flow Assumptione Perle System (15 pts) The following inton is beter W on...
EXERCISE & Cear Flow Assumptione Perle System (15 pts) The following inton is beter W on Com Assume that Waldron uses a periodic Inventory system and that there we 650 units on hand at the end of the month Instructions: Answer the following independent questions and show.computations supporting ...
1 answer
Sony is considered by some to be one of the most bureaucratic organizations in the world....
Sony is considered by some to be one of the most bureaucratic organizations in the world. Sony grew into a complex conglomerate consisting of multiple, diverse business units that, for the most part, operated independently and without regard for one another. As a result, Sony's performance slipp...
1 answer
When do magnetic fields affect charges? O A. Only when charges are moving. B. Only when...
When do magnetic fields affect charges? O A. Only when charges are moving. B. Only when charges are moving in a circle. O C. Only when charges are stationary. O D.Only when the charge's sign is opposite to the sign of the magnetic field. O E. Magnetic fields never affect charges....
1 answer
Signature Assignment: Case Study Comprehensive Case Study on COPD, Heart Failure, Hypertension, and Diabetes Mellitus M....
Signature Assignment: Case Study Comprehensive Case Study on COPD, Heart Failure, Hypertension, and Diabetes Mellitus M. K. is a 45-year-old female measuring 5'5" and weighing 225 lbs. M. K. has a history of smoking about 22 years along with a poor diet. She has a history of type II diabetes...
1 answer
Find the amortization table for a $13,000 loan amortized over 3 years with semiannual payments if...
Find the amortization table for a $13,000 loan amortized over 3 years with semiannual payments if the interest rate is 5.7% per year compounded semiannually. (Round your answers to the nearest cent.) End of Period Payment Made Payment Toward Interest Payment Toward Principal Outstanding Principle 13...
1 answer
How big is the sun compared to earth?
How big is the sun compared to earth?...
1 answer
Can some one show me how to do these problems What are Superman's x and y...
Can some one show me how to do these problems What are Superman's x and y components? What are the x and y components of the plane's velocity?...
1 answer
Question 51 (1 point) The compound shown below is CN C=C=C CH3 A) achiral B) chiral
Question 51 (1 point) The compound shown below is CN C=C=C CH3 A) achiral B) chiral...
1 answer
How do you multiply #(3+5i)(4+4i)#?
How do you multiply #(3+5i)(4+4i)#?...
1 answer
King Warrior(KW), the Official Waterloo Athletics Mascot, is considering three investment proposals. Each of them is...
King Warrior(KW), the Official Waterloo Athletics Mascot, is considering three investment proposals. Each of them is characterized by an initial cost, annual savings over four years, and no salvage value, as illustrated in the following table. KW can only invest in amaximum of two of these proposals...
1 answer
George and Weezy received $31,700 of Social Security benefits this year ($12,300 for George; $19,400 for...
George and Weezy received $31,700 of Social Security benefits this year ($12,300 for George; $19,400 for Weezy). They also received $5,450 of interest from jointly owned City of Ranburne Bonds and dividend income. What amount of the Social Security benefits must George and Weezy include in their gr...