Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA: I have a program that reads in from a file called File.txt, but need assis

ID: 3875512 • Letter: J

Question

JAVA: I have a program that reads in from a file called File.txt, but need assistance still. The issue is that as the employees are read in, Employee objects of the appropriate type should be created and they should be stored in one of two arrays depending upon the year. How can I achieve that?

Driver.java

package project1;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Arrays;

import java.util.Scanner;

public class Driver {

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

String[][] array2014 = new String[10][6];

String[][] array2015 = new String[10][6];

String[] array1 = new String[6];

Driver obj = new Driver();

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

Scanner inFile = new Scanner(myFile);

String oneLine;

int i = 0, k = 0;

while (inFile.hasNextLine()) {

oneLine = inFile.nextLine();

array1 = oneLine.split("[, ]");

if(array1[0] == "2014") {

for(int j = 0; j < array1.length; j++)

array2014[i][j] = array1[j];

i++;

}

else if(array1[0] == "2015") {

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

array2015[k][j] = array1[j];

k++;

}

}

}

//return Arrays.deepToString(array1);

System.out.println(Arrays.deepToString(array2014));

System.out.println(Arrays.deepToString(array2015));

}

}

package project1;

Employee.java

public class Employee {

private String name;

private int monthlySalary;

public Employee(String someName, int pay) {

name = someName;

monthlySalary = pay;

}

public int getAnnualSalary(int pay) {

int totalPay = 0;

totalPay = 12 * pay;

return totalPay;

}

public String toString(String name, int monthlySalary) {

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

+ " " + "The monthly salary is: " + monthlySalary;

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 someName, int pay, int someAnnualSales) {

super(someName, pay);

annualSales = someAnnualSales;

}

public int getAnnualSalary(int annualSalary, int pay) {

int number = 0;

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

if(number >= 20000) {

number = 20000;

}

int totalPay = pay + number;

return totalPay;

}

public String toString(String name, int monthlySalary, int annualSalary) {

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

+ " " + "The monthly salary is: " + monthlySalary

+ " " + "The annual salary is: " + annualSalary;

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 someName, int pay, int someStockPrice) {

super(someName, pay);

stockPrice = someStockPrice;

}

public int getStockBonus(int stockPrice, int pay) {

int bonus = 0;

if(stockPrice > 50)

bonus = 30000;

int totalPay = pay + bonus;

return totalPay;

}

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

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

+ " " + "The monthly salary is: " + monthlySalary

+ " " + "The stock price is: " + stockPrice;

return str;

}

public int getStockPrice() {

return stockPrice;

}

public void setStockPrice(int stockPrice) {

this.stockPrice = stockPrice;

}

}

File.txt

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

Explanation / Answer


Fixed the driver code. Also fixed all other classes since the toString() had to be fixed. It should use instance variables/methods and not receive any parameters. Given below are the modified files.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i

Employee.java
=============
package project1;
public class Employee {
private String name;
private int monthlySalary;
public Employee(String someName, int pay) {
name = someName;
monthlySalary = pay;
}
public int getAnnualSalary() {
int totalPay = 0;
totalPay = 12 * monthlySalary;
return totalPay;
}
public String toString() {
String str = "The name of the employee is: " + name
+ " " + "The monthly salary is: " + monthlySalary
+ " " + "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 someName, int pay, int someAnnualSales) {
super(someName, pay);
annualSales = someAnnualSales;
}
public int getAnnualSalary() {
int commission = 0;
commission = (int) (.02 * annualSales);
if(commission > 20000) {
commission = 20000;
}
int totalPay = 12 * getMonthlySalary() + commission;
return totalPay;
}
public String toString() {
String str = "The name of the employee is: " + getName()
+ " " + "The monthly salary is: " + getMonthlySalary()
+ " " + "The annual sales is: " + annualSales
+ " " + "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 someName, int pay, int someStockPrice) {
super(someName, pay);
stockPrice = someStockPrice;
}
public int getAnnualSalary() {
int bonus = 0;
if(stockPrice > 50)
bonus = 30000;
int totalPay = 12 * getMonthlySalary() + bonus;
return totalPay;
}
public String toString() {
String str = "The name of the employee is: " + getName()
+ " " + "The monthly salary is: " + getMonthlySalary()
+ " " + "The stock price is: " + stockPrice
+ " " + "The annual salary is: " + getAnnualSalary();
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[] emps, int len)
{
System.out.println(message);
for(int i = 0; i < len; i++)
System.out.println(emps[i] + " ");
System.out.println("----------------------- ");
}
public static void main(String[] args) throws FileNotFoundException {
Employee[] emps2014 = new Employee[10];
Employee[] emps2015 = new Employee[10];
File myFile = new File(args[0]);
Scanner inFile = new Scanner(myFile);
String oneLine;
int len2014 = 0, len2015 = 0;
while (inFile.hasNextLine()) {
> String[] tokens = oneLine.split("\s+"); //split based on one or more spaces
Employee emp = null;
if(tokens[1].equals("Employee"))
{
emp = new Employee(tokens[2], Integer.parseInt(tokens[3]));
}
else if(tokens[1].equals("Salesman"))
{
emp = new Salesman(tokens[2], Integer.parseInt(tokens[3]), Integer.parseInt(tokens[4]));
}
else if(tokens[1].equals("Executive"))
{
emp = new Executive(tokens[2], Integer.parseInt(tokens[3]), Integer.parseInt(tokens[4]));
}
else
System.out.println("Unknown employee type");
if(emp != null)
{
if (tokens[0].equals("2014"))
emps2014[len2014++] = emp;
else if (tokens[0].equals("2015"))
emps2015[len2015++] = emp;
}
}
inFile.close();
display("Employees of 2014", emps2014, len2014);
display("Employees of 2015", emps2015, len2015);
}
}


output
======
Employees of 2014
The name of the employee is: Sanchez,Carlos
The monthly salary is: 4000
The annual sales is: 200000
The annual salary is: 52000
-----------------------
Employees of 2015
The name of the employee is: Campbell,Steve
The monthly salary is: 3000
The annual salary is: 36000
The name of the employee is: Oduala,Barack
The monthly salary is: 6000
The stock price is: 60
The annual salary is: 102000
-----------------------