Test task (first job in my life)

Hello, everyone!

I really need your opinion about my program. It is a test task for Junior Java Developer. If I'll do it well they'll take me.

Task: demonstrate understanding of OOP.

Count salaryfor IT-company X. Salary consist of fixed salary and bonus.

Information about employee: name,role,experience(years),project(s), fixed salary.

Project: each project may have fixed budget or not.

Roles: managers and developers. Each developer must have only one project.

Bounus:

1) 50$for each year of experience after first year.

2) 500$for each project with unfixed budget.

The result of the program must be a table with name of employee and his or her salary. Sorted by salary.

My program consists of 6 java files:

publicclass Project{

private String name;

privateboolean costFixed;

//===================================================================

Project(){};

Project(String n){

name=n;

}

Project(String n,boolean b){

name=n;

costFixed=b;

}

//===================================================================

publicvoid setName(String n){

name=n;

}

publicvoid setCostFixed(boolean b){

costFixed=b;

}

//===================================================================

public String getName(){

return name;

}

publicboolean isCostFixed(){

return costFixed;

}

}

import java.util.*;

publicclass ProjectSystem{

private List<Project> pSystem;

ProjectSystem(){

pSystem=new ArrayList<Project>();

}

//===================================================================

publicvoid addProject(Project p){

if(! pSystem.contains(p))pSystem.add(p);

}

public Project getProject(int i){

Project p;

if(i<=pSystem.size()){p=pSystem.get(i);}

else{p=new Project("no poject");}

return p;

}

publicvoid remProject(String n){

int e=0;

for(int i=0;i<pSystem.size();i++){

if(pSystem.get(i).getName()==n)e=i;

}

if(e!=0)pSystem.remove(e);

}

//===================================================================

publicint countProjects(){

return pSystem.size();

}

publicint countFixCostProjects(){

int e=0;

for(int i=0;i<pSystem.size();i++){

if(pSystem.get(i).isCostFixed())e++;

}

return e;

}

}

publicclass Managerextends Employee{

private ProjectSystem projectSystem;

//===================================================================

Manager(String n,double e){

projectSystem=new ProjectSystem();

name=n;

setFixSalary(0);

setExperience(e);

}

Manager(String n,double e,int f){

projectSystem=new ProjectSystem();

name=n;

setFixSalary(f);

setExperience(e);

}

Manager(String n,double e,int f, ProjectSystem ps){

projectSystem=ps;

name=n;

setFixSalary(f);

setExperience(e);

}

//===================================================================

publicvoid setProjectSystem(ProjectSystem ps){

projectSystem=ps;

}

public ProjectSystem getProjectSystem(){

return projectSystem;

}

//===================================================================

publicint countTotalBonus(){

int t=0;

for(int i=0;i<projectSystem.countProjects();i++){

if(! projectSystem.getProject(i).isCostFixed())t+=500;

}

t+=countExperienceBonus();

return t;

}

}

publicclass Developerextends Employee{

private Project project;

//===================================================================

Developer(String n,double e){

project=new Project();

name=n;

project.setName(null);

setFixSalary(0);

setExperience(e);

}

Developer(String n,double e,int f, Project p){

name=n;

project=p;

setFixSalary(f);

setExperience(e);

}

//===================================================================

publicvoid setProject(Project p){

project=p;

}

public Project getProject(){

return project;

}

//===================================================================

publicint countTotalBonus(){

int t=0;

if(! getProject().isCostFixed())t=500;

t+=countExperienceBonus();

return t;

}

}

publicabstractclass Employee{

String name;

intfixSalary;

double experience;

//===================================================================

publicvoid setName(String n){

name=n;

}

publicvoid setFixSalary(int f){

fixSalary=f;

}

publicvoid setExperience(double e){

experience=e;

}

//===================================================================

public String getName(){

return name;

}

publicint getFixSalary(){

return fixSalary;

}

publicdouble getExperience(){

return experience;

}

//===================================================================

publicint countExperienceBonus(){

int e=0;

double a;

a=getExperience();

if(a>=2){

a=a-1;

e=(int)a;

}

return e*50;

}

publicabstractint countTotalBonus();

publicint countSalary(){

return getFixSalary()+countTotalBonus();

}

}

import java.util.*;

import java.io.*;

class EmploymentSystem{

private List<Employee> eSystem;

EmploymentSystem(){

eSystem=new ArrayList<Employee>();

}

publicvoid addEmployee(Employee m){

int e=0;

for(int i=0;i<eSystem.size();i++){

if(eSystem.get(i).getName()==m.getName())e=i;

}

if(e==0)eSystem.add(m);

}

publicint countEmployees(){

return eSystem.size();

}

//===================================================================

publicvoid printEmployees(){

System.out.println(" EMPLOYEESALARY");

System.out.println("======================");

for(int i=0;i<eSystem.size();i++){

System.out.println(eSystem.get(i).getName()+

""+eSystem.get(i).countSalary());

}

}

//===================================================================

privatevoid change(int a,int b){

Employee c;

c=eSystem.get(a);

eSystem.set(a,eSystem.get(b));

eSystem.set(b,c);

}

publicvoid sortEmployees(){

int a,b;

for(int i=0;i<eSystem.size();i++){

for(int e=0;e<eSystem.size()-1;e++){

a=eSystem.get(e).countSalary();

b=eSystem.get(e+1).countSalary();

if(a><b)change(e,e+1);

}

}

}

//===================================================================

publicstaticvoid main(String args[]){

EmploymentSystem es=new EmploymentSystem();

Project charEditor=new Project("Char Editor",true);

Project employmentSystem=new Project("Employment System",false);

Project bankTransaction =new Project("Bank Transaction",false);

ProjectSystem pSystem=new ProjectSystem();

pSystem.addProject(charEditor);

pSystem.addProject(employmentSystem);

pSystem.addProject(bankTransaction);

//===================================================================

Developer d1=new Developer("Ivan Ivanov",0.5);

d1.setProject(charEditor);

d1.setFixSalary(1700);

Developer d2=new Developer("Petrov Petr",2.0,2000,employmentSystem);

Developer d3=new Developer("Jim",2.0,3500,charEditor);

Managerm1=new Manager ("Mike",4.0,6000,pSystem);

//===================================================================

es.addEmployee(d1);

es.addEmployee(d2);

es.addEmployee(d3);

es.addEmployee(m1);

es.sortEmployees();

es.printEmployees();

}

}

>

[17563 byte] By [America70a] at [2007-11-27 11:18:37]
# 1

Is there any reason for this in the code?

//===================================================================

Also, have you not given thought to assigning roles to employees instead of defining them as subclasses in a hierarchy as you have. What you would do if you had something like this:

Manager

Salesman

Human Resources

Accounting

And the HR person also took care of the Accounting as they were multi-skilled and the company was pretty small so they worked multiple roles. How would represent that in your current structure?

Do you know anything about the "this" keyword in Java? Do you know what happens if you don't use it in setters and constructors?

_helloWorld_a at 2007-7-29 14:31:56 > top of Java-index,Java Essentials,Training...
# 2

arguments can shadow object fields. May I use not the same names and do not include "this" keyword?

America70a at 2007-7-29 14:31:56 > top of Java-index,Java Essentials,Training...
# 3

Ah yes, well spotted ;-).

So what about the other things then?

_helloWorld_a at 2007-7-29 14:31:56 > top of Java-index,Java Essentials,Training...
# 4

Idea about this and constructor overloading:

Manager(String n, double e){

this(n,e,0,new ProjectSystem());

}

Manager(String n, double e, int f){

this(n,e,f,new ProjectSystem());

}

Manager(String n, double e, int f, ProjectSystem ps){

projectSystem=ps;

name=n;

setFixSalary(f);

setExperience(e);

}

America70a at 2007-7-29 14:31:57 > top of Java-index,Java Essentials,Training...
# 5

> Idea about this and overloading constructors:

>

> >Manager(String n, double e){

>this(n,e,0,new ProjectSystem());

> }

>

> anager(String n, double e, int f){

>this(n,e,f,new ProjectSystem());

>}

>

> Manager(String n, double e, int f, ProjectSystem

> stem ps){

>projectSystem=ps;

>name=n;

>setFixSalary(f);

>setExperience(e);

>}

>

What about it? I still think you should use meaningful names for your constructor/method parameters, I would use the same name as you have for your instance variables and use the this keyword. Your signature should say something about the method other than n,e,f,ps

Is this how we should check for String comparison?

eSystem.get(i).getName()==m.getName()

_helloWorld_a at 2007-7-29 14:31:57 > top of Java-index,Java Essentials,Training...
# 6

Thx.

I forgot about Object class:

eSystem.get(i).getName().equals(m.getName());

America70a at 2007-7-29 14:31:57 > top of Java-index,Java Essentials,Training...
# 7

When you are doing this:

System.out.println(eSystem.get(i).getName()+

""+eSystem.get(i).countSalary());

You could be doing this:

System.out.println(eSystem.get(i).toString());

//in Employee add this-override toString inherited from Object, make it do something more useful

public String toString() {

return getName() + " " +

countSalary();

}

I am not sure I agree with some of your decisions, how much time do you have to get this finished?

_helloWorld_a at 2007-7-29 14:31:57 > top of Java-index,Java Essentials,Training...
# 8

I have 15 hours.

toString() is a good idea for demonstrating OOP. HelloWord, thank you for advices.

code version 2 revised and updated:

import java.util.*;

class EmploymentSystem{

private List<Employee> eSystem;

EmploymentSystem(){

eSystem=new ArrayList<Employee>();

}

public void addEmployee(Employee employee){

int e=0;

for(int i=0;i<eSystem.size();i++){

if(eSystem.get(i).getName().equals(employee.getName()))e=i;

}

if(e==0)eSystem.add(employee);

}

public int countEmployees(){

return eSystem.size();

}

//===================================================================

public void printEmployees(){

System.out.println(" EMPLOYEESALARY");

System.out.println("======================");

for(int i=0;i<eSystem.size();i++){

System.out.println(eSystem.get(i).toString());

}

}

//===================================================================

private void change(int a,int b){

Employee c;

c=eSystem.get(a);

eSystem.set(a,eSystem.get(b));

eSystem.set(b,c);

}

public void sortEmployees(){

int a,b;

for(int i=0;i<eSystem.size();i++){

for(int e=0;e<eSystem.size()-1;e++){

a=eSystem.get(e).countSalary();

b=eSystem.get(e+1).countSalary();

if(a><b)change(e,e+1);

}

}

}

//===================================================================

public static void main(String args[]){

EmploymentSystem es=new EmploymentSystem();

Project charEditor=new Project("Char Editor",true);

Project employmentSystem=new Project("Employment System",false);

Project bankTransaction =new Project("Bank Transaction", false);

ProjectSystem pSystem=new ProjectSystem();

pSystem.addProject(charEditor);

pSystem.addProject(employmentSystem);

pSystem.addProject(bankTransaction);

//===================================================================

Developer d1=new Developer("Ivan Ivanov",0.5);

d1.setProject(charEditor);

d1.setFixSalary(1700);

Developer d2=new Developer("Petrov Petr",2.0,2000,employmentSystem);

Developer d3=new Developer("Jim",2.0,3500,charEditor);

Managerm1=new Manager ("Mike",4.0,6000,pSystem);

//===================================================================

es.addEmployee(d1);

es.addEmployee(d2);

es.addEmployee(d3);

es.addEmployee(m1);

es.sortEmployees();

es.printEmployees();

}

}

public abstract class Employee {

String name;

intfixSalary;

double experience;

//===================================================================

public void setName(String name){

this.name=name;

}

public void setFixSalary(int fixSalary){

this.fixSalary=fixSalary;

}

public void setExperience(double experience){

this.experience=experience;

}

//===================================================================

public String getName(){

return name;

}

public int getFixSalary(){

return fixSalary;

}

public double getExperience(){

return experience;

}

//===================================================================

public int countExperienceBonus(){

int e=0;

double a;

a=getExperience();

if(a>=2){

a=a-1;

e=(int)a;

}

return e*50;

}

public abstract int countTotalBonus();

public int countSalary(){

return getFixSalary()+countTotalBonus();

}

public String toString() {

return getName() + "" +

countSalary();

}

}

public class Developer extends Employee {

private Project project;

//===================================================================

Developer(String name, double experience){

this(name,experience,0,new Project());

}

Developer(String name, double experience, int fixSalary, Project project){

this.setName(name);

this.setProject(project);

this.setFixSalary(fixSalary);

this.setExperience(experience);

}

//===================================================================

public void setProject(Project project){

this.project=project;

}

public Project getProject(){

return project;

}

//===================================================================

public int countTotalBonus(){

int t=0;

if(! getProject().isCostFixed())t=500;

t+=countExperienceBonus();

return t;

}

}

public class Manager extends Employee {

private ProjectSystem projectSystem;

//===================================================================

Manager(String name, double experience){

this(name,experience,0,new ProjectSystem());

}

Manager(String name, double experience, int fixSalary){

this(name,experience,fixSalary,new ProjectSystem());

}

Manager(String name, double experience, int fixSalary,

ProjectSystem projectSystem){

this.setProjectSystem(projectSystem);

this.setName(name);

this.setFixSalary(fixSalary);

this.setExperience(experience);

}

//===================================================================

public void setProjectSystem(ProjectSystem projectSystem){

this.projectSystem=projectSystem;

}

public ProjectSystem getProjectSystem(){

return projectSystem;

}

//===================================================================

public int countTotalBonus(){

int t=0;

for(int i=0;i<projectSystem.countProjects();i++){

if(! projectSystem.getProject(i).isCostFixed())t+=500;

}

t+=countExperienceBonus();

return t;

}

}

import java.util.*;

public class ProjectSystem {

private List><Project> pSystem;

ProjectSystem(){

pSystem=new ArrayList<Project>();

}

//===================================================================

public void addProject(Project project){

if(! pSystem.contains(project))pSystem.add(project);

}

public Project getProject(int id){

Project p;

if(id<=pSystem.size()){p=pSystem.get(id);}

else {p=new Project("no poject");}

return p;

}

public void remProject(String name){

int e=0;

for(int i=0;i<pSystem.size();i++){

if(pSystem.get(i).getName().equals(name)) e=i;

}

if(e!=0)pSystem.remove(e);

}

//===================================================================

public int countProjects(){

return pSystem.size();

}

public int countFixCostProjects(){

int e=0;

for(int i=0;i<pSystem.size();i++){

if(pSystem.get(i).isCostFixed())e++;

}

return e;

}

}

public class Project {

private String name;

private boolean costFixed;

//===================================================================

Project(){};

Project(String name){

this.name=name;

}

Project(String name, boolean costFixed){

this.name=name;

this.costFixed=costFixed;

}

//===================================================================

public void setName(String name){

this.name=name;

}

public void setCostFixed(boolean costFixed){

this.costFixed=costFixed;

}

//===================================================================

public String getName(){

return name;

}

public boolean isCostFixed(){

return costFixed;

}

}

>

America70a at 2007-7-29 14:31:57 > top of Java-index,Java Essentials,Training...
# 9

Personally I don't like lines like these:

if(! projectSystem.getProject(i).isCostFixed())t+=500;

if(pSystem.get(i).getName().equals(name)) e=i;

And I don't like your sorting method, I would imagine they expect you to use Collections.sort() method with your own comparator.

Here is an small example

import java.util.Comparator;

public class Employee {

String name;

double salary;

public Employee(String name, double salary) {

super();

this.name = name;

this.salary = salary;

}

static final Comparator<Employee> MAX_SALARY_FIRST =

new Comparator<Employee>() {

public int compare(Employee e1, Employee e2) {

return e2.getSalary() < e1.getSalary() ? -1 :

e2.getSalary() == e1.getSalary() ? 0 :1;

}

};

public String toString() {

return name +" "+salary;

}

public String getName() {

return name;

}

public double getSalary() {

return salary;

}

}

// Test Class

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class EmpTest {

public static void main(String[] args) {

Employee emp1 = new Employee("Jim",2.05);

Employee emp2 = new Employee("Suzie",4.9);

Employee emp3 = new Employee("Emma",1.99);

Employee emp4 = new Employee("Peter",2.35);

List<Employee> list = new ArrayList<Employee> ();

list.add(emp1);

list.add(emp2);

list.add(emp3);

list.add(emp4);

//Sort list using custom comparator provided in Employee class

Collections.sort(list,Employee.MAX_SALARY_FIRST);

System.out.println(list);

}

}

If you had more time then there are other things I would change, certainly one thing that looks unprofessional to me is the names you have used for your variables, use meaningful names, it is very important.

I would have structured my hierarchy much different to yours. I would have started with a Person Class and then had Employee extend this, I would have had an interface for the calculateSalary method that an employee who is eligible to be paid must implement. I would have used Roles and composition to build the concrete staff members rather than using inheritance only which is not flexible enough for staff doing more than one job.

And on top of this many people here could easily improve on what I recommended as I am pretty new myself.

Take a look at this

http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

_helloWorld_a at 2007-7-29 14:31:57 > top of Java-index,Java Essentials,Training...
# 10

Thanks for sorting. They want to see me again and discus my program d:|

And today I was in another company... they just kicked me with questions and I fall down. Blood everywhere.

Medical treatment:

1)Generics and collections(must).

2) SQL.

3)....mb hibernate struts...

America70a at 2007-7-29 14:31:57 > top of Java-index,Java Essentials,Training...
# 11

maybe actually learn some skills before getting a bloody nose for not knowing them during a job interview could help?

jwentinga at 2007-7-29 14:31:57 > top of Java-index,Java Essentials,Training...
# 12

public class JuniorJavaDeveloper extends Developer{

private int testPeriod; //months

public void setTestPeriod(int months){

this.testPeriod=months;

}

}

public static void main(){

JuniorJavaDeveloper America= new JuniorJavaDeveloper();

America.setTestPeriod(2);

}

America70a at 2007-7-29 14:31:57 > top of Java-index,Java Essentials,Training...
# 13

so ya got a job for 2 months congrads... let me guess your about 22? :)

mark07a at 2007-7-29 14:31:57 > top of Java-index,Java Essentials,Training...