真实员工数统计Java实现

//Employee.java
package cn.campsg.java.experiment;

public class Employee {
    String no;
    String name;
    float salary;
    String department;
    public String getNo() {    return no;}
    public void setNo(String no) {this.no = no;}
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public float getSalary() {return salary;}
    public void setSalary(float salary) {this.salary = salary;}
    public String getDepartment() {    return department;}
    public void setDepartment(String department) {    this.department = department;}//2 2/3

    public Employee() {    }
    public Employee(String no,String name,    String department,float salary) {
        this.no = no;
        this.name = name;
        this.salary = salary;
        this.department = department;
    }
    @Override
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        if(!(obj instanceof Employee))
            return false;
        Employee emp = (Employee)obj;
        if(!emp.getNo().equals(this.getNo()))
            return false;
        return true;
    }
    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer("工号");
        buffer.append(no);
        buffer.append(",姓名");
        buffer.append(name);
        buffer.append(",部门");
        buffer.append(department);
        buffer.append(",薪水");
        buffer.append(salary);
        return buffer.toString();
    }
}

//MainClass.java
package cn.campsg.java.experiment;

public class MainClass {
     public static boolean isRepeat(int index,Employee[] employees) {
         Employee emp = employees[index];
               for(int i = 0;i < employees.length; ++i) {
                        if(emp == employees[i]) {
                                continue;
                            }
                        if(emp.equals(employees[i]) == true) {
                                return true;
                           }
                   }
                return false;
           }
           
     public static void main(String argv[]) {
               Employee[] employees = new Employee[3];
                 employees[0] = new Employee("1001","张一","销售部",5000.0f);
                 employees[1] = new Employee("1002","王二","销售部",6500.0f);
                 employees[2] = new Employee("1001","Alan","研发部",15000.0f);
                 int count = 0;

                 for(int i=0;i<employees.length;++i){

                         if(isRepeat(i,employees)==false)
                             {
                                 count++;
                                 System.out.println(employees[i].toString());
                             }
                 }
             System.out.println("本公司有效员工数: "  + count);
         }
 }

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值