学生信息的添加与查询(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.*;

class student{
    String id;
    String name;
    Date birthday;
    float score;
    public void setid(String id) {
        this.id = id;

    }
    public String getid() {
        return this.id;

    }
    public void setname(String name) {
        this.name = name;
    }
    public  String getname() {
        return this.name;
    }
    public Date getBirthday()
    {
        return this.birthday;
    }
    public void setBirthday(Date birthday){
        this.birthday = birthday;
    }
    public void setScore(float score){
        this.score = score;
    }
    public float getscore() {
        return this.score;
    }
}
public class Main {
    public static Date strtobir(String bir) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(bir);
        return date;
    }
    public static String datetostr(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_M_d");
        String strd = sdf.format(date);
        return strd;
    }
    public static void main(String[] args) throws ParseException {
        Scanner input = new Scanner(System.in);
        int n,m;
        n = input.nextInt();
        m = input.nextInt();
        String[] filedname = new String[m];
        for(int i=0;i<m;i++) {
            String s = input.next();
            filedname[i] = s;
        }
        Map<String,student> map = new HashMap<String,student>();
        for(int i=0;i<n;i++){
            student stu = new student();
            stu.setid(input.next());
            stu.setname(input.next());
            String bb = input.next();
            Date dd = strtobir(bb);
            stu.setBirthday(dd);
            stu.setScore(input.nextFloat());
            map.put(stu.getid(),stu);

        }
        List<Map.Entry<String,student>>info = new ArrayList<Map.Entry<String, student>>(map.entrySet());
        Collections.sort(info, new Comparator<Map.Entry<String, student>>() {
            @Override
            public int compare(Map.Entry<String, student> o1, Map.Entry<String, student> o2) {
                return (o1.getKey().toString().compareTo(o2.getKey()));
            }

        });
        for(int i=0;i<info.size();i++) {
            student stu = info.get(i).getValue();
            System.out.print(filedname[0]+":"+stu.getid()+"\t");
            System.out.print(filedname[1]+":"+stu.getname()+"\t");
            System.out.print(filedname[2]+":"+datetostr(stu.getBirthday())+"\t");
            System.out.println(filedname[3]+":"+stu.getscore());

        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值