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 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...
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...
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-*
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...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Step 1: Your program will now take its input from this file instead of from the...
Step 1: Your program will now take its input from this file instead of from the user. Here's what you want to do: Remove the call to getUserInput(). You can also remove its definition and prototype if you like. For reasons that will become clear later, don't close the file until just before the return statement at the end of main. Run your code to make sure that the file is opened and read correctly. Hint: you may test the...
I need C++ program that Read an input file of text.txt one word at a time....
I need C++ program that Read an input file of text.txt one word at a time. The file should consist of about 500 words. The program should remove all punctuations,keep only words. Store the words in a built-in STL container, such as vector or map.Can someone help with any additional comments that I can understand the logic?thank you
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT