What is wrong with this Company Class OOP program?

The point is to write a program that finds an employee by searching by their id number, and by printing out all the information of all the employees and by printing out the information of a single employee. I need help with that (the search) and if printing a single employee alone is correct? Thanks!

Class Employee code:

public class Employee {
    private String name;
    private int id;
    private int salary;
    private boolean bonus;

public Employee(String n, int i, int s, boolean b) {
    name = n;
    id = i;
    salary = s;
    bonus = b;
}
public void computeSalary(int s, boolean b) {
    if (b == true) 
        salary += 2000;
}

public void printInfo() {
    System.out.print("Name: "+name+" ID: "+id+" Salary: "+salary+" Bonus: "+bonus+" ");
}   

Class EmployeeApp class code:

import java.util.Scanner;

public class EmployeeApp {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter the name: ");
    String n = in.nextLine();
    System.out.print("Enter the ID: ");
    int g = in.nextInt();
    System.out.print("Enter the salary: ");
    int s = in.nextInt(); in.nextLine();
    System.out.print("true or false for bonus? ");
    boolean b = in.nextBoolean();
    Employee e = new Employee(n, g, s, b);
    e.computeSalary(s, b);
    e.printInfo();
}
}
public class Company{
private Employee[] e = new Employee[4];

public void printAllEmployees() {
   for(int i = 0; i < e.length; i++)
       e[i].printInfo();
}

public Employee searchEmployee(int i) {
    Employee temp = null;
    for (int j = 0; j < e.length; j++) {
        if (Employee.id == i)
            temp = Employee;
    }
    return temp;
}

public void printAnEmployee(Employee e) {
    e.printInfo();
}
}

Answers


put employees in a structure that will enable simple searching latter, like the following:

private Map<Integer, Employee> e = new HashMap<Integer, Employee>();

public void printAllEmployees() {
    for( Integer key : e.keySet() )
        System.out.println(e.get( key ));
}

public Employee searchEmployee(int i) {
    return e.get(i);
} 

I do not know how the code that you gave us works for even for an user because you have a minor issues in your classes. First, it is a good practice to have setter and getter in your Employee because for safety is good to encapsulate a object , so how to construct them plz refer to this tutorial http://www.tutorialspoint.com/java/java_encapsulation.htm

Second, in your EmployeeApp class when you ask this

System.out.print("true or false for bonus? ");
boolean b = in.nextBoolean();

is not clear what kind of input user has to enter, so it is good to change first line to

System.out.print("true or false for bonus? \nplease enter true or false");

Third, it is good to put your company class in different file, and some minor issue in your Company class as follow

Note: Employee is a class and e is an array with Employee type

public Employee searchEmployee(int i) {
    Employee temp = null;
    for (int j = 0; j < e.length; j++) {
        if (**Employee.id** == i)
            **temp = Employee;**
    }
    return temp;
}

change to

public Employee searchEmployee(int i) {
        Employee temp = null;
        for (int j = 0; j < e.length; j++) {
            if (e[i].getId() == i) {
                temp = e[i];
            }
        }
        return temp;
    }

At the end, if you need to add more employee, you need to put it in a while loop. Hence, while loop is going to be ok till for example an user push X.


Need Your Help

Netflix in Android WebView

android android-webview

So let me preface this by saying downloading the Netflix App is not an option. So my thought is to just make a wrapper app that loads the netflix website in a WebView. This works for login/naviga...

VTD-XML Parsing Performance (speed critical factor). Requesting Feedback/Comments

java xml performance vtd-xml

I am about to use VTD-XML (found at http://vtd-xml.sourceforge.net/) but I am interested in getting real-case usage feedback, by any one that has used the library and has any comments.