/**
* User is prompted to enter the first and last name of an employee after
* the hourly payroll system is engage. Prompt for hours worked. Error messages
* and re=prompts are displayed as long as hours worked is greater than 40 or less
* than 5. The pay rate is prompted. Error messages and re-prompts are displayed
* as long as the pay rate is less than minimum wage or greater than 26. Lastly
* the employees 401(k) distribution percentage is prompted. Error message and
* re-prompt or displayed as long as the contribution percentages is greater than 10.
* After the correct entries are made, the gross pay is calculated along with the
* contribution to the 401(k). Totals or girls pay in 401(k) are accumulated.
* And if-else determines the printing of the $ sign for the first employee.
* The if-else also adds the employee information to the payroll expense report.
* The user is then asked whether or not to continue. If the user says ‘yes’,
* then the whole process begins again for the next employee. If the user says
* ‘no’ then the TOTALS line is added to the report and the report is printed.
*/
import java.util.Scanner; //This allows user input
import java.util.Calendar; //This will get the date from your computer
public class PA2 {
public static void main(String[] args)
/**
* Declare variables to be used in program
* String – first and last names are string
* double – the numbers are using decimals
* int – integers so no decimals
* char – single letter
*/
{
String first = “”, last = “”; //I find this way of declaring variables
double payRate = 0.0, //redundant, however this is
grossPay = 0.0, retire401k = 0.0, employees full name, double grossPay is percent401k = 0.0, retire401k is each total retirement amount |
//what I saw in the video // String first and last is for the |
// their total pay per employee, |
grossPayTotal = 0.0, // percent401k = is their percent
contributed from total pay, grossPayTotal is
total401k = 0.0; // how much employee pays out for salary.
total401k how much total gets paid out for
int hoursWorked = 0, trigger = 1; // their 401k. hours worked is hours
worked, trigger is for how to $ will be
char cont = ‘Y’; // placed, and cont is to continue on the loop.
Scanner input = new Scanner(System.in); //new data to be put in from
keyboard
Calendar dateTime = Calendar.getInstance(); //Captures the current date
and time
String payrollExpense = String.format(“WEEKLY HOURLY PAYROLL
SYSTEM.”
+ “%n%nDate: %tD”
+ “%nTime: %tr”
+ “%n%n%56S %21S”, dateTime, dateTime, “GROSS PAY”,
“401K”);//got this from the PA instructions,
//however this properly formats the
//output before the calculations
System.out.printf(“%nWEEKLY HOURLY PAYROLL
SYSTEM%n%nContinue? Enter ” +
“‘Y’ or ‘N’: “); //prompt user to use the payroll system
cont = input.nextLine().charAt(0); //captures first letter if user puts in ‘no’ or
‘yes’
if (Character.toUpperCase(cont) != ‘Y’ || Character.toUpperCase(cont) !=
‘N’)
{
input.next();
System.out.printf(“You must enter y or n”);
}
while (Character.toUpperCase(cont) == ‘Y’) // while the input is a ‘y’
the program will run in this while loop
{
System.out.printf(“%nEnter the employee’s first name press
enter then the last ”
+ “name press enter:%n”); // prompt user to
input first and last name of employee
first = input.next(); // gets first name, goes to next line
last = input.next(); // gets last name goes to next line
System.out.printf(“%nFirst Name: %s%nLast Name: %s%n”,
first, last); //I was not sure if you would want
// the first and last name read back
// so I commented it.
System.out.printf(“%nEnter the number of hours worked for
%s: “, first); // this is asking the user
// to input hours worked
// for that employee
while (!input.hasNextInt())
{
input.next();
System.out.printf(“%nYou must enter an integer: “);
}
hoursWorked = input.nextInt(); // the input is stored into
hoursWorked
// nextLine scans for next integer input
while (hoursWorked > 40 || hoursWorked < 5) //hoursWorked
that was inputted by
// the user is now being compared to 40 or 5
{
if (hoursWorked > 40) // users cannot input 41 or
more since employees cannot work that many hours
{
System.out.printf(“%nHours worked cannot
EXCEED 40. ” +
“Please re-enter: “); //tells user to
put in hoursWorked less than 40
}
if (hoursWorked < 5) // users cannot input 4 or less
since employees cannot work that little
{
System.out.printf(“%nHours worked cannot be
” +
“LESS than 5. Please re-enter:
“); // tells user to put more than 5
}
hoursWorked = input.nextInt(); // scans for next
integer since the user cannot get
// out of this loop unit conditions are met
}
System.out.printf(“%nEnter the employee’s hourly pay rate:
“);//prompting users for
// employees hourly pay rate
payRate = input.nextDouble(); // input that the user puts in
is
while (payRate > 26 || payRate < 7.25) // stored into
payRate and will be compared
{
if (payRate > 26) // payRate cannot exceed 26
{
System.out.printf(“%nHourly pay cannot
EXCEED ” +
“$26.00. Please re-enter: “); //
tells user it cannot exceed $26
}
if (payRate < 7.25) // payRate cannot be less than
7.25
{
System.out.printf(“%nHourly pay cannot be
LESS than ” +
“$7.25. Please re-enter:
“); | // tells user it cant be less than 7.25 } |
payRate = input.nextDouble(); // it is looking for the
double by the user
// until conditions are met it will exit loop
}
grossPay = hoursWorked * payRate; // hours worked times
hourly rate equals your total pay
grossPayTotal += grossPay; // grossPayTotal =
grossPayTotal + grossPay
System.out.printf(“%nEnter the employee’s 401K
contribution as a ” +
“percentage of salary (not to exceed 10%%):
“); // tells user to input 401k contribution
// cannot exceed 10%
percent401k = input.nextDouble(); // input for 401k
contribution
while (percent401k > 10) // cannot more than 10%
{
System.out.printf(“%nContribution cannot EXCEED
10%%. Please re-enter: “);
percent401k = input.nextDouble();
} // loop will end once it is less than 10%
retire401k = percent401k / 100 * grossPay; // calculation for
how much is going into
// retirement
total401k = total401k + retire401k; // adds up all the 401k
contribution
if (trigger == 1) {
payrollExpense += String.format(“%n%-26s %10s$
%,18.2f%7s$%,14.2f”, // lines everything up properly
first + ” ” + last, ” “, grossPay, ” “,
retire401k);
trigger = 0; // Resets the trigger variable
// so the code in the else, that
// doesn’t print the $ sign
} else {
payrollExpense += String.format(“%n%-23s %7s
%,24.2f %5s %,15.2f”, // this lines up the data properly
first + ” ” + last, ” “, grossPay, ” “,
retire401k); //The next 2 Java statements add
// the TOTALS line to payroll expense and
// then prints *payroll expense which now
// contains all the output information
}
input.nextLine();
System.out.printf(“%nWould you like to enter another ” +//
asking user if there are
“employee? Enter ‘Y’ or ‘N’: “); // more
employees they would like to enter
cont = input.next().charAt(0); // it will grab the first letter of
the answer
if (Character.toUpperCase(cont) == ‘N’) // if it is no it will
finish final calculations
{
payrollExpense += String.format(“%n%n%-21s
TOTALS %7s $%,18.2f %5s $%,14.2f”, ” “, ” “, //after proper
//formatting, this lines everything up
grossPayTotal, ” “, total401k); // this
prints the final line with the total
// amount being paid and total 401k contribution
System.out.printf(“%n%n%s”, payrollExpense);
} // END if -> ‘n’
} // END while(Character.toUpperCase(cont) == ‘Y’)
} // End method main()
} // End CastanedaR003PA01}
/** WEEKLY HOURLY PAYROLL SYSTEM
Continue? Enter ‘Y’ or ‘N’: y
Enter the employee’s first name press enter then the last name press enter:
Davey
Jones
Enter the number of hours worked for Davey: 4
Hours worked cannot be LESS than 5. Please re-enter: 5
Enter the employee’s hourly pay rate: 7
Hourly pay cannot be LESS than $7.25. Please re-enter: 30
Hourly pay cannot EXCEED $26.00. Please re-enter: 17
Enter the employee’s 401K contribution as a percentage of salary (not to exceed 10%):
12
Contribution cannot EXCEED 10%. Please re-enter: 3
Would you like to enter another employee? Enter ‘Y’ or ‘N’: y
Enter the employee’s first name press enter then the last name press enter:
Fred
Flintstone
Enter the number of hours worked for Fred: 40
Enter the employee’s hourly pay rate: 20
Enter the employee’s 401K contribution as a percentage of salary (not to exceed 10%):
0
Would you like to enter another employee? Enter ‘Y’ or ‘N’: n
WEEKLY HOURLY PAYROLL SYSTEM.
Date: 03/03/21
Time: 03:27:37 PM
GROSS PAY Davey Jones Fred Flintstone TOTALS */ |
401K 2.55 0.00 |
$ | 85.00 800.00 2.55 |
$ | $ | 885.00 | $ |
Tags: assignmentexpert, assignmenthelp, assignmenthelpaustralia, assignmenthelper, assignmenthelpuk, assignmenthelpusa, assignmentwriting, bestpriceguaranteed, bestqualityguaranteed, london, myassignmenthelp, plagiarismfreework