第三次大作业
前言
时间过的好快啊,转眼迎来了最后一次java的blog。突然有一种很不舍的感觉,因为疫情的缘故,大部分时间都是线上教学,我们的生活好像被按了加速键。在这段时间里,还是学到了点东西,自己对于java的理解更加深刻了,终于将自己的面向过程的思维转变了过来,能够面向对象的去解决问题。这次的作业总结主要是围绕电信计费系列的题目,主要考察了关于父类与子类的运用合理,还有抽象类的使用,并且更加深层次的使用容器,加强了类之间的联系与引用,还有近期的老朋友——正则表达式的使用。
接下来是题目的详细分析……
nchu-software-oop-2022-6
7-1 电信计费系列1-座机计费
这道题嘛..有几个点确实是过不去,是关于一些无效输入的判断与忽略,考虑了好久,真的想不到了,所以那几分就放弃了。情况的考虑不周。
以下是代码:
1 import java.util.Comparator; 2 import java.text.DecimalFormat; 3 import java.text.ParseException; 4 import java.util.ArrayList; 5 import java.util.Collections; 6 import java.util.Scanner; 7 import java.text.DateFormat; 8 import java.text.SimpleDateFormat; 9 import java.util.Date; 10 public class Main { 11 public static void main(String[] args) throws ParseException { 12 ArrayList <User> users= new ArrayList <User> (); 13 User user = null; 14 Scanner in = new Scanner(System.in); 15 String s=new String(); 16 boolean flag; 17 boolean flag1; 18 boolean flag2; 19 boolean f=false; 20 String[] s1; 21 String regexm = "u-0791\\d{7,8} 0"; 22 String regexn = "t-\\d{11,12} 0[1-9]{1}\\d{9,10} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2}"; 23 24 int n; 25 s=in.nextLine(); 26 do { 27 flag = s.matches(regexm);//判断功能 28 s1=s.split(" "); 29 if(!flag) { 30 s=in.nextLine(); 31 while(s.equals("")) 32 s=in.nextLine(); 33 continue; 34 } 35 user =new User(); 36 37 38 user.getNumber(s1[0].substring(2)); 39 40 for(int i=0;i<users.size();i++) { 41 if(user.number.equals(users.get(i).retNumber())) { 42 f=true; 43 break; 44 } 45 } 46 if(f) { 47 s=in.nextLine(); 48 while(s.equals("")) 49 s=in.nextLine(); 50 continue; 51 } 52 users.add(user); 53 s=in.nextLine(); 54 while(s.equals("")) 55 s=in.nextLine(); 56 user.getDevice(Integer.parseInt(s1[1])); 57 }while(s.charAt(0)=='u'); 58 59 do { 60 s1=s.split(" "); 61 flag = s.matches(regexn);//判断功能 62 if(!flag) { 63 s=in.nextLine(); 64 continue; 65 } 66 67 user=null; 68 for(int i=0;i<users.size();i++) { 69 if(s1[0].substring(2).equals(users.get(i).retNumber())) { 70 user=users.get(i); 71 break; 72 } 73 74 } 75 if(user==null) { 76 s=in.nextLine(); 77 while(s.equals("")) 78 s=in.nextLine(); 79 continue; 80 } 81 82 CommunicationRecord records =new CommunicationRecord(); 83 String sm=s1[2]+" "+s1[3]; 84 String sn=s1[4]+" "+s1[5]; 85 if(!(records.validDateTimeSimple(sn)&&records.validDateTimeSimple(sm))) { 86 s=in.nextLine(); 87 while(s.equals("")) 88 s=in.nextLine(); 89 continue; 90 } 91 92 String regex0 = "0791"; 93 String regex1 = "(079\\d)|0701"; 94 95 flag = s1[1].substring(0,4).matches(regex0);//判断功能 96 flag1 = s1[1].substring(0,4).matches(regex1);//判断功能 97 98 n=3; 99 if(flag1) 100 n=2; 101 if(flag) 102 n=1; 103 104 105 records.answerNumber=s1[1]; 106 records.begintime=s1[2]+" "+s1[3]; 107 records.endtime=s1[4]+" "+s1[5]; 108 records.chose=n; 109 user.setUserRecords(records); 110 s=in.nextLine(); 111 }while(!s.equals("end")); 112 113 DecimalFormat df =new DecimalFormat("0.0"); 114 Collections.sort(users,new UserComparator()); 115 for(int j=0;j<users.size();j++) { 116 users.get(j).calcost(); 117 System.out.println(users.get(j).number+" "+users.get(j).getCost() +" "+users.get(j).getBalance()); 118 } 119 } 120 public static class User { 121 ArrayList <CommunicationRecord> userRecords =new ArrayList <CommunicationRecord>(); 122 double balance=100; 123 ChargeRules chargeRules =new ChargeRules(); 124 String number; 125 Device device; 126 double sumcost=0; 127 128 public void setUserRecords(CommunicationRecord Records) { 129 userRecords.add(Records); 130 } 131 public void setChargeRules(ChargeRules land) { 132 this.chargeRules=chargeRules; 133 } 134 public void calcost() throws ParseException { 135 for(int i=0;i<userRecords.size();i++) { 136 double time =userRecords.get(i).sumtime(); 137 if(userRecords.get(i).chose==1) { 138 cost(time*0.1); 139 } 140 else if(userRecords.get(i).chose==2) { 141 cost(time*0.3); 142 } 143 else 144 cost(time*0.6); 145 } 146 } 147 public void getNumber(String s) { 148 149 this.number=s; 150 151 } 152 public String retNumber() { 153 return this.number; 154 } 155 156 public void cost(double x) { 157 this.balance=this.balance -x; 158 sumcost+=x; 159 } 160 public double getBalance() { 161 return balance; 162 } 163 public double getCost() { 164 return sumcost; 165 } 166 public void getDevice(int x) { 167 if(x==0) { 168 device =new Landlines(); 169 LandRule land =new LandRule(); 170 setChargeRules(land); 171 balance=balance-Landlines.monthlyRent; 172 } 173 } 174 175 } 176 177 public static class ChargeRules { 178 public double calCost(ArrayList<CallRecord> callRecords){ 179 return 0; 180 } 181 } 182 public static class UserRecords { 183 184 } 185 public static class UserComparator implements Comparator<User>{ 186 187 @Override 188 public int compare(User s, User s1) { 189 int flag = new Integer(s.number.compareTo(s1.number)); 190 return flag; 191 } 192 193 } 194 public static class Device { 195 double monthlyRent; 196 ArrayList <ChargeRules> chargeRules=new ArrayList <ChargeRules>(); 197 198 public void getChargeRules(ArrayList <ChargeRules> chargeRules) { 199 this.chargeRules =chargeRules; 200 } 201 } 202 public static class CommunicationRecord { 203 String callingNumber; 204 String answerNumber; 205 String begintime; 206 String endtime; 207 int chose; 208 209 public static boolean validDateTimeSimple(String dateTime) { 210 if(dateTime == null ) { 211 return false; 212 } 213 DateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 214 df.setLenient(false);//表示严格验证 215 216 try { 217 df.parse(dateTime); 218 } catch (ParseException e) { 219 return false; 220 } 221 return true; 222 } 223 224 public long sumtime() throws ParseException { 225 226 //一、时间减法可以用Calendar对象加法中,把第二个参数换成负数可以时间,这边就不列举了;使用第二种方式做时间减法 227 //1、指定两个Date对象,业务中可以以参数的形式传递,不用new当前时间。 228 DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 229 String nowday = this.endtime; 230 Date nowDate = dateFormat.parse(nowday);//当前时间 231 String yesterday = this.begintime; 232 Date yesterdayDate = dateFormat.parse(yesterday);//假设一个时间,工作业务中可以参数传递的形式来获取。 233 //2、时间戳相减 234 long yesterdayDateTime = yesterdayDate.getTime();//昨天的时间戳 235 long nowDateTime = nowDate.getTime();//当前时间戳 236 long result = nowDateTime - yesterdayDateTime;//毫秒 237 238 long diffSecond = result / 1000; //1000毫秒等于1秒 239 240 //4、换算成分钟 241 long diffMinute = result / 60000 ;//60秒等于1分钟 242 if(result%60000!=0){ 243 diffMinute=diffMinute+1; 244 } 245 return diffMinute; 246 } 247 } 248 public static class CallRecord extends CommunicationRecord{ 249 String callingAddressAreaCode; 250 String answerAddressAreaCode; 251 String begintime; 252 String endtime; 253 } 254 public static class Landlines extends Device{ 255 static double monthlyRent=20; 256 257 } 258 public static class LandRule extends ChargeRules{ 259 260 } 261 }
设计与分析:首先,我是设计好了user类和需要判断日期和计算时间的类,还有记录通话记录的类、设备类作为座机的父类。然后再去按照输入输出去设计Main类里的一些判断和与其他类的交互,中途很多次添加了一些额外判断去忽略那些错误输入。
采坑心得:一开始不知道impleDateFormat类可以去判断日期输入是否合法,还是自己去判断,发现真的很麻烦,后面通过询问老师,才知道要用这个类里的功能。
改进建议:因为没有多边形复杂,自己又懒得写注释了,这是一个不好的下意识。感觉自己还是要多写注释,也是方便自己的使用。判断其实后面越改越觉得比较繁琐,一开始应该写个类来专门实现判断功能的。一开始写并没有考虑后面添加了手机功能的交互性,后面改其实麻烦很多,一开始以为自己一开始的设计可以交互,后面使用的时候才发现不行,以后还是要先多思考一会。
7-2 多态测试
这道题还是比较简单的,因为老师将主要的方法都告诉我们了,我们只要简单的实现一些功能就好了。主要是训练多态的使用,还有容器的使用。
以下是代码:
1 import java.text.DecimalFormat; 2 import java.util.Scanner; 3 public class Main { 4 public static void main(String[] args) { 5 Scanner in =new Scanner (System.in); 6 String s; 7 int n=in.nextInt(); 8 Container[] c =new Container[n]; 9 for(int i=0;i<n;i++) { 10 s=in.nextLine(); 11 s=in.nextLine(); 12 if(s.equals("cube")) { 13 c[i]=new Cube(); 14 ((Cube)c[i]).l=in.nextDouble(); 15 } 16 if(s.equals("cylinder")) { 17 c[i]=new Cylinder(); 18 ((Cylinder)c[i]).r=in.nextDouble(); 19 ((Cylinder)c[i]).h=in.nextDouble(); 20 21 } 22 23 } 24 DecimalFormat df =new DecimalFormat("0.00"); 25 System.out.println(df.format(Container.sumofArea(c))); 26 System.out.println(df.format(Container.sumofVolume(c))); 27 28 29 } 30 interface Container { 31 public static final double pi=3.1415926; 32 public abstract double area(); 33 public abstract double volume(); 34 static double sumofArea(Container c[]) { 35 double sa=0; 36 for(int i=0;i<c.length;i++) { 37 sa+=c[i].area(); 38 } 39 return sa; 40 } 41 static double sumofVolume(Container c[]) { 42 double sv=0; 43 for(int i=0;i<c.length;i++) { 44 sv+=c[i].volume(); 45 } 46 return sv; 47 } 48 } 49 public static class Cube implements Container { 50 double l=0; 51 @Override 52 public double area() { 53 54 return 6*l*l; 55 } 56 57 @Override 58 public double volume() { 59 60 return l*l*l; 61 } 62 63 } 64 public static class Cylinder implements Container{ 65 double h=0; 66 double r=0; 67 @Override 68 public double area() { 69 70 return 2*pi*r*h+2*pi*r*r; 71 } 72 73 @Override 74 public double volume() { 75 76 return pi*r*r*h; 77 } 78 79 } 80 81 }
nchu-software-oop-2022-7
7-1 电信计费系列2-手机+座机计费
这道题主要是在上一次的大作业基础上添加了手机的相关功能。
以下是代码:
1 import java.util.Comparator; 2 import java.text.DecimalFormat; 3 import java.text.ParseException; 4 import java.util.ArrayList; 5 import java.util.Collections; 6 import java.util.Scanner; 7 import java.text.DateFormat; 8 import java.text.SimpleDateFormat; 9 import java.util.Date; 10 public class Main { 11 12 public static void main(String[] args) throws ParseException { 13 ArrayList <User> users= new ArrayList <User> (); 14 User user = null; 15 Scanner in = new Scanner(System.in); 16 String s=new String(); 17 Device diver = null ; 18 boolean flag; 19 boolean flag1; 20 boolean flag2; 21 boolean f=false; 22 String[] s1; 23 String regexm = "u-0791\\d{7,8} 0"; //座机 24 String regexp = "u-1\\d{10} 1"; //手机 25 String regexn = "t-\\d{11,12} 0[1-9]{1}\\d{9,10} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2}"; 26 //座机打座机 27 String regexn0 = "t-\\d{11,12} 1\\d{10} 0\\d{2,3} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2}"; 28 //座机打手机 29 String regexn1 ="t-\\d{11} 0\\d{2,3} 1\\d{10} 0\\d{2,3} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2}"; 30 //手机互打 31 String regexn2 ="t-1\\d{10} 0\\d{2,3} 0[1-9]{1}\\d{9,10} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2}"; 32 //手机打座 33 int n; 34 s=in.nextLine(); 35 do { 36 flag = s.matches(regexm)||s.matches(regexp);//判断功能 37 s1=s.split(" "); 38 if(!flag) { 39 s=in.nextLine(); 40 while(s.equals("")) 41 s=in.nextLine(); 42 continue; 43 } 44 user =new User(); 45 46 47 user.getNumber(s1[0].substring(2)); 48 49 for(int i=0;i<users.size();i++) { 50 if(user.number.equals(users.get(i).retNumber())) { 51 f=true; 52 break; 53 } 54 } 55 if(f) { 56 s=in.nextLine(); 57 while(s.equals("")) 58 s=in.nextLine(); 59 f=false; 60 continue; 61 } 62 users.add(user); 63 s=in.nextLine(); 64 while(s.equals("")) 65 s=in.nextLine(); 66 user.getDevice(Integer.parseInt(s1[1])); 67 }while(s.charAt(0)=='u'); 68 69 do { 70 s1=s.split(" "); 71 flag = s.matches(regexn)||s.matches(regexn0)||s.matches(regexn1)||s.matches(regexn2);//判断功能 72 if(!flag) { 73 s=in.nextLine(); 74 continue; 75 } 76 77 user=null; 78 for(int i=0;i<users.size();i++) { 79 if(s1[0].substring(2).equals(users.get(i).retNumber())) { 80 user=users.get(i); 81 break; 82 } 83 84 } 85 if(user==null) { 86 String regex0 = "0791"; 87 String regex1 = "(079\\d)|0701"; 88 boolean flagx = s1[3].matches(regex0)||s1[2].matches(regex0);//判断功能 89 boolean flagy = s1[3].matches(regex1)||s1[2].matches(regex0);//判断功能 90 int m=3;//省外 91 if(flagy) 92 m=2; //省内 93 if(flagx) 94 m=1; //市内 95 96 for(int i=0;i<users.size();i++) { 97 if(s1[2].equals(users.get(i).retNumber())) { 98 if(m==3) { 99 CommunicationRecord records =new CommunicationRecord(); 100 records.begintime=s1[4]+" "+s1[5]; 101 records.endtime=s1[6]+" "+s1[7]; 102 users.get(i).cost(records.sumtime()*0.3); 103 } 104 } 105 if(s1[1].equals(users.get(i).retNumber())) { 106 if(m==3) { 107 CommunicationRecord records =new CommunicationRecord(); 108 records.begintime=s1[3]+" "+s1[4]; 109 records.endtime=s1[5]+" "+s1[6]; 110 users.get(i).cost(records.sumtime()*0.3); 111 } 112 } 113 } 114 s=in.nextLine(); 115 continue; 116 } 117 118 if(s.matches(regexn)) { 119 diver =new Landlines(); 120 int i=((Landlines)diver).input1(user, s1, s); 121 if(i==-1) { 122 s=in.nextLine(); 123 continue; 124 } 125 } 126 else if(s.matches(regexn0)) { //考虑漫游 127 diver=new Landlines(); 128 int i=((Landlines)diver).input2(users,user, s1, s); 129 if(i==-1) { 130 s=in.nextLine(); 131 continue; 132 } 133 } 134 else if(s.matches(regexn1)) { //考虑漫游 135 diver=new Phone(); 136 int i=((Phone)diver).input1(users,user, s1, s); 137 if(i==-1) { 138 s=in.nextLine(); 139 continue; 140 } 141 } 142 else if(s.matches(regexn2)) { 143 diver=new Phone(); 144 int i=((Phone)diver).input2(user, s1, s); 145 if(i==-1) { 146 s=in.nextLine(); 147 continue; 148 } 149 } 150 151 s=in.nextLine(); 152 while(s.equals("")) 153 s=in.nextLine(); 154 }while(!s.equals("end")); 155 156 Collections.sort(users,new UserComparator()); 157 for(int j=0;j<users.size();j++) { 158 users.get(j).calcost(); 159 System.out.println(users.get(j).number+" "+users.get(j).getCost() +" "+users.get(j).getBalance()); 160 } 161 } 162 public static class User { 163 ArrayList <CommunicationRecord> userRecords =new ArrayList <CommunicationRecord>(); 164 double balance=100; 165 ChargeRules chargeRules =new ChargeRules(); 166 String number; 167 Device device; 168 double sumcost=0; 169 170 public void setUserRecords(CommunicationRecord Records) { 171 userRecords.add(Records); 172 } 173 public void setChargeRules(ChargeRules land) { 174 this.chargeRules=chargeRules; 175 } 176 public void calcost() throws ParseException { 177 for(int i=0;i<userRecords.size();i++) { 178 double time =userRecords.get(i).sumtime(); 179 if(userRecords.get(i).chose1==0) { 180 if(userRecords.get(i).chose==1) 181 cost(time*0.1); 182 183 else if(userRecords.get(i).chose==2) 184 cost(time*0.3); 185 186 else 187 cost(time*0.6); 188 } 189 190 else { 191 if(userRecords.get(i).chose==1) { 192 if(userRecords.get(i).chose1==1) 193 cost(time*0.1); 194 else if(userRecords.get(i).chose1==2) 195 cost(time*0.2); 196 197 else 198 cost(time*0.3); 199 } 200 else if(userRecords.get(i).chose==2) { 201 cost(time*0.3); 202 } 203 else if(userRecords.get(i).chose==3) { 204 cost(time*0.6); 205 } 206 207 } 208 } 209 } 210 public void getNumber(String s) { 211 this.number=s; 212 } 213 public String retNumber() { 214 return this.number; 215 } 216 217 public void cost(double x) { 218 this.balance=this.balance -x; 219 sumcost+=x; 220 } 221 public double getBalance() { 222 return balance; 223 } 224 public double getCost() { 225 return sumcost; 226 } 227 public void getDevice(int x) { 228 if(x==0) { 229 device =new Landlines(); 230 LandRule land =new LandRule(); 231 setChargeRules(land); 232 balance=balance-Landlines.monthlyRent; 233 } 234 if(x==1) { 235 device =new Phone(); 236 LandRule land =new LandRule(); 237 setChargeRules(land); 238 balance=balance-Phone.monthlyRent; 239 } 240 } 241 242 } 243 244 public static class ChargeRules { 245 public double calCost(ArrayList<CallRecord> callRecords){ 246 return 0; 247 } 248 } 249 public static class UserRecords { 250 251 } 252 public static class UserComparator implements Comparator<User>{ 253 254 @Override 255 public int compare(User s, User s1) { 256 int flag = new Integer(s.number.compareTo(s1.number)); 257 return flag; 258 } 259 260 } 261 public static class Device { 262 double monthlyRent; 263 ArrayList <ChargeRules> chargeRules=new ArrayList <ChargeRules>(); 264 265 public void getChargeRules(ArrayList <ChargeRules> chargeRules) { 266 this.chargeRules =chargeRules; 267 } 268 } 269 public static class CommunicationRecord { 270 String callingNumber; 271 String answerNumber; 272 String begintime; 273 String endtime; 274 int chose=0; 275 int chose1=0; 276 int month; 277 278 279 public static boolean validDateTimeSimple(String dateTime) { 280 if(dateTime == null ) { 281 return false; 282 } 283 DateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 284 df.setLenient(false);//表示严格验证 285 286 try { 287 df.parse(dateTime); 288 } catch (ParseException e) { 289 return false; 290 } 291 return true; 292 } 293 294 public long sumtime() throws ParseException { 295 296 //一、时间减法可以用Calendar对象加法中,把第二个参数换成负数可以时间,这边就不列举了;使用第二种方式做时间减法 297 //1、指定两个Date对象,业务中可以以参数的形式传递,不用new当前时间。 298 DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 299 String nowday = this.endtime; 300 Date nowDate = dateFormat.parse(nowday);//当前时间 301 String yesterday = this.begintime; 302 Date yesterdayDate = dateFormat.parse(yesterday);//假设一个时间,工作业务中可以参数传递的形式来获取。 303 //2、时间戳相减 304 long yesterdayDateTime = yesterdayDate.getTime();//昨天的时间戳 305 long nowDateTime = nowDate.getTime();//当前时间戳 306 long result = nowDateTime - yesterdayDateTime;//毫秒 307 308 long diffSecond = result / 1000; //1000毫秒等于1秒 309 310 //4、换算成分钟 311 long diffMinute = result / 60000 ;//60秒等于1分钟 312 if(result%60000!=0){ 313 diffMinute=diffMinute+1; 314 } 315 return diffMinute; 316 } 317 } 318 public static class CallRecord extends CommunicationRecord{ 319 String callingAddressAreaCode; 320 String answerAddressAreaCode; 321 String begintime; 322 String endtime; 323 } 324 public static class Landlines extends Device{ 325 static double monthlyRent=20; 326 327 public int input1(User user,String[] s1,String s) { 328 329 boolean flag; 330 boolean flag1; 331 boolean flag2; 332 333 int n=0; 334 CommunicationRecord records =new CommunicationRecord(); 335 String sm=s1[2]+" "+s1[3]; 336 String sn=s1[4]+" "+s1[5]; 337 if(!(records.validDateTimeSimple(sn)&&records.validDateTimeSimple(sm))) { 338 return -1; 339 } 340 341 String regex0 = "0791"; 342 String regex1 = "(079\\d)|0701"; 343 344 flag = s1[1].substring(0,4).matches(regex0);//判断功能 345 flag1 = s1[1].substring(0,4).matches(regex1);//判断功能 346 347 n=3; 348 if(flag1) 349 n=2; 350 if(flag) 351 n=1; 352 353 records.answerNumber=s1[1]; 354 records.begintime=s1[2]+" "+s1[3]; 355 records.endtime=s1[4]+" "+s1[5]; 356 records.chose=n; 357 user.setUserRecords(records); 358 return 1; 359 } 360 public int input2(ArrayList<User> users, User user,String[] s1,String s) throws ParseException { 361 362 boolean flag; 363 boolean flag1; 364 boolean flag2; 365 boolean flag3; 366 int m=0; 367 int n=0; 368 CommunicationRecord records =new CommunicationRecord(); 369 String sm=s1[3]+" "+s1[4]; 370 String sn=s1[5]+" "+s1[6]; 371 if(!(records.validDateTimeSimple(sn)&&records.validDateTimeSimple(sm))) { 372 return -1; 373 } 374 375 String regex0 = "0791"; 376 String regex1 = "(079\\d)|0701"; 377 378 flag = s1[0].substring(2,6).matches(regex0);//判断功能 379 flag1 = s1[0].substring(2,6).matches(regex1);//判断功能 380 flag2 = s1[2].matches(regex0);//判断功能 381 flag3 = s1[2].matches(regex1);//判断功能 382 383 384 385 n=3; //省外 386 if(flag3) 387 n=2; //省内 388 if(flag2) 389 n=1; //市内 390 m=3; //省外 391 if(flag3) 392 m=2; //省内 393 if(flag2) 394 m=1; //市内 395 396 records.answerNumber=s1[1]; 397 records.begintime=s1[3]+" "+s1[4]; 398 records.endtime=s1[5]+" "+s1[6]; 399 records.chose=n; 400 user.setUserRecords(records); 401 402 if(m==3) { 403 for(int i=0;i<users.size();i++) { 404 if(users.get(i).number.equals(s1[1])) { 405 users.get(i).cost(records.sumtime()*0.3); 406 } 407 408 } 409 } 410 return 1; 411 } 412 } 413 414 415 public static class LandRule extends ChargeRules{ 416 417 } 418 public static class Phone extends Device{ 419 static double monthlyRent=15; 420 421 public int input1(ArrayList<User> users, User user,String[] s1,String s) throws ParseException { 422 boolean flag; 423 boolean flag1; 424 boolean flag2; 425 boolean flag3; 426 int n=0; 427 int m=0; 428 CommunicationRecord records =new CommunicationRecord(); 429 String sm=s1[4]+" "+s1[5]; 430 String sn=s1[6]+" "+s1[7]; 431 if(!(records.validDateTimeSimple(sn)&&records.validDateTimeSimple(sm))) { 432 return -1; 433 } 434 435 String regex0 = "0791"; 436 String regex1 = "(079\\d)|0701"; 437 438 flag = s1[1].matches(regex0);//判断功能 439 flag1 = s1[1].matches(regex1);//判断功能 440 flag2 = s1[3].matches(regex0);//判断功能 441 flag3 = s1[3].matches(regex1);//判断功能 442 443 n=3; //省外 444 if(flag1) 445 n=2; //省内 446 if(flag) 447 n=1; //市内 448 449 m=3; //省外 450 if(flag3) 451 m=2; //省内 452 if(flag2) 453 m=1; //市内 454 455 records.answerNumber=s1[2]; 456 records.begintime=s1[4]+" "+s1[5]; 457 records.endtime=s1[6]+" "+s1[7]; 458 records.chose=n; 459 records.chose1=m; 460 user.setUserRecords(records); 461 if(m==3) { 462 for(int i=0;i<users.size();i++) { 463 if(users.get(i).number.equals(s1[2])) { 464 users.get(i).cost(records.sumtime()*0.3); 465 } 466 467 } 468 } 469 470 471 return 1; 472 } 473 474 public int input2(User user,String[] s1,String s) { 475 boolean flag; 476 boolean flag1; 477 boolean flag2; 478 boolean flag3; 479 int n=0; 480 481 CommunicationRecord records =new CommunicationRecord(); 482 String sm=s1[3]+" "+s1[4]; 483 String sn=s1[5]+" "+s1[6]; 484 if(!(records.validDateTimeSimple(sn)&&records.validDateTimeSimple(sm))) { 485 return -1; 486 } 487 488 String regex0 = "0791"; 489 String regex1 = "(079\\d)|0701"; 490 491 flag = s1[1].matches(regex0);//判断功能 492 flag1 = s1[1].matches(regex1);//判断功能 493 flag2 = s1[2].substring(0,4).matches(regex0);//判断功能 494 flag3 = s1[2].substring(0,4).matches(regex1);//判断功能 495 496 n=3; //省外 497 if(flag1) 498 n=2; //省内 499 if(flag) 500 n=1; //市内 501 502 503 504 records.answerNumber=s1[2]; 505 506 records.begintime=s1[3]+" "+s1[4]; 507 records.endtime=s1[5]+" "+s1[6]; 508 records.chose=n; 509 510 user.setUserRecords(records); 511 return 1; 512 } 513 } 514 515 }
设计与分析:设计思路跟写上一题座机的差不多,毕竟我也是在那个代码的基础上改进的,主要是将一部分的判断代码写到别的类里去了,但是复用性也不高,因为都有一部分的改动,主要是添加了手机类,并且让设备类成为它的父类。主要是判断相关的功能比较繁琐。
采坑心得:时间花费的不太多,所以分数不高,因为遇上了端午,一直在准备回家的东西,确实有点没心思写作业了,一开始的很多时间也浪费了,确实有点遗憾。
改进建议:还是要多花点时间在作业上,在一些时候也要学会沉下心来去认认真真的写点东西,不要一直拖。
7-2 sdut-Collection-sort--C~K的班级(II)
这道题主要考察了对于容器HashMap的使用,集合容器,不会让元素重复出现,是个很好的特点。其中还有关于元素的排序,我是在网上学习借鉴了别人的方法写的。
以下是代码:
1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Comparator; 4 import java.util.HashMap; 5 import java.util.LinkedHashMap; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Scanner; 9 import java.util.Set; 10 11 12 public class Main { 13 14 public static void main(String[] args) { 15 HashMap<String,Student> students =new HashMap<String,Student>(); 16 Scanner in = new Scanner(System.in); 17 int n=in.nextInt(); 18 String s=new String(); 19 s=in.nextLine(); 20 for(int i=0;i<n;i++) { 21 s=in.nextLine(); 22 String[] ns=s.split(" "); 23 Student s1 =new Student(ns[0],ns[1],ns[2],ns[3]); 24 students.put(s1.num,s1); 25 } 26 27 28 HashMap<String,Student> sortHashMap = sortHashMap(students); 29 30 //返回所有value值 !!!这个很便捷。 31 System.out.println(sortHashMap.size()); 32 for(Student value :sortHashMap.values()) { 33 System.out.println(value.num+" "+value.name+" "+value.age+" "+value.sex); 34 35 } 36 } 37 38 39 40 41 public static HashMap<String,Student> sortHashMap(HashMap<String,Student> map) { 42 // 首先拿到 map 的键值对集合 43 Set<Map.Entry<String,Student>> entrySet = map.entrySet(); 44 // 将 set 集合转为 List 集合,为什么,为了使用工具类的排序方法 45 List<Map.Entry<String,Student>> list = new ArrayList<Map.Entry<String,Student>>(entrySet); 46 // 使用 Collections 集合工具类对 list 进行排序,排序规则使用匿名内部类来实现 47 Collections.sort(list, new Comparator<Map.Entry<String,Student>>() { 48 @Override 49 public int compare(Map.Entry<String,Student> o1, Map.Entry<String,Student> o2) { 50 //按照要求根据 User 的 age 的升序进行排,如果是倒序就是o2-o1 51 int flag = new Integer(o1.getValue().num.compareTo(o2.getValue().num)); 52 return flag; 53 } 54 }); 55 //创建一个新的有序的 HashMap 子类的集合 56 LinkedHashMap<String,Student> linkedHashMap = new LinkedHashMap<String,Student>(); 57 //将 List 中的数据存储在 LinkedHashMap 中 58 for (Map.Entry<String,Student> entry : list) { 59 linkedHashMap.put(entry.getKey(), entry.getValue()); 60 } 61 //返回结果 62 return linkedHashMap; 63 } 64 65 } 66 class Student { 67 String name; 68 String num; 69 String sex; 70 String age; 71 public Student(String num, String name,String age, String sex) { 72 super(); 73 this.age=age; 74 this.name = name; 75 this.num = num; 76 this.sex = sex; 77 } 78 79 }
7-3 阅读程序,按照题目需求修改程序
这道题更简单了,老师已经给了大部分代码,略微修改就好了。
以下是代码
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 //1、导入相关包 5 6 //定义员工类 7 class Employee { 8 9 private String name; 10 private int age; 11 12 public Employee() { 13 super(); 14 } 15 16 public Employee(String name, int age) { 17 super(); 18 name=name.substring(0,2); 19 this.name = name; 20 this.age = age; 21 } 22 public String getName() { 23 return name; 24 } 25 26 public void setName(String name) { 27 this.name = name; 28 } 29 30 public int getAge() { 31 return age; 32 } 33 34 public void setAge(int age) { 35 this.age = age; 36 } 37 } 38 39 //主函数 40 public class Main { 41 42 public static void main(String[] args) { 43 // 1、创建有序集合对象 44 ArrayList<Employee> c =new ArrayList<Employee>(); 45 46 // 创建3个员工元素对象 47 Scanner sc = new Scanner(System.in); 48 for (int i = 0; i < 3; i++) { 49 String employeeName = sc.nextLine(); 50 int employeeAge = Integer.parseInt(sc.nextLine()); 51 Employee employee = new Employee(employeeName, employeeAge); 52 c.add(employee); 53 } 54 55 56 //3、遍历 57 for (int i=0;i<3;i++) { 58 System.out.println(c.get(i).getName() + "---" + c.get(i).getAge()); 59 } 60 } 61 62 }
nchu-software-oop-2022-8
7-1 电信计费系列3-短信计费
这个应该算的上是在座机的基础上改进的,因为需求比较简单,老师的要求也是只考虑短信计费,不考虑通信费用以及月租费。需求比较简单。但我为了方便还是在上一题的基础上改的。
以下是代码
1 import java.util.Comparator; 2 import java.text.DecimalFormat; 3 import java.text.ParseException; 4 import java.util.ArrayList; 5 import java.util.Collections; 6 import java.util.Scanner; 7 import java.text.DateFormat; 8 import java.text.SimpleDateFormat; 9 import java.util.Date; 10 import java.text.DecimalFormat; 11 public class Main { 12 13 public static void main(String[] args) throws ParseException { 14 ArrayList <User> users= new ArrayList <User> (); 15 User user = null; 16 Scanner in = new Scanner(System.in); 17 String s=new String(); 18 Device diver = null ; 19 boolean flag; 20 boolean flag1; 21 boolean flag2; 22 boolean f=false; 23 String[] s1; 24 String regexm = "u-0791\\d{7,8} 0"; //座机 25 String regexp = "u-1\\d{10} 1"; //手机 26 String regext = "u-1\\d{10} 3"; //短信 27 String regexn = "t-\\d{10,13} \\d{10,13} \\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d \\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d"; 28 //座机打座机 29 String regexn0 = "t-\\d{11,12} 1\\d{10} 0\\d{2,3} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2}"; 30 //座机打手机 31 String regexn1 ="t-\\d{11} 0\\d{2,3} 1\\d{10} 0\\d{2,3} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2}"; 32 //手机互打 33 String regexn2 ="t-1\\d{10} 0\\d{2,3} 0[1-9]{1}\\d{9,10} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2} \\d{4}.\\d{1,2}.\\d{1,2}+ \\d{1,2}:\\d{1,2}:\\d{1,2}"; 34 //手机打座 35 String text ="m-1\\d{10} 1\\d{10} [A-Za-z0-9 ,.]+"; 36 int n; 37 s=in.nextLine(); 38 do { 39 flag =s.matches(regext)||s.matches(regexm)||s.matches(regexp);//判断功能 40 s1=s.split(" "); 41 if(!flag) { 42 s=in.nextLine(); 43 while(s.equals("")) 44 s=in.nextLine(); 45 continue; 46 } 47 user =new User(); 48 49 50 user.getNumber(s1[0].substring(2)); 51 52 for(int i=0;i<users.size();i++) { 53 if(user.number.equals(users.get(i).retNumber())) { 54 f=true; 55 break; 56 } 57 } 58 if(f) { 59 s=in.nextLine(); 60 while(s.equals("")) 61 s=in.nextLine(); 62 f=false; 63 continue; 64 } 65 users.add(user); 66 s=in.nextLine(); 67 while(s.equals("")) 68 s=in.nextLine(); 69 user.getDevice(Integer.parseInt(s1[1])); 70 }while(s.charAt(0)=='u'); 71 72 do { 73 s1=s.split(" "); 74 flag = s.matches(text)||s.matches(regexn)||s.matches(regexn0)||s.matches(regexn1)||s.matches(regexn2);//判断功能 75 if(!flag) { 76 s=in.nextLine(); 77 continue; 78 } 79 80 user=null; 81 for(int i=0;i<users.size();i++) { 82 if(s1[0].substring(2).equals(users.get(i).retNumber())) { 83 user=users.get(i); 84 break; 85 } 86 87 } 88 if(user==null) { 89 // String regex0 = "0791"; 90 // String regex1 = "(079\\d)|0701"; 91 // boolean flagx = s1[3].matches(regex0)||s1[2].matches(regex0);//判断功能 92 // boolean flagy = s1[3].matches(regex1)||s1[2].matches(regex0);//判断功能 93 // int m=3;//省外 94 // if(flagy) 95 // m=2; //省内 96 // if(flagx) 97 // m=1; //市内 98 99 // for(int i=0;i<users.size();i++) { 100 // if(s1[2].equals(users.get(i).retNumber())) { 101 // if(m==3) { 102 // CommunicationRecord records =new CommunicationRecord(); 103 // records.begintime=s1[4]+" "+s1[5]; 104 // records.endtime=s1[6]+" "+s1[7]; 105 // users.get(i).cost(records.sumtime()*0.3); 106 // } 107 // } 108 // if(s1[1].equals(users.get(i).retNumber())) { 109 // if(m==3) { 110 // CommunicationRecord records =new CommunicationRecord(); 111 // records.begintime=s1[3]+" "+s1[4]; 112 // records.endtime=s1[5]+" "+s1[6]; 113 // users.get(i).cost(records.sumtime()*0.3); 114 // } 115 // } 116 // } 117 s=in.nextLine(); 118 continue; 119 } 120 if( s.matches(text)) { 121 user.message(s.substring(26)); 122 } 123 if(s.matches(regexn)) { 124 diver =new Landlines(); 125 int i=((Landlines)diver).input1(user, s1, s); 126 if(i==-1) { 127 s=in.nextLine(); 128 continue; 129 } 130 } 131 else if(s.matches(regexn0)) { //考虑漫游 132 diver=new Landlines(); 133 int i=((Landlines)diver).input2(users,user, s1, s); 134 if(i==-1) { 135 s=in.nextLine(); 136 continue; 137 } 138 } 139 else if(s.matches(regexn1)) { //考虑漫游 140 diver=new Phone(); 141 int i=((Phone)diver).input1(users,user, s1, s); 142 if(i==-1) { 143 s=in.nextLine(); 144 continue; 145 } 146 } 147 else if(s.matches(regexn2)) { 148 diver=new Phone(); 149 int i=((Phone)diver).input2(user, s1, s); 150 if(i==-1) { 151 s=in.nextLine(); 152 continue; 153 } 154 } 155 156 s=in.nextLine(); 157 while(s.equals("")) 158 s=in.nextLine(); 159 }while(!s.equals("end")); 160 161 Collections.sort(users,new UserComparator()); 162 163 DecimalFormat df =new DecimalFormat("0.0##"); 164 165 for(int j=0;j<users.size();j++) { 166 // users.get(j).calcost(); 167 users.get(j).messagecost(); 168 System.out.println(users.get(j).number+" "+df.format(users.get(j).getCost()) +" "+df.format(users.get(j).getBalance())); 169 } 170 } 171 public static class User { 172 ArrayList <CommunicationRecord> userRecords =new ArrayList <CommunicationRecord>(); 173 double balance=100; 174 ChargeRules chargeRules =new ChargeRules(); 175 String number; 176 Device device; 177 double sumcost=0; 178 MessageRecord text=new MessageRecord(); 179 public void setUserRecords(CommunicationRecord Records) { 180 userRecords.add(Records); 181 } 182 public void setChargeRules(ChargeRules land) { 183 this.chargeRules=chargeRules; 184 } 185 public void calcost() throws ParseException { 186 for(int i=0;i<userRecords.size();i++) { 187 double time =userRecords.get(i).sumtime(); 188 if(userRecords.get(i).chose1==0) { 189 if(userRecords.get(i).chose==1) 190 cost(time*0.1); 191 192 else if(userRecords.get(i).chose==2) 193 cost(time*0.3); 194 195 else 196 cost(time*0.6); 197 } 198 199 else { 200 if(userRecords.get(i).chose==1) { 201 if(userRecords.get(i).chose1==1) 202 cost(time*0.1); 203 else if(userRecords.get(i).chose1==2) 204 cost(time*0.2); 205 206 else 207 cost(time*0.3); 208 } 209 else if(userRecords.get(i).chose==2) { 210 cost(time*0.3); 211 } 212 else if(userRecords.get(i).chose==3) { 213 cost(time*0.6); 214 } 215 216 } 217 } 218 } 219 public void message(String s) { 220 221 text.jisuan(s); 222 223 } 224 public void messagecost() { 225 cost(text.cost(text.num)); 226 } 227 public void getNumber(String s) { 228 this.number=s; 229 } 230 public String retNumber() { 231 return this.number; 232 } 233 234 public void cost(double x) { 235 this.balance=this.balance -x; 236 sumcost+=x; 237 } 238 public double getBalance() { 239 return balance; 240 } 241 public double getCost() { 242 return sumcost; 243 } 244 public void getDevice(int x) { 245 if(x==0) { 246 device =new Landlines(); 247 LandRule land =new LandRule(); 248 setChargeRules(land); 249 balance=balance-Landlines.monthlyRent; 250 } 251 if(x==1) { 252 device =new Phone(); 253 LandRule land =new LandRule(); 254 setChargeRules(land); 255 balance=balance-Phone.monthlyRent; 256 } 257 if(x==3) { 258 device =new Phone(); 259 LandRule land =new LandRule(); 260 setChargeRules(land); 261 } 262 } 263 264 } 265 266 267 public static class ChargeRules { 268 public double calCost(ArrayList<CallRecord> callRecords){ 269 return 0; 270 } 271 } 272 public static class UserRecords { 273 274 } 275 public static class UserComparator implements Comparator<User>{ 276 277 @Override 278 public int compare(User s, User s1) { 279 int flag = new Integer(s.number.compareTo(s1.number)); 280 return flag; 281 } 282 283 } 284 public static class Device { 285 double monthlyRent; 286 ArrayList <ChargeRules> chargeRules=new ArrayList <ChargeRules>(); 287 288 public void getChargeRules(ArrayList <ChargeRules> chargeRules) { 289 this.chargeRules =chargeRules; 290 } 291 } 292 public static class CommunicationRecord { 293 String callingNumber; 294 String answerNumber; 295 String begintime; 296 String endtime; 297 int chose=0; 298 int chose1=0; 299 int month; 300 301 302 public static boolean validDateTimeSimple(String dateTime) { 303 if(dateTime == null ) { 304 return false; 305 } 306 DateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 307 df.setLenient(false);//表示严格验证 308 309 try { 310 df.parse(dateTime); 311 } catch (ParseException e) { 312 return false; 313 } 314 return true; 315 } 316 317 public long sumtime() throws ParseException { 318 319 //一、时间减法可以用Calendar对象加法中,把第二个参数换成负数可以时间,这边就不列举了;使用第二种方式做时间减法 320 //1、指定两个Date对象,业务中可以以参数的形式传递,不用new当前时间。 321 DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 322 String nowday = this.endtime; 323 Date nowDate = dateFormat.parse(nowday);//当前时间 324 String yesterday = this.begintime; 325 Date yesterdayDate = dateFormat.parse(yesterday);//假设一个时间,工作业务中可以参数传递的形式来获取。 326 //2、时间戳相减 327 long yesterdayDateTime = yesterdayDate.getTime();//昨天的时间戳 328 long nowDateTime = nowDate.getTime();//当前时间戳 329 long result = nowDateTime - yesterdayDateTime;//毫秒 330 331 long diffSecond = result / 1000; //1000毫秒等于1秒 332 333 //4、换算成分钟 334 long diffMinute = result / 60000 ;//60秒等于1分钟 335 if(result%60000!=0){ 336 diffMinute=diffMinute+1; 337 } 338 return diffMinute; 339 } 340 } 341 public static class CallRecord extends CommunicationRecord{ 342 String callingAddressAreaCode; 343 String answerAddressAreaCode; 344 String begintime; 345 String endtime; 346 } 347 public static class Landlines extends Device{ 348 static double monthlyRent=20; 349 350 public int input1(User user,String[] s1,String s) { 351 352 boolean flag; 353 boolean flag1; 354 boolean flag2; 355 356 int n=0; 357 CommunicationRecord records =new CommunicationRecord(); 358 String sm=s1[2]+" "+s1[3]; 359 String sn=s1[4]+" "+s1[5]; 360 if(!(records.validDateTimeSimple(sn)&&records.validDateTimeSimple(sm))) { 361 return -1; 362 } 363 364 String regex0 = "0791"; 365 String regex1 = "(079\\d)|0701"; 366 367 flag = s1[1].substring(0,4).matches(regex0);//判断功能 368 flag1 = s1[1].substring(0,4).matches(regex1);//判断功能 369 370 n=3; 371 if(flag1) 372 n=2; 373 if(flag) 374 n=1; 375 376 records.answerNumber=s1[1]; 377 records.begintime=s1[2]+" "+s1[3]; 378 records.endtime=s1[4]+" "+s1[5]; 379 records.chose=n; 380 user.setUserRecords(records); 381 return 1; 382 } 383 public int input2(ArrayList<User> users, User user,String[] s1,String s) throws ParseException { 384 385 boolean flag; 386 boolean flag1; 387 boolean flag2; 388 boolean flag3; 389 int m=0; 390 int n=0; 391 CommunicationRecord records =new CommunicationRecord(); 392 String sm=s1[3]+" "+s1[4]; 393 String sn=s1[5]+" "+s1[6]; 394 if(!(records.validDateTimeSimple(sn)&&records.validDateTimeSimple(sm))) { 395 return -1; 396 } 397 398 String regex0 = "0791"; 399 String regex1 = "(079\\d)|0701"; 400 401 flag = s1[0].substring(2,6).matches(regex0);//判断功能 402 flag1 = s1[0].substring(2,6).matches(regex1);//判断功能 403 flag2 = s1[2].matches(regex0);//判断功能 404 flag3 = s1[2].matches(regex1);//判断功能 405 406 407 408 n=3; //省外 409 if(flag3) 410 n=2; //省内 411 if(flag2) 412 n=1; //市内 413 m=3; //省外 414 if(flag3) 415 m=2; //省内 416 if(flag2) 417 m=1; //市内 418 419 records.answerNumber=s1[1]; 420 records.begintime=s1[3]+" "+s1[4]; 421 records.endtime=s1[5]+" "+s1[6]; 422 records.chose=n; 423 user.setUserRecords(records); 424 425 if(m==3) { 426 for(int i=0;i<users.size();i++) { 427 if(users.get(i).number.equals(s1[1])) { 428 users.get(i).cost(records.sumtime()*0.3); 429 } 430 431 } 432 } 433 return 1; 434 } 435 } 436 437 438 public static class LandRule extends ChargeRules{ 439 440 } 441 public static class Phone extends Device{ 442 static double monthlyRent=15; 443 444 public int input1(ArrayList<User> users, User user,String[] s1,String s) throws ParseException { 445 boolean flag; 446 boolean flag1; 447 boolean flag2; 448 boolean flag3; 449 int n=0; 450 int m=0; 451 CommunicationRecord records =new CommunicationRecord(); 452 String sm=s1[4]+" "+s1[5]; 453 String sn=s1[6]+" "+s1[7]; 454 if(!(records.validDateTimeSimple(sn)&&records.validDateTimeSimple(sm))) { 455 return -1; 456 } 457 458 String regex0 = "0791"; 459 String regex1 = "(079\\d)|0701"; 460 461 flag = s1[1].matches(regex0);//判断功能 462 flag1 = s1[1].matches(regex1);//判断功能 463 flag2 = s1[3].matches(regex0);//判断功能 464 flag3 = s1[3].matches(regex1);//判断功能 465 466 n=3; //省外 467 if(flag1) 468 n=2; //省内 469 if(flag) 470 n=1; //市内 471 472 m=3; //省外 473 if(flag3) 474 m=2; //省内 475 if(flag2) 476 m=1; //市内 477 478 records.answerNumber=s1[2]; 479 records.begintime=s1[4]+" "+s1[5]; 480 records.endtime=s1[6]+" "+s1[7]; 481 records.chose=n; 482 records.chose1=m; 483 user.setUserRecords(records); 484 if(m==3) { 485 for(int i=0;i<users.size();i++) { 486 if(users.get(i).number.equals(s1[2])) { 487 users.get(i).cost(records.sumtime()*0.3); 488 } 489 490 } 491 } 492 493 494 return 1; 495 } 496 497 public int input2(User user,String[] s1,String s) { 498 boolean flag; 499 boolean flag1; 500 boolean flag2; 501 boolean flag3; 502 int n=0; 503 504 CommunicationRecord records =new CommunicationRecord(); 505 String sm=s1[3]+" "+s1[4]; 506 String sn=s1[5]+" "+s1[6]; 507 if(!(records.validDateTimeSimple(sn)&&records.validDateTimeSimple(sm))) { 508 return -1; 509 } 510 511 String regex0 = "0791"; 512 String regex1 = "(079\\d)|0701"; 513 514 flag = s1[1].matches(regex0);//判断功能 515 flag1 = s1[1].matches(regex1);//判断功能 516 flag2 = s1[2].substring(0,4).matches(regex0);//判断功能 517 flag3 = s1[2].substring(0,4).matches(regex1);//判断功能 518 519 n=3; //省外 520 if(flag1) 521 n=2; //省内 522 if(flag) 523 n=1; //市内 524 525 526 527 records.answerNumber=s1[2]; 528 529 records.begintime=s1[3]+" "+s1[4]; 530 records.endtime=s1[5]+" "+s1[6]; 531 records.chose=n; 532 533 user.setUserRecords(records); 534 return 1; 535 } 536 } 537 538 static class MessageRecord extends CommunicationRecord{ 539 int num=0; 540 541 public void jisuan(String s) { 542 int l=s.length()/10; 543 if(s.length()%10!=0) 544 l=l+1; 545 num +=l; 546 } 547 548 public double cost(int x) { 549 if(x<=3) { 550 return x*0.1; 551 } 552 else if(x>3&&x<=5) { 553 return 0.3+(x-3)*0.2; 554 } 555 else { 556 return 0.3+0.4+(x-5)*0.3; 557 } 558 } 559 } 560 561 }
设计与分析:我只是在原有的基础上增添了一些新的判断语句,还有改进计算花费的方式。增添了MessageRecord类去记录短信,方便计算费用。
采坑心得:判断短信字符串的方式一开始就是/S,后面发现不行,仔细一看题才发现是:短信内容只能由数字、字母、空格、英文逗号、英文句号组成。后面又改进。而且发现自己设计类的时候,一开始是考虑的比较周全的,写着写着自己居然会忘记一些东西,感觉以后自己要多用记事本了。
改进建议:多写注释吧。
7-2 编写一个类Shop(商店)、内部类InnerCoupons(内部购物券)
这道题比较简单,因为老师把需求写的比较详细,而且把结构的设计方法都告诉我们了。考了成员内部类的相关内容。
以下是代码:
1 import java.util.Scanner; 2 3 public class Main{ 4 public static void main(String[] args) { 5 Shop myshop =new Shop(); 6 Scanner in =new Scanner(System.in); 7 int x= in.nextInt(); 8 myshop.setMilkCount(x); 9 10 myshop.setMilkCount(myshop.getMilkCount()-myshop.coupons50.buy()); 11 System.out.println("使用了面值为"+myshop.coupons50.value+"的购物券进行支付"); 12 System.out.println("牛奶还剩"+myshop.getMilkCount()+"箱"); 13 myshop.setMilkCount(myshop.getMilkCount()-myshop.coupons100.buy()); 14 System.out.println("使用了面值为"+myshop.coupons100.value+"的购物券进行支付"); 15 System.out.println("牛奶还剩"+myshop.getMilkCount()+"箱"); 16 17 } 18 } 19 class Shop { 20 private int milkCount; 21 InnerCoupons coupons50; 22 InnerCoupons coupons100; 23 public Shop() { 24 super(); 25 this.coupons50 =new InnerCoupons(50); 26 this.coupons100 =new InnerCoupons(100); 27 } 28 29 public void setMilkCount(int x ) { 30 this.milkCount=x; 31 } 32 public int getMilkCount() { 33 return this.milkCount; 34 } 35 36 public class InnerCoupons { 37 public int value; 38 39 public InnerCoupons(int value) { 40 super(); 41 this.value = value; 42 } 43 public int buy() { 44 45 return value/50; 46 } 47 48 } 49 50 } 51 52
7-3 动物发声模拟器(多态)
这道题更简单了,老师把基本模板给了我们,只要我们把一些细节添加上去就好了。主要考察的还是多态的运用。
以下是代码:
1 //动物发生模拟器. 请在下面的【】处添加代码。 2 public class Main{ 3 public static void main(String[] args) { 4 Cat cat = new Cat(); 5 Dog dog = new Dog(); 6 Goat goat = new Goat(); 7 speak(cat); 8 speak(dog); 9 speak(goat); 10 } 11 static //定义静态方法speak() 12 void speak(Animal animal) { 13 animal.shout(); 14 } 15 16 } 17 18 //定义抽象类Animal 19 abstract class Animal{ 20 public abstract void getAnimalclass(); 21 public abstract void shout(); 22 } 23 //基于Animal类,定义猫类Cat,并重写两个抽象方法 24 class Cat extends Animal{ 25 26 @Override 27 public void getAnimalclass() { 28 // TODO Auto-generated method stub 29 30 } 31 32 @Override 33 public void shout() { 34 System.out.println("猫的叫声:喵喵"); 35 36 } 37 38 } 39 //基于Animal类,定义狗类Dog,并重写两个抽象方法 40 class Dog extends Animal{ 41 42 @Override 43 public void getAnimalclass() { 44 // TODO Auto-generated method stub 45 46 } 47 48 @Override 49 public void shout() { 50 System.out.println("狗的叫声:汪汪"); 51 52 } 53 54 } 55 //基于Animal类,定义山羊类Goat,并重写两个抽象方法 56 class Goat extends Animal{ 57 58 @Override 59 public void getAnimalclass() { 60 // TODO Auto-generated method stub 61 62 } 63 64 @Override 65 public void shout() { 66 System.out.println("山羊的叫声:咩咩"); 67 68 } 69 70 }
总结
这次的大作业总体来说还是比之前简单很多的,但知识点还是不少的,有关于类之间的运用,父类与之类的合理运用,多态的使用,容器的详细使用方法和小技巧,还有最近经常遇见的正则表达式的使用。这次我是敲好再贴上来哒!突然意识到我们好像到了要跟面向对象的学习说再见的时候了,但我知道这只是暂时的,后续还是会有很多机会去进一步的学习的(我知道还有java的课设)。没有人能限制我们的学习。回顾这一学期,确实很多时候会被java的题目搞崩溃过,觉得很难,跟c语言很不一样,觉得难度突然升级。但我们还是熬过来了,并且真的认认真真的学到了很多东西。还是觉得练习最重要,多多练习才会让自己熟悉java。还有就是多花时间学习东西吧,她们不会辜负我们的。以后也要继续加油鸭!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统