編寫一個程序,做一個簡易的停車場使用數據記錄儀。
此程序能夠監聽命令行輸入,命令行可以輸入三類命令:(下述命令中的參數值均爲舉例,實際使用中不會限定數值)
1: checkin –t=225959 –n=AT4257
2: checkout –t=233000 –n=AT4257
3: listrecord –all
4: listrecord –n=AT4257
其中第一條命令代表車牌號爲AT4257的汽車在22點59分59秒進入停車場
第二條命令代表車牌號爲AT4257的汽車在23:30:00駛出停車場
在執行了第1、2條命令的情況下,執行第三條命令,則會輸出如下:
Record1: AT4257 22:59:59 in
Record2: AT4257 23:30:30 out
其中第四條命令代表查詢車牌號爲AT4257的汽車在此停車場的使用記錄,輸出格式與第三條相同
Hint: 請注意輸入時的各種異常輸入以及邊界條件。
BONUS:
支持另外一種命令:
5: listrecord –st=220000 -et=235959
且令4,5時間複雜度儘可能低
第五條命令代表查詢某一個時間段內停車場的使用記錄,st代表開始時間et代表結束時間,輸出格式與第三條命令相同。
(注意:第四條與第五條命令的參數可以同時使用)
渣渣原本用c++寫,反正已掛。。。
借鑑網上的java代碼,自己寫了一遍,共享。
public class PakingRecord {
//定義一個Record類
private static class Record{
String time;
String carnumber;
boolean in;
Record(){
}
Record(boolean in,String time,String carnumber){
this.in=in;
this.time=time;
this.carnumber=carnumber;
}
@Override
public String toString() {
return carnumber+" "+time+" "+(in?"in":"out");//輸出格式
}
}
//輸入的字符串轉換爲record形式(輸出需要的格式)
public static Record stringconvertrecord(String s){
Record r=new Record();
String matcher="^check(in|out) -t=(([0-1][0-9])|(2[0-3]))([0-5][0-9]){2} -n=[0-9A-Z]{6}$";
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;}
String[] arr=s.split(" ");
String time=arr[1].substring(3);
r.time=time.substring(0,2)+":"+time.substring(2,4)+":"+time.substring(4);
r.carnumber=arr[2].substring(3);
return r;
}
public static void IsCommandEnd(String[] command,List<Record>records){
if(command.length==1){
for(int i=0;i<records.size();i++){
System.out.println("Record"+(i+1)+": "+records.get(i));
}
}
else {
String[] next=Arrays.copyOfRange(command,1,command.length);
listrecord(next,records);
}
}
//顯示記錄
public 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<>();//創建一個list存儲一個車牌號的記錄集
for(Record record:records){
if(record.carnumber.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);
}
}));
*/
for(Record record:records){
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);
}));
*/
for(Record record:records){
if(et.compareTo(record.time.replace(":",""))>=0){
rs.add(record);
}
}
IsCommandEnd(command,rs);
}
else if (command[0].startsWith("listrecord")) {
IsCommandEnd(command, records);
}
else {
System.out.println("輸入格式不正確");
}
}
public static List<Record> records=new ArrayList<>();
public static void main(String[] args) {
PakingRecord c=new PakingRecord();
Scanner scanner=new Scanner(System.in);
while (scanner.hasNext()){
String s=scanner.nextLine();
if(s.startsWith("checkin")||s.startsWith("checkout"))
//添加記錄
records.add(stringconvertrecord(s));
else if(s.startsWith("listrecord")){
//先拆解,拷貝,注意是從1開始,相當於略去了listrecord
String[] next=Arrays.copyOfRange(s.split(" "),1,s.split(" ").length);
listrecord(next,records);
}
}
}
}