Question

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

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

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

+ "\n" + "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()

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

+ "\n" + "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()

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

+ "\n" + "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] + "\n");

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

}

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

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

I have changed the code which is printing data from all the files

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Driver {
private static void display(String message, Object[] emp, int len) {
System.out.println(message);
for(int i = 0; i < len; i++)
{
if(emp[i] instanceof Employee)
{
System.out.println(emp[i] + "\n");
System.out.println("------------------------\n");
}
else if(emp[i] instanceof Salesman)
{
System.out.println(emp[i] + "\n");
System.out.println("------------------------\n");
}
else if(emp[i] instanceof Executive)
{
System.out.println(emp[i] + "\n");
System.out.println("------------------------\n");
}
}
}
public static void main(String[] args) throws FileNotFoundException {
Object[] emp2014 = new Object[10];
Object[] emp2015 = new Object[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+");
Object 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 Executive(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);
}
}

Output

koushikhp@koushikhpnew: koushikhp@koushikhpnew: java Driver File.txt Employees of 2014 The name of the employee is: Sanchez,C

Add a comment
Know the answer?
Add Answer to:
JAVA: How do I output all the data included for each employee? I can only get...
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
  • DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to...

    DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value? Make a package com.acme.midmanager. This package should contain the Manager class. Make a package com.acme.personnel. This package should contain the DataSetEmployee class. Make a package com.acme.topmanagement. This package should contain the...

  • 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...

  • departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE =...

    departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee;    } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int...

    Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employee...

    C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employees to be compared by the Name, Age, and Pay, respectively. Code: Employee.Core.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Employees { partial class Employee { // Field data. private string empName; private int empID; private float currPay; private int empAge; private string empSSN = ""; private string empBene...

  • In the Employee class, add an implementation for the calculatePension() method declared in the interface. Calculate...

    In the Employee class, add an implementation for the calculatePension() method declared in the interface. Calculate the pension to date as the equivalent of a monthly salary for every year in service. 4) Print the calculated pension for all employees. ************** I added the interface and I implemented the interface with Employee class but, I don't know how can i call the method in main class ************ import java.io.*; public class ManagerTest { public static void main(String[] args) { InputStreamReader...

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
ADVERTISEMENT