本帖最后由 韩慧阳 于 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;
}
}
|
|