【JDBC】ScannerTools类:校验输入

package com.hzyc.design;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;

public class ScannerTools {
    int validInteger() {
        int valid = 0;
        while (true) {
            try {
                Scanner sc = new Scanner(System.in);
                System.out.println("\n(请输入一个整数:)");
                valid = sc.nextInt();
                //输入的是整数:那么就直接跳出,返回一个验证的值。
                break;
            } catch (Exception e) {
                System.out.println("您的输入有误,请重新输入一个整数!");
                e.printStackTrace();
            }
        }
        return valid;
    }

    String validString() {
        String valid = "";
        while (true) {
            try {
                Scanner sc = new Scanner(System.in);
                valid = sc.next();
                break;
            } catch (Exception e) {
                System.out.println("您的输入有误,请重新输入!");
                e.printStackTrace();
            }
        }
        return valid;
    }

    /**
     * 用于输入1-指定的数字
     * */
    int validIntegerIn(int num) {
        ScannerTools sc = new ScannerTools();
        while (true) {
            try {
                int scan = sc.validInteger();
                if (0 < scan && scan <= num) {
                    return scan;
                }
            } catch (Exception e) {
                System.out.println("请重新输入1 —— " + num + "范围内的数字");
                e.printStackTrace();
            }
        }
    }

    String validDate() {
        String valid = "";
        boolean continueornot = true;
        while (continueornot) {
            Scanner sc = new Scanner(System.in);
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            System.out.println("请按指定的格式输入日期:" +
                    "例如:2020年10月10日【20201010】");
            String validMess = sc.next();
            try {
                if (validMess == null || validMess.length() == 0) {
                    continueornot = true;
                }
                valid = validMess.replaceAll("[/\\- ]", "");
                Date date = format.parse(valid);
                if (format.format(date).equals(valid)) {
                    break;
                }
                System.out.println("您的输入不合法,该日期的火车票暂不售卖。请重新输入日期信息……\n");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return valid;
    }

    String validVipSeat() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            switch (sc.next()) {
                case "sw":
                    return "sw";
                case "yd":
                    return "yd";
                case "ed":
                    return "ed";
                default:
                    System.out.println("请重新输入您要选择的坐席级别");
            }
        }
    }
    String validSeat() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            switch (sc.next()) {
                case "rw":
                    return "rw";
                case "yw":
                    return "yw";
                case "yz":
                    return "yz";
                default:
                    System.out.println("请重新输入您要选择的坐席级别");
            }
        }
    }

    String getStation() {
        String[] province = {"广东省",  "河南省","吉林省", "辽宁省",  "黑龙江省", "江苏省", "浙江省", "安徽省",
                "福建省", "江西省", "山东省", "山西省", "湖北省", "湖南省","河北省", "海南省", "四川省",
                "贵州省", "云南省", "陕西省", "甘肃省", "青海省", "广西省", "宁夏", "行政/直辖", "西藏", "内蒙古", "新疆维吾尔"};


        System.out.println("\n**************选择省份/直辖市*************");
        for (int i = 0; i < province.length; i++) {
            System.out.print("(" + (i + 1) + ")" + province[i] + "\t");
            if ((i + 1) % 5 == 0) {
                System.out.println("");
            }
        }
//        输入的是几就给他展示多少,然后记得返回地址就行
        ArrayList<String> city = new ArrayList<>();
        switch (validIntegerIn(28)) {
            case 1:
                String[] guangdong = {"广州","深圳","福田", "肇庆", "东莞",  "佛山", "雷州", "茂名", "南江口", "南雄",
                        "湛江", "韶关",  "坪石", "源潭", "湛江", "潮汕", "虎门"};
                city.addAll(Arrays.asList(guangdong));
                break;
            case 2:
                String[] 河南 = {"灵宝", "洛阳", "南阳", "信阳", "商丘", "许昌", "郑州"};
                city.addAll(Arrays.asList(河南));
                break;
            case 3:
                String[] jilin = {"长春", "吉林"};
                city.addAll(Arrays.asList(jilin));
                break;
            case 4:
                String[] liaoning = {"大连", "盖州", "金州", "锦州", "沈阳", "铁岭"};
                city.addAll(Arrays.asList(liaoning));
                break;
            case 5:
                String[] 黑龙江 = {"哈尔滨", "佳木斯", "绥化", ""};
                city.addAll(Arrays.asList(黑龙江));
                break;
            case 6:
                String[] jiangsu = {"南京"};
                city.addAll(Arrays.asList(jiangsu));
                break;
            case 7:
                String[] 浙江 = {"海宁", "杭州", "嘉善", "金华", "义乌", "诸暨"};
                city.addAll(Arrays.asList(浙江));
                break;
            case 8:
                String[] anhui = {"合肥","亳州"};
                city.addAll(Arrays.asList(anhui));
                break;
            case 9:
                String[] fujian = {"福州", "莆田", "泉州", "厦门"};
                city.addAll(Arrays.asList(fujian));
                break;
            case 10:
                String[] jiangxi = {"大余", "赣州", "吉安", "井冈山", "九江", "南昌", "上饶"};
                city.addAll(Arrays.asList(jiangxi));
                break;
            case 11:
                String[] shandong = {"菏泽", "聊城"};
                city.addAll(Arrays.asList(shandong));
                break;
            case 12:
                String[] shanxi = {"大同"};
                city.addAll(Arrays.asList(shanxi));
                break;
            case 13:
                String[] hubei = {"定州", "衡水", "武汉", "襄阳", "咸宁", "武昌", "赤壁"};
                city.addAll(Arrays.asList(hubei));
                break;
            case 14:
                String[] hunan = {"安江", "长沙", "郴州", "洞口", "衡阳", "隆回", "娄底", "邵阳", "湘潭",
                        "芷江", "岳阳"};
                city.addAll(Arrays.asList(hunan));
                break;
            case 15:
                String[] 河北 = {"保定", "定州", "邯郸", "秦皇", "任丘", "山海关", "张家口", "石家庄"};
                city.addAll(Arrays.asList(河北));
                break;
            case 16:
                String[] hainan = {"东方", "海口", "三亚"};
                city.addAll(Arrays.asList(hainan));
                break;
            case 17:
                String[] sichuan = {"成都", "广元", "乐山", "南充", "唐山", "宜宾"};
                city.addAll(Arrays.asList(sichuan));
                break;
            case 18:
                String[] 贵州 = {"安顺", "贵阳", "凯里", "铜仁"};
                city.addAll(Arrays.asList(贵州));
                break;
            case 19:
                String[] yunnan = {"六盘水"};
                city.addAll(Arrays.asList(yunnan));
                break;
            case 20:
                String[] 陕西 = {"宝鸡", "西安", "渭南"};
                city.addAll(Arrays.asList(陕西));
                break;
            case 21:
                String[] 甘肃 = {"安顺", "嘉峪关", "金昌", "兰州", "柳园", "陇南", "天水", "武威", "张掖"};
                city.addAll(Arrays.asList(甘肃));
                break;
            case 22:
                String[] 青海 = {"西宁"};
                city.addAll(Arrays.asList(青海));
                break;
            case 23:
                String[] 广西 = {"桂林", "贵港", "南宁", "平南", "梧州", "玉林"};
                city.addAll(Arrays.asList(广西));
                break;
            case 24:
                String[] 宁夏 = {"格尔木", "安多"};
                city.addAll(Arrays.asList(宁夏));
                break;
            case 25:
                String[] Municipality = {"北京", "天津", "上海", "重庆", "涪陵", "秀山", "武隆"};
                city.addAll(Arrays.asList(Municipality));
                break;
            case 26:
                String[] 新疆 = {"德令哈", "格尔木", "哈密", "安多", "吐鲁番", "乌鲁木齐"};
                city.addAll(Arrays.asList(新疆));
                break;
            case 27:
                String[] 西藏 = {"拉萨", "那曲"};
                city.addAll(Arrays.asList(西藏));
                break;
            case 28:
                String[] 内蒙古 = {"集宁"};
                city.addAll(Arrays.asList(内蒙古));
                break;
        }
        for (int i = 0; i < city.size(); i++) {
            System.out.print("(" + (i + 1) + ")" + city.get(i) + "市(县)\t");
            if ((i + 1) % 5 == 0) {
                System.out.println("");
            }
        }

        return city.get(validInteger() - 1);
    }

//    public static void main(String[] args) {
//        ScannerTools sc = new ScannerTools();
//        sc.getStation();
//    }
}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页