利用 Map,完成下面的功能:
从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。
如果该年没有举办世界杯,则输出:没有举办世界杯。
冠军类
public class GuanJun {
/**
* 届数
*/
private Integer jie;
private Integer year;
/**
* 冠军国家的而名字
*/
private String countryName;
public GuanJun() {
}
public GuanJun(Integer jie, Integer year, String countryName) {
this.jie = jie;
this.year = year;
this.countryName = countryName;
}
public Integer getJie() {
return jie;
}
public void setJie(Integer jie) {
this.jie = jie;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
@Override
public String toString() {
return "GuanJun{" +
"jie=" + jie +
", year=" + year +
", countryName='" + countryName + '\'' +
'}';
}
}
世界杯
public class WorldCup {
/**
* 用来存放所有冠军的容器
*/
private Map<Integer, GuanJun> maps = new HashMap<>(16);
public WorldCup() {
// 初始化数据
maps.put(2006,new GuanJun(18,2006, "意大利"));
maps.put(2002, new GuanJun(17,2002, "巴西"));
maps.put(1998, new GuanJun(16,1998, "法国"));
maps.put(1994, new GuanJun(15,1994, "巴西"));
maps.put(1990, new GuanJun(14,1990, "德国"));
maps.put(1986, new GuanJun(13,1986, "阿根廷"));
maps.put(1982, new GuanJun(12,1982, "意大利"));
maps.put(1978, new GuanJun(11,1978, "阿根廷"));
maps.put(1974, new GuanJun(10,1974, "意大利"));
maps.put(1970, new GuanJun(9,1970, "德国"));
maps.put(1966, new GuanJun(8,1966, "巴西"));
maps.put(1962, new GuanJun(7,1962, "英格兰"));
maps.put(1958, new GuanJun(6,1958, "巴西"));
maps.put(1954, new GuanJun(5,1954, "德国"));
maps.put(1950, new GuanJun(4,1950, "乌拉圭"));
maps.put(1938, new GuanJun(3,1938, "意大利"));
maps.put(1934, new GuanJun(2,1934, "意大利"));
maps.put(1930, new GuanJun(1,1930, "乌拉圭"));
}
/**
* 通过年份获取冠军
* @param year : 年份
* @return
*/
public GuanJun getGuanJunByYear(Integer year){
if(year <= 0){
return null;
}
// 先判断是否有这个key
if (maps.containsKey(year)) {
return maps.get(year);
}
return null;
}
/**
* 通过国家的名字去获取冠军的年份
* @param countryName : 国家的名字
* @return
*/
public List<Integer> getYearsByCountry(String countryName){
if (countryName == null || countryName.isEmpty()) {
return null;
}
// 准备一个装年份的容器
List<Integer> years = new ArrayList<>();
// 把maps的值全部拿到
Collection<GuanJun> values = maps.values();
// 遍历values
for (GuanJun value : values) {
if (value.getCountryName().equals(countryName)){
// 有冠军
years.add(value.getYear());
}
}
return years;
}
}
测试类
import java.util.List;
import java.util.Scanner;
public class MyTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
WorldCup worldCup = new WorldCup();
/*System.out.println("请输入年份:");
String year = scanner.next();
// 这个地方需要用一个正则的校验
GuanJun guanJun = worldCup.getGuanJunByYear(Integer.valueOf(year));
if (guanJun == null) {
System.out.println(year+"这年没有冠军!");
}else{
System.out.println(year+"这年的冠军是:"+guanJun.getCountryName());
}*/
System.out.println("请输入国家的而名字:");
String countryName = scanner.next();
List<Integer> years = worldCup.getYearsByCountry(countryName);
if (years == null || years.size() == 0) {
System.out.println("这个国家没有得过冠军");
}else{
System.out.println(years);
}
}
}