[java]代码库
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
Teacher[] t = new Teacher[num];
TeacherManagement tea = new TeacherManagement(num);
for(int i = 0;i < num; i++)
{
int no = scan.nextInt();
String name = scan.next();
int age = scan.nextInt();
String se = scan.next();
t[i] = new Teacher(no,name,age,se);
tea.add(t[i]);
}
Arrays.sort(t);
for( int i = 0; i < num; i++)
System.out.println(t[i].toString());
String name = scan.next();
tea.search(name);
num = scan.nextInt();
tea.search(num);
scan.close();
}
}
class Teacher implements Comparable
{
int no;
int age;
String name;
String seminary;
public Teacher(int n, String na, int a, String s)
{
no = n;
name = na;
age = a;
seminary = s;
}
void setno(int n)
{
no = n;
}
void setname(String n)
{
name = n;
}
void setage(int a)
{
age = a;
}
void setseminary(String s)
{
seminary = s;
}
int getno()
{
return no;
}
int getage()
{
return age;
}
String getname()
{
return name;
}
String getseminary()
{
return seminary;
}
boolean equals(Teacher t)
{
if(this.no == t.getno())
return true;
else
return false;
}
public String toString()
{
return "no: "+no+", name: "+name+", age: "+age+", seminary: "+seminary;
}
@Override//重写
public int compareTo(Object a)
{
Teacher b = (Teacher) a;
if(this.no > b.no)
return 1;
else if(this.no < b.no)
return -1;
else
return 0;
}
}
class TeacherManagement
{
private static int num = 0;
Teacher[] teacher ;
public TeacherManagement(int a)
{
teacher = new Teacher[a];
}
void add(Teacher t)
{
teacher[num] = t;
num++;
}
void search(String n)
{
int flag = 0;
System.out.println("search by name:");
for(int i = 0; i < num; i++)
{
if(n.equals(teacher[i].getname()))
{
System.out.println(teacher[i].toString());
flag = 1;
}
}
if(flag == 0)
System.out.println("no such teacher");
}
void search(int a)
{
int flag = 0;
System.out.println("search by age:");
for(int i = 0; i < num; i++)
{
if(a == teacher[i].getage())
{
System.out.println(teacher[i].toString());
flag = 1;
}
}
if(flag == 0)
System.out.println("no such teacher");
}
}