Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TimeSheet {
- public int location; // work location
- public int locationID; //an ID for location
- public double rate, workHours, finalPayment;
- public String times[] = new String[15];
- //set the rate based on input and set locationID here
- public void setRate(int loc) {
- if(loc >= 500) // 500-599
- { rate = 8.0; locationID = 5; }
- else if (loc >= 400) //400-499
- { rate = 13.50; locationID = 4; }
- else if (loc >= 300) //300-399
- { rate = 9.25; locationID = 3; }
- else if (loc >= 200) //200-299
- { rate = 7.50; locationID = 2; }
- else if (loc >= 100) //100-199
- { rate = 10; locationID = 1; }
- }
- public void Paycheck() {
- double totalWeeklyHours = 0.0; //for locations where rate is based on total weekly hours
- if (locationID < 4) //loop only for locationID = 1, 2, or 3
- // for 4 and 5, the calculation is quite different
- {
- for(int i = 1; i < times.length; i=i+2) {
- workHours = code2Time(times[i+1]) - code2Time(times[i]);
- totalWeeklyHours += workHours;
- }
- }
- finalPayment = calculatePay(locationID, totalWeeklyHours);
- System.out.println();
- System.out.println("Paycheck amount is $" + finalPayment);
- }
- private double calculatePay(int loc, double hours) {
- double payment = 0.0;
- //based on location, calculation is different
- switch (loc) {
- case 1:
- //$10.00 per hour with time and a half for any hours over 30 for the week.
- if (hours < 30) {
- payment = hours * rate;
- }
- else {
- payment = 30 * rate + (hours - 30) * rate * 1.5;
- }
- break;
- case 2:
- // $7.50 per hour with double time for hours worked over 40 for the week.
- if (hours < 40) {
- payment = hours * rate;
- }
- else {
- payment = 40 * rate + (hours - 40) * rate * 2;
- }
- break;
- case 3:
- // $9.25 for the first 20 hours and $10.50 for the rest.
- if (hours <= 20) {
- payment = hours * rate;
- }
- else {
- payment = 20 * rate + (hours - 20) * 10.50;
- }
- break;
- case 4:
- // $13.50 per hour on Sundays (day 1) and Saturdays (day 7) and $6.75 otherwise.
- // to do code here
- break;
- case 5: //the calculation of hours is done independently here
- // $8.00 per hour for the first 6 hours per day and $12.00 per hour after that.
- double subAmount = 0.0;
- for(int i = 1; i < times.length; i=i+2) {
- workHours = code2Time(times[i+1]) - code2Time(times[i]);
- if(workHours <= 6) {
- subAmount += workHours * rate;
- }
- else {
- subAmount += 6.0 * rate + (workHours - 6) * 12.0;
- }
- }
- payment = subAmount;
- break;
- }
- return payment;
- }
- //code below converts code to hours worked
- private double code2Time(String code) {
- double hoursWorked = 0;
- code = code.toUpperCase();
- if (code.equals("1")) hoursWorked = 0.0;
- if (code.equals("2")) hoursWorked = 0.5;
- if (code.equals("3")) hoursWorked = 1.0;
- if (code.equals("4")) hoursWorked = 1.5;
- if (code.equals("5")) hoursWorked = 2.0;
- if (code.equals("6")) hoursWorked = 2.5;
- if (code.equals("7")) hoursWorked = 3.0;
- if (code.equals("8")) hoursWorked = 3.5;
- if (code.equals("9")) hoursWorked = 4.0;
- if (code.equals("A")) hoursWorked = 4.5;
- if (code.equals("B")) hoursWorked = 5.0;
- if (code.equals("C")) hoursWorked = 5.5;
- if (code.equals("D")) hoursWorked = 6.0;
- if (code.equals("E")) hoursWorked = 6.5;
- if (code.equals("F")) hoursWorked = 7.0;
- if (code.equals("G")) hoursWorked = 7.5;
- if (code.equals("H")) hoursWorked = 8.0;
- return hoursWorked;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement