package jp.co.jid.collectionSample;

import java.util.HashMap;
import java.util.LinkedHashMap;

public class GradesData02 {

	public static void main(String[] args) {
		// 01の解答
		System.out.println("-- 【演習⑩】01 --");
		HashMap<String, Integer> map1 = new HashMap<String, Integer>();
		Data02 data02 = new Data02();
		String[] nameArray = data02.getNames();
		int[] pointArray = data02.getPoints();
		for (int i = 0; i < nameArray.length; i++) {
			String name = nameArray[i];
			if (!map1.containsKey(name) || map1.get(name).intValue() < pointArray[i]) {
				// まだ受講結果がない、もしくは受講結果があっても過去の得点が低い場合
				map1.put(name, Integer.valueOf(pointArray[i]));
			}
		}
		for (String name : map1.keySet()) {
			System.out.println(name + " : " + map1.get(name));
		}

		// 02の解答
		System.out.println("-- 【演習⑩】02 --");
		LinkedHashMap<String, Integer> map2 = new LinkedHashMap<String, Integer>();
		while (!map1.isEmpty()) {
			String maxPointerName = "";
			Integer maxPoint = Integer.valueOf(0);
			for (String name : map1.keySet()) {
				Integer point = map1.get(name);
				if (point.intValue() >= maxPoint) {
					maxPointerName = name;
					maxPoint = point;
				}
			}
			map2.put(maxPointerName, maxPoint);
			map1.remove(maxPointerName);
		}
		for (String name : map2.keySet()) {
			System.out.println(name + " : " + map2.get(name));
		}
	}

}
