例如写一个排班管理系统
首先创建一个Scanner,从System.in读入
然后写一个命令行解析方法,将scanner传给它
先打印">>>",等待用户输入
读一个新行,获得字符串,用一个新的Scanner解析它,
根据输入的第一个参数解析命令,调用对应的命令的方法,将scanner传给具体命令方法
private static int commander(Scanner scanner,
Set<Employee> employees,
DutyIntervalSet<Employee> intervalSet){
String buf;
String command;
Scanner parseLine;
if(method == 0) {
System.out.print(">>>");
}
buf = scanner.nextLine();
//System.out.println(buf);
parseLine = new Scanner(buf).useDelimiter("[:,]");
if (parseLine.hasNext()){
command = parseLine.next();
switch (command){
case "help":{
help(parseLine);
break;
}
case "add":{
add(parseLine, employees);
break;
}
case "rm":{
rm(parseLine, employees, intervalSet);
break;
}
case "del":{
del(parseLine, employees, intervalSet);
break;
}
case "insert":{
insert(parseLine, employees, intervalSet);
break;
}
case "auto":{
auto(parseLine, employees, intervalSet);
break;
}
case "full":{
full(parseLine, intervalSet);
break;
}
case "clear":{
clear(parseLine, intervalSet);
break;
}
case "view":{
view(parseLine, intervalSet);
break;
}
case "quit":{
return -1;
}
default:{
System.out.println(" Invalid command!\nInput \"help\" to get prompt message");
}
}
}
return 0;
}
如果命令是quit,返回-1,表示退出,其他命令返回0,表示继续输入
具体的命令方法如下:
/**
* command rm
* @param scanner the input flow
* @param employees the employee set
* @param intervalSet the interval set
*/
private static void rm(Scanner scanner,
Set<Employee> employees,
DutyIntervalSet<Employee> intervalSet){
String name;
if(scanner.hasNext())
name = scanner.next();
else {
System.out.println(" Invalid command!\n Input \"help\" to get prompt message");
return;
}
if(scanner.hasNext()){
System.out.println(" Invalid command!\n Input \"help\" to get prompt message");
return;
}
Employee employeePoint = null;
for(Employee employee:employees){
if (employee.getName().equals(name)){
employeePoint = employee;
break;
}
}
if(employeePoint == null){
System.out.println(" " + name + " is not exist!");
return;
}
else if(intervalSet.labels().contains(employeePoint)){
System.out.println(" " + name + " has already been arranged!");
System.out.println(" Please use the command del to clear the arrange information of " + name);
return;
}
employees.remove(employeePoint);
}
/**
* command del
* @param scanner the input flow
* @param employees the employee set
* @param intervalSet the interval set
*/
private static void del(Scanner scanner,
Set<Employee> employees,
DutyIntervalSet<Employee> intervalSet){
String name;
if(scanner.hasNext())
name = scanner.next();
else {
System.out.println(" Invalid command!\n Input \"help\" to get prompt message");
return;
}
if(scanner.hasNext()){
System.out.println(" Invalid command!\n Input \"help\" to get prompt message");
return;
}
Employee employeePoint = null;
for(Employee employee:employees){
if (employee.getName().equals(name)){
employeePoint = employee;
break;
}
}
if(employeePoint == null){
System.out.println(" " + name + " is not exist!");
return;
}
intervalSet.remove(employeePoint);
}
/**
* command insert
* @param scanner the input flow
* @param employees the employee set
* @param intervalSet the interval set
*/
private static void insert(Scanner scanner,
Set<Employee> employees,
DutyIntervalSet<Employee> intervalSet){
String name;
long start;
long end;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date;
if(scanner.hasNext())
name = scanner.next();
else {
System.out.println(" Invalid command!\n Input \"help\" to get prompt message");
return;
}
if(scanner.hasNext()){
try {
date = df.parse(scanner.next());
start = date.getTime() / 1000 / 86400 + 1;
}
catch (ParseException e){
System.out.println(" Wrong Date Format!");
return;
}
}
else {
System.out.println(" Invalid command!\n Input \"help\" to get prompt message");
return;
}
if(scanner.hasNext()){
try {
date = df.parse(scanner.next());
end = date.getTime() / 1000 / 86400 + 1;
}
catch (ParseException e){
System.out.println(" Wrong Date Format!");
return;
}
}
else {
System.out.println(" Invalid command!\n Input \"help\" to get prompt message");
return;
}
if(scanner.hasNext()){
System.out.println(" Invalid command!\n Input \"help\" to get prompt message");
return;
}
Employee employeePoint = null;
for(Employee employee:employees){
if (employee.getName().equals(name)){
employeePoint = employee;
break;
}
}
if(employeePoint == null){
System.out.println(" " + name + " is not exist!");
System.out.println(" Please use \"add\" command to add an employee first!");
return;
}
if(start < intervalSet.getStartTime() || start > intervalSet.getEndTime() ||
end < intervalSet.getStartTime() || end > intervalSet.getEndTime()){
System.out.println(" Date out of boundary!");
}
intervalSet.insert(start, end, employeePoint);
}
/**
* command auto
* @param scanner the input flow
* @param employees the employee set
* @param intervalSet the interval set
*/
private static void auto(Scanner scanner,
Set<Employee> employees,
DutyIntervalSet<Employee> intervalSet){
if(scanner.hasNext()){
System.out.println(" Invalid command!\n Input \"help\" to get prompt message");
return;
}
intervalSet.clear();
long num = employees.size();
long startTime = intervalSet.getStartTime();
long endTime = intervalSet.getEndTime();
long div = (endTime - startTime + 1) / num;
long i = 0;
for(Employee employee:employees){
i++;
if(i < num){
intervalSet.insert(startTime + (i - 1) * div, startTime + i * div - 1, employee);
}
else
intervalSet.insert(startTime + (i - 1) * div, endTime, employee);
}
}
/**
* command full
* @param scanner the input flow
* @param intervalSet the interval set
*/
private static void full(Scanner scanner,
DutyIntervalSet<Employee> intervalSet){
if(scanner.hasNext()){
System.out.println(" Invalid command!\n Input \"help\" to get prompt message");
return;
}
if(Math.abs(new APIs<Employee>().calcFreeTimeRatio(intervalSet,
intervalSet.getStartTime(), intervalSet.getEndTime())) < 0.001){
System.out.println(" Duty table is already full");
return;
}
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
for(long i = intervalSet.getStartTime(); i <= intervalSet.getEndTime(); i++){
if (intervalSet.getLabel(i).size() == 0) {
date.setTime(i * 1000 * 86400);
System.out.println(" " + df.format(date));
}
}
System.out.printf(" Free time ratio: %.2f\n",
new APIs<Employee>().calcFreeTimeRatio(intervalSet,
intervalSet.getStartTime(), intervalSet.getEndTime()));
}
/**
* command clear
* @param scanner the input flow
* @param intervalSet the interval set
*/
private static void clear(Scanner scanner,
DutyIntervalSet<Employee> intervalSet){
if(scanner.hasNext()){
System.out.println(" Invalid command!\n Input \"help\" to get prompt message");
return;
}
intervalSet.clear();
System.out.println(" Table has been cleared");
}
/**
* command view
* @param scanner the input flow
* @param intervalSet the interval set
*/
private static void view(Scanner scanner, DutyIntervalSet<Employee> intervalSet){
if(scanner.hasNext()){
System.out.println(" Invalid command!\n Input \"help\" to get prompt message");
return;
}
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
long startTime = intervalSet.getStartTime();
long endTime = intervalSet.getEndTime();
System.out.println(" Date\tName\tDuty\tphone");
for(long i = startTime; i <= endTime; i++){
Employee employee = intervalSet.getLabelL(i);
date.setTime(i * 1000 * 86400);
if (employee != null){
System.out.print(" " + df.format(date) + "\t" +
employee.getName() + "\t" +
employee.getDuty() + "\t" +
employee.getPhone() + "\n");
}
else{
System.out.println(" " + df.format(date) + "\t\t\t");
}
}
}
如果命令有错,输出提示信息并返回,等待下一条输入
效果: