Java Question

0 comments

So here is the basis of my code, I need help with inputting the correct code which will allow me to create a class that writes employee payroll records to a file and a class that checks that the records have been written. please help!

import java.util.Scanner;

public class EmployeePayroll
{
//STUDENTS INSERT LINE COMMENTS FOR EACH VARIABLE.
private String employeeName = “”;
private int hoursWorked = 0;
private double payRate = 0.0;
private double grossPay = 0.0;
private double retire401K = 0.0;
private double percent401K = 0.0;
private double grossPayTotal = 0.0;
private double total401K = 0.0;
private int firstNmLength = 0;

private Scanner input = new Scanner(System.in);

/**
* STUDENTS ARE TO INSERT COMMENT BOXES FOR EACH METHOD.
*/
public EmployeePayroll()
{
}//STUDENTS ARE TO INSERT LINE COMMENTS FOR EACH CLOSE BRACE.

public EmployeePayroll(String emplName, int hrsWrkd, double hourlyPay, double retirementPercent)
{
setEmployeeName(emplName);
setHoursWorked(hrsWrkd);
setPayRate(hourlyPay);
set401K(retirementPercent);
}

public final void setEmployeeName(String employeeName)
{
while(!isAlpha(employeeName))
{
System.out.printf(“%nEnter valid first name: “);
employeeName = input.nextLine();
}

this.employeeName = employeeName;
}

public void setEmployeeName(int i)
{
String first = “”, last = “”;

System.out.printf(“%nEnter the %semployee’s first name press enter then the last “
+ “name press enter: “, i == 0 ? “” : “next “);
first = input.nextLine();
last = input.nextLine();

while(!isAlpha(first))
{
System.out.printf(“%nEnter valid first name: “);
first = input.nextLine();
}

firstNmLength = first.length();

while(!isAlpha(last))
{
System.out.printf(“%nEnter valid last name: “);
last = input.nextLine();
}

employeeName = first + ” ” + last;
}

public final void setHoursWorked(int hoursWorked)
{
this.hoursWorked = hoursWorked;
}

public void setHoursWorked(String first)
{
System.out.printf(“%nEnter the number of hours worked for %s: “, first);

while(!input.hasNextInt())
{
input.next();
System.out.printf(“%nInvalid type! Re-enter the number of “
+ “hours worked for %s: “, first);
}
hoursWorked = input.nextInt();

while(hoursWorked > 40 || hoursWorked < 5)
{
hoursWorked = testHoursWorked(hoursWorked);
}

}

public final void setPayRate(double payRate)
{
this.payRate = payRate;
}

public void setPayRate()
{
System.out.printf(“%nEnter the employee’s hourly pay rate: “);

while(!input.hasNextDouble())
{
input.next();
System.out.printf(“%nInvalid type! Re-enter the hourly pay rate: “);
}
payRate = input.nextDouble();

while(payRate < 7.25 || payRate > 26.00)
{
payRate = testPayRate(payRate);
}
}

public void calcGrossPay()
{
grossPay = hoursWorked * payRate;
}

public final void set401K(double percent401K)
{
this.percent401K = percent401K;
}

public void set401K()
{
System.out.printf(“%nEnter the employee’s 401K contribution “
+ “as a percentage of salary (not to exceed 10%%): “);

while(!input.hasNextDouble())
{
input.next();
System.out.printf(“%nInvalid type! Re-enter the 401K contribution “
+ “not to exceed 10%% of salary: “);
}
percent401K = input.nextDouble();

while(percent401K > 10.00)
{
percent401K = test401K();
}
}

public void calcRetire401K()
{
retire401K = percent401K/100 * grossPay;
}

public String getEmployeeName()
{
return employeeName ;
}

public int getHoursWorked()
{
return hoursWorked;
}

public double getPayRate()
{
return payRate;
}

public double get401K()
{
return percent401K;
}

public double getGrossPay()
{
return grossPay;
}

public double getRetire401K()
{
return retire401K;
}

public int testHoursWorked(int hoursWorked)
{
if(hoursWorked > 40)
{
System.out.printf(“%nHours worked cannot EXCEED 40. Please re-enter: “);
}

if(hoursWorked < 5)
{
System.out.printf(“%nHours worked cannot be LESS than 5. Please re-enter: “);
}

while(!input.hasNextInt())
{
input.next();
System.out.printf(“%nInvalid type! Re-enter the number of hours “
+ “worked not less than 5 or greater than 40: “);
}

return input.nextInt();
}

public double testPayRate(double payRate)
{
if(payRate < 7.25)
{
System.out.printf(“%nHourly pay cannot be LESS than $7.25. Please re-enter: “);
}

if(payRate > 26)
{
System.out.printf(“%nHourly pay cannot EXCEED $26.00. Please re-enter: “);
}

while(!input.hasNextDouble())
{
input.next();
System.out.printf(“%nInvalid type! Re-enter an hourly pay rate that “
+ “is not less than $7.25 or greater than $26.00: “);
}

return input.nextDouble();
}

public double test401K()
{
System.out.printf(“%nContribution cannot EXCEED 10%%. Please re-enter: “);

while(!input.hasNextDouble())
{
input.next();
System.out.printf(“%nInvalid type! Re-enter a contribution “
+ “NOT exceeding 10%% of salary: “);
}
return input.nextDouble();
}

public boolean isAlpha(String name)
{
return name != null && name.chars().allMatch(Character::isLetter);
}

}

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}