Question

Let's update our program, SalaryCalcLoop.java, from last week, to now take input about each employee from...

Let's update our program, SalaryCalcLoop.java, from last week, to now take input about each employee from a file and write their information and salary/pay information to a file.

Rashid
night
60
20
Chin
day
30
15
Tran
day
45
10
Deeshan
night
55
11.5
Serena
day
24
10
Deepak
day
66
12
Tyrone
night
11
18
Andre
night
80
15

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

public class SalaryCalcLoop{
double pay=0, OTpay=0;
void gpay(double hours, double wage){
if (hours <= 40){
pay = hours * wage;
OTpay = 0;
}
else{
double hr, OThr;
hr = 40;
OThr = hours - hr;
pay = hr * wage;
OTpay = OThr *(wage * 1.5);
}
}
public static void main(String[] args) throws IOException {


Scanner input = new Scanner(System.in);
String name;
String shifts;
int shift;
Double wage, hours;
int sentinel = 1;

while (sentinel != 0){
System.out.println("Please enter employee name");
name = input.next();
System.out.println("Please enter the shift.");
System.out.println("For the DAY shift enter 1. For the NIGHT shift enter 2.");
shift = input.nextInt();
System.out.println("How many hours did the employee work this week?");
hours = input.nextDouble();
System.out.println("Please enter your hourly wage.");
wage = input.nextDouble();
SalaryCalc s = new SalaryCalc();
s.gpay(hours, wage);
double totalpay = s.pay + s.OTpay;
System.out.println("Employee Name: "+name);
System.out.println("Employee's regular pay: "+s.pay);
System.out.println("Employee's overtime pay: "+s.OTpay);
System.out.println("Employee's total pay: "+totalpay);
if(shift == 1){
System.out.println("Employee's pay period is Friday");
}
else if(shift == 2){
System.out.println("Employee's pay period is Saturday");
}
System.out.println("Do you wish to enter another employee?");
System.out.println("Enter a number to continue or 0 to end");
sentinel = input.nextInt();
input.close();
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

public class SalaryCalcFile {
double pay = 0, OTpay = 0;

void gpay(final double hours, final double wage) {
if (hours <= 40) {
pay = hours * wage;
OTpay = 0;
} else {
double hr, OThr;
hr = 40.0;
OThr = hours - hr;
pay = hr * wage;
OTpay = OThr * (wage * 1.5);
}
}
// methoid to read data from given file name and call methd to write the data to given output file name
static void readFromFile(final String fileNameInput, final String fileNameOutput) throws IOException {
// Scanner object to read from file
final Scanner fileInput = new Scanner(new File(fileNameInput));
// FIle Writer object to write data to the file
final FileWriter fw = new FileWriter(new File(fileNameOutput));
// some req variables
String name;
String shift;
Double wage;
Double hours;

//while file has some input
while (fileInput.hasNextLine()) {
// read data into variables
name = fileInput.nextLine();
shift = fileInput.nextLine();
hours = Double.valueOf(fileInput.nextLine());
wage = Double.valueOf(fileInput.nextLine());
// call write to file method to write data to file
writeToFile(name, shift, hours, wage, fw);
}

// close Scanner object
fileInput.close();
// close file writer object
fw.close();
}
// method to write data to the file
static void writeToFile(final String name, final String shift, final Double hours, final Double wage, final FileWriter fw) {
final SalaryCalcFile s = new SalaryCalcFile();
s.gpay(hours, wage);
final double totalpay = s.pay + s.OTpay;
// using try catch to catch some unexpected behaviour
try {
fw.write("Employee Name: " + name + '\n');
fw.write("Employee's regular pay: " + s.pay + '\n');
fw.write("Employee's overtime pay: " + s.OTpay + '\n');
fw.write("Employee's total pay: " + totalpay + '\n');
if (shift.equals("day")) {
fw.write("Employee's pay period is Friday" + "\n\n");
} else if (shift.equals("night")) {
fw.write("Employee's pay period is Saturday" + "\n\n");
}
} catch (final Exception e) {
// display the caught exception
System.out.println(e);
}

}

public static void main(final String[] args) throws IOException {

final Scanner input = new Scanner(System.in);
// variables to store name of input and output files
String fileNameInput;
String fileNameOutput;
System.out.println("Enter the name of the input file: ");
fileNameInput = input.nextLine();
System.out.println("Entere the Name of the Output File");
fileNameOutput = input.nextLine();
// cll read from file method
readFromFile(fileNameInput, fileNameOutput);
// close Scanner object
input.close();
// output messege to the console
System.out.println("Success...");
}
}

ScreenShots:

Output:

//for any other query please comment and if you like the answer please give an upvote :)

Add a comment
Know the answer?
Add Answer to:
Let's update our program, SalaryCalcLoop.java, from last week, to now take input about each employee from...
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
  • Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import...

    Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import java.util.*; public class SalaryCalc { public static void main(String [] args) { Scanner input = new Scanner(System.in); int sentinelValue = 1; //initializes sentinelValue value to 1 to be used in while loop while (sentinelValue == 1) { System.out.println("Enter 1 to enter employee, or enter 2 to end process: ");    sentinelValue = input.nextInt(); input.nextLine(); System.out.println("enter employee name: "); String employeeName = input.nextLine(); System.out.println("enter day...

  • Chapter 4 Loop logic and using loop logic for File Input/0utput. 1. Read Chapter 4 sections...

    Chapter 4 Loop logic and using loop logic for File Input/0utput. 1. Read Chapter 4 sections 4.10 until end of chapter. 2. Review Questions and Exercises, short answers. 3. [20 points] submit SalaryCalcLoopFile.java Let's update our program, SalaryCalcLoop.java, from last week, to now take input about each employee from a file and write their information and salary/pay information to a file. use input file, where each employee has name,shift,hours worked,hourly rate: EmployeePayroll.txt output file should be named: EmployeePayStubs.txt EmployeePayroll.txt Rashid...

  • I need this java program to contain methods that do some of the work that can...

    I need this java program to contain methods that do some of the work that can be modularized. It must logically be the same as this loop program but instead the logic will be modularized into Java Methods. 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...

  • This is my code but my overtime calculation is wrong and I cant figure out how...

    This is my code but my overtime calculation is wrong and I cant figure out how to round to two decimal places import java.util.Scanner; public class HourlyWage { public static void main(String[] args) { //Variables double totalWage; double totalOvertimePay; double totalPay; String name; double overtimeHours = 0.0; double hoursWorked = 0.0; double hourlyWage = 0.0; Scanner keyboard = new Scanner(System.in); System.out.println("Please enter your name "); name = keyboard.next(); System.out.println("Please enter your hourly wage "); hourlyWage = keyboard.nextDouble(); System.out.println("How many hours...

  • Write a Java method that should take an ArrayList as a parameter, print its element in...

    Write a Java method that should take an ArrayList as a parameter, print its element in the reverse order. The main function that calls the method is the following: import java.util.*; public class ReverseGen {   public static void main(String[] args) {     Scanner input = new Scanner(System.in);       System.out.println("How many numbers do you want to input?");       int num = input.nextInt();       ArrayList<Double> d = new ArrayList<Double>(num);       for(int i = 0 ; i < num; i++){           System.out.print("Enter...

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so...

    Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so they throw exceptions when the following errors occur. - The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. - The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. - The ProductionWorker class should thrown anexception named InvalidPayRate when it receives a negative number...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • How would you write the following program using switch statements instead of if-else statements (in java)...

    How would you write the following program using switch statements instead of if-else statements (in java) import java.util.*; public class ATM { public static void main(String[] args) { Scanner input = new Scanner (System.in); double deposit, withdrawal; double balance = 6985.00; //The initial balance int transaction; System.out.println ("Welcome! Enter the number of your transaction."); System.out.println ("Withdraw cash: 1"); System.out.println ("Make a Deposit: 2"); System.out.println ("Check your balance: 3"); System.out.println ("Exit: 4"); System.out.println ("***************"); System.out.print ("Enter Your Transaction Number "); transaction...

  • Take the following code and add a joption pane to make it eaiser to read. Can...

    Take the following code and add a joption pane to make it eaiser to read. Can anyone tell me whats wrong with my code? It wont compile JAVA. import java.util.Scanner; import java.util.Queue; import java.util.LinkedList; class que { public static void main(String args[]) { int count=0,min=0,hr=0; String a; Scanner inp=new Scanner(System.in); Queue q=new LinkedList<>(); while(count<10) { System.out.println("Enter your name:"); a=inp.nextLine(); q.add(a); count=count+1; min=min+5; if(min>=60) { hr=hr+1; } System.out.println("You are "+count+" in the queue"); System.out.println("Average wait time is "+hr+" hours and "+min+"...

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