SS-Lab3 接收并使用正则表达式验证客户端输入

客户端交互需要做到

  • 提示用户输入什么
  • 接收用户输入
  • 验证输入合法

实现

  • MyReader : 负责与用户的交互
  • MyRegularExpression : 负责检验用户输入

MyReader

readInt - 读取一个满足一定条件的整数

  • 效果
>> useage: 
0. show this usage table
1. set the duty start and end date
2. load duty table from file
3. exit

>> option: s
invalid input
>> option: #
invalid input
>> option: -1
not a valid option id
>> option: 4
not a valid option id
>> option: 1
Please input the [start date] and [end date] in form [yyyy-MM-dd]
start date: 
  • 实现
/**
 * @method readInt
 * @param1 mark if this int value represent an option id
 * @param2 the min option id, only valid when param1 is true
 * @param3 the max option id, only valid when param1 is true
 * @param4 the name of the value to be read, aim to show for user
 * */
public int readInt(boolean readOpt, int firstOpt, int lastOpt, String name) {
    
    
	int opt = -1;
	while(opt == -1) {
    
    
		print(name + ": ");
		if(!scanner.hasNextInt()) {
    
     // if not receive an integer
			print("invalid input\n");
			scanner.next();
			continue;
		}
		opt = scanner.nextInt();
		if(!readOpt) {
    
    
			break;
		}
		if(opt < firstOpt || opt > lastOpt) {
    
    
			print("not a valid option id\n");
			opt = -1;
		}
	}
	return opt;
}

readString - 读取一个通过特定正则验证的字符串

  • 效果
>> Please input the [start date] and [end date] in form [yyyy-MM-dd]
start date: s
start date: 1
start date: #
start date: 21-7-7
start date: 2021.7.7
start date: 2021-12-32
start date: 2021-7-7
start date: 2021-07-07
end date: 
  • 实现
/**
* @method readString
 * @param1 the name of the string to be read, also the name for a regular expression
 * @param2 the message show for user
 * @return an string that match the regular expression
 * */
public String readString(String name, String show) {
    
    
	String string;
	do {
    
    
		string = "";
		print(show + " " + name + ": ");
		while(string.isEmpty()) {
    
    
			string = scanner.nextLine();
		}
	} while(!myRegularExpression.checkAttr(string, name));
	return string;
}

MyRegularExpression

addRule - 向正则集中添加一个正则表达式

  • 实现
/**
 * @method addRule
 * @param1 the name of this rule
 * @param2 a correct regular expression
 * */
public void addRule(String ruleName, String ruleExpression) {
    
    
	rule.put(ruleName, ruleExpression);
}

checkAttr - 检验一个字符串是否和指定的正则匹配

  • 实现
/**
 * @method checkAttr
 * @param1 the string to be check
 * @param2 the name of an regular expression registered in the rule
 * */
public boolean checkAttr(String attrString, String attrName) {
    
    
	Pattern pattern = Pattern.compile(rule.get(attrName));
	Matcher matcher = pattern.matcher(attrString);
	return matcher.matches();
}

客户端使用流程

1、实例化 MyReader 对象
2、实例化 MyRegularExpression 对象,并添加需要接收的数据的正则表达式
3、为 MyReader 设置 MyRegularExpression
4、调用 MyReader 的读写接口,并传入相应参数

设计模式 - 委托

回顾开头提到的两个主要问题

  • 接收用户输入 : App 委托给 MyReader
  • 验证用户输入 : Myreader 委托给 MyRegularExpression

附 - 本实验用到的正则表达式

/* note : clear the indent of the file before use */

public static final String extractEmployeePart = 
		"Employee\\{[\\s]?([A-Za-z]+\\{[A-Za-z]+(\\s[A-Za-z]+)*,[0-9]{3}-[0-9]{4}-[0-9]{4}\\})+\\}";
public static final String extractEmployeeInfo = 
		"[A-Za-z]+\\{[A-Za-z]+(\\s[A-Za-z]+)*,[0-9]{3}-[0-9]{4}-[0-9]{4}\\}";
public static final String extractEmployeeName = 
		"[A-Za-z]+\\{";
public static final String extractEmployeeDuty = 
		"\\{[A-Za-z]+(\\s[A-Za-z]+)*,";
public static final String extractEmployeePhone = 
		",[0-9]{3}-[0-9]{4}-[0-9]{4}";

public static final String extractPeriodPart = 
		"Period\\{[\\s]?[0-9]{4}-[0-9]{2}-[0-9]{2},[0-9]{4}-[0-9]{2}-[0-9]{2}\\}";
public static final String extractPeriodInfo = 
		"\\{[0-9]{4}-[0-9]{2}-[0-9]{2},[0-9]{4}-[0-9]{2}-[0-9]{2}\\}";
public static final String extractStartDate = 
		"\\{[0-9]{4}-[0-9]{2}-[0-9]{2}";
public static final String extractEndDate = 
		",[0-9]{4}-[0-9]{2}-[0-9]{2}";

public static final String extractRosterPart = 
		"Roster\\{[\\s]?([A-Za-z]+\\{[0-9]{4}-[0-9]{2}-[0-9]{2},[0-9]{4}-[0-9]{2}-[0-9]{2}\\})+\\}";
public static final String extractRosterInfo =
		"[A-Za-z]+\\{[0-9]{4}-[0-9]{2}-[0-9]{2},[0-9]{4}-[0-9]{2}-[0-9]{2}\\}";

/* use to check the input */

public static final String DATE = 
		"(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";

public static final String NAME = 
		"[A-Za-z]+";

public static final String DUTY = 
		"[A-Za-z]+(\\s[A-Za-z]+)*";

public static final String PHONE = 
		"[0-9]{3}-[0-9]{4}-[0-9]{4}";

public static final String FILE = 
		"[\\s\\S]*";

public static final String ID =
		"[0-9]+";

public static final String CLASSROOM = 
		"[A-Z]-[0-9]{3}";

public static final String HOURSOFWEEK =
		"([1-9][02468]$)|([2468])";

public static final String EXECUTETIME = 
		"[1-9][0-9]*";

Guess you like

Origin blog.csdn.net/weixin_39578432/article/details/118551601