A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 韩慧阳 中级黑马   /  2012-5-20 15:28  /  1434 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 韩慧阳 于 2012-5-20 16:22 编辑

里面的将教师按照总工资排序我想着用TreeSet集合做一下,但是排序功能不知道哪里错了,高手给指点下吧!
帖子最后有附件,童鞋们把附件下载下来贴到EditPlus里面应该就容易看了。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Test;
/**
*
* @author gj
*/
import java.util.*;
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Teacher tea[]={
    new Professor(1001,"zhangsan",18,200,2000.2),
    new Professor(1002,"lisi",19,200,2000.7),
                                new Professor(1002,"lisi2",19,200,2000.1),
    new Lecture(1003,"wangwu",40,150,100)
  };
  College col=new College(1001,"信息学院",tea);
  col.show();
                col.getMinWageTeacher(tea);
                System.out.println("教师人数:"+col.getTeachersNo());
                System.out.println("教师总工资:"+col.getTotalTeacherWage(tea));
                System.out.println("所有教师的平均工资:"+col.getAveWage(tea));
                col.getWageOrder(tea);

    }
}
abstract class Teacher /*implements Comparable*/{
protected int id;
protected String name;
protected int times;
protected int perfee;
public Teacher(int id,String name,int times,int perfee){
  this.id=id;
  this.name=name;
  this.times=times;
  this.perfee=perfee;
}
public abstract void show();
//计算总工资的方法
        /*public abstract double wage();
         public int compareTo(Object obj){
            if(!(obj instanceof Lecture)){
                throw new RuntimeException("错误对象!");
            }
            if(this.wage()>)
        }*/
}
class Professor extends Teacher{//教授
private double bonus;//奖金
public Professor(int id,String name,int times,int perfee,double bonus){
  super(id,name,times,perfee);
  this.bonus=bonus;
}
public void show(){
  System.out.println(this.id+"\t"+this.name+"\t"+"基本工资:"+this.perfee+"\t"+"奖金:"+this.bonus+"\t"+this.times+"\t"+"总工资:"+this.wage());
}
//计算总工资的方法
        public double wage(){
            double wage=bonus+perfee;
            return wage;
        };
public int getId() {
  return id;
}
public void setId(int id) {
  this.id = id;
}
public String getName() {
  return name;
}
public void setName(String name) {
  this.name = name;
}
public int getTimes() {
  return times;
}
public void setTimes(int times) {
  this.times = times;
}
public int getPerfee() {
  return perfee;
}
public void setPerfee(int perfee) {
  this.perfee = perfee;
}
public double getBonus() {
  return bonus;
}
public void setBonus(double bonus) {
  this.bonus = bonus;
}
}
class Lecture extends Teacher{//讲座
private float laowu;
public Lecture(int id,String name,int times,int perfee,float laowu){
  super(id,name,times,perfee);
  this.id=id;
  this.name=name;
  this.times=times;
  this.perfee=perfee;
  this.laowu=laowu;
}
public int getId() {
  return id;
}
public void setId(int id) {
  this.id = id;
}
public String getName() {
  return name;
}
public void setName(String name) {
  this.name = name;
}
public int getTimes() {
  return times;
}
public void setTimes(int times) {
  this.times = times;
}
public int getPerfee() {
  return perfee;
}
public void setPerfee(int perfee) {
  this.perfee = perfee;
}
public float getLaowu() {
  return laowu;
}
public void setLaowu(float laowu) {
  this.laowu = laowu;
}
public void show(){
  System.out.println(this.id+"\t"+this.name+"\t"+"基本工资:"+this.perfee+"\t"+"讲座费:"+this.laowu+"\t"+this.times+"\t"+"总工资:"+this.wage());
}
//计算总工资的方法
        public double wage(){
            double wage=laowu+perfee;
            return wage;
        }
}
class College {
private int cid;
private String cname;
private Teacher tea[];
public College(){
}
public College(int cid,String cname,Teacher tea[]){
  this.cid=cid;
  this.cname=cname;
  this.tea=tea;
}
public College(int cid,String cname,int num){
  this.cid=cid;
  this.cname=cname;
  this.tea=new Teacher[num];
}
public void show(){
  System.out.println(this.cname+"学校的教师工资信息如下:");
  for(int i=0;i<tea.length;i++){
   if(tea instanceof Professor)
   ((Professor)tea).show();
   else
   ((Lecture)tea).show();
  }
}
public int getCid() {
  return cid;
}
public void setCid(int cid) {
  this.cid = cid;
}
public String getCname() {
  return cname;
}
public void setCname(String cname) {
  this.cname = cname;
}
public Teacher[] getTea() {
  return tea;
}
public void setTea(Teacher[] tea) {
  this.tea = tea;
}
//求学院实际教师人数的方法
        public int getTeachersNo(){
            return tea.length;
        }
//求最高工资的教师信息
        public Object getMaxWageTeacher(Teacher tea[])
        {
            double twage=0;
            int num=-1;
            Object obj=new Object();
            Object obj2=new Object();
            for(int i=0;i<tea.length;i++)
            {
               if(tea instanceof Professor){
                   if(((Professor)tea).wage()>twage){
                       twage=((Professor)tea).wage();
                       obj=(Professor)tea;
                       num=i;
                   }
                }
  else{
                    if(((Lecture)tea).wage()>twage){
                        twage=((Lecture)tea).wage();
                        obj=(Lecture)tea;
                        num=i;
                    }
                }
            }
            if(tea[num] instanceof Professor){
                ((Professor)tea[num]).show();
            }
            else{
                ((Lecture)tea[num]).show();
            }
            return obj;
        }
//求最低工资教师的信息
        public Object getMinWageTeacher(Teacher tea[])
        {
            double twage=999999999;
            int num=-1;
            Object obj=new Object();
            for(int i=0;i<tea.length;i++)
            {
               if(tea instanceof Professor){
                   if(((Professor)tea).wage()<twage){
                       twage=((Professor)tea).wage();
                       obj=(Professor)tea;
                       num=i;
                   }
                }
  else{
                    if(((Lecture)tea).wage()<twage){
                        twage=((Lecture)tea).wage();
                        obj=(Lecture)tea;
                        num=i;
                    }
                }
            }
            if(tea[num] instanceof Professor){
                ((Professor)tea[num]).show();
            }
            else{
                ((Lecture)tea[num]).show();
            }
            return obj;
        }
//求所有教师的总工资
        public double getTotalTeacherWage(Teacher tea[])
        {
            double twage=0;
            for(int i=0;i<tea.length;i++)
            {
               if(tea instanceof Professor){
                      twage+=((Professor)tea).wage();
                }
  else{
                        twage+=((Lecture)tea).wage();
                    }
            }
            return twage;
        }
//求所有教师的平均工资
        public double getAveWage(Teacher tea[]){
            double twage=this.getTotalTeacherWage(tea);
            int num=this.getTeachersNo();
            return twage/num;
        }
//对所有教师按总工资排序
        public void getWageOrder(Teacher tea[]){
            int k=0;
            //Teacher tea[]={}
            TreeSet ts=new TreeSet(new MyCompare());
            for(int i=0;i<tea.length;i++)
            {
                ts.add(tea);
            }
            Iterator it=ts.iterator();
            while(it.hasNext()){
                  if(tea[k] instanceof Professor)
                        ((Professor)tea[k]).show();   
                  else
                        ((Lecture)tea[k]).show();
                  k++;      
  
                 // this.show();
                /*if(it.next() instanceof Professor){
                    ((Professor)tea[k]).show();
                  //  pf.show();
                    k++;
                }
                else{
                    ((Lecture)tea[k]).show();
                    //lt.show();
                    k++;
                }*/
                //System.out.println(k);
            }
           }
//按学号查找某个教师
//新增一个教师
//删除一个教师
//修改教师信息(不包括修改编号)

}
class MyCompare implements Comparator{
    private double t1=-1,t2=-1;
    public int compare(Object obj1,Object obj2){
        //double t1=-1,t2=-1;
        String str1=null,str2=null;
        if(obj1 instanceof Professor){
            t1=((Professor)obj1).wage();
            str1=((Professor)obj1).getName();
        }
        else{
            t1=((Lecture)obj1).wage();
            str1=((Lecture)obj1).getName();
        }
        if(obj2 instanceof Professor){
            t2=((Professor)obj2).wage();
            str2=((Professor)obj2).getName();
        }
        else{
            t2=((Lecture)obj2).wage();
            str2=((Lecture)obj2).getName();
        }
        System.out.println("00000000000"+"..."+t1+"..."+t2);
        if(t1>t2)
            return 1;
        if(t1==t2)
            return 0;//str1.compareTo(str2);
        return -1;
    }
}

Main.rar

2.08 KB, 下载次数: 101

2 个回复

倒序浏览
阳阳啊   你这代码看得我眼睛都花了。。。。不过应该是用Collections类来排序  我把我的代码给你看看
首先需要新建一个类,继承comparator接口,重写其compare方法,然后调用collections方法中的sotr(List list,comparator c)方法进行排序。
import java.util.ArrayList;  
import java.util.Arrays;  
import java.util.Collections;  
import java.util.Comparator;  
class comp implements Comparator{  
  
    @Override  
    public int compare(Object arg0, Object arg1) {  
        // TODO Auto-generated method stub  
        Person1 p1=(Person1)arg0;  
        Person1 p2=(Person1)arg1;  
        if(p1.getAge()>p2.getAge()) return 1;  
        else if(p1.getAge()<p2.getAge()) return -1;  
         
        else return 0;  
    }  
      
}  
public class  Test3   
{  
      
  
     public static void main(String[] args)  
    {  
         ArrayList<Person1> list=new ArrayList();  
         list.add(new Person1("jiengyh",30));  
         list.add(new Person1("jiengyh1",29));  
         list.add(new Person1("huazehui",25));  
        comp comparator=new comp();         
         Collections.sort(list, comparator);  
         for(Person1 p:list){System.out.println(p.getName()+"  "+p.getAge());}  
  
           
           
           
              
      
    }  
  
  
  
}  
class Person1{  
    private String name;  
    private int age;  
    public Person1(String name,int age){  
        this.name=name;  
        this.age=age;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
    public int getAge() {  
        return age;  
    }  
}  
回复 使用道具 举报
除了蒋同学怎么没人给点回复啊?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马