Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to get rid of exceptions? In order to populate the array, you will need to split() each String on the (,) character so you

How to get rid of exceptions?

In order to populate the array, you will need to split() each String on the (,) character so you can access each individual value of data. Based on the first value (e.g., #) your method will know whether to create a new Manager, HourlyWorker, or CommissionWorker object. Once created, populate the object with the remaining values then store it in the array. Finally, iterate through the array of employees using the enhanced for loop syntax, and print out each employee in the following format:

Manager:

Steve Davis $2000

Commission Employee:

John Kanet $1500 (Base salary: $800, sales: $7000, Commission rate: 10%)

Hourly Worker:

Thomas Hill $1100 (Hourly wage: $20, hours worked: 50)

 

this is what the text file says:

#Steve, Davis, 2000 *John, Kanet, 800, 7000, 0.10 @Thomas, Hill,20,50 *Lisa, Green,800,6000,0.10 *Chasidy , Funderburk,1000,5000,0.08 @Kayleigh , Bertrand, 25, 45 *Zane, Eckhoff,1800,900, 0.09 @Teressa, Bitterman, 15,35 *Yuri, Viera, 600, 5000,0.10

this is what I have completed so far:

public abstract class Employee { //WE ARE NOT ALLOWED TO CHANGE THE EMPLOYEE CLASS AT ALL private String firstName; private String lastName; private static int employeeID=100; public Employee(String firstName, String lastName) { setFirstName(firstName); setLastName(lastName); employeeID++; }

public void setFirstName(String firstName){ this.firstName=firstName; } public String getFirstName(){ return firstName; } public void setLastName(String lastName){ this.lastName=lastName; } public String getLastName(){ return lastName; } public int getEmplyeeID(){ return employeeID; } public String toString(){ return String.format("%20s%20s", firstName, lastName); } public abstract double earnings(); }

________________________________________________________

public class Manager extends Employee {

private double weeklySalary; public Manager(String firstName, String lastName, double weeklySalary) { super(firstName, lastName); this.weeklySalary = weeklySalary; } public double getWeeklySalary() { return weeklySalary; } public void setWeeklySalary(double weeklySalary) { this.weeklySalary = weeklySalary; }

public double earnings() {

return getWeeklySalary();

}

}

_____________________________________

public class HourlyWorker extends Employee { private int hoursWorked; private double wage;

public HourlyWorker(String firstName, String lastName, int hoursWorked,double wage) {

super(firstName, lastName); this.hoursWorked = hoursWorked; this.wage = wage; } public int getHoursWorked() { return hoursWorked; } public double getWage() { return wage; } public void setHoursWorked(int hoursWorked) { this.hoursWorked = hoursWorked; } public void setWage(int wage) { this.wage = wage; } public double earnings() { double pay; if (hoursWorked > 40) { pay = (40 * wage) + (1.5 * (hoursWorked - 40) * wage); } else { pay = wage * hoursWorked; } return pay; }

}

_________________________________________

public class CommissionEmployee extends Employee {

double weeklySalary, commissionRate, weeklySales;

public CommissionEmployee(String firstName, String lastName, double weeklySalary, double commissionRate, double weeklySales) { super(firstName, lastName); this.weeklySalary = weeklySalary; this.commissionRate = commissionRate; this.weeklySales = weeklySales; } public double getWeeklySalary() { return weeklySalary; } public double getCommissionRate() { return commissionRate; } public double getWeeklySales() { return weeklySales; } public void setWeeklySalary(double weeklySalary) { this.weeklySalary = weeklySalary; } public void setCommissionRate(double commissionRate) { this.commissionRate = commissionRate; } public void setWeeklySales(double weeklySales) { this.weeklySales = weeklySales; } public double earnings() { return weeklySalary + (commissionRate * weeklySales);

}

}

____________________________________________________________

import java.io.File;

import java.util.*;

public class Payroll2 {

public static void main(String[] args) {

Scanner scanner = null;

try {

Scanner input = new Scanner(new File(args[0]));

Employee[] employees;

int n = 0;

if (input.hasNext())

n = Integer.parseInt(input.nextLine()); employees = new Employee[n];

char empType;

int i = 0;

while (scanner.hasNext()) {

String line = scanner.nextLine();

empType = line.charAt(0);

System.out.println(line);

String lineArr[] = line.split(",");

// String firstName = lineArr[0].substring(1);// this will remove the first letter of last name, you should remove the substring part

String firstName = lineArr[0];

String lastName = lineArr[1];

switch (empType) {

case '#': {

double weeklySalary = Double.parseDouble(lineArr[2].trim());

Manager managers = new Manager(firstName, lastName,

weeklySalary);

employees[i] = managers;

}

break;

case '@': {

int wage = Integer.parseInt(lineArr[2].trim());

int hoursWorked = Integer.parseInt(lineArr[3].trim());

HourlyWorker hourlyWorker = new HourlyWorker(firstName,

lastName, hoursWorked, wage);

employees[i] = hourlyWorker;

}

break;

case '*': {

double weeklySalary = Double.parseDouble(lineArr[2].trim());

double weeklySales = Double.parseDouble(lineArr[3].trim());

double commissionRate = Double.parseDouble(lineArr[4]

.trim());

CommissionEmployee commissionEmployee = new CommissionEmployee(

firstName, lastName, weeklySalary, commissionRate,

weeklySales);

employees[i] = commissionEmployee;

}

break;

default:

break;

}

i++;

}

// for (int j = 0; j < employees.length; j++) {

// System.out.println(employees[j].getClass() + ": "

// + employees[j]);

// }

for (Employee j: employees) { // enhanced for loop

if(j instanceof Manager) // checking if he is a manager

{

System.out.println("Manager:");

System.out.println(j.getFirstName()+ " "+j.getLastName()+" $"+((Manager)j).getWeeklySalary() );

}

else if(j instanceof CommissionEmployee)// checking if he is a CommissionEmployee

{

System.out.println("CommissionEmployee:");

System.out.println(j.getFirstName()+ " "+j.getLastName()+" $(Base salary:"

+((CommissionEmployee)j).weeklySalary+",sales: $"+((CommissionEmployee)j).weeklySales+",Commission rate: "+((CommissionEmployee)j).commissionRate+"%)");

}

else

{

System.out.println("Hourly Worker:");

double wage = ((HourlyWorker)j).getWage();

int hours = ((HourlyWorker)j).getHoursWorked();

double earnings = ((HourlyWorker)j).earnings();

System.out.println(j.getFirstName()+ " "+j.getLastName()+" $"+earnings+"(Hourly wage: $"+wage+",hours worked: "+hours+")" );

}

}

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

}

_____________________________________________________

im getting exception errors, please help me!

java.lang.NumberFormatException: For input string: "#Steve, Davis, 2000"

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

at java.lang.Integer.parseInt(Integer.java:569)

at java.lang.Integer.parseInt(Integer.java:615)

at Payroll2.main(Payroll2.java:25)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions

Question

Discuss the states of accounting

Answered: 1 week ago

Question

3. You can gain power by making others feel important.

Answered: 1 week ago