7-3 sdut-Colleciton-5 学生信息的添加与查询(HashMap) (15 分)
设计一个学生信息添加和查询的系统,从键盘读入学生的数据,然后通过屏幕进行显示。
输入格式:
第一行有1个整数N,表示学生数量;
接下来有N行学生数据,分别表示学生的id(编号)、name(姓名)、birthday(生日)、score(成绩)属性的值,关键字(id)相同的记录代表同一个学生(如果id相同,后来读入的学生信息会覆盖已有的学生信息)
输出格式:
按照id从小到大的顺序,输出所有学生的属性名称及属性值,其中score(成绩)保留1位有效数字,具体输出格式见输出样例。
提示:可以利用Student类的toString()方法来实现类对象属性的展示。
输入样例:
5
0001 Mike 1990-05-20 98.5
0002 John 1992-05-20 67
0003 Hill 1994-05-20 36.5
0004 Christ 1996-05-20 86.5
0001 Jack 1998-05-20 96
输出样例:
Student [id=0001, name=Jack, birthday=1998年05月20日, score=96.0]
Student [id=0002, name=John, birthday=1992年05月20日, score=67.0]
Student [id=0003, name=Hill, birthday=1994年05月20日, score=36.5]
Student [id=0004, name=Christ, birthday=1996年05月20日, score=86.5]
代码
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
import javax.xml.crypto.Data;
class Student implements Comparable<Student> {
public String id;
public String name;
// public Data bir;
public int y;
public int m;
public int d;
public double score;
public String mm;
public String dd;
public Student(String id, String name, int y, int m, int d, double score) {
super();
this.id = id;
this.name = name;
this.y = y;
this.m = m;
this.d = d;
this.score = score;
if (m < 10) {
mm = "0" + m;
} else {
mm = String.valueOf(m);
}
if (d < 10) {
dd = "0" + d;
} else {
dd = String.valueOf(d);
}
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", birthday=" + y + "年" + mm + "月"
+ dd + "日, score=" + String.format("%.1f", score) + "]";
}
public int compareTo(Student s1) {
return this.id.compareTo(s1.id);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
LinkedList<Student> list = new LinkedList<>();
for (int i = 0; i < num; i++) {
String id = null;
String name = null;
int y = 0;
int m = 0;
int d = 0;
double score = 0;
for (int j = 0; j < 4; j++) {
String str = sc.next();
if (j == 0) {
id = str;
}
if (j == 1) {
name = str;
}
if (j == 2) {
String[] strr = str.split("-");
for (int k = 0; k < 3; k++) {
if (k == 0) {
y = Integer.parseInt(strr[0]);
}
if (k == 1) {
m = Integer.parseInt(strr[1]);
}
if (k == 2) {
d = Integer.parseInt(strr[2]);
}
}
}
if (j == 3) {
score = Double.valueOf(str);
}
}
Student stu = new Student(id, name, y, m, d, score);
if (list.contains(stu)) {
int indx = 0;
for (int g = 0; g < list.size(); g++) {
if (stu.id.equals(list.get(g).id)) {
indx = g;
break;
}
}
list.set(indx, stu);
} else {
list.add(stu);
}
}
Collections.sort(list);
for (Student s : list) {
System.out.println(s.toString());
}
}
}