习题:写员工Employee类,包含工号id、姓名name、工资salary等属性。

完整题干:

写员工Employee类,包含工号id、姓名name、工资salary等属性,令Employee类实现java.lang.comparable接口,并实现接口中的compareTo()方法,制定比较的规则。在测试类中创建几个Employee对象,利用java.util.Arrays类对其进行排序并打印输出排序结果。

关于比较的规则∶建议分别按照name和salary进行升序排序。

个人答案:

import java.util.Comparator;

    public class Employee implements Comparable<Employee>{

        private String name;

        private String id;

        private int salary;

        public Employee(String id,String name,int salary) {

            this.id = id;

            this.name = name;

            this.salary = salary;

        }



        @Override

        public int compareTo(Employee u) {

            if(this.salary<u.salary) {

                return 1;

            }else if(this.salary==u.salary) {

                return this.name.compareTo(u.name);

            }else {

                return -1;

            }

        }

          public String getId() {

           return id;

           }

   public void setId(String id) {

           this.id = id;

        }



    public String getName() {



           return name;

      }



  public void setName(String name) {

         this.name = name;

       }

   public int getSalary() {



            return salary;



        }



   public void setSalary(int salary) {



           this.salary = salary;



  }



        @Override

    public String toString() {



                 return "User [id=" + id + ", name=" + name + ", salary=" + salary + "]";



            }





  public int compare(Employee o) {




              int result =  ((o.getSalary() - this.getSalary()) * 10);//按照工资的升序排序



         //int result = o.getName().compareTo(this.getName());//按照英文字典名字顺序的降序



         //int result = this.getName().compareTo(o.getName());//按照英文字典名字顺序的升序



                 return result;

    }

 }

import java.util.ArrayList;

 import java.util.Collections;

 import java.util.List;



 public class Main {



    public static void main(String[] args) {



           List users = new ArrayList();



        Employee user1 = new Employee("1", "zhangsan", 10000);



        Employee user2 = new Employee("2", "lisi", 12000);



        Employee user3 = new Employee("3", "wangwu", 11000);



        Employee user4 = new Employee("4", "xiaoliu", 11500);




            users.add(user1);



            users.add(user2);



            users.add(user3);



            users.add(user4);



                System.out.println("Before sorting -------------------- >>>");

                for(int i = 0;i < users.size();i++){

                       System.out.println(users.get(i));

                }

          Collections.sort(users);



       System.out.println("After sorting-------------------- >>>");



        for(int i = 0;i < users.size();i++){



          System.out.println(users.get(i));



         }

   }



 }



 

 参考答案:

package chap6.excercise.ex7;

import java.util.Arrays;

public class Employee implements Comparable {

	private String id;

	private String name;

	private double salary;

	

	public Employee() {		

	}

	

	public Employee(String id, String name, double salary) {

		super();

		this.id = id;

		this.name = name;

		this.salary = salary;

	}

	public String getId() {

		return id;

	}

	public void setId(String id) {

		this.id = id;

	}

	public String getName() {

		return name;

	}

	public void setName(String name) {

		this.name = name;

	}

	public double getSalary() {

		return salary;

	}

	public void setSalary(double salary) {

		this.salary = salary;

	}



	@Override

	public int compareTo(Object obj) {

		if(obj instanceof Employee){

			Employee e = (Employee)obj;

			//return this.name.compareTo(e.name);  //按照name升序排序

			return (int)(this.salary-e.salary);	//按照工资升序排序

		}

		return 0;

	}

	

	public String toString(){	//重写toString方法

		return id+","+name+","+salary;

	}

	

	public static void main(String[] args){

		Employee[] es = new Employee[5];

		es[0]= new Employee("001","zhang",5000);

		es[1]= new Employee("002","li",8000);

		es[2]= new Employee("003","zhao",6000);

		es[3]= new Employee("004","song",6500);

		es[4]= new Employee("005","zhu",9500);



		Arrays.sort(es);

		

		for(Employee e: es){

			System.out.println(e);

		}		

	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hellenionia

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值