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