Question

In: Computer Science

I need to update this java program to take input about each employee from a file...

I need to update this java program to take input about each employee from a file and write their information and salary/pay information to a file. use input file

payroll.txt

import java.util.Scanner;
public class SalaryCalcLoop
{
double Rpay = 0, Opay = 0;
void calPay(double hours, double rate) {
if (hours <= 40)
{
Rpay = hours * rate;
Opay = 0;
}
else
{
double Rhr, Ohr;
Rhr = 40;
Ohr = hours - Rhr;
Rpay = Rhr * rate;
Opay = Ohr * (1.5 * rate);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name;
int shift = 0;
Double rate, hours;
System.out.println("Pay Calculator");
String ch = "";
do
{
System.out.println("Enter Your Name");
name = sc.next();
System.out.println("Enter Your Shift, Enter 0 for Day, Enterv1 for Night");
System.out.println("0=Day, 1= Night");
shift=sc.nextInt();
System.out.println("Enter Number of Hours Worked");
hours = sc.nextDouble();
System.out.println("Enter Hourly Pay");
rate = sc.nextDouble();
SalaryCalc c = new SalaryCalc();
c.calPay(hours, rate);
Double Tpay = c.Rpay + c.Opay;
System.out.println();
System.out.println("Calculate Pay");
System.out.println("Employee Name: " + name);
System.out.println("Employee Regular Pay: " + c.Rpay);
System.out.println("Employee Overtime Pay: " + c.Opay);
System.out.println("Employee Total Pay: " + Tpay);
if (shift == 0)
{
System.out.println("Employee PayPeriod is Friday");
}
else
{
System.out.println("Employee PayPeriod is Saturday");
}
System.out.println("Press Y to continue. Press any other key to exit ");
ch=sc.next();
}
while(ch.equalsIgnoreCase("y"));
}
}

Saturday"); } System.out.println("Press Y to continue. Press any other key to exit "); ch=sc.next(); } while(ch.equalsIgnoreCase("y")); } }

Payroll.txt

Kevin

Yang

60

20

Trey

Adams

30

15

Rick

Johnson

45

10

Cynthia

Wheeler

55

11.50

Sarah

Davis

24

10

Muhammad

Rabish

66

12

Dale

Allen

11

18

Andrew

Jimenez

80

15

Solutions

Expert Solution

Thanks for the question.

Here is the completed code for this problem. 

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer. 

Thanks

Note:

Update the input and output file file name in the below code

 // read from the below file 
private static final String INPUT_FILE = "Payroll.txt"; 

// writes the calculated pay to the below file 
private static final String OUTPUT_FILE = "Payroll_Output.txt";

===========================================================================

import java.io.*;
import java.util.Scanner;

public class SalaryCalculator {

    // read from the below file
    private static final String INPUT_FILE = "Payroll.txt";
    // writes the calculated pay to the below file
    private static final String OUTPUT_FILE = "Payroll_Output.txt";

    public static void main(String[] args) {


        try {
            Scanner fileReader = new Scanner(new File(INPUT_FILE));
            PrintWriter writer = new PrintWriter(new FileWriter(OUTPUT_FILE));

            while (fileReader.hasNext()) {
                String firstName = fileReader.nextLine();
                String lastName = fileReader.nextLine();
                int hours = Integer.parseInt(fileReader.nextLine());
                double rate = Double.parseDouble(fileReader.nextLine());

                double regularPay = regularPay(hours, rate);
                double overtimePay = overtimePay(hours, rate);

                writer.write(firstName + " " + lastName + "\r\n");
                writer.write("Regular Pay: $" + String.format("%.2f\r\n", regularPay));
                writer.write("Overtime Pay: $" + String.format("%.2f\r\n", overtimePay));
                writer.write("Total Pay: $" + String.format("%.2f\r\n", overtimePay + regularPay));
            }

            fileReader.close();
            writer.close();
        } catch (FileNotFoundException e) {
            System.out.println("Unable to read data from file: " + INPUT_FILE);
        } catch (IOException e) {
            System.out.println("Unable to write to file: " + OUTPUT_FILE);
        }
    }

    // takes in hours and rate returns the regular pay
    public static double regularPay(int hours, double rate) {
        if (hours <= 40) return hours * rate;
        else return 40 * rate;
    }

    // takes in hours and rate and returns the overtime pay
    public static double overtimePay(int hours, double rate) {
        if (hours <= 40) return 0;
        else return (hours - 40) * hours;
    }
}

=================================================================


Related Solutions

I need to update this java program to take input about each employee from a file...
I need to update this java program to take input about each employee from a file and write their information and salary/pay information to a file. use input file payroll.txt Kevin Yang 60 20 Trey Adams 30 15 Rick Johnson 45 10 Cynthia Wheeler 55 11.50 Sarah Davis 24 10 Muhammad Rabish 66 12 Dale Allen 11 18 Andrew Jimenez 80 15 import java.util.Scanner; public class SalaryCalcLoop { double Rpay = 0, Opay = 0; void calPay(double hours, double rate)...
Updating the following Java program below, I need to continue to take input for every employee...
Updating the following Java program below, I need to continue to take input for every employee in a company, and display their information until a sentinel value is entered that exits the loop and ends the program import java.util.Scanner; public class SalaryCalc { double Rpay=0, Opay=0; void calPay(double hours, double rate) { if(hours<=40) { Rpay = hours * rate; Opay = 0; } else { double Rhr,Ohr; Rhr = 40; Ohr = hours-Rhr; Rpay = Rhr * rate; Opay =...
I only need to update this JAVA program to be able to : 1 ) Exit...
I only need to update this JAVA program to be able to : 1 ) Exit cleanly after creating the chart 2 ) change the pie chart to a perfect circle rather than an oval 3 ) provide a separate class driver import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.util.Scanner; import javax.swing.JComponent; import javax.swing.JFrame; // class to store the value and color mapping of the // pie segment(slices) class Segment { double value; Color color; // constructor public...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not against it being included in the answer* I must read off of an excel file (comma separated) to input five different things for a book, all comma separated, there are 360 books in the file. The book title, author, ISBN number, number of pages, and finally the year it was published. Now some of the ISBN or Pg numbers may be missing and will...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
Program Language C++ How do I take lines of string from an input file, and implement...
Program Language C++ How do I take lines of string from an input file, and implement them into a stack using a double linked list? Example input, command.txt, and output file. Ex: Input.txt postfix: BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D / E+ Command.txt printList printListBackwards Output.txt List: postfix:BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D/E+ Reversed List: postfix:AB-CF*-D/E+ postfix:AB-C-D/ postfix:FE-C/B*A+ prefix:+A*B/C-EF postfix:BAC-*
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
In Java, I need a program that will ask a user to input how many tests...
In Java, I need a program that will ask a user to input how many tests they want the average of. For example, It needs to ask the user how many tests would you like the average of? When the user enters the amount of tests they want the average score of, the program needs to give them that many test scores to input. Then with the average, determine what their grade is, if 90-100=A if 80-90 = B etc....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT