Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class WeirdWages {
- public static void main(String[] args) {
- // initializing variables
- final double DAILY_WAGE = 30.00;
- final double DAILY_OVERTIME_BONUS = 25.50;
- final double WEEKLY_OVER_TIME_BONUS = 15.00;
- final double SATURDAY_BONUS_PERCENTAGE = 2.25;
- final double SUNDAY_BONUS_PERCENTAGE = 1.50;
- final int MAX_DAILY_HOURS = 8;
- final int MAX_WEEKLY_HOURS = 40;
- File Prog213a = new File("Prog213a.dat");
- try {
- // creating an array to store each day's number of hours
- Scanner scnr = new Scanner(Prog213a);
- int hours[] = new int[7];
- int totalHours = 0;
- int weeklyCounter = 0;
- double pay = 0;
- while (scnr.hasNextLine()) {
- weeklyCounter++;
- // prints out the number of hours worked each week
- // (just like in the file, 7 hours per line)
- String line = scnr.nextLine();
- System.out.println("Hours worked: " + line);
- // creates a string for each number in the line
- String[] hoursString = line.split(" ");
- for (int i = 0; i < 7; i++) {
- // picking out each day's amount of hours
- hours[i] = Integer.parseInt(hoursString[i]);
- totalHours += hours[i];
- }
- for (int i = 0; i < 7; i++) {
- // if Saturday...
- if (i == 5) {
- // if working overtime...
- if (hours[i] > MAX_DAILY_HOURS) {
- // adds Saturday-specific overtime bonus
- pay += (hours[i] - MAX_DAILY_HOURS) * DAILY_OVERTIME_BONUS * SATURDAY_BONUS_PERCENTAGE;
- }
- // adds normal daily wage for Saturday
- pay += hours[i] * DAILY_WAGE * SATURDAY_BONUS_PERCENTAGE;
- // if Sunday...
- } else if (i == 6) {
- // if working overtime...
- if (hours[i] > MAX_DAILY_HOURS) {
- // adds Sunday-specific overtime bonus
- pay += (hours[i] - MAX_DAILY_HOURS) * DAILY_OVERTIME_BONUS * SUNDAY_BONUS_PERCENTAGE;
- }
- // adds normal daily wage for Sunday
- pay += hours[i] * DAILY_WAGE * SUNDAY_BONUS_PERCENTAGE;
- // if any weekday...
- } else {
- // if working overtime...
- if (hours[i] > MAX_DAILY_HOURS) {
- //adds weekday-specific overtime bonus
- pay += (hours[i] - MAX_DAILY_HOURS) * DAILY_OVERTIME_BONUS;
- }
- // adds normal daily wage for weekdays
- pay += hours[i] * DAILY_WAGE;
- }
- }
- // adds overtime bonus for whole week
- if (totalHours > MAX_WEEKLY_HOURS) {
- pay += (totalHours - MAX_WEEKLY_HOURS) * WEEKLY_OVER_TIME_BONUS;
- }
- System.out.println("Week #" + weeklyCounter + " :$" + EasyFormat.format(pay, 1, 2));
- System.out.println();
- pay = 0;
- totalHours = 0;
- }
- } catch (FileNotFoundException e) {
- // prints out what the exception is if there is one
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement