/*
* 异常练习:判断qq是否合法:
要求:1.首字母不能是0 2.全是数字 3.位数在5-13位
要求:使用异常求解
*/
package com.qianfeng.zy;
import java.util.Scanner;
public class Day11WorksT1Method2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个qq号:");
String a = sc.nextLine();
QQ qq = new QQ(a);
try {
qq.heFa(a);
System.out.println("qq合法");
} catch (WeiLingException e) {
e.printStackTrace();
} catch (ShuZiException e) {
e.printStackTrace();
} catch (WeiShuException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class WeiLingException extends Exception {
public WeiLingException() {
}
public WeiLingException(String message) {
super(message);
}
}
class ShuZiException extends Exception {
public ShuZiException() {
}
public ShuZiException(String message) {
super(message);
}
}
class WeiShuException extends Exception {
public WeiShuException() {
}
public WeiShuException(String message) {
super(message);
}
}
class QQ{
String Q;
public QQ() {
super();
}
public QQ(String q) {
super();
Q = q;
}
public String getQ() {
return Q;
}
public void setQ(String q) {
Q = q;
}
public boolean arr(String Q){
for (int i = 0; i < Q.length(); i++) {
if (Q.charAt(i)<'0' || Q.charAt(i)>'9') {
return false;
}
}
return true;
}
public void heFa(String Q) throws WeiLingException, ShuZiException, WeiShuException
{
if(Q.substring(0, 1).equals("0")){
throw new WeiLingException("qq不合法:首字母不能为0");
}
if(!arr(Q)){
throw new ShuZiException("qq不合法:应全为数字");
}
if (Q.length()<5 || Q.length()>13) {
throw new WeiShuException("qq不合法:应为5-13位");
}
}
}