Question
write this program in Java
Problem 5. (30 Points) Write a program that reads employee work data from a text file employee.txt), and then calculates payr
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class EmployeePayroll {

   public static void main(String[] args) {
       // File names
       String inputFileName = "employee.txt";
       String outputFileName = "payroll.txt";

       // represent name of employees with highest and lowest pay
       String lowestPaid = "", highestPaid = "";
       double lowest = Double.MAX_VALUE, highest = Double.MIN_VALUE;
       try {
           // Open file to read
           BufferedReader br = new BufferedReader(new FileReader(inputFileName));
           // Open file to write
           PrintWriter pr = new PrintWriter(new FileWriter(outputFileName));
           String line;

           // Print header in output file
           pr.printf("%-15s%-15s%-15s\n", "Name", "Hours", "Pay");

           // Iterate loop till there is line in input file
           while ((line = br.readLine()) != null) {
               // Split line by delimiter space " "
               String tokens[] = line.trim().split(" ");
               // name and wage from from the first two places
               String name = tokens[0].trim();
               double wage = Double.parseDouble(tokens[1].trim());
               double hours = 0;

               // Iterate loop for remaining values in token
               // Add the extracted value to hours
               for (int i = 2; i < tokens.length; i++) {
                   hours += Double.parseDouble(tokens[i].trim());
               }

               // Calculate pay
               double pay = wage * hours;

               // If lowest > pay, set lowest = pay and lowestPaid = current employee name
               if (lowest > pay) {
                   lowest = pay;
                   lowestPaid = name;
               }

               // If highest < pay, set highest = pay and highestPaid = current employee name
               if (highest < pay) {
                   highest = pay;
                   highestPaid = name;
               }
               // Print name, hours and pay to file
               pr.printf("%-15s%-15.2f$%-15.2f\n", name, hours, pay);
           }

           // Close both files
           pr.close();
           br.close();

           // print lowest and highest
           System.out.println("Lowest Paid employee: " + lowestPaid);
           System.out.println("Highest Paid employee: " + highestPaid);
       } catch (FileNotFoundException e) {
           System.err.println(e.getMessage());
       } catch (IOException e) {
           System.err.println(e.getMessage());
       }
   }
}

OUTPUT

Problems @ Javadoc Declaration > Searc <terminated> Employee Payroll [Java Application Lowest Paid employee: Manny Highest Pa

OUTPUT FILE

payroll - Notepad File Edit Format View Help Name Hours Smith 42.00 Manny 21.50 Miller 18.50 Baker 22.50 Sammy 25.50 Choi 50.

Add a comment
Know the answer?
Add Answer to:
write this program in Java Problem 5. (30 Points) Write a program that reads employee work...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON CODE First Code: Write a program that uses a text file to store the daysand...

    PYTHON CODE First Code: Write a program that uses a text file to store the daysand hours that a user worked in a week. The program should begin by prompting for the number of days worked in the week. It should continue with a loop for input of the days and hours and for writing these to the file, each on its own line. Sample Output (inputs shown in boldface) How many days did you work this week? 5 Enter...

  • Need this program ASAP C++ language 1b. Write the implementation file timeClock.cpp for the class TimeClock....

    Need this program ASAP C++ language 1b. Write the implementation file timeClock.cpp for the class TimeClock. Com pile the file, and save it in the Chap 13 folder of your Student Data Files. 1c. Write a C++ program named weeklyPay.cpp to test your class Timeclock Your program should ask the user how many hours the user worked each day of the week. An object should be created for each day of the week with the number of hours for each...

  • Coding in C++ Write a program using structures to store the following weather information: - Month...

    Coding in C++ Write a program using structures to store the following weather information: - Month name - Day of month (Monday, Tuesday, etc) - High Temperature - Low Temperature - Rainfall for the day Use an the input.txt file to load the data into weather structures. Once the data for all the days is entered, the program should calculate and display the: - Total rainfall for the data - Average daily temperatures. (note: you'll need to calculate the days...

  • Lab Assignment 4 CIS 2571 Due: 10 Points Create a Java application with the following specifications:...

    Lab Assignment 4 CIS 2571 Due: 10 Points Create a Java application with the following specifications: Your company's IT dept. is creating a schedule for Thanksgiving week. The Thursday is a holiday and therefore no one will be working. There are 3 employees and they have submitted their preferred days to work during that 5-day week ("X" marks the employee's work day): M F Employee # First Name Tu Th 1 Jane X X X Holiday 2 Pablo X Holiday...

  • PYTHON PROGRAMMING LANGUAGE (NEEDED ASAP) Using a structured approach to writing the program: This section will...

    PYTHON PROGRAMMING LANGUAGE (NEEDED ASAP) Using a structured approach to writing the program: This section will guide you in starting the program in a methodical way. The program will display a window with GUI widgets that are associated with particular functions: Employee Payroll O X -- - - - - - - - - - - Show Payroll Find Employee by Name Highest Lowest Find Employee by Amount Write Output to Fie Cancel - -------- ------------- Notice that some of...

  • Libby is an hourly employee who earns $14.32/hour. She is paid overtime only for hours worked...

    Libby is an hourly employee who earns $14.32/hour. She is paid overtime only for hours worked past 40 in one week. During a weekly pay period, she worked the following hours: Day Hours Minutes Monday 8 47 Tuesday 8 22 Wednesday 6 25 Thursday 9 6 Friday 7 58 What is the difference between her pay using the quarter-hour and the hundredth-hour methods? Which method pays Libby the greater amount? (Do not round interim calculations. Round final answer to two...

  • Complete a time card for the employee below: Adam Spruce (SSN 565-56-5656) worked six days during...

    Complete a time card for the employee below: Adam Spruce (SSN 565-56-5656) worked six days during the week of 01/15/2018 through 01/21/2018 (he was off work on Tuesday). He arrived at 9:00 a.m. each day (except Friday, when he was four minutes early, and Sunday, when he was two minutes late). He left for lunch at 12:30 p.m. each day (except Wednesday, when he left at 12:28 p.m., and Thursday, when he left at 1:01 p.m.), and arrived back at...

  • Java Programming Reading from a Text File Write a Java program that will use an object...

    Java Programming Reading from a Text File Write a Java program that will use an object of the Scanner class to read employee payroll data from a text file The Text file payroll.dat has the following: 100 Washington Marx Jung Darwin George 40 200 300 400 Kar Car Charles 50 22.532 15 30 The order of the data and its types stored in the file are as follows: Data EmployeelD Last name FirstName HoursWorked double HourlyRate Data Tvpe String String...

  • Your professor is hoping to get a summer job flipping burgers at the local burger joint....

    Your professor is hoping to get a summer job flipping burgers at the local burger joint. His starting salary will be $7.25 per hour. However, there are certain incentives to encourage him to work extra hours. It is calculated as follows per day: •$7.25 for the first 6 hours he works per day, Monday through Friday .•$8.25 for the next 2 hours he works per day, Monday through Friday. •$11.25 for each hour he works over 8 hours in a...

  • IN PYTHON the original problem was Design (pseudocode) and implement (source code) a program (name it...

    IN PYTHON the original problem was Design (pseudocode) and implement (source code) a program (name it WeeklyHours) to compute the total weekly hours for 3 employees. The program main method defines a two-dimensional array of size 3x7 to store employers’ daily hours for the week. Each row represents one employee and each column represent one day of the week such that column 0 designates Monday, column 1 designates Tuesday, etc. The program main method populates the array with random numbers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT