package student
;
import java
.util
.Collections
;
import java
.util
.Comparator
;
import java
.util
.Scanner
;
import java
.util
.Vector
;
interface st{
public void init();
public void sortted_by_score();
public void sortted_by_id();
public void add(String name
, int iD
, String sex
, int score
);
public void search_id(int id
);
public void search_name(String name
);
void delete(int id
);
public void printf();
public int number
= 0;
}
class Student {
public int student_number
;
public String name
;
public int
ID;
public String sex
;
public int score
;
public Student(String name
, int iD
, String sex
, int score
) {
super();
this.name
= name
;
ID = iD
;
this.sex
= sex
;
this.score
= score
;
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public int
getID() {
return ID;
}
public void setID(int iD
) {
ID = iD
;
}
public String
getSex() {
return sex
;
}
public void setSex(String sex
) {
this.sex
= sex
;
}
public int
getScore() {
return score
;
}
public void setScore(int score
) {
this.score
= score
;
}
public String
toString() {
return "学生: 姓名是 " + name
+ ", 学号是 " + ID + ", 性别是 " + sex
+ ", 成绩是 " + score
+"\n";
}
}
class StudentManager implements st
{
public int number
;
public Vector
<Student
> student_list
= new Vector<Student
>();
public void init()
{
String
[] name
= {"张三", "李四", "王五", "范二"};
String
[] sex
= {"male","male","male","male"};
Integer
[] id
= {1909060101,1909060102,1909060103,1909060104};
Integer
[] score
= {100,99,90,95};
for(int i
=0;i
<name
.length
;i
++)
{
Student people
= new Student(name
[i
],id
[i
],sex
[i
],score
[i
] );
student_list
.add(people
);
}
number
= 4;
}
public void sortted_by_score()
{
Collections
.sort(student_list
,new Comparator<Student
>(){
public int
compare(Student p1
, Student p2
) {
if(p2
.getScore()> p1
.getScore()) {
return 1;
}
else if(p1
.getScore()> p2
.getScore()) {
return -1;
}
else return 0;
}
});
printf();
}
public void sortted_by_id()
{
Collections
.sort(student_list
,new Comparator<Student
>(){
public int
compare(Student p1
, Student p2
) {
if(p2
.getID()> p1
.getID()) {
return -1;
}
else if(p1
.getID()> p2
.getID()) {
return 1;
}
else return 0;
}
});
}
public void search_id(int id
)
{
for (int i
=0;i
<student_list
.size();i
++)
{
if (student_list
.get(i
).ID==id
)
System
.out
.println("搜索结果为:\n"+student_list
.get(i
));
}
}
public void search_name(String name
)
{
for (int i
=0;i
<student_list
.size();i
++)
{
if(student_list
.get(i
).name
==name
)
System
.out
.println("搜索结果为:\n"+student_list
.get(i
));
}
}
public void add(String name
, int iD
, String sex
, int score
)
{
number
++;
Student student1
= new Student(name
,iD
,sex
,score
);
student_list
.add(student1
);
}
public void printf()
{
for (Student x
:student_list
)
System
.out
.println(x
);
}
public void delete(int id
)
{
number
--;
for (int i
=0;i
<student_list
.size();i
++)
{
if (student_list
.get(i
).ID==id
)
{
System
.out
.println(student_list
.get(i
)+"上面学生信息已被删除");
student_list
.remove(student_list
.get(i
));
}
}
}
}
public class Student_Manage {
public static void main(String
[] args
) {
StudentManager people
= new StudentManager();
people
.init();
while (true)
{
System
.out
.println("请输入你的操作\n添加信息请按1,删除信息请按2,查看当前信息请按3" +
"统计信息请按4,搜索学生请按5,退出当前系统请按6");
System
.out
.println("此时学生有:"+people
.number
+"个");
try {
Scanner input
=new Scanner(System
.in);
int butt
=input
.nextInt();
if (butt
==1)
{
System
.out
.println("请依次输入学生的姓名,学号,性别,成绩,以回车分割" +
"回车视为输入结束");
String name
= input
.next();
int id
= input
.nextInt();
String sex
= input
.next();
int score
= input
.nextInt();
people
.add(name
,id
,sex
,score
);
System
.out
.println("添加完毕");
continue;
}
if (butt
==2)
{
System
.out
.println("请输入要删除的学生的学号");
int id
= input
.nextInt();
people
.delete(id
);
continue;
}
if (butt
==3)
{
people
.printf();
continue;
}
if (butt
==4)
{
System
.out
.println("按成绩排序请按1,按学号排序请按2");
int k
= input
.nextInt();
if (k
==1)
{
people
.sortted_by_score();
}
else
people
.sortted_by_id();
continue;
}
if (butt
==5)
{
System
.out
.println("1为按学号搜索,2为按姓名搜索");
int information
= input
.nextInt();
if(information
==1)
{
System
.out
.println("请输入搜索的学号\n");
try {
int id1
= input
.nextInt();
people
.search_id(id1
);
}
catch (Exception e
){
System
.out
.println("错误为:"+e
);
System
.out
.println("程序继续运行");
continue;
}
}
else
if(information
==2)
{
System
.out
.println("请输入待搜索的姓名:");
try {
String name1
= input
.next();
people
.search_name(name1
);
}
catch (Exception e
){
System
.out
.println("错误为:"+e
);
System
.out
.println("程序继续运行\n");
continue;
}
}
else
continue;
}
if (butt
==6)
{
break;
}
}
catch (Exception e
){
System
.out
.println("错误为:"+e
);
continue;
}
}
}
}
转载请注明原文地址: https://lol.8miu.com/read-24313.html