停车场记录
编写一个程序,做一个建议的停车场使用数据记录仪
此程序能够监听命令行的输入,命令行可以输入三类命令:(下列命令中的数值均为举例,实际使用中不会限定数值)
1:checkin -t=225959 -n=AT4257
2:checkout -t=233000 -n=AT4257
3:listrecord -all
其中第一条命令代表车牌号为AT4257的汽车在22点59分59秒进入停车场
第二条命令代表车牌号为AT4257的汽车在23点30分00秒驶出停车场
在执行第1、2条命令的情况下,执行第三条命令,则会输出如下:
Record1: AT4257 22:59:59 in
Record2: AT4257 23:30:00 out
Hint:注意输入时的各种异常输入以及边界条件
BONUS:
支持另外两种命令:
4:listrecord -n=AT4257
5:listrecord -st=220000 -et=235959
其中第四条命令代表查询车牌号为AT4257的汽车在此停车场的使用记录,输出格式与第三条相同
第五条命令代表查询某一时间段内停车场的使用记录,st代表开始时间,et代表结束时间,
输出格式与第三条命令相同。
(注意:第四条与第五条命令的参数可以同时使用。如:listrecord -n=AT4257 -st=220000 -et=235959
和listrecord -st=220000 -et=235959 -n=AT4257
)
解题代码如下:
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/** * 停车场记录 * Created by Relish on 2016/8/3. */
public class ParkingRecord {
//输入数据:
//checkin -t=225959 -n=AT4257
//checkout -t=233000 -n=AT4257
//listrecord -all
//listrecord -n=AT4257
//listrecord -st=220000 -et=235959
//listrecord -n=AT4257 -st=220000 -et=235959
@Test
public void testRegex() {
Record r = stringConvert2Record("checkin -t=225959 -n=AT4257");
System.out.println(r);
System.out.println("checkin -t=225959 -n=AT4257".matches("^check(in|out) -t=(([0-1][0-9])|(2[0-3]))([0-5][0-9]){2} -n=[0-9A-Z]{6}$"));
}
@Test
public void testListRecord() {
List<Record> records = new ArrayList<>();
records.add(new Record(true, "22:59:59", "AT4257"));
records.add(new Record(false, "23:30:59", "AT4257"));
listRecord("listrecord -st=220000 -et=235959".split(" "), records);
}
private static List<Record> records = new ArrayList<>();
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
String s = cin.nextLine();
if (s.startsWith("checkin") || s.startsWith("checkout")) {//进库
records.add(stringConvert2Record(s));
} else if (s.startsWith("listrecord")) {
String[] next = Arrays.copyOfRange(s.split(" "), 1, s.split(" ").length);
listRecord(next, records);
}
}
}
private static void listRecord(String[] command, List<Record> records) {
if (command[0].startsWith("-all")) {
for (int i = 0; i < records.size(); i++) {
System.out.println("Record" + (i + 1) + ": " + records.get(i));
}
} else if (command[0].startsWith("-n")) {
List<Record> rs = new ArrayList<>();
records.forEach((record -> {
if (record.num.equals(command[0].substring(3)))
rs.add(record);
}));
isCommandEnd(command, rs);
} else if (command[0].startsWith("-st")) {
String st = command[0].substring(4);
List<Record> rs = new ArrayList<>();
records.forEach((record -> {
if (st.compareTo(record.time.replace(":", "")) <= 0)
rs.add(record);
}));
isCommandEnd(command, rs);
} else if (command[0].startsWith("-et")) {
String et = command[0].substring(4);
List<Record> rs = new ArrayList<>();
records.forEach((record -> {
if (et.compareTo(record.time.replace(":", "")) >= 0)
rs.add(record);
}));
isCommandEnd(command, rs);
} else if (command[0].startsWith("listrecord")) {
isCommandEnd(command, records);
} else {
System.err.println("命令格式不正确");
}
}
private static void isCommandEnd(String[] command, List<Record> records) {
if (command.length == 1) {
printRecords(records);
} else {
String[] next = Arrays.copyOfRange(command, 1, command.length);
listRecord(next, records);
}
}
private static void printRecords(List<Record> records) {
if (records == null || records.size() == 0) {
System.err.println("records are null!");
return;
}
for (int i = 0; i < records.size(); i++) {
System.out.println("Record" + (i + 1) + ": " + records.get(i));
}
}
private static Record stringConvert2Record(String s) {
String matcher = "^check(in|out) -t=(([0-1][0-9])|(2[0-3]))([0-5][0-9]){2} -n=[0-9A-Z]{6}$";
Record r = new Record();
if (s.matches(matcher)) {
if (s.startsWith("checkin")) {
r.in = true;
} else if (s.startsWith("checkout")) {
r.in = false;
} else {
System.err.println("命令格式不正确");
return null;
}
} else {
System.err.println("命令格式不正确");
return null;
}
String[] arr = s.split(" ");
String rawTime = arr[1].substring(3);
r.time = rawTime.substring(0, 2) + ":" + rawTime.substring(2, 4) + ":" + rawTime.substring(4);
r.num = arr[2].substring(3);
return r;
}
private static class Record {
String time;
String num;
boolean in;
Record() {
}
Record(boolean in, String time, String num) {
this.time = time;
this.num = num;
this.in = in;
}
@Override
public String toString() {
return num + " " + time + " " + (in ? "in" : "out");
}
}
}