I need help compiling this code and any improvements/corrections to be added: Te
ID: 3683848 • Letter: I
Question
I need help compiling this code and any improvements/corrections to be added:
Test Class
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test{
public static void main(String[] args) throws IOException {
Employee[] employees2014 = new Employee[20];
Employee[] employees2015 = new Employee[20];
int i2014 = 0, i2015 = 0;
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("employeelist.txt"));
while ((sCurrentLine = br.readLine()) != null) {
String data[] = sCurrentLine.split(" ");
int year = Integer.parseInt(data[0]);
String type = data[1];
String name = data[2];
int monthlySalary = Integer.parseInt(data[3]);
Employee employee = null;
if(type.equalsIgnoreCase("Employee")){
employee = new Employee(name,monthlySalary);
}else if(type.equalsIgnoreCase("Salesman")){
int annualSales = Integer.parseInt(data[4]);
employee = new Salesman(name,monthlySalary,annualSales);
}else if(type.equalsIgnoreCase("Executive")){
int stockPrice = Integer.parseInt(data[4]);
employee = new Executive(name,monthlySalary,stockPrice);
}
if(year==2014)
employees2014[i2014++] = employee;
else
employees2015[i2015++] = employee;
}
} catch (IOException e) {
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
}
}
double total2014 = 0;
for(Employee e : employees2014){
System.out.println(e);
total2014 = total2014 + e.annualSalary();
System.out.println("Annual salary = "+e.annualSalary());
}
System.out.println("Average annual salary of all employees for the year 2014 = "+ (total2014/i2014));
double total2015 = 0;
for(Employee e : employees2015){
System.out.println(e);
total2015 = total2015 + e.annualSalary();
System.out.println("Annual salary = "+e.annualSalary());
}
System.out.println("Average annual salary of all employees for the year 2015 = "+ (total2015/i2015));
}
}
Salesman Class
class Salesman extends Employee{
int annualSales;
public Salesman(String name, int monthlySalary, int annualSales) {
super(name, monthlySalary);
this.annualSales = annualSales;
}
public int annualSalary() {
int commision = (int) (0.03 * annualSales > 25000 ? 25000 : 0.03 * annualSales);
return super.annualSalary() + commision;
}
@Override
public String toString() {
return "Salesman{" +
"name='" + name + ''' +
", monthlySalary=" + monthlySalary +
", annualSales=" + annualSales+
'}';
}
Executive Class:
}
class Executive extends Employee{
int stockPrice;
public Executive(String name, int monthlySalary, int stockPrice) {
super(name, monthlySalary);
this.stockPrice = stockPrice;
}
public int annualSalary() {
int bonus = stockPrice > 100 ? 20000 : 0;
return super.annualSalary() + bonus;
}
@Override
public String toString() {
return "Executive{" +
"name='" + name + ''' +
", monthlySalary=" + monthlySalary +
", stockPrice=" + stockPrice+
'}';
}
}
Employee Class:
class Employee{
String name;
int monthlySalary;
public Employee(String name, int monthlySalary) {
this.name = name;
this.monthlySalary = monthlySalary;
}
public int annualSalary(){
return 12*monthlySalary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + ''' +
", monthlySalary=" + monthlySalary +
'}';
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class HelloWorld{
public static void main(String[] args) throws IOException {
Employee[] employees2014 = new Employee[20];
Employee[] employees2015 = new Employee[20];
int i2014 = 0, i2015 = 0;
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("employeelist.txt"));
while ((sCurrentLine = br.readLine()) != null) {
String data[] = sCurrentLine.split(" ");
int year = Integer.parseInt(data[0]);
String type = data[1];
String name = data[2];
int monthlySalary = Integer.parseInt(data[3]);
Employee employee = null;
if(type.equalsIgnoreCase("Employee")){
employee = new Employee(name,monthlySalary);
}else if(type.equalsIgnoreCase("Salesman")){
int annualSales = Integer.parseInt(data[4]);
employee = new Salesman(name,monthlySalary,annualSales);
}else if(type.equalsIgnoreCase("Executive")){
int stockPrice = Integer.parseInt(data[4]);
employee = new Executive(name,monthlySalary,stockPrice);
}
if(year==2014)
employees2014[i2014++] = employee;
else
employees2015[i2015++] = employee;
}
} catch (IOException e) {
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
}
}
double total2014 = 0;
for(Employee e : employees2014){
System.out.println(e);
total2014 = total2014 + e.annualSalary();
System.out.println("Annual salary = "+e.annualSalary());
}
System.out.println("Average annual salary of all employees for the year 2014 = "+ (total2014/i2014));
double total2015 = 0;
for(Employee e : employees2015){
System.out.println(e);
total2015 = total2015 + e.annualSalary();
System.out.println("Annual salary = "+e.annualSalary());
}
System.out.println("Average annual salary of all employees for the year 2015 = "+ (total2015/i2015));
}
}
class Employee{
String name;
int monthlySalary;
public Employee(String name, int monthlySalary) {
this.name = name;
this.monthlySalary = monthlySalary;
}
public int annualSalary(){
return 12*monthlySalary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + ''' +
", monthlySalary=" + monthlySalary +
'}';
}
}
class Executive extends Employee{
int stockPrice;
public Executive(String name, int monthlySalary, int stockPrice) {
super(name, monthlySalary);
this.stockPrice = stockPrice;
}
public int annualSalary() {
int bonus = stockPrice > 100 ? 20000 : 0;
return super.annualSalary() + bonus;
}
public String toString() {
return "Executive{" +
"name='" + name + ''' +
", monthlySalary=" + monthlySalary +
", stockPrice=" + stockPrice+
'}';
}
}
class Salesman extends Employee{
int annualSales;
public Salesman(String name, int monthlySalary, int annualSales) {
super(name, monthlySalary);
this.annualSales = annualSales;
}
public int annualSalary() {
int commision = (int) (0.03 * annualSales > 25000 ? 25000 : 0.03 * annualSales);
return super.annualSalary() + commision;
}
@Override
public String toString() {
return "Salesman{" +
"name='" + name + ''' +
", monthlySalary=" + monthlySalary +
", annualSales=" + annualSales+
'}';
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.