Question

I need help creating a class for my program. The class must... 1. Contains the main...

I need help creating a class for my program.

The class must...

1. Contains the main method.

2.Reads in employee information from a text file.

3.As the employees are read in, Employee objects of the appropriate type are created and stored in one of two arrays depending upon the year

4.Once all the employee data is read in, a report displays on the console for each of the two years

5.Each line of the report contains all original data supplied for each employee together with that employee's annual salary for the year.

6.For each of the two years, an average of all salaries for all employees for that year is computed and displayed.

Other classes from program...

public class Employee {
public String name;
public double monthlySalary;

public Employee(String name, double monthlySalary) {
this.name = name;
this.monthlySalary = monthlySalary;
}

public double annualSalary(double monthlySalary) {
return monthlySalary * 12;
}

public String toString() {
return "Name: " + name + "\nMonthly Salary: $" + monthlySalary;
}

}

public class Salesman extends Employee {
public double annualSales;
  
public Salesman(String name, double monthlySalary, double annualSales) {
super(name, monthlySalary);
this.annualSales = annualSales;
}
  
public double annualSalary() {
double annualSalary;
double commission;
  
annualSalary = monthlySalary * 12;
commission = annualSales * 0.02;
  
if (commission < 0) {
commission = 0;
} else if (commission >= 0 && commission <= 20000) {
commission = commission;
} else {
commission = 20000;
}
  
annualSalary += commission;
return annualSalary;
}
  
public String toString() {
return "Name: " + name + "\nMonthly Salary: $" + monthlySalary + "\nAnnual Sales: $" + annualSales;
}
}

public class Executive extends Employee {
private double stockPrice;
  
public Executive(String name, double monthlySalary, double stockPrice) {
super(name, monthlySalary);
this.stockPrice = stockPrice;
}
  
public double annualSalary() {
double annualSalary;
double bonus;
  
annualSalary = monthlySalary * 12;
  
if (stockPrice > 50) {
bonus = 30000;
} else {
bonus = 0;
}
  
annualSalary += bonus;
return annualSalary;
}
  
public String toString() {
return "Name: " + name + "\nMonthly Salary: $" + monthlySalary + "\nStock Price: $" + stockPrice;
}
}

Text file which contains required information:

2014 Employee Smith,John 2000

2015 Salesman Jones,Bill 3000 100000

2014 Executive Bush,George 5000 55

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

Solution:

Solution.java

import java.io.*;
import java.util.ArrayList;

class Employee{
    public String name;
    public double monthlySalary;

    public Employee(String name, double monthlySalary){
        this.name = name;
        this.monthlySalary = monthlySalary;
    }

    public double annualSalary(){
        return monthlySalary*12;
    }

    public String toString(){
        return "Name: "+name+"\nMonthly Salary: $"+monthlySalary;
    }
}

class Salesman extends Employee{
    public double annualSales;

    public Salesman(String name, double monthlySalary, double annualSales){
        super(name,monthlySalary);
        this.annualSales = annualSales;
    }

    public double annualSalary(){
        double annualSalary;
        double commission;

        annualSalary = monthlySalary * 12;
        commission = annualSales * 0.02;

        if(commission < 0){
            commission = 0;
        }
        else if(commission >= 0 && commission <= 20000 ){
            commission = commission;
        }
        else{
            commission = 20000;
        }

        annualSalary += commission;
        return annualSalary;
    }

    public String toString(){
        return "Name : "+name+"\nMonthly Salary:$"+monthlySalary+"\nAnnual Sales:$"+annualSales;
    }
}

class Executive extends Employee {
    private double stockPrice;

    public Executive(String name, double monthlySalary, double stockPrice) {
        super(name, monthlySalary);
        this.stockPrice = stockPrice;
    }

    public double annualSalary() {
        double annualSalary;
        double bonus;

        annualSalary = monthlySalary * 12;

        if (stockPrice > 50) {
            bonus = 30000;
        } else {
            bonus = 0;
        }

        annualSalary += bonus;
        return annualSalary;
    }

    public String toString() {
        return "Name: " + name + "\nMonthly Salary: $" + monthlySalary + "\nStock Price: $" + stockPrice;
    }
}



public class Solution {

    public static void main(String[] args) {
        //locating the file path
        File file = new File("C:\\Users\\input.txt");
        BufferedReader bufferedReader = null;
        
        //Creating two arraylist objects
        ArrayList<Employee> list2014 = new ArrayList<>();
        ArrayList<Employee> list2015 = new ArrayList<>();

        try {
            bufferedReader = new BufferedReader(new FileReader(file));
            String line;
            //Reading the file line by line
            while ((line = bufferedReader.readLine()) != null){
                String []data = line.split(" ");

                int year = Integer.parseInt(data[0]);
                String type = data[1];
                
                //Store the object for year - 2014
                if(year == 2014){
                    //create Employee object
                    if(type.equals("Employee")){
                        Employee employee = new Employee(data[2], Double.parseDouble(data[3]));
                        list2014.add(employee);
                    }
                    //create Salesman object
                    else if(type.equals("Salesman")){
                        Employee employee = new Salesman(data[2], Double.parseDouble(data[3]), Double.parseDouble(data[4]));
                        list2014.add(employee);
                    }
                    //create Executive object
                    else {
                        Employee employee = new Executive(data[2], Double.parseDouble(data[3]), Double.parseDouble(data[4]));
                        list2014.add(employee);
                    }

                }
                //Store the object for year - 2015
                else {
                    //create Employee object
                    if(type.equals("Employee")){
                        Employee employee = new Employee(data[2], Double.parseDouble(data[3]));
                        list2015.add(employee);
                    }
                    //create Salesman object
                    else if(type.equals("Salesman")){
                        Employee employee = new Salesman(data[2], Double.parseDouble(data[3]), Double.parseDouble(data[4]));
                        list2015.add(employee);
                    }
                    //create Executive object
                    else {
                        Employee employee = new Executive(data[2], Double.parseDouble(data[3]), Double.parseDouble(data[4]));
                        list2015.add(employee);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Print the data
        System.out.println("******* 2014 Employee Data ******");
        for( Employee emp : list2014 ){
            double avgsalary = emp.annualSalary()/12;
            System.out.println("\n"+emp+"\nAnnual Salary :$"+emp.annualSalary()+"\nAverage Salary :$"+avgsalary);
        }
        System.out.println("\n\n******* 2015 Employee Data ******");
        for( Employee emp : list2015 ){
            double avgsalary = emp.annualSalary()/12;
            System.out.println("\n"+emp+"\nAnnual Salary :$"+emp.annualSalary()+"\nAverage Salary :$"+avgsalary);
        }
    }
}

input.txt

2014 Employee Smith,John 2000
2015 Salesman Jones,Bill 3000 100000
2014 Executive Bush,George 5000 55

OUTPUT:

******* 2014 Employee Data ******

Name: Smith,John
Monthly Salary: $2000.0
Annual Salary :$24000.0
Average Salary :$2000.0

Name: Bush,George
Monthly Salary: $5000.0
Stock Price: $55.0
Annual Salary :$90000.0
Average Salary :$7500.0


******* 2015 Employee Data******

Name : Jones,Bill
Monthly Salary:$3000.0
Annual Sales:$100000.0
Annual Salary :$38000.0
Average Salary :$3166.6666666666665

Add a comment
Know the answer?
Add Answer to:
I need help creating a class for my program. The class must... 1. Contains the main...
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
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