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

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) {
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")); } }

Solutions

Expert Solution

//Java code

import java.text.NumberFormat;

public class Employee {
    private String firstName;
    private String lastName;
    private double hours;
    private double rate;

    //Overload constructor
    public Employee(String firstName, String lastName, double hours, double rate) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.hours = hours;
        this.rate = rate;
    }
    //Constructor
    public Employee()
    {

    }
    //getters and setters

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public double getHours() {
        return hours;
    }

    public void setHours(double hours) {
        this.hours = hours;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }
    // Calculate pay of employee
    public double calPay() {
        double salary =0;
        if (hours <= 40)
        {
            salary = hours * rate;

        }
        else
        {
            salary = 40*rate + (hours-40)*rate*1.5;
        }
        return salary;
    }

    /**
     * 
     * @return Description of the employee object
     */
    @Override
    public String toString() {
        return String.format("%5s %20.20s %10.20s","Name: ",firstName+" "+lastName," Salary: "+ NumberFormat.getCurrencyInstance().format(calPay()));
    }
}

//=======================================

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SalaryCalcLoop
{
    public static void main(String[] args) {
        //Create employee object
        Employee employee = new Employee();
        try {
            //Open file to read
            Scanner sc = new Scanner(new File("payroll.txt"));

            //Read the file until it has next line
            while (sc.hasNextLine())
            {
                //Set the attributes
                employee.setFirstName(sc.next());
                employee.setLastName(sc.next());
                employee.setHours(Double.parseDouble(sc.next()));
                employee.setRate(Double.parseDouble(sc.next()));
                //Print employee object with pay details
                System.out.println(employee);
            }
        } catch (FileNotFoundException e) {
            System.err.println("File not found!");
        }


    }
}

//File

//Output

//If you need any help regarding this solution .......... please leave a comment ...... thanks


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 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 *...
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