'Employee java program
So I've been trying to write a program and it requires me to use a bunch of methods and such but I'm still a beginner I was wondering what I'm suppose to do to make this happen.
A static field, empCount, keeps track of the number of instantiated employees and can be retrieved using the static method getCount( )
he methods getName( ) and getNumber( ) return the employee's name and employee number respectively
this is what I have now
public class Employee {
private static int empCount;
private String empName;
private double empSalary;
private double empRate;
private double empHour;
private double empBase;
private static int empPieces;
private static int empType;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
empCount = 0;
empCount ++;
System.out.printf("Enter Employee Name: " );
String name =input.next();
Employee e1= new Employee(name);
System.out.printf( "Employee count is " + empCount+ "\n" );
empCount ++;
e1.setEmployeePay(253553);
System.out.printf("Enter Employee Name: ");
name =input.next();
Employee e2= new Employee(name);
System.out.println( "Employee count is " + empCount );
empCount ++;
System.out.printf("Enter Employee Name: ");
name =input.next();
Employee e3= new Employee(name);
System.out.println( "Employee count is " + empCount );
empCount ++;
System.out.printf("Enter Employee Name: ");
name =input.next();
Employee e4= new Employee(name);
System.out.println( "Employee count is " + empCount );
empCount ++;
System.out.println("Employee "+name+" earned: " +"");
System.out.println("Employee "+name+" earned: " +"");
System.out.println("Employee "+name+" earned: " +"");
System.out.println("Employee "+name +" earned: " +"");
}
public Employee(String name){
}
public String getName(){
return empName;
}
public static int getNumber(){
Random random = new Random();
int empnumber = 1000 + random.nextInt(9999);
return empnumber;
}
Solution 1:[1]
It is good practice to keep your model in a separate class
import java.util.*;
public class emp
{
public static ArrayList<Employee> list=new ArrayList<Employee>();
public static void main(String args[]){
Scanner s=new Scanner(System.in);
while(true){
System.out.print("Enter Name : ");
String name=s.nextLine();
Employee e=new Employee(name);
list.add(e);
System.out.println("Count : " +list.size());
for(Employee x:list){
System.out.println("Employee : " +x.getName());
}
}
}
}
class Employee{
private String empName;
private double empSalary;
private double empRate;
private double empHour;
private double empBase;
// any other properies
Employee(String name){
empName=name;
}
public String getName(){
return empName;
}
public void setName(String name){
empName=name;
}
//any other getters and setters
}
Solution 2:[2]
I notice that you increment the empCount variable in your main method each time a new instance of an Employee is created.
If you are trying to track the number of instances of Employee with the empCount variable, an easier approach would be to increment it in your constructor, ie:
public Employee(String name)
{
empName = name;
empCount++; //Increment counter here to avoid redundant code
}
By adding the code to increment it to your constructor, you save yourself the trouble of having to rewrite the same code upon each instantiation of a new Employee.
Also, it would be easier to create a field for the Employee's number, add the initialization code to your constructor, and just return it in your getter method for the number; you could even use the empCount variable to determine the number rather than the Random class, that way each instance of Employee has a unique identifier, whereas your current implementation could produce duplicates.
Solution 3:[3]
I see few mistakes here in your code :
String name =input.next(); // you have declared "name" variable here
Employee e1= new Employee(name);
name =input.next(); // you are assigning a new String here
Employee e2= new Employee(name);
name =input.next(); // you are assigning new String again
Employee e3= new Employee(name);
// here you are trying to print "name" , which will print only last assigned value for "name" variable.
System.out.println("Employee "+name+" earned: " +"");
System.out.println("Employee "+name+" earned: " +"");
System.out.println("Employee "+name+" earned: " +"");
System.out.println("Employee "+name +" earned: " +"");
And last mistake is
// your constructor has no definition inside whereas everytime you are calling
Employee e2= new Employee(name);
//with name parameter
public Employee(String name){
}
//So, what you need to do is
since your object properties are private like
private String empName;
you need to have setter and getter property defined for this variable
public String getEmpName() {
return this.empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
or instead of setter method, you can set while creating instance,
public Employee(String name){
this.empName = name;
}
Then, instead of
System.out.println("Employee "+name+" earned: " +"");
you can use,
System.out.println("Employee "+e1.getEmpName()+" earned: " +"");
And so on.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | prasadmadanayake |
| Solution 2 | DMC |
| Solution 3 | Nielarshi |
