【java集合框架】SDUT 3360 學生資訊的新增與查詢
阿新 • • 發佈:2018-11-26
Problem Description
設計一個學生新增和查詢的系統,從鍵盤讀入學生的資料,然後再從螢幕顯示出來。
Input
第一行有2個整數N和M,其中:N——學生數量,M——學生屬性數量;
第二行有M個字串,表示學生的屬性名稱,其中第1個屬性id表示關鍵字;其中各欄位屬性的資料型別是確定的。
接下來有N行M列資料,分別表示學生各種屬性的值,關鍵字相同的記錄代表一個學生(後來讀入的資訊覆蓋前面讀入資料)
Output
輸出所有學生的屬性及資料。(每行的列資料之間用‘\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
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; class Student { String id, name, date; double score; public Student(String id, String name, String date, double score) { super(); this.id = id; this.name = name; this.date = date; this.score = score; } String dateConvert(String date) throws ParseException { SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy_M_d"); Date date1 = sdf1.parse(date); return sdf2.format(date1); } @Override public String toString() { String str = null; try { str = "id:" + id + "\tname:" + name + "\tbirthday:" + dateConvert(date) + "\tscore:" + String.format("%.1f", score); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return str; } } public class Mainn { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); Map<String, Student> map = new HashMap<String, Student>(); s.nextLine(); s.nextLine(); for (int i = 1; i <= n; i++) { Student stu = new Student(s.next(), s.next(), s.next(), s.nextDouble()); map.put(stu.id, stu); } Set<String> keySet = map.keySet(); List<String> list = new ArrayList<String>(keySet); Collections.sort(list); for (String id : list) { Student student = map.get(id); System.out.println(student); } s.close(); } }