员工java程序

时间:2015-03-04 04:41:33

标签: java

所以我一直在尝试编写一个程序,它要求我使用一堆方法等但我仍然是初学者我想知道我想做什么来做这发生了。

静态字段empCount跟踪实例化员工的数量,可以使用static方法getCount( )检索

方法getName( )getNumber( )分别返回员工的姓名和员工编号

这就是我现在所具有的内容。

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;
 }

3 个答案:

答案 0 :(得分:0)

最好将模型保存在单独的类中

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

 } 

答案 1 :(得分:0)

我注意到,每次创建empCount的新实例时,都会在main方法中增加Employee变量。

如果您尝试使用Employee变量跟踪empCount的实例数,则更简单的方法是在构造函数中增加它,即:

public Employee(String name) 
{
    empName = name;
    empCount++; //Increment counter here to avoid redundant code
}

通过添加代码将其递增到构造函数,您可以省去在每次实例化新Employee时必须重写相同代码的麻烦。

此外,为Employee的数字创建一个字段会更容易,将初始化代码添加到构造函数中,然后在getter方法中返回该数字;您甚至可以使用empCount变量来确定数字而不是Random类,这样Employee的每个实例都有唯一的标识符,而您当前的实现可能会产生重复。

答案 2 :(得分:0)

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){

}

//所以,你需要做的是

因为您的对象属性是私有的,如

private String empName;

您需要为此变量定义setter和getter属性

public String getEmpName() {
    return this.empName;
}

public void setEmpName(String empName) {
    this.empName = empName;
}

或代替setter方法,您可以在创建实例时进行设置,

public Employee(String name){
    this.empName = name;
}

然后,而不是

System.out.println("Employee "+name+" earned: " +"");

你可以使用,

System.out.println("Employee "+e1.getEmpName()+" earned: " +"");

等等。