java 实现的电话号码查询程序 优化版(课程设计)

試著忘記壹切 提交于 2020-03-05 00:58:04

本程序可查询3位、5位、7位、8位、11位的电话号码,并且能够分辨出其号码是电信、移动、联通、卫星、座机、还有地址、省、区市。这个程序的代码和前几天发的那篇博客,部分代码进行了优化。

号码号段编辑
中国移动号段:134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、172、178、182、183、184、187、188、195、197、198
中国联通号段:130、131、132、145、155、156、166、176、185、186、196
中国电信号段:133、149、153 、180 、181 、189、173、177、190、191、193、199
中国广电号段:192
虚拟运营商号段:
联通:167、1704、1707、1708、1709、171
电信:162、1700、1701、1702
移动:165、1705、1706
物联网号段:
联通:1400、146
电信:1410
移动:1440、148
卫星电话号段:
电信:1349、1740(0-5)

package phonemuber;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.io.DataInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class Phonenuber extends JFrame implements ActionListener {

    JPanel jpN;
    JPanel jpS;
    JLabel jLabel;
    JTextField jTextField;
    JButton jButton;
    JTextArea jTextArea;

    Phonenuber() {
        jpN = new JPanel();
        jpS = new JPanel();
        jLabel = new JLabel("请输入电话号码");
        jTextField = new JTextField(20);
        jButton = new JButton("查询");
        jTextArea = new JTextArea(50, 50);
        jpN.add(jLabel);
        jpN.add(jTextField);
        jpN.add(jButton);
        jpS.add(jTextArea);
        jButton.addActionListener(this);
        this.add(jpN, BorderLayout.NORTH);
        this.add(jpS, BorderLayout.CENTER);
        this.setTitle("电话号码检测程序");
        this.setVisible(true);
        this.setBounds(400, 200, 800, 600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton) {
            String regex = "^((13[0-9])|(14[5,7,9])|(15[0-3][5-9])|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
            String phoneNumber = jTextField.getText().replace(" ", "");
            boolean isNumber=isInteger(phoneNumber);
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phoneNumber);
            boolean isMatch = m.matches();
            if (isNumber){
                if (phoneNumber.length() >= 3) {
                    if (phoneNumber.length() == 11) {
                        if (isMatch) {
                            String result = elevenValidate(phoneNumber);
                            jTextArea.setText("手机号码  "+result+""+getCityUrl(phoneNumber));
                        } else {
                            jTextArea.setText("请输入正确的电话号码");
                        }
                    } else if (phoneNumber.length() == 3) {
                        jTextArea.setText(threeValidate(phoneNumber));
                    } else if (phoneNumber.length() == 5) {

                        jTextArea.setText(fiveValidate(phoneNumber));
                    }else if (phoneNumber.length() == 7||phoneNumber.length() == 8) {

                        jTextArea.setText(sevenAndEightValidate(phoneNumber));
                    } else if (phoneNumber.length() == 10) {

                        jTextArea.setText("服务电话号码");
                    }else {
                        jTextArea.setText("请输入正确的电话号码");
                    }

                } else {
                    jTextArea.setText("请输入正确的电话号码");
                }
            }else{
                jTextArea.setText("请输入字符为数字的正确的电话号码");
            }
        }
    }
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }
    public  String getCityUrl(String mobile) {
        String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm";
        StringBuilder sb = new StringBuilder(url);
        Map<String, String> params = new HashMap<String, String>();
        params.put("tel", mobile);
     /*   params.put("status","1");//固定值
        params.put("modeltype", "7");//固定值*/
        String result = GetPostUrl(sb.toString(), params, "GET",null, 0, 0);

        return result;
    }

    public static String GetPostUrl(String sendUrl, Map<String, String> params, String sendType, String charset,
                                    int repeat_request_count, int repeat_request_max_count) {
        URL url = null;
        HttpURLConnection httpurlconnection = null;
        try {
            // 构建请求参数
            StringBuffer paramSb = new StringBuffer();
            if (params != null) {
                for (java.util.Map.Entry<String, String> e : params.entrySet()) {
                    paramSb.append(e.getKey());
                    paramSb.append("=");
                    // 将参数值urlEncode编码,防止传递中乱码
                    paramSb.append(URLEncoder.encode(e.getValue(), "UTF-8"));
                    paramSb.append("&");
                }
                paramSb.substring(0, paramSb.length() - 1);
            }
            url = new URL(sendUrl + "?" + paramSb.toString());
            httpurlconnection = (HttpURLConnection) url.openConnection();
            httpurlconnection.setRequestMethod("GET");
            httpurlconnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpurlconnection.setDoInput(true);
            httpurlconnection.setDoOutput(true);

            // 设置http请求超时时间30000毫秒(30秒)
            httpurlconnection.setConnectTimeout(30000);
            httpurlconnection.setReadTimeout(30000);
            httpurlconnection.setUseCaches(true);
            int code = httpurlconnection.getResponseCode();
            if (code == 200) {
                DataInputStream in = new DataInputStream(httpurlconnection.getInputStream());
                int len = in.available();
                byte[] by = new byte[len];
                in.readFully(by);
                String rev = new String(by, "GBK");

                in.close();
                String arr=rev.toString().split("\t")[0];
                String city=arr.split(",")[2].replace("catName","运营商")+arr.split(",")[1].replace("province","归属地");
                return city;
            } else {
                // http 请求返回非 200状态时处理
                return "<?xml version=\"1.0\" encoding=\"utf-8\" ?><error>发送第三方请求失败</error>";
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpurlconnection != null) {
                httpurlconnection.disconnect();
            }
        }
        return null;
    }

    public String sevenAndEightValidate(String phoneNumber){
        String result = "座机号码   ";
        return result;
    }

    public String fiveValidate(String phoneNumber) {
        String result = "服务电话号码   ";
        switch(phoneNumber){
            case "12110":
                result +="公安短信报警号码";
                break;
            case "11185":
                result +="邮政客户服务电话";
                break;
            case "12348":
                result +="全国法律服务热线";
                break;
            case "12395":
                result +="水上遇险求救电话";
                break;
            case "12121":
                result +="气象服务电话";
                break;
            case "12117":
                result +="报时服务电话";
                break;
            case "95119":
                result +="森林火警电话";
                break;
            case "12306":
                result +="全国铁路客服中心";
                break;

            case "95598":
                result +="电力系统客服电话";
                break;
            case "12315":
                result +="消费者投诉举报专线电话";
                break;
            case "12365":
                result +="质量监督电话";
                break;
            case "12369":
                result +="环保局监督电话";
                break;
            case "12345":
                result +="政府公益服务接入网";
                break;
            case "12318":
                result +="文化市场统一举报电话";
                break;
            case "12358":
                result +="价格监督举报电话";
                break;
            case "12310":
                result +="机构编制违规举报热线";
                break;
            case "12333":
                result +="民工维权热线电话";
                break;
            case "12320":
                result +="公共卫生环境投诉";
                break;
            case "10000":
                result +="中国电信客户服务热线";
                break;
            case "10086":
                result +="中国移动客服热线";
                break;
            case "17911":
                result +="中国联通IP号码";
                break;
            case "10050":
                result +="铁通客户服务";
                break;
            case "10010":
                result +="中国联通客服热线";
                break;
            case "17900":
                result +="中国电信IP电话卡";
                break;
            case "17951":
                result +="中国移动IP号码";
                break;
            case "95555":
                result +="招商银行";
                break;
            case "95566":
                result +="中国银行";
                break;
            case "95533":
                result +="建设银行";
                break;
            case "95588":
                result +="工商银行";
                break;
            case "95558":
                result +="中信银行";
                break;
            case "95528":
                result +="浦发银行";
                break;
            case "95501":
                result +="深发银行";
                break;
            case "95599":
                result +="农业银行";
                break;

            case "95568":
                result +="民生银行";
                break;
            case "95595":
                result +="光大银行";
                break;
            case "95559":
                result +="交通银行";
                break;
            case "95508":
                result +="广发银行";
                break;
            case "95577":
                result +="华夏银行";
                break;
            case "95561":
                result +="兴业银行";
                break;
            case "95543":
                result +="申通快递";
                break;
            case "95546":
                result +="韵达快递";
                break;
            case "95311":
                result +="中通快递";
                break;
            case "95338":
                result +="顺丰速运";
                break;
            case "95554":
                result +="圆通快递";
                break;
            case "95353":
                result +="德邦物流";
                break;
            case "95500":
                result +="太平洋保险";
                break;
            case "95511":
                result +="平安保险";
                break;
            case "95515":
                result +="合众人寿保险";
                break;
            case "95519":
                result +="中国人寿保险";
                break;
            case "95522":
                result +="合众人寿保险";
                break;
            case "95567":
                result +="新华人寿保险";
                break;
            case "95589":
                result +="太平保险";
                break;
            case "95596":
                result +="民生人寿保险";
                break;
            case "95510":
                result +="阳光保险";
                break;
            case "95512":
                result +="平安产险";
                break;
            case "95518":
                result +="中国人民保险";
                break;
            case "95535":
                result +="生命人寿保险";
                break;
            case "95585":
                result +="中华保险";
                break;
            case "95590":
                result +="大地保险";
                break;
            default:
                result="请输入正确的号码";
        }
        return result;
    }

    public String threeValidate(String phoneNumber) {
        String result = null;
        switch(phoneNumber) {
            case "110":
                result = "紧急电话  报警求助电话";
                break;
            case "120":
                result = "紧急电话  急救中心电话";
                break;
            case "119":
                result = "紧急电话  火警报警电话";
                break;
            case "117":
                result = "公共服务电话  报时服务电话";
                break;
            case "184":
                result = "公共服务电话  国内邮政编码查询电话";
                break;
            case "122":
                result = "紧急电话  交通事故报警电话";
                break;
            case "999":
                result = "紧急电话  红十字会急救台";
                break;
            case "114":
                result = "公共服务电话  电话及长途区号查询";
                break;
            case "121":
                result = "公共服务电话  天气预报查询号码";
                break;
            default:
                result = "请输入正确号码";
        }

        return result;
    }
    public String elevenValidate(String phoneNumber) {
        String result = null;
        String subResult = phoneNumber.substring(0, 3);
        switch (subResult) {
            case "133":
                result = "中国电信";
                break;
            case "149":
                result = "中国电信";
                break;
            case "153":
                result = "中国电信";
                break;
            case "170":
                String subString = phoneNumber.substring(0, 4);
                switch(subString) {
                    case "1700":
                        result = "虚拟运营商 电信";
                        break;
                    case "1701":
                        result = "虚拟运营商 电信";
                        break;
                    case "1702":
                        result = "虚拟运营商 电信";
                        break;
                    case "1703":
                        result = "虚拟运营商 移动";
                        break;
                    case "1705":
                        result = "虚拟运营商 移动";
                        break;
                    case "1706":
                        result = "虚拟运营商 移动";
                        break;
                    case "1704":
                        result = "虚拟运营商 联通";
                        break;
                    case "1707":
                        result = "虚拟运营商 联通";
                        break;
                    case "1708":
                        result = "虚拟运营商 联通";
                        break;
                    case "1709":
                        result = "虚拟运营商 联通";
                        break;
                    default:
                        result = "中国电信";
                        break;
                }
                break;
            case "173":
                result = "中国电信";
                break;
            case "177":
                result = "中国电信";
                break;
            case "180":
                result = "中国电信";
                break;
            case "181":
                result = "中国电信";
                break;
            case "189":
                result = "中国电信";
                break;
            case "199":
                result = "中国电信";
                break;
            case "130":
                result = "中国联通";
                break;
            case "131":
                result = "中国联通";
                break;
            case "132":
                result = "中国联通";
                break;
            case "145":
                result = "中国联通";
                break;
            case "155":
                result = "中国联通";
                break;
            case "156":
                result = "中国联通";
                break;
            case "166":
                result = "中国联通";
                break;
            case "171":
                result = "虚拟运营商 联通";
                break;
            case "175":
                result = "中国联通";
                break;
            case "176":
                result = "中国联通";
                break;
            case "185":
                result = "中国联通";
                break;
            case "186":
                result = "中国联通";
                break;
            case "134":
                result = "中国移动";
                break;
            case "135":
                result = "中国移动";
                break;
            case "136":
                result = "中国移动";
                break;
            case "137":
                result = "中国移动";
                break;
            case "138":
                result = "中国移动";
                break;
            case "139":
                result = "中国移动";
                break;
            case "147":
                result = "中国移动";
                break;
            case "150":
                result = "中国移动";
                break;
            case "151":
                result = "中国移动";
                break;
            case "152":
                result = "中国移动";
                break;
            case "157":
                result = "中国移动";
                break;
            case "158":
                result = "中国移动";
                break;
            case "159":
                result = "中国移动";
                break;
            case "178":
                result = "中国移动";
                break;
            case "182":
                result = "中国移动";
                break;
            case "183":
                result = "中国移动";
                break;
            case "184":
                result = "中国移动";
                break;
            case "187":
                result = "中国移动";
                break;
            case "188":
                result = "中国移动";
                break;
            case "198":
                result = "中国移动";
                break;
        }
        return result;
    }
    public static void main(String[] s) {
        new Phonenuber();
    }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!