/*
* 编写程序,用某种集合接口的实现类作存储,实现具有自定义排序功能的包含
* 姓名、年龄、身高、职称等内容的人事信息输入和打印。
* 林炳
* 2009年11月22日
*/
import java.io.*;
import java.util.*;
class Student {
int age;
int stature;
String status;
String name;
Student(int age,int stature,String status,String name){
this.age=age;
this.stature=stature;
this.status=status;
this.name=name;
}
public boolean equals(Student o){
if((this.age==o.age)&&(this.stature==o.stature)&&(this.status.equals(status))&&(this.name.equals(name)))
return true;
else
return false;
}
}
//构造基于Student的Name的Comparator类
class StuNameComparator implements Comparator<Student>{
public int compare(Student st1,Student st2)
{
return st1.name.compareTo(st2.name);
}
}
//构造基于Student的Status的Comparator类
class StuStatusComparator implements Comparator<Student>{
public int compare(Student st1,Student st2)
{
return st1.status.compareTo(st2.status);
}
}
//构造基于Student的Age的Comparator类
class StuAgeComparator implements Comparator<Student>{
public int compare(Student st1,Student st2)
{
return (st1.age-st2.age);
}
}
//构造基于Student的Stature的Comparator类
class StuStatureComparator implements Comparator<Student>{
public int compare(Student st1,Student st2)
{
return (st1.stature-st2.stature);
}
}
public class test_linkedlist {
/**
* @param args
*/
//菜单函数
private static int MainMenu() throws IOException{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));//创建输入流缓冲器
//菜单页面显示内容
System.out.println("人事信息管理系统");
System.out.println("1、添加信息");
System.out.println("2、删除信息");
System.out.println("3、信息显示");
System.out.println("4、退出");
System.out.print("请选择操作:");
String s=input.readLine();//读入输入量
return Integer.parseInt(s);
}
//添加信息输入函数
private static Student input()throws IOException{
System.out.println("请输入名字 职称 年龄 身高(中间以空格分隔):");
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
String s=input.readLine();
String[] s_a=s.split(" ");//将字符串以空格分成几个字符串数组
Student st;
st=new Student(Integer.parseInt(s_a[2]),Integer.parseInt(s_a[3]),s_a[1],s_a[0]);
return st;
}
//显示各种根据不同要求排序的结果
private static void DisplaySort(LinkedList<Student> stu_list){
Iterator<Student> stu_iterator=stu_list.iterator();
//根据职称字母自然排序
Collections.sort(stu_list,new StuStatusComparator());
System.out.println("输出根据职称字母自然排序");
while(stu_iterator.hasNext()){
Student stu=stu_iterator.next();
System.out.println("name:"+stu.name+" Status:"+stu.status+" Age:"+stu.age+" Stature:"+stu.stature+"cm");
}
stu_iterator=stu_list.iterator();
//根据年龄升序排序
Collections.sort(stu_list,new StuAgeComparator());
System.out.println("输出根据年龄升序排序:");
while(stu_iterator.hasNext()){
Student stu=stu_iterator.next();
System.out.println("name:"+stu.name+" Status:"+stu.status+" Age:"+stu.age+" Stature:"+stu.stature+"cm");
}
stu_iterator=stu_list.iterator();
//根据身高升序排序
Collections.sort(stu_list,new StuStatureComparator());
System.out.println("输出根据身高升序排序:");
while(stu_iterator.hasNext()){
Student stu=stu_iterator.next();
System.out.println("name:"+stu.name+" Status:"+stu.status+" Age:"+stu.age+" Stature:"+stu.stature+"cm");
}
stu_iterator=stu_list.iterator();
//根据姓名字母自然排序
Collections.sort(stu_list,new StuNameComparator());
System.out.println("输出根据姓名字母自然排序");
while(stu_iterator.hasNext()){
Student stu=stu_iterator.next();
System.out.println("name:"+stu.name+" Status:"+stu.status+" Age:"+stu.age+" Stature:"+stu.stature+"cm");
}
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
LinkedList<Student> stu_list=new LinkedList<Student>();
Student st1,st2,st3,st4,st5;
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
String StrInput;
boolean exit=false;
//已有的人事信息
st1=new Student(50,170,"Professor","Rose");
st2=new Student(56,178,"Professor","Mike");
st3=new Student(45,165,"Associate.Pro","Jack");
st4=new Student(43,175,"Associate.Pro","Dove");
st5=new Student(51,163,"Assistant.Res","Tony");
stu_list.add(st1);
stu_list.add(st2);
stu_list.add(st3);
stu_list.add(st4);
stu_list.add(st5);
//菜单选项,对人事信息进行添加、删除、查看排序结果等操作
while(exit!=true)
{
int temp;
temp=MainMenu();
Student st;
switch(temp)
{
case 1:
st=input();
stu_list.add(st);
while(true)
{
System.out.println("按回车继续!");
input.readLine();
break;
}
break;
case 2:
System.out.print("输入删除人事信息的位置:");
StrInput=input.readLine();
stu_list.remove(Integer.parseInt(StrInput)-1);
while(true)
{
System.out.println("按回车继续!");
input.readLine();
break;
}
break;
case 3:
DisplaySort(stu_list);
while(true)
{
System.out.println("按回车继续!");
input.readLine();
break;
}
break;
case 4:
exit=true;
break;
default:
break;
}
}
}
}
- 1
- 2
前往页