BLOG_3
(1)前言:本次BLOG主要对过去几周的内容做一个总结,主要是分三次作业做了一个电信计费项目。
1.第一次:经过前面图形判断的非常不人性化折磨,对于本次的错误格式判断有了更深的理解,但第一次做的还是非常垃圾,主要是前面有点摆,时间有些许不够了。
2.第二次:第二次的题量是最大的,但是因为写第二题的时候留的时间较为充足,做了很多的准备,所以分数反而要更高。
3.第三次:第三次作业是最简单的一次,主要是对代码结构的审查和优化,对于题目本身的答案要求只需要简单的添加信息。
(2)设计与分析:
1.第一次电信计费题目;
先上类图:
首先是之前非常折磨人的错误判断,之前是步步拆开字符串分析,而在这次电信计费中用了正则表达式,非常方便快捷。
pattern1=Pattern.compile("u-((079\\d|0701)\\d{7,8})\\s([012])");
pattern2=Pattern.compile("t-((079\\d|0701)\\d{7,8})\\s(\\d{11,12})" +
"\\s(\\d{4}\\.(0?\\d|1[0123])\\.(0?[1-9]|1\\d|2\\d|3[0-1])\\s([01]\\d|2[0-4]):([0-5]\\d):([0-5]\\d))" +
"\\s(\\d{4}\\.(0?\\d|1[0123])\\.(0?[1-9]|1\\d|2\\d|3[0-1])\\s([01]\\d|2[0-4]):([0-5]\\d):([0-5]\\d))");
2.第三次作业7-1:直接上图说明。
第二题新加的正则表达式:
//register move-phone 1 2 pattern3=Pattern.compile("u-(1\\d{10})\\s([012])"); //static to move 4 1 3 4 5 11 pattern4=Pattern.compile("t-((079\\d|0701)\\d{7,8})\\s(1\\d{10})\\s(\\d{3,4})" + "\\s(\\d{4}\\.(0?\\d|1[0123])\\.(0?[1-9]|1\\d|2\\d|3[0-1])\\s([01]\\d|2[0-4]):([0-5]\\d):([0-5]\\d))" + "\\s(\\d{4}\\.(0?\\d|1[0123])\\.(0?[1-9]|1\\d|2\\d|3[0-1])\\s([01]\\d|2[0-4]):([0-5]\\d):([0-5]\\d))"); //move to move 5 1 2 3 4 5 11 pattern5=Pattern.compile("t-(1\\d{10})\\s(\\d{3,4})\\s(1\\d{10})\\s(\\d{3,4})" + "\\s(\\d{4}\\.(0?\\d|1[0123])\\.(0?[1-9]|1\\d|2\\d|3[0-1])\\s([01]\\d|2[0-4]):([0-5]\\d):([0-5]\\d))" + "\\s(\\d{4}\\.(0?\\d|1[0123])\\.(0?[1-9]|1\\d|2\\d|3[0-1])\\s([01]\\d|2[0-4]):([0-5]\\d):([0-5]\\d))"); //move to static 6 1 2 3 4 10 pattern6=Pattern.compile("t-(1\\d{10})\\s(\\d{3,4})\\s(0\\d{9,11})" + "\\s(\\d{4}\\.(0?\\d|1[0123])\\.(0?[1-9]|1\\d|21|3[0-1])\\s([01]\\d|2[0-4]):([0-5]\\d):([0-5]\\d))" + "\\s(\\d{4}\\.(0?\\d|1[0123])\\.(0?[1-9]|1\\d|2\\d|3[0-1])\\s([01]\\d|2[0-4]):([0-5]\\d):([0-5]\\d))");
分别判断的是注册移动电话,座机打移动电话,移动电话打移动电话,移动电话打座机。
下面是用matcher的group分别获取每一段输入的信息。
public String getNumber1(){ return matcher1.group(1); }//1 public String getNumber2(){ return matcher3.group(1); }//3 public String getMode1(){ return matcher1.group(3); }//1 public String getMode2(){ return matcher3.group(2); }//3 public String getCallNumber1(){ return matcher2.group(1); }//2 public String getCallNumber2(){ return matcher4.group(1); }//4 public String getCallNumber3(){ return matcher5.group(1); }//5 public String getCallNumber4(){ return matcher6.group(1); }//6 public String getAnswerNumber1(){ return matcher2.group(3); }//2 public String getAnswerNumber2(){ return matcher4.group(3); }//4 public String getAnswerNumber3(){ return matcher5.group(3); }//5 public String getAnswerNumber4(){ return matcher6.group(3); }//6 public String getCallArea1() { return matcher2.group(1).substring(0,4); }//2 public String getCallArea2() { return matcher4.group(1).substring(0,4); }//4 public String getCallArea3() { return matcher5.group(2); }//5 public String getCallArea4() { return matcher6.group(2); }//6 public String getAnswerArea1(){ if(matcher2.group(3).length()>=11) return matcher2.group(3).substring(0,4); else return matcher2.group(3).substring(0,3); }//2 public String getAnswerArea2(){ return matcher4.group(4); }//4 public String getAnswerArea3(){ return matcher5.group(4); }//5 public String getAnswerArea4(){ if(matcher6.group(3).length()>=11) return matcher6.group(3).substring(0,4); else return matcher6.group(3).substring(0,3); }//6 public String StartTime1(){ return matcher2.group(4); }//2 public String StartTime2(){ return matcher4.group(5); }//4 public String StartTime3(){ return matcher5.group(5); }//5 public String StartTime4(){ return matcher6.group(4); }//6 public String EndTime1(){ return matcher2.group(10); }//2 public String EndTime2(){ return matcher4.group(11); }//4 public String EndTime3(){ return matcher5.group(11); }//5 public String EndTime4(){ return matcher6.group(10); }//6 public Date getStartTime1(){ SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { return simpleDateFormat.parse(StartTime1()); } catch (ParseException e) { throw new RuntimeException(e); } }//2 public Date getStartTime2(){ SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { return simpleDateFormat.parse(StartTime2()); } catch (ParseException e) { throw new RuntimeException(e); } }//4 public Date getStartTime3(){ SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { return simpleDateFormat.parse(StartTime3()); } catch (ParseException e) { throw new RuntimeException(e); } }//5 public Date getStartTime4(){ SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { return simpleDateFormat.parse(StartTime4()); } catch (ParseException e) { throw new RuntimeException(e); } }//6 public Date getEndTime1(){ SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { return simpleDateFormat.parse(EndTime1()); } catch (ParseException e) { throw new RuntimeException(e); } }//2 public Date getEndTime2(){ SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { return simpleDateFormat.parse(EndTime2()); } catch (ParseException e) { throw new RuntimeException(e); } }//4 public Date getEndTime3(){ SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { return simpleDateFormat.parse(EndTime3()); } catch (ParseException e) { throw new RuntimeException(e); } }//5 public Date getEndTime4(){ SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { return simpleDateFormat.parse(EndTime4()); } catch (ParseException e) { throw new RuntimeException(e); } }//6
3.第三次电信计费:
类图:
第三次的只增加了一个发送短信的功能,对于已经有点模块化的代码来说这题的难度已经不大,这是第三次电信计费的正则表达式。
//register message 7 1 2 pattern7=Pattern.compile("u-(1\\d{10})\\s(3)"); //send message 8 1 2 3 pattern8=Pattern.compile("m-(1\\d{10})\\s(1\\d{10})\\s((\\w|\\s|[.]|,)+)");
4第七次PTA大作页的2题:
这2题主要是对集合框架的应用。
class Student implements Comparable<Student>{ String id; String name; String age; String sex; Student(String id,String name,String age,String sex){ this.id=id; this.name=name; this.age=age; this.sex=sex; } public void output(){ System.out.println(id+" "+name+" "+age+" "+sex); } @Override public int compareTo(Student o) { return this.id.compareTo(o.id); } @Override public int hashCode(){ int result=0; if(id!=null) result=result+id.hashCode(); if(name!=null) result=result+name.hashCode(); result=result+Integer.parseInt(age); if(sex!=null) result=result+sex.hashCode(); return result; } @Override public boolean equals(Object o){ if(this==o) return true; if(o==null) return false; if(getClass()!=o.getClass()) return true; Student other=(Student) o; if(!id.equals(other.id)) return false; if(!name.equals(other.name)) return false; if(!age.equals(other.age)) return false; if(!sex.equals(other.sex)) return false; return true; }
hashset可以根据hashcode进行删除,所以重写了一手hashcode()自定义hashcode以便比较,以及重写equals()自定义两者是否是同一类。
让stusent继承Comparable<T>接口再自定义compareTo(Object)即可自定义hashset内的元素的排序。
(3)踩坑心得:
1.电信计费,正则表达式需要更加细节,第一次题目没有写好,第二次题目虽然考虑的更仔细,但因为要判断的多了所以有些漏洞,另外还要注意代码量多的时候可能出现的逻辑问题。
3.集合框架作业: 一开始不知道hashset是根据原来的hashcode和equals进行默认比较,还以为是自动通过我输入的值判断。
(4)改进建议:
1.电信计费;正则表达式还可以再细致一点,有2个特判总是过不了,代码的模块化和结构化有待提高,类与类之间的使用还有写生疏。
2.集合框架主要是知识点比较多,且难度也有,要多学多看。
(5)总结:
1.学到了什么:对java代码的模块化有了些许理解,也初步学习了集合框架与正则表达式,收获还可以。
2.改进意见:这几次作业比前几次要好很多,并没有在数据上***难太多,而是把更多注意力放在对知识点和代码结构上。