Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. public class WeirdWages {
  6.  
  7.     public static void main(String[] args) {
  8.         // initializing variables
  9.         final double DAILY_WAGE = 30.00;
  10.         final double DAILY_OVERTIME_BONUS = 25.50;
  11.         final double WEEKLY_OVER_TIME_BONUS = 15.00;
  12.         final double SATURDAY_BONUS_PERCENTAGE = 2.25;
  13.         final double SUNDAY_BONUS_PERCENTAGE = 1.50;
  14.         final int MAX_DAILY_HOURS = 8;
  15.         final int MAX_WEEKLY_HOURS = 40;
  16.         File Prog213a = new File("Prog213a.dat");
  17.  
  18.         try {
  19.             // creating an array to store each day's number of hours
  20.             Scanner scnr = new Scanner(Prog213a);
  21.             int hours[] = new int[7];
  22.             int totalHours = 0;
  23.             int weeklyCounter = 0;
  24.             double pay = 0;
  25.  
  26.             while (scnr.hasNextLine()) {
  27.                 weeklyCounter++;
  28.                 // prints out the number of hours worked each week
  29.                 // (just like in the file, 7 hours per line)
  30.                 String line = scnr.nextLine();
  31.                 System.out.println("Hours worked: " + line);
  32.                 // creates a string for each number in the line
  33.                 String[] hoursString = line.split(" ");
  34.  
  35.                 for (int i = 0; i < 7; i++) {
  36.                     // picking out each day's amount of hours
  37.                     hours[i] = Integer.parseInt(hoursString[i]);
  38.                     totalHours += hours[i];
  39.                 }
  40.  
  41.                 for (int i = 0; i < 7; i++) {
  42.  
  43.                     // if Saturday...
  44.                     if (i == 5) {
  45.                         // if working overtime...
  46.                         if (hours[i] > MAX_DAILY_HOURS) {
  47.                             // adds Saturday-specific overtime bonus
  48.                             pay += (hours[i] - MAX_DAILY_HOURS) * DAILY_OVERTIME_BONUS * SATURDAY_BONUS_PERCENTAGE;
  49.                         }
  50.                         // adds normal daily wage for Saturday
  51.                         pay += hours[i] * DAILY_WAGE * SATURDAY_BONUS_PERCENTAGE;
  52.  
  53.                     // if Sunday...
  54.                     } else if (i == 6) {
  55.                         // if working overtime...
  56.                         if (hours[i] > MAX_DAILY_HOURS) {
  57.                             // adds Sunday-specific overtime bonus
  58.                             pay += (hours[i] - MAX_DAILY_HOURS) * DAILY_OVERTIME_BONUS * SUNDAY_BONUS_PERCENTAGE;
  59.                         }
  60.                         // adds normal daily wage for Sunday
  61.                         pay += hours[i] * DAILY_WAGE * SUNDAY_BONUS_PERCENTAGE;
  62.  
  63.                     // if any weekday...   
  64.                     } else {
  65.                         // if working overtime...
  66.                         if (hours[i] > MAX_DAILY_HOURS) {
  67.                             //adds weekday-specific overtime bonus
  68.                             pay += (hours[i] - MAX_DAILY_HOURS) * DAILY_OVERTIME_BONUS;
  69.                         }
  70.                         // adds normal daily wage for weekdays
  71.                         pay += hours[i] * DAILY_WAGE;
  72.                     }
  73.  
  74.                 }
  75.  
  76.                 // adds overtime bonus for whole week
  77.                 if (totalHours > MAX_WEEKLY_HOURS) {
  78.                     pay += (totalHours - MAX_WEEKLY_HOURS) * WEEKLY_OVER_TIME_BONUS;
  79.                 }
  80.  
  81.                 System.out.println("Week #" + weeklyCounter + " :$" + EasyFormat.format(pay, 1, 2));
  82.                 System.out.println();
  83.                 pay = 0;
  84.                 totalHours = 0;
  85.             }
  86.  
  87.         } catch (FileNotFoundException e) {
  88.             // prints out what the exception is if there is one
  89.             e.printStackTrace();
  90.  
  91.         }
  92.  
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement