学生信息的添加与查询(Java集合框架)

学生信息的添加与查询

Time Limit: 1000MS  Memory Limit: 65536KB

Problem Description

设计一个学生添加和查询的系统,从键盘读入学生的数据,然后再从屏幕显示出来。

Input

第一行有2个整数N和M,其中:N——学生数量,M——学生属性数量;
第二行有M个字符串,表示学生的属性名称,其中第1个属性id表示关键字;其中各字段属性的数据类型是确定的。
接下来有N行M列数据,分别表示学生各种属性的值,关键字相同的记录代表一个学生(后来读入的信息覆盖前面读入数据)

Output

输出所有学生的属性及数据。(每行的列数据之间用‘\t’进行分隔)

Example Input

5 4
id name birthday score
0001 Mike 1990-05-20 98.5
0002 John 1992-05-20 67
0003 Hill 1994-05-02 36.5
0004 Christ 1996-05-20 86.5
0001 Jack 1998-05-20 96

Example Output

id:0001	name:Jack	birthday:1998_5_20	score:96.0
id:0002	name:John	birthday:1992_5_20	score:67.0
id:0003	name:Hill	birthday:1994_5_2	score:36.5
id:0004	name:Christ	birthday:1996_5_20	score:86.5

Hint


Author

zhouxq

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
//import java.util.*;
//import java.text.*; //可以直接使用这两个简单的导入包

class Student {
    private String id;  //id一定要是String型,Integer提交错误
    private String name;
    private String birthday;
    private double score;

    public Student(String id, String name, String birthday, double score) {
        this.id = id;
        this.name = name;
        this.birthday = birthday;
        this.score = score;
    }

    public String getId(){
        return id;
    }

    public String toString() {
        //重写toString方法
        return "id:"+id+"\tname:"+name+"\tbirthday:"+DateUtil.dateConvert(birthday)
                +"\tscore:"+String.format("%.1f",score);
    }

}

class DateUtil {
    //时间转换的类
    public static String dateConvert(String birthday) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy_M_dd");
        String st = null;
        try {
            st = sdf2.format(sdf1.parse(birthday));
            //只需要在输出时将输入的时间的格式转换为规定输出的格式即可
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return st;
    }
}

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        //读入学生属性
        String [] fieldName = new String[m];
        for ( int i =0; i < m; i++ ) {
            fieldName[i] = in.next();
        }
        HashMap<String,Student> hashMap = new HashMap<String,Student>();
        for ( int i = 0; i < n; i++ ) {
            Student student = new Student(in.next(),in.next(),in.next(),in.nextDouble());
            hashMap.put(student.getId(),student);
        }
        //将hashMap的所有的键值放入list中,方便排序
        List<String> list = new ArrayList<String>(hashMap.keySet());
        //对id进行排序
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        //for-each循环进行输出操作
        for ( String id : list ) {
            System.out.println(hashMap.get(id));
        }
    }
}


猜你喜欢

转载自blog.csdn.net/youth_mr6/article/details/70293427