Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 toString() {

String str = "The name of the employee is: " + getName()

+ " " + "The monthly salary is: " + getMonthlySalary()

+ " " + "The annual salary is: " + getAnnualSalary();

return str;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getMonthlySalary() {

return monthlySalary;

}

public void setMonthlySalary(int monthlySalary) {

this.monthlySalary = monthlySalary;

}

}

Salesman.java

package project1;

public class Salesman extends Employee{

private int annualSales;

public Salesman(String name, int monthlySalary, int annualSales) {

super(name, monthlySalary);

this.annualSales = annualSales;

}

public int getAnnualSalary() {

int num = 0;

num = (int) (.02 * annualSales);

if(num >= 20000) {

num = 20000;

}

int totalPay = (getMonthlySalary() * 12) + num;

return totalPay;

}

public String toString() {

String str = "The name of the employee is: " + getName()

+ " " + "The monthly salary is: " + getMonthlySalary()

+ " " + "The annual salary is: " + getAnnualSalary();

return str;

}

public int getAnnualSales() {

return annualSales;

}

public void setAnnualSales(int annualSales) {

this.annualSales = annualSales;

}

}

Executive.java

package project1;

public class Executive extends Employee{

private int stockPrice;

public Executive(String name, int monthlySalary, int stockPrice) {

super(name, monthlySalary);

this.stockPrice = stockPrice;

}

public int getAnnualSalary() {

int bonus = 0;

if(stockPrice > 50)

bonus = 30000;

int totalPay = (getMonthlySalary() * 12) + bonus;

return totalPay;

}

public String toString() {

String str = "The name of the employee is: " + getName()

+ " " + "The monthly salary is: " + getMonthlySalary()

+ " " + "The stock price is: " + getStockPrice();

return str;

}

public int getStockPrice() {

return stockPrice;

}

public void setStockPrice(int stockPrice) {

this.stockPrice = stockPrice;

}

}

Driver.java

package project1;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Driver {

private static void display(String message, Employee[] emp, int len) {

System.out.println(message);

for(int i = 0; i < len; i++)

System.out.println(emp[i] + " ");

System.out.println("------------------------ ");

}

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

Employee[] emp2014 = new Employee[10];

Employee[] emp2015 = new Employee[10];

File myFile = new File(args[0]);

Scanner inFile = new Scanner(myFile);

String oneLine;

int i = 0, j = 0;

while (inFile.hasNextLine()) {

oneLine = inFile.nextLine();

String[] inputArr = oneLine.split("\\s+");

Employee emp = null;

if(inputArr[1].equals("Employee")) {

emp = new Employee(inputArr[2], Integer.parseInt(inputArr[3]));

}

else if(inputArr[1].equals("Salesman")) {

emp = new Salesman(inputArr[2], Integer.parseInt(inputArr[3]), Integer.parseInt(inputArr[4]));

}

else if(inputArr[1].equals("Executive")) {

emp = new Salesman(inputArr[2], Integer.parseInt(inputArr[3]), Integer.parseInt(inputArr[4]));

}

else

System.out.println("Unknown Type of Employee: " + inputArr[2]);

if(emp != null) {

if(inputArr[0].equals("2014"))

emp2014[i++] = emp;

else if(inputArr[0].equals("2015"))

emp2015[j++] = emp;

}

}

inFile.close();

display("Employees of 2014", emp2014, i);

display("Employees of 2015", emp2015, j);

}

}

File.txt

2015 Employee Campbell,Steve 3000 2014 Salesman Sanchez,Carlos 4000 200000 2015 Executive Oduala,Barack 6000 60

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

Relational Database Technology

Authors: Suad Alagic

1st Edition

354096276X, 978-3540962762

More Books

Students also viewed these Databases questions

Question

What is the LIFO reserve, and when is it used?

Answered: 1 week ago