Adding and querying student information

Problem Description

Design a system for students to add and query, read the student's data from the keyboard, and then display it from the screen.

Input

The first line has 2 integers N and M, where: N - the number of students, M - the number of student attributes;
the second line has M strings, representing the student's attribute name, of which the first attribute id represents the keyword ; The data type of each field attribute is determined.
Next, there are N rows and M columns of data, which represent the values ​​of various attributes of the student, and the records with the same keyword represent a student (the information read later covers the data read earlier)

Output

Output the attributes and data of all students. (The column data of each row is separated by '\t')

Sample 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

Sample 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

code:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static void main(String[] args) throws ParseException  {
		// TODO Auto-generated method stub
		Scanner reader = new Scanner(System.in);
		
		int n, m;
		n = reader.nextInt();
		m = reader.nextInt();
		Map<String, Student> map = new HashMap<String, Student>();
		reader.nextLine();
		reader.nextLine();
		for(int i = 0;i<n;i++)
		{
			String id = reader.next();
			Student stu = new Student(reader.next(), reader.next(), reader.nextDouble());
			map.put(id, stu);
		}
		Set<String> keySet = map.keySet();
		List<String> list = new ArrayList<String>(keySet);
		Collections.sort(list, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				// TODO Auto-generated method stub
				return o1.compareTo(o2);
			}
		});
		
		for(int i = 0;i<list.size();i++)
		{
			System.out.print("id:"+list.get(i)+"\t");
			System.out.println(map.get(list.get(i)));
		}
		
	}
	
}
class Student
{
	String name, bir;
	double sc;
	public Student(String name, String bir, double sc) {
		super();
		this.name = name;
		this.bir = bir;
		this.sc = sc;
	}
	public String date_change() throws ParseException
	{
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat df2 = new SimpleDateFormat("yyyy_M_d");
		return df2.format(df1.parse(bir));
	}
	public String toString() 
	{
		try {
			return "name:"+name+"\tbirthday:"+date_change()+"\tscore:"+String.format("%.1f", sc);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "error";
	}
	
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325544167&siteId=291194637