Im having trouble with the last part of my java code. I only want help with the
ID: 3687268 • Letter: I
Question
Im having trouble with the last part of my java code. I only want help with the last code. If the other ones need to be change please explain why. Any and all explanations would be most helpful. 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
Hello there ,
I have mainly modified constuctors of all classes as ,we do not need to pass the entity (bonus/commission) that we are going to calculate. Instead we should pass the entity(monthly salary) which is a base and on which bases we cacluate bonus and commissions.
Kindly find attached code.
====
/**
*
* @author welcome
*/
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, int monthlySalary) {
this.name = name;
this.monthlySalary = monthlySalary;
}
public Employee() {
}
public double 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();
}
}
====
/**
*
* @author welcome
*/
public class Executive extends Employee {
private int bonus;
private int stockPrice;
double annualSalary;
public Executive(String name, int monthlySalary, int stockPrice) {
super(name, monthlySalary);
annualSalary = 12 * monthlySalary;
this.stockPrice = stockPrice;
this.bonus = bonus;
if (this.stockPrice >= 50) {
this.bonus = 30000;
}
if (this.stockPrice < 50) {
this.bonus = 0;
}
}
@Override
public double getAnnualSalary() {
annualSalary = annualSalary + bonus;
return annualSalary;
}
public String toString() {
return " Name: " + this.name + " Monthly Salary: " + monthlySalary + " Annual Salary (including bonus):" + getAnnualSalary() + " Stock Price: " + stockPrice;
}
}
====
/**
*
* @author welcome
*/
class Salesman extends Employee {
private double commission;
private int annualSales;
double annualSalary;
public Salesman(String name, int monthlySalary, int annualSales) {
super(name, monthlySalary);
annualSalary = 12 * monthlySalary;
this.annualSales = annualSales;
this.commission = commission * (annualSales * .03);
if (this.commission > 25000) {
this.commission = 25000;
}
}
@Override
public double getAnnualSalary() {
annualSalary = annualSalary + commission;
return annualSalary;
}
public String toString() {
return " Name: " + name + " Monthly Salary: " + monthlySalary + " Annual Salary (including commission):" + getAnnualSalary() + " Annual Sales: " + annualSales;
}
}
=====
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author welcome
*/
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class EmployeeMain {
private static Scanner x;
public static void main(String[] args) {
openFile();
}
public static void openFile() {
List<Employee> listOfEmployee2014 = new ArrayList<Employee>();
List<Employee> listOfEmployee2015 = new ArrayList<Employee>();
try {
x = new Scanner(new File("EmployeeList"));
} catch (Exception e) {
e.printStackTrace();
}
while (x.hasNextLine()) {
String line = x.nextLine();
String[] strArray = line.split(" ");
if (strArray[0].equals("2014")) {
if (strArray[1].equals("Employee")) {
listOfEmployee2014.add(new Employee(strArray[2], Integer.parseInt(strArray[3])));
} else if (strArray[1].equals("Salesman")) {
listOfEmployee2014.add(new Salesman(strArray[2], Integer.parseInt(strArray[3]), Integer.parseInt(strArray[4])));
} else if (strArray[1].equals("Executive")) {
listOfEmployee2014.add(new Executive(strArray[2], Integer.parseInt(strArray[3]), Integer.parseInt(strArray[4])));
}
} else if (strArray[0].equals("2015")) {
if (strArray[1].equals("Employee")) {
listOfEmployee2015.add(new Employee(strArray[2], Integer.parseInt(strArray[3])));
} else if (strArray[1].equals("Salesman")) {
listOfEmployee2015.add(new Salesman(strArray[2], Integer.parseInt(strArray[3]), Integer.parseInt(strArray[4])));
} else if (strArray[1].equals("Executive")) {
listOfEmployee2015.add(new Executive(strArray[2], Integer.parseInt(strArray[3]), Integer.parseInt(strArray[4])));
}
}
}
System.out.println("Year 2014");
System.out.println("============================");
double sumMonthly = 0, sumAnnually = 0;
for (int index = 0; index < listOfEmployee2014.size(); index++) {
System.out.println(listOfEmployee2014.get(index));
sumMonthly = sumMonthly + listOfEmployee2014.get(index).monthlySalary;
sumAnnually = sumAnnually + listOfEmployee2014.get(index).getAnnualSalary();
}
System.out.println("Avg. Of Monthly Salaries of all employees is:" + (sumMonthly / listOfEmployee2014.size()));
System.out.println("Avg. Of Annual Salaries of all employees is:" + (sumAnnually / listOfEmployee2014.size()));
sumMonthly = 0;
sumAnnually = 0;
System.out.println(" Year 2015");
System.out.println("============================");
for (int index = 0; index < listOfEmployee2015.size(); index++) {
System.out.println(listOfEmployee2015.get(index));
sumMonthly = sumMonthly + listOfEmployee2015.get(index).monthlySalary;
sumAnnually = sumAnnually + listOfEmployee2015.get(index).getAnnualSalary();
}
System.out.println("Avg. Of Monthly Salaries of all employees is:" + sumMonthly / listOfEmployee2015.size());
System.out.println("Avg. Of Annual Salaries of all employees is:" + sumAnnually / listOfEmployee2015.size());
}
}
====EmployeeList===
2014 Employee Smithson,John 2000
2015 Salesman Jokey,Will 3000 100000
2014 Executive Bush,George 5000 150
====O/P===
run:
Year 2014
============================
Name: Smithson,John Monthly Salary: 2000 Annual Salary:24000.0
Name: Bush,George Monthly Salary: 5000 Annual Salary (including bonus):90000.0 Stock Price: 150
Avg. Of Monthly Salaries of all employees is:3500.0
Avg. Of Annual Salaries of all employees is:72000.0
Year 2015
============================
Name: Jokey,Will Monthly Salary: 3000 Annual Salary (including commission):36000.0 Annual Sales: 100000
Avg. Of Monthly Salaries of all employees is:3000.0
Avg. Of Annual Salaries of all employees is:36000.0
BUILD SUCCESSFUL (total time: 0 seconds)
you can have the format of O/P in your desire way .
Let me know if you have any queries.
Thanks.