Im having trouble with the las part of my java code. this is the instructions fo
ID: 3687236 • Letter: I
Question
Im having trouble with the las part of my java code. this is the instructions for my program
The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of four classes.
The first class is the Employee class, which contains the employee's name and monthly salary, which is specified in whole dollars. It should have three methods:
A constructor that allows the name and monthly salary to be initialized.
A method named annualSalary that returns the salary for a whole year.
A toString method that returns a string containing the name and monthly salary, appropriately labeled.
The Employee class has two subclasses: Salesman and Executive.
The Salesman class has an additional instance variable that contains the annual sales in whole dollars for that salesman. It should have the same three methods:
A constructor that allows the name, monthly salary and annual sales to be initialized.
An overridden method annualSalary that returns the salary for a whole year. The salary for a salesman consists of the base salary computed from the monthly salary plus a commission. The commission is computed as 3% of that salesman's annual sales. The maximum commission a salesman can earn is $25,000.
An overridden toString method that returns a string containing the name, monthly salary and annual sales, appropriately labeled.
The Executive class has an additional instance variable that reflects the current stock price. It should have the same three methods:
A constructor that allows the name, monthly salary and stock price to be initialized.
An overridden method annualSalary that returns the salary for a whole year. The salary for an executive consists of the base salary computed from the monthly salary plus a bonus. The bonus is $20,000 if the current stock price is greater than $100 and nothing otherwise.
An overridden toString method that returns a string containing the name, monthly salary and stock price, appropriately labeled.
Finally there should be a fourth class that contains the main method. It should read in employee information from a text file input.txt. Each line of the text file will represent the information for one employee for one year. An example of how the text file will look is shown below:
2014 Employee Smithson,John 2000
2015 Salesman Jokey,Will 3000 100000
2014 Executive Bush,George 5000 150
The year is the first data element on the line. The file will contain employee information for only two years: 2014 and 2015. Next is the type of the employee followed by the employee name and the monthly salary. For salesmen, the final value is their annual sales and for executives the stock price. As the employees are read in, Employee objects of the appropriate type should be created and stored in an array depending upon the year (there should be two arrays, one corresponding to 2014 and one corresponding to year 2015). You may assume that the file will contain no more than 20 employee records for each year and that the data in the file will be formatted correctly.
Once all the employee data is read in, a report should be displayed on the console for each of the two years. Each line of the report should contain all original data supplied for each employee together with that employee's annual salary for the year. For each of the two years, an average of all salaries for all employees for that year should be computed and displayed.
Your program should compile without errors.
Be sure to follow good programming style, which means making all instance variables private, naming all constants, avoiding the duplication of code and using Java code conventions. Furthermore you must select enough different kinds of employees to completely test the program.
This is what i got so far
Employee.java
public class Employee {
protected int monthlySalary;
protected String name;
/**
* Declare name and monthlySalary data
* fields to be used in methods and constructors
*/
public Employee(String name){
this.name = name;
}
public Employee(int monthlySalary){
this.monthlySalary = monthlySalary;
}
private int getAnnualSalary() {
int annualSalary = monthlySalary * 12;
return annualSalary;
}
/* toString() method to return name, monthlySalary, and annualSalary
*
*/
public String toString(){
return " Name: " + name + " Monthly Salary: " + monthlySalary + " Annual Salary:" + getAnnualSalary();
}
}
Salesman.java
class Salesman extends Employee
{
private double commission;
private int annualSales;
double annualSalary;
public Salesman(String name,int annualSales,double commision)
{
super(name);
this.annualSales = annualSales;
this.commission = commission *(annualSales*.03);
if (this.commission > 25000){
this.commission = 25000;
}
}
private double getAnnualSalary() {
annualSalary = annualSalary + commission;
return annualSalary;
}
public String toString() {
return " Name: " + name + " Monthly Salary: " + monthlySalary + " Annual Salary (including commission):" + getAnnualSalary() + " Annual Sales: " + annualSales;
}
}
Executive.java
public class Executive extends Employee{
private int bonus;
private int stockPrice;
double annualSalary;
public Executive(String name, int stockPrice,int bonus)
{
super(name);
this.stockPrice = stockPrice;
this.bonus = bonus;
if (this.stockPrice >= 50){
this.bonus = 30000;
}
if(this.stockPrice < 50){
this.bonus = 0;
}
}
private double getAnnualSalary() {
annualSalary = annualSalary + bonus;
return annualSalary;
}
public String toString() {
return " Name: " + name + " Monthly Salary: " + monthlySalary + " Annual Salary (including bonus):" + getAnnualSalary() + " Stock Price: " + stockPrice;
}
}
This is where i got to in the last java file.
EmployeeMain.java
import java.io.File;
import java.util.Scanner;
public class EmployeeMain {
private Scanner x;
public void openFile(){
try{
x = new Scanner(new File("EmployeeList"));
}
catch(Exception e){
}
while (x.hasNext()){
if (x.next()== 2014){
}
}
}
}
I only need help with the last code. If the other ones need to be change please explain why. Any and all explanations would be most helpful. Please help
Explanation / Answer
MainMethod.java
import java.io.*;
import java.util.*;
public class MainMethod {
public static void main(String[] args) throws FileNotFoundException {
double employeeMonthlySalary2014 = 0;
double employeeAnnualSalary2014 = 0;
double employeeSalarySum2014 = 0;
double employeeMonthlySalary2015 = 0;
double employeeAnnualSalary2015 = 0;
double employeeSalarySum2015 = 0;
double salesmanSalarySum2014 = 0;
double salesmanAnnualSales2014 = 0;
double salesmanMonthlySalary2014 = 0;
double salesmanAnnualSalary2014 = 0;
double salesmanSalarySum2015 = 0;
double salesmanAnnualSales2015 = 0;
double salesmanMonthlySalary2015 = 0;
double salesmanAnnualSalary2015 = 0;
double executiveStockPrice2014 = 0;
double executiveMonthlySalary2014 = 0;
double executiveAnnualSalary2014 = 0;
double executiveAnnualSalarySum2014 = 0;
double executiveStockPrice2015 = 0;
double executiveMonthlySalary2015 = 0;
double executiveAnnualSalary2015 = 0;
double executiveAnnualSalarySum2015 = 0;
int numberOfEmployees2014 = 0;
int numberOfEmployees2015 = 0;
int numberOfSalesman2014 = 0;
int numberOfSalesman2015 = 0;
int numberOfExecutives2014 = 0;
int numberOfExecutives2015 = 0;
double avgSalaries2014 = 0;
double avgSalaries2015 = 0;
String file = ("TextFile.txt");
Scanner s = new Scanner(new FileReader(file));
String[][] inputArray = new String[12][6];
Employee[] employee2014 = new Employee[10];
Employee[] employee2015 = new Employee[10];
int i = 0;
while(s.hasNextLine()){
String[] splitEntries = s.nextLine().split(" ");
Employee employee = null;
if (splitEntries[1].equalsIgnoreCase("Employee"))
employee = new Employee(splitEntries[2], Double.parseDouble(splitEntries[3]));
else if(splitEntries[1].equalsIgnoreCase("Salesman"))
employee = new Salesman(splitEntries[2], Double.parseDouble(splitEntries[3]), Double.parseDouble(splitEntries[4]));
for(int j = 0; j < splitEntries.length; j++){
inputArray[i][j] = splitEntries[j];
}
i++;
}
for(int j = 0; j < inputArray.length; j++){
if(inputArray[j][0].equals("2014") && inputArray[j][1].equals("Employee")){
double c = 0;
Employee monthlySalary2014 = new Employee();
Employee annualSalary2014 = new Employee();
employeeMonthlySalary2014 = monthlySalary2014.getMonthlySalary(Integer.parseInt(inputArray[j][3]));
employeeAnnualSalary2014 = annualSalary2014.getAnnualSalary(employeeMonthlySalary2014, c);
inputArray[j][5] = Double.toString(employeeAnnualSalary2014);
inputArray[j][4] = "";
numberOfEmployees2014++;
employeeSalarySum2014 += employeeAnnualSalary2014;
}
}
for(int j = 0; j < inputArray.length; j++){
if(inputArray[j][0].equals("2015") && inputArray[j][1].equals("Employee")){
double c = 0;
Employee monthlySalary2015 = new Employee();
Employee annualSalary2015 = new Employee();
employeeMonthlySalary2015 = monthlySalary2015.getMonthlySalary(Integer.parseInt(inputArray[j][3]));
employeeAnnualSalary2015 = annualSalary2015.getAnnualSalary(employeeMonthlySalary2015, c);
inputArray[j][5] = Double.toString(employeeAnnualSalary2015);
inputArray[j][4] = "";
numberOfEmployees2015++;
employeeSalarySum2015 += employeeAnnualSalary2015;
}
}
for(int j = 0; j < inputArray.length; j++){
if(inputArray[j][0].equals("2014") && inputArray[j][1].equals("Salesman")){
Salesman annSales = new Salesman();
Salesman monthlySalary = new Salesman();
Salesman annSalary = new Salesman();
salesmanAnnualSales2014 = annSales.getAnnualSales(Integer.parseInt(inputArray[j][4]));
salesmanMonthlySalary2014 = monthlySalary.getMonthlySalary(Integer.parseInt(inputArray[j][3]));
salesmanAnnualSalary2014 = annSalary.getAnnualSalary(salesmanMonthlySalary2014, salesmanAnnualSales2014);
inputArray[j][5] = Double.toString(salesmanAnnualSalary2014);
numberOfSalesman2014++;
salesmanSalarySum2014 += salesmanAnnualSalary2014;
}
}
for(int j = 0; j < inputArray.length; j++){
if(inputArray[j][0].equals("2015") && inputArray[j][1].equals("Salesman")){
Salesman annSales = new Salesman();
Salesman monthlySalary = new Salesman();
Salesman annSalary = new Salesman();
salesmanAnnualSales2015 = annSales.getAnnualSales(Integer.parseInt(inputArray[j][4]));
salesmanMonthlySalary2015 = monthlySalary.getMonthlySalary(Integer.parseInt(inputArray[j][3]));
salesmanAnnualSalary2015 = annSalary.getAnnualSalary(salesmanMonthlySalary2015, salesmanAnnualSales2015);
inputArray[j][5] = Double.toString(salesmanAnnualSalary2015);
numberOfSalesman2015++;
salesmanSalarySum2015 += salesmanAnnualSalary2015;
}
}
for(int j = 0; j < inputArray.length; j++){
if(inputArray[j][0].equals("2014") && inputArray[j][1].equals("Executive")){
Executive stockPrice = new Executive();
Executive monthlySalary = new Executive();
Executive annSalary = new Executive();
executiveStockPrice2014 = stockPrice.getStockPrice(Integer.parseInt(inputArray[j][4]));
executiveMonthlySalary2014 = monthlySalary.getMonthlySalary(Integer.parseInt(inputArray[j][3]));
executiveAnnualSalary2014 = annSalary.getAnnualSalary(executiveMonthlySalary2014, executiveStockPrice2014);
inputArray[j][5] = Double.toString(executiveAnnualSalary2014);
numberOfExecutives2014++;
executiveAnnualSalarySum2014 += executiveAnnualSalary2014;
}
}
for(int j = 0; j < inputArray.length; j++){
if(inputArray[j][0].equals("2015") && inputArray[j][1].equals("Executive")){
Executive stockPrice = new Executive();
Executive monthlySalary = new Executive();
Executive annSalary = new Executive();
executiveStockPrice2015 = stockPrice.getStockPrice(Integer.parseInt(inputArray[j][4]));
executiveMonthlySalary2015 = monthlySalary.getMonthlySalary(Integer.parseInt(inputArray[j][3]));
executiveAnnualSalary2015 = annSalary.getAnnualSalary(executiveMonthlySalary2015, executiveStockPrice2015);
inputArray[j][5] = Double.toString(executiveAnnualSalary2015);
numberOfExecutives2015++;
executiveAnnualSalarySum2015 += executiveAnnualSalary2015;
}
}
System.out.printf("%-20s%-20s%-20s%-20s%-20s%-20s", "Year", "Employee", "Name", "Monthly Salary", "Salesman Sales or", "Annual Salary");
System.out.println();
System.out.printf("%-20s%-20s%-20s%-20s%-20s%-20s", "", "Type", "", "", "Executive Stock", "");
System.out.println();
System.out.println();
for(int y = 0; y < inputArray.length; y++){
for(int x = 0; x < inputArray[y].length; x++){
System.out.printf("%-20s", inputArray[y][x]);
}
System.out.println();
}
avgSalaries2014 = (employeeSalarySum2014 + salesmanSalarySum2014 + executiveAnnualSalarySum2014) / (numberOfEmployees2014 +
numberOfSalesman2014 + numberOfExecutives2014);
avgSalaries2015 = (employeeSalarySum2015 + salesmanSalarySum2015 + executiveAnnualSalarySum2015) / (numberOfEmployees2015 +
numberOfSalesman2015 + numberOfExecutives2015);
System.out.println();
System.out.println("The average of the 2014 salaries is: " + (int)avgSalaries2014);
System.out.println();
System.out.println("The average of the 2015 salaries is: " + (int)avgSalaries2015);
}
}
Employee.java
public class Employee {
private String name = "";
double monthlySalary;
Employee(){
}
Employee(String name, double montlhlySalary){
this.name = name;
this.monthlySalary = monthlySalary;
}
public String getName(){
return name;
}
public double getMonthlySalary(double monthlySalary){
this.monthlySalary = monthlySalary;
return monthlySalary;
}
public double getAnnualSalary(double monthlySalary, double commision){
this.monthlySalary = monthlySalary;
return monthlySalary * 12;
}
public String toString(){
String output = " Employee Name: " + getName() +
" Monthly Salary: " + getMonthlySalary(monthlySalary);
return output;
}
}
Salesman.java
public class Salesman extends Employee {
private double annualSales;
Salesman(){
super("Jones,Bill", 2000);
}
Salesman(String name, double monthlySalary, double annualSales){
super(name, monthlySalary);
this.annualSales = annualSales;
}
@Override
public double getMonthlySalary(double monthlySalary){
this.monthlySalary = monthlySalary;
return monthlySalary;
}
public double getAnnualSales(double annualSales){
return this.annualSales = annualSales;
}
@Override
public double getAnnualSalary(double monthlySalary, double annualSales){
this.annualSales = annualSales;
this.monthlySalary = monthlySalary;
double commission;
if(getAnnualSales(annualSales) * 0.03 >= 25000)
commission = 25000;
else
commission = getAnnualSales(annualSales) * 0.03;
return getMonthlySalary(monthlySalary) * 12 + commission;
}
@Override
public String toString(){
String output = super.toString() +
" Annual Sales: " + getAnnualSales(annualSales);
return output;
}
}
Executive.java
public class Executive extends Employee {
private double stockPrice;
Executive(){
super("Bush, George", 5000);
}
Executive(String name, double monthlySalary, double stockPrice){
super(name, monthlySalary);
this.stockPrice = stockPrice;
}
public double getStockPrice(double stockPrice){
this.stockPrice = stockPrice;
return stockPrice;
}
@Override
public double getMonthlySalary(double monthlySalary){
this.monthlySalary = monthlySalary;
return monthlySalary;
}
@Override
public double getAnnualSalary(double monthlySalary, double stockPrice){
this.stockPrice = stockPrice;
this.monthlySalary = monthlySalary;
double annualSalary;
if(getStockPrice(stockPrice) > 100)
annualSalary = getMonthlySalary(monthlySalary) * 12 + 20000;
else
annualSalary = getMonthlySalary(monthlySalary) * 12;
return annualSalary;
}
@Override
public String toString(){
String output = super.toString() +
" Stock Price: " + getStockPrice(stockPrice);
return output;
}
}
TextFile.txt
2014 Employee Smith,John 2000
2015 Salesman Jones,Bill 3000 100000
2014 Executive Bush,George 5000 55