利用 Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。 如果该年没有举办世界杯,则输出:没有举办世界杯。

利用 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);
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值