Assignment #2 The MISER CORPORATION must meet its payroll within one week. They
ID: 3868665 • Letter: A
Question
Assignment #2
The MISER CORPORATION must meet its payroll within one week. They wish to save on pencils, paper and accountants. They have, therefore, decided to do all the calculations by computer. Having heard of your programming experience and your willingness to work for pennies, the company has hired you to write a complete C++ program, including comments to do the following:
1. Print the following heading at the top of the output page: Miser Corporation Payroll
2. Read an unknown number of employee data records as shown below. Each group of data will contain an employee's first and last name, hours worked, rate of pay and age. A typical group of data will be:
Duck Donald
45.0 3.25 51
Print the data as it is read; together with appropriate messages (e.g., the name is ..., the rate of pay is ..., etc.).
3. For each employee, compute and print the employee's base pay, which includes overtime (paid at one and a half times the normal rate) for each hour over 40. For example, if an employee earning $20.00 per hour works for 48 hours, then she will be paid for 40 hours at her normal rate plus 8 extra hours at $30.00 (one and a half times $20.00).
4. For each employee compute the tax paid by the employee, according to this formula: If the employee is 55 years old (or older), then the employee pays tax at a rate of 50% of the base pay; if the employee is below 55, then the tax is 10% of the base pay. Print the tax and the net pay after taxes.
5. Repeat this process until you have read the last employee. You decide how to detect the end of the data (you should explain your method in a comment).
6. Print the age and name of the oldest employee. Do the same for the youngest employee.
7. After all your results have been printed, print a message saying that the5 payroll program is complete.
I got most of it but I need help with number 5 and how to have it read to a txt file
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int NAME_SIZE = 51;
struct records {
public:
char name[NAME_SIZE];
int hours;
double rate;
int age;
double basepay;
double tax;
double netpay;
};
int main() {
cout << "tttMiser Corporation Payrolln" << endl;
ifstream datafile;
datafile.open("datafile.txt", ios::in);
if (datafile.fail())
{
cout << "Error opening file; program halted." << endl;
return 0;
}
records employee;
datafile.read(reinterpret_cast<char *>(&employee),sizeof(employee));
while (!datafile.eof()){
cout << "Name:t" << employee.name << endl;
cout << "Hours worked:t" << employee.hours << endl;
cout << "Rate of pay:t" << employee.rate << endl;
cout << "Age:t" << employee.age << endl;
cout << endl;
datafile.read(reinterpret_cast<char *>(&employee),sizeof(employee));
}
datafile.close();
cout << "The payroll program is complete.";
return 0;
}
public class PayrollCalc
{
private String name;
private double hoursWorked;
private double hourlyPayRate;
private static final double FEDERAL_TAX_WITHHOLDING_RATE = 0.20;
private static final double STATE_TAX_WITHHOLDING_RATE = 0.09;
public PayrollCalc()
{
}
public PayrollCalc(String n, double hw, double hpr)
{
this.name = n;
this.hoursWorked = hw;
this.hourlyPayRate = hpr;
}
public double grossPay(double hourlyRate, double hrsWorked)
{
double grossPay = (hourlyRate * hrsWorked);
return grossPay;
}
public double federalTax(double gross)
{
return FEDERAL_TAX_WITHHOLDING_RATE * gross;
}
public double stateTax(double gross)
{
return STATE_TAX_WITHHOLDING_RATE * gross;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHoursWorked() {
return hoursWorked;
}
public void setHoursWorked(double hoursWorked) {
this.hoursWorked = hoursWorked;
}
public double getHourlyPayRate() {
return hourlyPayRate;
}
public void setHourlyPayRate(double hourlyPayRate) {
this.hourlyPayRate = hourlyPayRate;
}
}
import java.util.*;
public class PayrollDriver
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
PayrollCalc data = new PayrollCalc();
System.out.println("Enter your name: ");
data.setName(input.next());
System.out.println("Number of Hours Worked: ");
data.setHoursWorked(Double.parseDouble(input.next()));
System.out.println("Hourly Pay Rate: ");
data.setHourlyPayRate(Double.parseDouble(input.next()));
double grossAmount = data.grossPay(data.getHourlyPayRate(), data.getHoursWorked());
System.out.println();
System.out.println("------------------------------------");
System.out.println("Name: " + data.getName());
System.out.println("Hours Worked: " + data.getHoursWorked() + "hrs");
System.out.println("Pay Rate: $" + data.getHourlyPayRate());
System.out.println("Gross Pay: $" + grossAmount + ' ');
System.out.println("DEDUCTIONS");
System.out.println("Federal Tax Withholding (20%): $" + data.federalTax(grossAmount));
System.out.println("State Tax Withholding (9%): $" + data.stateTax(grossAmount));
System.out.println("Total Deductions: $" + (data.federalTax(grossAmount) + data.stateTax(grossAmount)));
System.out.println("Net Pay: $" + (grossAmount - (data.federalTax(grossAmount) + data.stateTax(grossAmount))));
input.close();
}
}