(一)前言
(1)知识点
1.ArrayList类的使用
2.输入字符串的处理
3.类设计的搭建与实现
4.SimpleDateFormat类实现输入时间格式化处理
(2)题量
三次以电信计费系列为主的pta,其他题目都比较基础。
(3)难度
相比之前的四边形所需的算法部分简单很多,主要是程序设计,但是代码量还是很大,还是要花时间,由于我水平有限,借鉴了很多网上代码。虽然课程结束了,之后假期还是要花时间。
(二)设计与分析
7-1 电信计费系列1-座机计费 分数 80 作者 蔡轲 单位 南昌航空大学实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
南昌市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
建议类图:
参见图1、2、3,可根据理解自行调整:
1 import java.util.*; 2 import java.text.*; 3 public class Main { 4 public static void main(String[] args) throws ParseException { 5 Scanner input = new Scanner(System.in); 6 ArrayList<String> informationString=new ArrayList<>();//字符串 7 ArrayList<char[]> informationChar=new ArrayList<>();//字符串转为字符 8 ArrayList<User> user=new ArrayList<>(); //用户列表 9 String str="end"; 10 String regex1 = "^u-(1[0-9]{10} 1|0[0-9]{9,11} 0)$"; 11 String regex2 = "^t-1[0-9]{10} 0[0-9]{2,3} 1[0-9]{10} 0[0-9]{2,3} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//手机打手机 12 String regex3 = "^t-0[0-9]{9,11} 1[0-9]{10} 0[0-9]{2,3} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//座机打手机 13 String regex4 = "^t-1[0-9]{10} 0[0-9]{2,3} 0[0-9]{9,11} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//手机打座机 14 String regex5 = "^t-0[0-9]{9,11} 0[0-9]{9,11} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//座机打座机 15 String regex6 = "^[1-9]\\d{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$"; 16 17 while(true) { //输入信息并获取 18 String cin=input.nextLine(); 19 if(cin.equals(str)) { //判断输入的是否为end结束符 20 break; 21 } 22 informationString.add(cin); 23 } 24 int isSecondRow1 = 0; //在类型1下记录用户个数 25 boolean isSame1 = false; //判断是否用户相同 26 for(int j=0;j<informationString.size();j++) { //将信息进行分割 27 if(informationString.get(j).toCharArray().length!=0) { 28 informationChar.add(informationString.get(j).toCharArray());//将字符串变为字符数组 29 boolean flag1 = informationString.get(j).matches(regex1); 30 boolean flag2 = informationString.get(j).matches(regex2);//手机打手机 31 boolean flag3 = informationString.get(j).matches(regex3);//座机打手机 32 boolean flag4 = informationString.get(j).matches(regex4);//手机打座机 33 boolean flag5 = informationString.get(j).matches(regex5);//座机打座机 34 //输入类型1,以u开头进行用户开户 35 //if(informationChar.get(j)[0]=='u') { 36 if(flag1==true) { 37 int number=-1; 38 isSecondRow1++; 39 String []thirdString = informationString.get(j).split("[- ]");//将第一种信息分割成三个要第二个 40 if(isSecondRow1>=2) { //从第二个用户开始进行判断是否重复与比大小 41 isSame1 = false; 42 int recordPlace =0; //记录插入时列表的位置 43 for(int k=0;k<user.size();k++) {//在用户列表从开始到结束进行判断 44 if(thirdString[1].equals(user.get(k).getNumber())) {//判断第二个用户是否与用户列表中的用户相同 45 isSame1 = true; //相同直接跳出查询 46 break; 47 } 48 if(thirdString[1].charAt(0)==user.get(k).getNumber().charAt(0)) { 49 if(thirdString[1].compareTo(user.get(k).getNumber())<0 ) { 50 //System.out.println("here"); 51 recordPlace=k; 52 } 53 else { 54 recordPlace++; //使用这个目的为了不让程序继续无意义的比下去而浪费时间,在之后没有在用到 55 } 56 } 57 else { 58 if( thirdString[1].charAt(0)>user.get(k).getNumber().charAt(0) ) { 59 recordPlace++; 60 } 61 } 62 } 63 if(!isSame1) { //用户不同,需要添加到用户列表里 64 user.add(recordPlace,new User());//给用户开户并按照大小顺序进行插入 65 user.get(recordPlace).setNumber(thirdString[1]); 66 if( thirdString[2].charAt(0)=='0' ) user.get(recordPlace).selectCharge='0'; 67 if( thirdString[2].charAt(0)=='1' ) user.get(recordPlace).selectCharge='1'; 68 } 69 } 70 else { //第一个用户,直接放到用户列表中;从第二个开始进行判断 71 user.add(new User());//给用户开户 72 user.get(++number).setNumber(thirdString[1]); 73 if( thirdString[2].charAt(0)=='0' ) user.get(number).selectCharge='0'; 74 if( thirdString[2].charAt(0)=='1' ) user.get(number).selectCharge='1'; 75 } 76 } 77 78 else { 79 String []fiveString = informationString.get(j).split("[- ]");//将第二种信息分割成七个 80 if(flag5) { //座机打座机 81 for(int k=0;k<user.size();k++) { //在用户列表从开始到结束进行判断 82 if(fiveString[1].equals(user.get(k).getNumber())) { //用户的通讯信息与号码进行匹配 83 CallRecord callRecord1 = new CallRecord(); //通话记录 84 callRecord1.setCallingNumber(fiveString[1]);//拨打电话号 85 callRecord1.setAnswerNumber(fiveString[2]);//就听电话号 86 callRecord1.setCallingAddressAreaCode(fiveString[1].substring(0,4));//拨打电话的区号 87 callRecord1.setAnswerAddressAreaCode(fiveString[2].substring(0,4));//接收电话的区号 88 String startTime1 =fiveString[3]+" "+fiveString[4]; //传入开始时间 89 String endTime =fiveString[5]+" "+fiveString[6]; //传入结束时间 90 boolean flag6=startTime1.matches(regex6); 91 boolean flag7=endTime.matches(regex6); 92 if(flag6==true&&flag7==true) { 93 SimpleDateFormat totalUse1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//固定格式 94 Date ss1 = totalUse1.parse(startTime1); 95 callRecord1.setStartTime(ss1); 96 Date ss2 = totalUse1.parse(endTime); 97 callRecord1.setEndTime(ss2); 98 if( (judgeQuHao(fiveString[1],0)==judgeQuHao(fiveString[2],0)&&(judgeQuHao(fiveString[1],0)==1)||judgeQuHao(fiveString[1],0)==2) ) { //市内1、省内2、国内3 99 user.get(k).getUserRecords().addCallingInCityRecords(callRecord1); 100 } 101 else if( (judgeQuHao(fiveString[1],0)!=judgeQuHao(fiveString[2],0))&&( judgeQuHao(fiveString[1],0)==1||judgeQuHao(fiveString[1],0)==2 )&&( judgeQuHao(fiveString[2],0)==1||judgeQuHao(fiveString[2],0)==2 ) ) { 102 user.get(k).getUserRecords().addCallingInProvinceRecords(callRecord1); 103 } 104 else { 105 user.get(k).getUserRecords().addCallingInLandRecords(callRecord1); 106 } 107 } 108 } 109 } 110 } 111 // if(regex3==true) {//座机与手机互打 112 if(flag3) {//座机在前面 113 boolean judge1=true; 114 for(int k=0;k<user.size();k++) { //在用户列表从开始到结束进行判断 115 if(fiveString[1].equals(user.get(k).getNumber())) { //用户的通讯信息与号码进行匹配 116 judge1=false; //当打电话的人没开户时 117 CallRecord callRecord1 = new CallRecord(); //创建通话记录 118 callRecord1.setCallingNumber(fiveString[1]);//拨打电话号 119 callRecord1.setAnswerNumber(fiveString[2]);//接听电话号 120 callRecord1.setCallingAddressAreaCode(fiveString[1].substring(0,4));//拨打电话的区号 121 callRecord1.setAnswerAddressAreaCode(fiveString[3]);//接收电话的区号 122 String startTime1 =fiveString[4]+" "+fiveString[5]; //传入开始时间 123 String endTime =fiveString[6]+" "+fiveString[7]; //传入结束时间 124 boolean flag6=startTime1.matches(regex6); 125 boolean flag7=endTime.matches(regex6); 126 if(flag6==true&&flag7==true) { 127 SimpleDateFormat totalUse1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//固定格式 128 Date ss1 = totalUse1.parse(startTime1); 129 callRecord1.setStartTime(ss1); 130 Date ss2 = totalUse1.parse(endTime); 131 callRecord1.setEndTime(ss2); 132 //判断接听区号 133 if( judgeQuHao(fiveString[1],0)==1 ){ 134 //判断接电话的人在哪里 135 //接电话的人也在市内,则将打电话人的用户记录添加到市内拨打记录列表里 136 if( judgeQuHao(fiveString[3],1)==1 ) { 137 user.get(k).getUserRecords().addCallingInCityRecords(callRecord1); 138 } 139 //接电话的人在省内,则将打电话人的用户记录添加到省内拨打记录列表里 140 else if( judgeQuHao(fiveString[3],1)==2 ) { 141 user.get(k).getUserRecords().addCallingInProvinceRecords(callRecord1); 142 } 143 //接电话的人在省外,则将打电话人的用户记录添加到省外拨打记录列表里,并将接电话的人记录添加到省外 144 else if( judgeQuHao(fiveString[3],1)==3 ) { 145 user.get(k).getUserRecords().addCallingInLandRecords(callRecord1); 146 int q; 147 for(q=0;q<user.size();q++) { 148 if( fiveString[2].equals(user.get(q).getNumber()) ) break; 149 } 150 if(q!=user.size()) { 151 user.get(q).getUserRecords().addAnswerLandRecords(callRecord1); 152 } 153 } 154 } 155 } 156 } 157 } 158 if(judge1==true) { 159 CallRecord callRecord1 = new CallRecord(); //创建通话记录 160 callRecord1.setCallingNumber(fiveString[1]);//拨打电话号 161 callRecord1.setAnswerNumber(fiveString[2]);//接听电话号 162 callRecord1.setCallingAddressAreaCode(fiveString[1].substring(0,4));//拨打电话的区号 163 callRecord1.setAnswerAddressAreaCode(fiveString[3]);//接收电话的区号 164 String startTime1 =fiveString[4]+" "+fiveString[5]; //传入开始时间 165 String endTime =fiveString[6]+" "+fiveString[7]; //传入结束时间 166 boolean flag6=startTime1.matches(regex6); 167 boolean flag7=endTime.matches(regex6); 168 if(flag6==true&&flag7==true) { 169 SimpleDateFormat totalUse1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//固定格式 170 Date ss1 = totalUse1.parse(startTime1); 171 callRecord1.setStartTime(ss1); 172 Date ss2 = totalUse1.parse(endTime); 173 callRecord1.setEndTime(ss2); 174 //打电话的人在省外,走省外漫游, 175 if( judgeQuHao(fiveString[3],1)==3 ) { 176 int q; 177 for(q=0;q<user.size();q++) { 178 if( fiveString[2].equals(user.get(q).getNumber()) ) break; 179 } 180 if(q!=user.size()) user.get(q).getUserRecords().addAnswerLandRecords(callRecord1); 181 } 182 } 183 } 184 } 185 //手机打座机 186 if(flag4==true) { 187 for(int k=0;k<user.size();k++) { //在用户列表从开始到结束进行判断 188 if(fiveString[1].equals(user.get(k).getNumber())) { //用户的通讯信息与号码进行匹配 189 CallRecord callRecord1 = new CallRecord(); //创建通话记录 190 callRecord1.setCallingNumber(fiveString[1]);//拨打电话号 191 callRecord1.setAnswerNumber(fiveString[3]);//接听电话号 192 callRecord1.setCallingAddressAreaCode(fiveString[2]);//拨打电话的区号 193 callRecord1.setAnswerAddressAreaCode(fiveString[3].substring(0,4));//接收电话的区号 194 String startTime1 =fiveString[4]+" "+fiveString[5]; //传入开始时间 195 String endTime =fiveString[6]+" "+fiveString[7]; //传入结束时间 196 boolean flag6=startTime1.matches(regex6); 197 boolean flag7=endTime.matches(regex6); 198 if(flag6==true&&flag7==true) { 199 SimpleDateFormat totalUse1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//固定格式 200 Date ss1 = totalUse1.parse(startTime1); 201 callRecord1.setStartTime(ss1); 202 Date ss2 = totalUse1.parse(endTime); 203 callRecord1.setEndTime(ss2); 204 //判断接听区号 205 if( judgeQuHao(fiveString[2],1)==1 ){ 206 user.get(k).getUserRecords().addNewCallingInCityRecords(callRecord1); //改了 207 } 208 else if( judgeQuHao(fiveString[2],1)==2 ) { 209 user.get(k).getUserRecords().addCallingInProvinceRecords(callRecord1); 210 } 211 else if(judgeQuHao(fiveString[2],1)==3) { 212 user.get(k).getUserRecords().addCallingInLandRecords(callRecord1); 213 } 214 } 215 } 216 } 217 } 218 // } 219 if(flag2) {//手机打手机 220 boolean judge1=true; 221 for(int k=0;k<user.size();k++) { //在用户列表从开始到结束进行判断 222 if(fiveString[1].equals(user.get(k).getNumber())) { //用户的通讯信息与号码进行匹配 223 judge1=false; //当打电话的人没开户时 224 CallRecord callRecord1 = new CallRecord(); //创建通话记录 225 callRecord1.setCallingNumber(fiveString[1]);//拨打电话号 226 callRecord1.setAnswerNumber(fiveString[3]);//接听电话号 227 callRecord1.setCallingAddressAreaCode(fiveString[2]);//拨打电话的区号 228 callRecord1.setAnswerAddressAreaCode(fiveString[4]);//接收电话的区号 229 String startTime1 =fiveString[5]+" "+fiveString[6]; //传入开始时间 230 String endTime =fiveString[7]+" "+fiveString[8]; //传入结束时间 231 boolean flag6=startTime1.matches(regex6); 232 boolean flag7=endTime.matches(regex6); 233 if(flag6==true&&flag7==true) { 234 SimpleDateFormat totalUse1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//固定格式 235 Date ss1 = totalUse1.parse(startTime1); 236 callRecord1.setStartTime(ss1); 237 Date ss2 = totalUse1.parse(endTime); 238 callRecord1.setEndTime(ss2); 239 //分别判断两个号的区号,在长度为9中,都是手机号,没有座机号, 240 //打电话的人在本市内打 241 if( judgeQuHao(fiveString[2],1)==1 ){ 242 //判断接电话的人在哪里 243 //接电话的人也在市内,则将打电话人的用户记录添加到市内拨打记录列表里 244 if( judgeQuHao(fiveString[4],1)==1 ) { 245 user.get(k).getUserRecords().addNewCallingInCityRecords(callRecord1); 246 } 247 //接电话的人在省内,则将打电话人的用户记录添加到省内拨打记录列表里 248 else if( judgeQuHao(fiveString[4],1)==2 ) { 249 user.get(k).getUserRecords().addNewCallingInProvinceRecords(callRecord1); 250 } 251 //接电话的人在省外,则将打电话人的用户记录添加到省外拨打记录列表里,并将接电话的人记录添加到省外 252 else if( judgeQuHao(fiveString[4],1)==3 ) { 253 user.get(k).getUserRecords().addNewCallingInLandRecords(callRecord1); 254 int q; 255 for(q=0;q<user.size();q++) { 256 if( fiveString[3].equals(user.get(q).getNumber()) ) break; 257 } 258 if(q!=user.size()) { 259 user.get(q).getUserRecords().addAnswerLandRecords(callRecord1); 260 } 261 } 262 } 263 //打电话的人在省内,走省内漫游, 264 else if( judgeQuHao(fiveString[2],1)==2 ) { 265 //判断接电话人的位置 266 //先判断接电话的人在省外, 267 if( judgeQuHao(fiveString[4],1)==3 ) { 268 int q; 269 for(q=0;q<user.size();q++) { 270 if( fiveString[3].equals(user.get(q).getNumber()) ) break; 271 } 272 if(q!=user.size()) user.get(q).getUserRecords().addAnswerLandRecords(callRecord1); 273 } 274 user.get(k).getUserRecords().addCallingInProvinceRecords(callRecord1); 275 } 276 //打电话的人在省外,走省外漫游, 277 else if( judgeQuHao(fiveString[2],1)==3 ) { 278 //判断接电话人的位置 279 //先判断接电话的人在省外, 280 if( judgeQuHao(fiveString[4],1)==3 ) { 281 int q; 282 for(q=0;q<user.size();q++) { 283 if( fiveString[3].equals(user.get(q).getNumber()) ) break; 284 } 285 if(q!=user.size()) user.get(q).getUserRecords().addAnswerLandRecords(callRecord1); 286 } 287 user.get(k).getUserRecords().addCallingInLandRecords(callRecord1); 288 } 289 } 290 } 291 } 292 if(judge1==true) { 293 CallRecord callRecord1 = new CallRecord(); //创建通话记录 294 callRecord1.setCallingNumber(fiveString[1]);//拨打电话号 295 callRecord1.setAnswerNumber(fiveString[3]);//接听电话号 296 callRecord1.setCallingAddressAreaCode(fiveString[2]);//拨打电话的区号 297 callRecord1.setAnswerAddressAreaCode(fiveString[4]);//接收电话的区号 298 String startTime1 =fiveString[5]+" "+fiveString[6]; //传入开始时间 299 String endTime =fiveString[7]+" "+fiveString[8]; //传入结束时间 300 boolean flag6=startTime1.matches(regex6); 301 boolean flag7=endTime.matches(regex6); 302 if(flag6==true&&flag7==true) { 303 SimpleDateFormat totalUse1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//固定格式 304 Date ss1 = totalUse1.parse(startTime1); 305 callRecord1.setStartTime(ss1); 306 Date ss2 = totalUse1.parse(endTime); 307 callRecord1.setEndTime(ss2); 308 //接电话的人只判断是否在省外, 309 if( judgeQuHao(fiveString[4],1)==3 ) { 310 int q; 311 for(q=0;q<user.size();q++) { 312 if( fiveString[3].equals(user.get(q).getNumber()) ) break; 313 } 314 if(q!=user.size()) user.get(q).getUserRecords().addAnswerLandRecords(callRecord1); 315 } 316 } 317 } 318 } 319 } 320 } 321 } 322 for(int j=0;j<user.size();j++) { 323 LandlinePhoneCharging landlinePhoneCharging =new LandlinePhoneCharging(); 324 user.get(j).setChargeMode(landlinePhoneCharging); 325 user.get(j).calBalance(); 326 if(j==user.size()-1){ 327 System.out.print(user.get(j).getNumber()+" "+user.get(j).calCost()+" "+user.get(j).getBalance()); 328 } 329 else 330 System.out.println(user.get(j).getNumber()+" "+user.get(j).calCost()+" "+user.get(j).getBalance()); 331 } 332 } 333 public static int judgeQuHao(String s,int select) { //判断是市内、省内、国内 334 String s1 = new String(); 335 if(select==0) s1 = s.substring(0,4); //将前四位截取出来 336 else if(select==1) s1 = s; 337 int s2 = Integer.parseInt(s1); //将其转为数字 338 if(s2==791) return 1; //判断市内 339 else { 340 if( (s2>=790&&s2<=799) || s2==701 ) return 2; //判断省内 341 return 3; //判断国内 342 } 343 } 344 345 } 346 347 class User{ 348 public char selectCharge ='0'; 349 private UserRecords userRecords = new UserRecords(); //用户记录 350 private double balance = 100; //余额 351 private ChargeMode chargeMode; //计费方式 352 private String number; //号码 353 public double calBalance() //拨打余额 354 { 355 double bal=this.balance; 356 double a1=chargeMode.calCost(userRecords); 357 double b2=chargeMode.getMonthlyRent(selectCharge); //返回月租 358 this.balance=bal-a1-b2; 359 return Double.parseDouble(String.format("%.1f",balance)); 360 } 361 public double calCost() //拨打费用 362 { 363 double a2=chargeMode.calCost(userRecords); 364 return Double.parseDouble(String.format("%.1f",a2)); 365 } 366 public UserRecords getUserRecords() 367 { 368 return userRecords; 369 } 370 public void setUserRecords(UserRecords userRecords) //设置该用户记录 371 { 372 this.userRecords = userRecords; 373 } 374 public double getBalance() //返回余额 375 { 376 return balance; 377 } 378 public ChargeMode getChargeMode() 379 { 380 return chargeMode; 381 } 382 public void setChargeMode(ChargeMode chargeMode) 383 { 384 this.chargeMode = chargeMode; 385 } 386 public String getNumber() //返回此用户号码判断是否为同一用户 387 { 388 return number; 389 } 390 public void setNumber(String number) //创建用户时设置号码 391 { 392 this.number=number; 393 } 394 } 395 396 abstract class ChargeMode{ //计费方式的抽象类 397 public abstract double calCost(UserRecords userRecords) ; 398 public abstract double getMonthlyRent(char selectCharge);//返回月租 399 } 400 401 class LandlinePhoneCharging extends ChargeMode{//固定电话缴费 402 private double monthlyRent = 20; //以前月租 403 private double monthlyRent1 = 15;//新模式月租 404 @Override 405 public double calCost(UserRecords userRecords) 406 { 407 ArrayList<ChargeRule> chargeRules1=new ArrayList<ChargeRule>(); 408 LandPhoneInCityRule landPhoneInCityRule=new LandPhoneInCityRule(); 409 LandPhoneInProvinceRule landPhoneInProvinceRule=new LandPhoneInProvinceRule(); 410 LandPhoneInLandRule landPhoneInLandRule=new LandPhoneInLandRule(); 411 newLandPhoneInCityRule newlandPhoneInCityRule=new newLandPhoneInCityRule(); 412 newLandPhoneInProvinceRule newlandPhoneInProvinceRule=new newLandPhoneInProvinceRule(); 413 newLandPhoneInLandRule newlandPhoneInLandRule=new newLandPhoneInLandRule(); 414 AnswerInLandRule answerInLandRecords = new AnswerInLandRule(); 415 chargeRules1.add(landPhoneInCityRule); 416 chargeRules1.add(landPhoneInProvinceRule); 417 chargeRules1.add(landPhoneInLandRule); 418 chargeRules1.add(newlandPhoneInCityRule); 419 chargeRules1.add(newlandPhoneInProvinceRule); 420 chargeRules1.add(newlandPhoneInLandRule); 421 chargeRules1.add(answerInLandRecords); 422 double a=chargeRules1.get(0).calCost(userRecords.getCallingInCityRecords()); 423 double b=chargeRules1.get(1).calCost(userRecords.getCallingInProvinceRecords()); 424 double c=chargeRules1.get(2).calCost(userRecords.getCallingInLandRecords()); 425 double d=chargeRules1.get(3).calCost(userRecords.getNewCallingInCityRecords()); 426 double e=chargeRules1.get(4).calCost(userRecords.getNewCallingInProvinceRecords()); 427 double f=chargeRules1.get(5).calCost(userRecords.getNewCallingInLandRecords()); 428 double g=chargeRules1.get(6).calCost(userRecords.getAnswerInLandRecords()); 429 // System.out.println(a); 430 // System.out.println(b); 431 // System.out.println(c); 432 // System.out.println(d); 433 // System.out.println(e); 434 // System.out.println(f); 435 // System.out.println(g); 436 return a+b+c+d+e+f+g; 437 } 438 @Override 439 public double getMonthlyRent(char selectCharge){ 440 if(selectCharge=='0') return monthlyRent; 441 else return monthlyRent1; 442 } 443 } 444 445 class UserRecords { //用户记录类,保存用户各种通话、短信的记录 446 private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); //市内拨打电话记录列表 447 private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); //省内拨打电话记录列表 448 private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); //国内拨打电话记录列表 449 450 private ArrayList<CallRecord> newCallingInCityRecords = new ArrayList<CallRecord>(); //新款市内拨打电话记录列表 451 private ArrayList<CallRecord> newCallingInProvinceRecords = new ArrayList<CallRecord>(); //新款省内拨打电话记录列表 452 private ArrayList<CallRecord> newCallingInLandRecords = new ArrayList<CallRecord>(); //新款国内拨打电话记录列表 453 454 private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); //市内接听电话记录列表 455 private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();//省内接听电话记录列表 456 private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); //国内接听电话记录列表 457 private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); 458 private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); 459 public void addCallingInCityRecords(CallRecord callRecord) { 460 callingInCityRecords.add(callRecord); 461 } 462 public void addCallingInProvinceRecords(CallRecord callRecord) { 463 callingInProvinceRecords.add(callRecord); 464 } 465 public void addCallingInLandRecords(CallRecord callRecord) { 466 callingInLandRecords.add(callRecord); 467 } 468 public void addNewCallingInCityRecords(CallRecord callRecord) { //新的 469 newCallingInCityRecords.add(callRecord); 470 } 471 public void addNewCallingInProvinceRecords(CallRecord callRecord) {//新的 472 newCallingInProvinceRecords.add(callRecord); 473 } 474 public void addNewCallingInLandRecords(CallRecord callRecord) {//新的 475 newCallingInLandRecords.add(callRecord); 476 } 477 public void addAnswerlnCityRecords(CallRecord answerRecord) { 478 answerInCityRecords.add(answerRecord); 479 } 480 public void addAnswerlnProvinceRecords(CallRecord answerRecord) { 481 answerInProvinceRecords.add(answerRecord); 482 } 483 public void addAnswerLandRecords(CallRecord answerRecord) { 484 answerInLandRecords.add(answerRecord); 485 } 486 public void addSendMessageRecords(MessageRecord sendMessageRecord){ 487 sendMessageRecords.add(sendMessageRecord); 488 } 489 public void addReceiveMessageRecords(MessageRecord receiveMessageRecord){ 490 receiveMessageRecords.add(receiveMessageRecord); 491 } 492 public ArrayList<MessageRecord> getSendMessageRecords(){ 493 return sendMessageRecords; 494 } 495 public ArrayList<MessageRecord> getReceiveMessageRecords(){ 496 return receiveMessageRecords; 497 } 498 public ArrayList<CallRecord> getCallingInCityRecords(){ //返回市内拨打记录 499 return callingInCityRecords; 500 } 501 public ArrayList<CallRecord> getCallingInProvinceRecords(){ 502 return callingInProvinceRecords; 503 } 504 public ArrayList<CallRecord> getCallingInLandRecords(){ 505 return callingInLandRecords; 506 } 507 public ArrayList<CallRecord> getNewCallingInCityRecords(){ //返回新款市内拨打记录 508 return newCallingInCityRecords; 509 } 510 public ArrayList<CallRecord> getNewCallingInProvinceRecords(){ 511 return newCallingInProvinceRecords; 512 } 513 public ArrayList<CallRecord> getNewCallingInLandRecords(){ 514 return newCallingInLandRecords; 515 } 516 public ArrayList<CallRecord> getAnswerInCityRecords(){ 517 return answerInCityRecords; 518 } 519 public ArrayList<CallRecord> getAnswerInProvinceRecords(){ 520 return answerInProvinceRecords; 521 } 522 public ArrayList<CallRecord> getAnswerInLandRecords(){ 523 return answerInLandRecords; 524 } 525 } 526 527 abstract class CommunicationRecord { //抽象的通讯记录类 528 protected String callingNumber; //拨打号码 529 protected String answerNumber; //接听号码 530 531 public String getCallingNumber() { 532 return callingNumber; 533 } 534 public void setCallingNumber(String callingNumber) { 535 this.callingNumber = callingNumber; 536 } 537 538 public String getAnswerNumber() { 539 return answerNumber; 540 } 541 public void setAnswerNumber(String answerNumber) { 542 this.answerNumber = answerNumber; 543 } 544 } 545 546 class CallRecord extends CommunicationRecord{ //通话记录 547 private Date startTime; //通话的起始时间 548 private Date endTime; //通话的结束时间 549 private String callingAddressAreaCode; //拨号地点的区号 550 private String answerAddressAreaCode; //接听地点的区号 551 public Date getStartTime() { //返回起始时间 552 return startTime; 553 } 554 public void setStartTime(Date startTime) { //设置起始时间 555 this.startTime = startTime; 556 } 557 public Date getEndTime() { //返回结束时间 558 return endTime; 559 } 560 public void setEndTime(Date endTime) { //设置结束时间 561 this.endTime = endTime; 562 } 563 public String getCallingAddressAreaCode() { 564 return callingAddressAreaCode; 565 } 566 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 567 this.callingAddressAreaCode = callingAddressAreaCode; 568 } 569 public String getAnswerAddressAreaCode() { 570 return answerAddressAreaCode; 571 } 572 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 573 this.answerAddressAreaCode = answerAddressAreaCode; 574 } 575 576 } 577 578 class MessageRecord extends CommunicationRecord{ //短信记录 579 private String message; 580 581 public String getMessage() { 582 return message; 583 } 584 585 public void setMessage(String message) { 586 this.message = message; 587 } 588 589 } 590 591 abstract class ChargeRule { 592 public double calCost(ArrayList<CallRecord> callingInCityRecords){//计费规则 593 return 1; 594 } 595 } 596 abstract class CallChargeRule extends ChargeRule{ 597 public double calCost(ArrayList<CallRecord> callingInCityRecords){//计费规则 598 return 1; 599 } 600 } 601 class LandPhoneInCityRule extends CallChargeRule{ //座机拨打市内电话的计费规则类 602 public double calCost(ArrayList<CallRecord> callRecords) { 603 double cost = 0; 604 for(int i=0;i<callRecords.size();i++) { 605 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 606 if(totoalTime%60000>=1) { 607 totoalTime = totoalTime/60000+1; 608 } 609 else totoalTime = totoalTime/60000; 610 cost+=totoalTime*0.1; 611 } 612 return cost; 613 } 614 } 615 class LandPhoneInProvinceRule extends CallChargeRule{//座机拨打省内电话的计费规则类 616 public double calCost(ArrayList<CallRecord> callRecords) { 617 double cost = 0; 618 for(int i=0;i<callRecords.size();i++) { 619 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 620 if(totoalTime%60000>=1) { 621 totoalTime = totoalTime/60000+1; 622 } 623 else totoalTime = totoalTime/60000; 624 cost+=totoalTime*0.3; 625 } 626 return cost; 627 } 628 } 629 class LandPhoneInLandRule extends CallChargeRule{//座机拨打省外电话的计费规则类 630 public double calCost(ArrayList<CallRecord> callRecords) { 631 double cost = 0; 632 for(int i=0;i<callRecords.size();i++) { 633 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 634 if(totoalTime%60000>1) { 635 totoalTime = totoalTime/60000+1; 636 } 637 else totoalTime = totoalTime/60000; 638 cost+=totoalTime*0.6; 639 } 640 return cost; 641 } 642 } 643 class newLandPhoneInCityRule extends CallChargeRule{ //新座机拨打市内电话的计费规则类 644 public double calCost(ArrayList<CallRecord> callRecords) { 645 double cost = 0; 646 for(int i=0;i<callRecords.size();i++) { 647 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 648 if(totoalTime%60000>1) { 649 totoalTime = totoalTime/60000+1; 650 } 651 else totoalTime = totoalTime/60000; 652 cost+=totoalTime*0.1; 653 } 654 return cost; 655 } 656 } 657 class newLandPhoneInProvinceRule extends CallChargeRule{//新座机拨打省内电话的计费规则类 658 public double calCost(ArrayList<CallRecord> callRecords) { 659 double cost = 0; 660 for(int i=0;i<callRecords.size();i++) { 661 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 662 if(totoalTime%60000>=1) { 663 totoalTime = totoalTime/60000+1; 664 } 665 else totoalTime = totoalTime/60000; 666 cost+=totoalTime*0.2; 667 } 668 return cost; 669 } 670 } 671 class newLandPhoneInLandRule extends CallChargeRule{//新座机拨打省外电话的计费规则类 672 public double calCost(ArrayList<CallRecord> callRecords) { 673 double cost = 0; 674 for(int i=0;i<callRecords.size();i++) { 675 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 676 if(totoalTime%60000>=1) { 677 totoalTime = totoalTime/60000+1; 678 } 679 else totoalTime = totoalTime/60000; 680 cost+=totoalTime*0.3; 681 } 682 return cost; 683 } 684 } 685 class AnswerInLandRule extends CallChargeRule{//座机拨打省外电话的计费规则类 686 public double calCost(ArrayList<CallRecord> callRecords) { 687 double cost = 0; 688 for(int i=0;i<callRecords.size();i++) { 689 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 690 if(totoalTime%60000>=1) { 691 totoalTime = totoalTime/60000+1; 692 } 693 else totoalTime = totoalTime/60000; 694 cost+=totoalTime*0.3; 695 } 696 return cost; 697 } 698 } 699 abstract class MessageChargeRule{ 700 public abstract double calCost(ArrayList<MessageRecord> messageRecords); 701 } 702 class SendMessageRule extends MessageChargeRule{ 703 @Override 704 public double calCost(ArrayList<MessageRecord> messageRecords) { 705 return 1; 706 } 707 }View Code
按类图编写代码,由于代码部分设计有缺陷,多个用户综合计算会运行超时。代码重复度高,还需改进。
7-2 多态测试 分数 20 作者 董卫萍 单位 绍兴文理学院元培学院定义容器Container接口。模拟实现一个容器类层次结构,并进行接口的实现、抽象方法重写和多态机制测试。各容器类实现求表面积、体积的方法。
- 定义接口Container:
属性:
public static final double pi=3.1415926;
抽象方法:
public abstract double area();
public abstract double volume();
static double sumofArea(Container c[]);
static double sumofVolume(Container c[]);
其中两个静态方法分别计算返回容器数组中所有对象的面积之和、周长之和; - 定义Cube类、Cylinder类均实现自Container接口。
Cube类(属性:边长double类型)、Cylinder类(属性:底圆半径、高,double类型)。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); Shape3D[] list = new Shape3D[n]; for (int i=0;i<n;i++) { String str = in.next(); if (str.equals("cube")) { list[i] = new Cube(in.nextDouble()); } else if (str.equals("cylinder")) { list[i] = new Cylinder(in.nextDouble(), in.nextDouble()); } } double sumv = 0; double suma = 0; for (Shape3D i : list) { sumv += i.volume(); suma += i.area(); } System.out.printf("%.2f\n%.2f",suma,sumv); } } abstract class Shape3D implements Container { } class Cylinder extends Shape3D { private double r, h; public Cylinder(double r, double h) { super(); this.r = r; this.h = h; } public double area() { return pi*r*r*2+2*pi*r*h; } public double volume() { return pi*r*r*h; } } class Cube extends Shape3D { private double a; public Cube(double a) { this.a = a; } public double area() { return 6*a*a; } public double volume() { return a*a*a; } } interface Container { public static final double pi=3.1415926; public abstract double area(); public abstract double volume(); static double sumofArea(Container c[]) { double sum = 0; for(Container i:c) { sum += i.area(); } return sum; }; static double sumofVolume(Container c[]) { double sum = 0; for(Container i:c) { sum += i.volume(); } return sum; } }View Code 7-1 电信计费系列2-手机+座机计费 分数 80 作者 蔡轲 单位 南昌航空大学
实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
1、针对市内座机用户采用的计费方式(与电信计费系列1内容相同):
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
假设本市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
2、针对手机用户采用实时计费方式:
月租15元,市内省内接电话均免费,市内拨打市内电话0.1元/分钟,市内拨打省内电话0.2元/分钟,市内拨打省外电话0.3元/分钟,省内漫游打电话0.3元/分钟,省外漫游接听0.3元/分钟,省外漫游拨打0.6元/分钟;
注:被叫电话属于市内、省内还是国内由被叫电话的接听地点区号决定,比如以下案例中,南昌市手机用户13307912264在区号为020的广州接听了电话,主叫号码应被计算为拨打了一个省外长途,同时,手机用户13307912264也要被计算省外接听漫游费:
u-13307912264 1
t-079186330022 13307912264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
建议类图:
参见图1、2、3:
import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); HashMap<String,User> map = new HashMap<String,User>(); HashMap<String,User> map1 = new HashMap<String,User>(); ArrayList<String> list = new ArrayList<>(); ArrayList<String> list1 = new ArrayList<>(); while (true) { String s = input.nextLine(); if (s.equals("end")) { break; } else { if (s.matches("u-[0-9]{11}\\s(0|1)")) { String[] s1 = s.split("\\s"); String s2[] = s1[0].split("-"); User user = new User(100, s2[1]); ChargeMode chargeMode = new MobilePhoneCharging(); ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); chargeRules.add(new MobilePhonelnCityToCityRule()); chargeRules.add(new MobilePhonelnCityToProvinceRule()); chargeRules.add(new MobilePhonelnCityToLandRule()); chargeRules.add(new MobilePhonelnProvinceToCityRule()); chargeRules.add(new MobilePhonelnProvinceToProvinceRule()); chargeRules.add(new MobilePhonelnProvinceToLandRule()); chargeRules.add(new MobilePhonelnLandToCityRule()); chargeRules.add(new MobilePhonelnLandToProvinceRule()); chargeRules.add(new MobilePhonelnLandToLandRule()); chargeRules.add(new MobliePhonelnLandAnswerRule()); chargeMode.setChargeRules(chargeRules); user.setChargeMode(chargeMode); if(!map1.containsKey(s2[1])) { map1.put(s2[1], user); list1.add(s2[1]); } } else if (s.matches("u-[0-9]{11,12}\\s(0|1)")) { String[] s1 = s.split("\\s"); String s2[] = s1[0].split("-"); User user = new User(100, s2[1]); ChargeMode chargeMode = new LandlinePhoneCharging(); ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); chargeRules.add(new LandPhonelnCityRule()); chargeRules.add(new LandPhonelnProvinceRule()); chargeRules.add(new LandPhonelnLandRule()); chargeMode.setChargeRules(chargeRules); user.setChargeMode(chargeMode); if(!map.containsKey(s2[1])) { map.put(s2[1], user); list.add(s2[1]); } } else if (s.matches("t-[0-9]{11,12}\\s[0-9]{10,12}\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) { String[] s1 = s.split("\\s"); String s2[] = s1[0].split("-"); if (map.containsKey(s2[1])) { User user = map.get(s2[1]); CallRecord callRecord = new CallRecord(); try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()); Date time1 = simpleDateFormat.parse(s1[2] + " " + s1[3]); Date time2 = simpleDateFormat.parse(s1[4] + " " + s1[5]); callRecord.setStartTime(time1); callRecord.setEndTime(time2); callRecord.setCallingAddressAreaCode(s2[1].substring(0, 4)); callRecord.setAnswerAddressAreaCode(s1[1].substring(0, 4)); callRecord.setCallingNumber(s2[1]); callRecord.setAnswerNumber(s1[1]); if (s1[1].substring(0, 3).equals("079") || s1[1].substring(0, 4).equals("0701")) { if (s1[1].substring(0, 4).equals("0791")) { user.userRecords.addCallinglnCityRecords(callRecord); } else { user.userRecords.addCallinglnProvinceRecords(callRecord); } } else { user.userRecords.addCallinglnLandRecords(callRecord); } } catch (Exception ji) { } } } else if (s.matches("t-[0-9]{10,12}\\s[0-9]{10,12}\\s[0-9]{3,4}\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) { String[] s1 = s.split("\\s"); String s2[] = s1[0].split("-"); if (map.containsKey(s2[1]) || map1.containsKey(s1[1])) { User user = null; if (map.containsKey(s2[1])) { user = map.get(s2[1]); } User user1 = null; if (map1.containsKey(s1[1])) { user1 = map1.get(s1[1]); } CallRecord callRecord = new CallRecord(); try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()); Date time1 = simpleDateFormat.parse(s1[3] + " " + s1[4]); Date time2 = simpleDateFormat.parse(s1[5] + " " + s1[6]); callRecord.setStartTime(time1); callRecord.setEndTime(time2); callRecord.setCallingAddressAreaCode(s2[1].substring(0, 4)); callRecord.setAnswerAddressAreaCode(s1[2]); callRecord.setCallingNumber(s2[1]); callRecord.setAnswerNumber(s1[1]); if (s1[2].substring(0, 3).equals("079") || s1[2].substring(0, 4).equals("0701")) { if (s1[2].substring(0, 4).equals("0791")) { if (map.containsKey(s2[1])) user.userRecords.addCallinglnCityRecords(callRecord); } else { if (map.containsKey(s2[1])) user.userRecords.addCallinglnProvinceRecords(callRecord); } } else { if (map.containsKey(s2[1])) user.userRecords.addCallinglnLandRecords(callRecord); if (map1.containsKey(s1[1])) user1.userRecords.addCallinglnLandAnswerRecords(callRecord); } } catch (Exception ji) { } } } else if (s.matches("t-[0-9]{10,12}\\s[0-9]{3,4}\\s[0-9]{10,12}\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) { String[] s1 = s.split("\\s"); String s2[] = s1[0].split("-"); if (map1.containsKey(s2[1]) || map.containsKey(s1[2])) { User user = null; if (map1.containsKey(s2[1])) { user = map1.get(s2[1]); } User user1 = null; if (map.containsKey(s1[2])) { user1 = map.get(s1[2]); } CallRecord callRecord = new CallRecord(); try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()); Date time1 = simpleDateFormat.parse(s1[3] + " " + s1[4]); Date time2 = simpleDateFormat.parse(s1[5] + " " + s1[6]); callRecord.setStartTime(time1); callRecord.setEndTime(time2); callRecord.setCallingAddressAreaCode(s1[1]); callRecord.setAnswerAddressAreaCode(s1[2].substring(0, 4)); callRecord.setCallingNumber(s2[1]); callRecord.setAnswerNumber(s1[2]); if (s1[1].substring(0, 3).equals("079") || s1[1].equals("0701")) { if (s1[1].equals("0791")) { if (map1.containsKey(s2[1])) user.userRecords.addCallinglnCityToCityRecords(callRecord); } else { if (map1.containsKey(s2[1])) user.userRecords.addCallinglnProvinceToCityRecords(callRecord); } } else { if (map1.containsKey(s2[1])) user.userRecords.addCallinglnLandToCityRecords(callRecord); } } catch (Exception ji) { } } } else if (s.matches("t-[0-9]{10,12}\\s[0-9]{3,4}\\s[0-9]{10,12}\\s[0-9]{3,4}\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) { String[] s1 = s.split("\\s"); String s2[] = s1[0].split("-"); if (map1.containsKey(s2[1]) || map.containsKey(s1[2])) { User user = null; if (map1.containsKey(s2[1])) { user = map1.get(s2[1]); } User user1 = null; if (map1.containsKey(s1[2])) { user1 = map1.get(s1[2]); } CallRecord callRecord = new CallRecord(); try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()); Date time1 = simpleDateFormat.parse(s1[4] + " " + s1[5]); Date time2 = simpleDateFormat.parse(s1[6] + " " + s1[7]); callRecord.setStartTime(time1); callRecord.setEndTime(time2); callRecord.setCallingAddressAreaCode(s1[1]); callRecord.setAnswerAddressAreaCode(s1[3]); callRecord.setCallingNumber(s2[1]); callRecord.setAnswerNumber(s1[2]); if (s1[1].substring(0, 3).equals("079") || s1[1].substring(0, 4).equals("0701")) { if (s1[1].substring(0, 4).equals("0791")) { if (s1[3].substring(0, 3).equals("079") || s1[3].substring(0, 4).equals("0701")) { if (s1[3].substring(0, 4).equals("0791")) { if (map1.containsKey(s2[1])) user.userRecords.addCallinglnCityToCityRecords(callRecord); } else { if (map1.containsKey(s2[1])) user.userRecords.addCallinglnCityToProvinceRecords(callRecord); } } else { if (map1.containsKey(s2[1])) user.userRecords.addCallinglnCityToLandRecords(callRecord); if (map1.containsKey(s1[2])) user1.userRecords.addCallinglnLandAnswerRecords(callRecord); } } else { if (map1.containsKey(s2[1])) user.userRecords.addCallinglnProvinceToCityRecords(callRecord); if (!s1[3].substring(0, 3).equals("079") && !s1[3].substring(0, 4).equals("0701")) { if (map1.containsKey(s1[2])) user1.userRecords.addCallinglnLandAnswerRecords(callRecord); } } } else { if (map1.containsKey(s2[1])) user.userRecords.addCallinglnLandToCityRecords(callRecord); if (!s1[3].substring(0, 3).equals("079") && !s1[3].substring(0, 4).equals("0701")) { if (map1.containsKey(s1[2])) user1.userRecords.addCallinglnLandAnswerRecords(callRecord); } } } catch (Exception ji) { } } } } } Collections.sort(list); Iterator<String> it = list.iterator(); while (it.hasNext()) { String s = it.next(); User value = map.get(s); System.out.printf("%s %.1f %.1f\n",value.getNumber(),value.calCost(),value.calBalance()); } Collections.sort(list1); Iterator<String> it1 = list1.iterator(); while (it1.hasNext()) { String s = it1.next(); User value = map1.get(s); System.out.printf("%s %.1f %.1f\n",value.getNumber(),value.calCost(),value.calBalance()); } } } class User{ UserRecords userRecords = new UserRecords(); double balance = 100; ChargeMode chargeMode; String number; public double calCost(){ double sum=chargeMode.calCost(userRecords); return sum; } public double calBalance(){ return balance-chargeMode.calCost(userRecords)-chargeMode.getMonthlyRent(); } User(double balance, String number) { this.balance = balance; this.number = number; } public UserRecords getUserRecords() { return userRecords; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public ChargeMode getChargeMode() { return chargeMode; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } } abstract class ChargeMode{ ArrayList<ChargeRule> chargeRules = new ArrayList<>(); public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } abstract double calCost(UserRecords userRecords); abstract double getMonthlyRent(); } class MobilePhoneCharging extends ChargeMode{ double monthlyRent = 15; @Override double calCost(UserRecords userRecords) { double sum=0; sum+=getChargeRules().get(0).calCost(userRecords.getCallinglnCityToCityRecords()); sum+=getChargeRules().get(1).calCost(userRecords.getCallinglnCityToProvinceRecords()); sum+=getChargeRules().get(2).calCost(userRecords.getCallinglnCityToLandRecords()); sum+=getChargeRules().get(3).calCost(userRecords.getCallinglnProvinceToCityRecords()); sum+=getChargeRules().get(4).calCost(userRecords.getCallinglnProvinceToProvinceRecords()); sum+=getChargeRules().get(5).calCost(userRecords.getCallinglnProvinceToLandRecords()); sum+=getChargeRules().get(6).calCost(userRecords.getCallinglnLandToCityRecords()); sum+=getChargeRules().get(7).calCost(userRecords.getCallinglnLandToProvinceRecords()); sum+=getChargeRules().get(8).calCost(userRecords.getCallinglnLandToLandRecords()); sum+=getChargeRules().get(9).calCost(userRecords.getCallinglnLandAnswerRecords()); return sum; } @Override double getMonthlyRent() { return monthlyRent; } } class LandlinePhoneCharging extends ChargeMode{ double monthlyRent = 20; @Override public double calCost(UserRecords userRecords){ double sum=0; sum+=getChargeRules().get(0).calCost(userRecords.getCallinglnCityRecords()); sum+=getChargeRules().get(1).calCost(userRecords.getCallinglnProvinceRecords()); sum+=getChargeRules().get(2).calCost(userRecords.getCallinglnLandRecords()); return sum; } @Override public double getMonthlyRent() { return monthlyRent; } } class UserRecords{ ArrayList<CallRecord> callinglnCityToCityRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callinglnCityToProvinceRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callinglnCityToLandRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callinglnProvinceToCityRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callinglnProvinceToProvinceRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callinglnProvinceToLandRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callinglnLandToCityRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callinglnLandToProvinceRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callinglnLandToLandRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callinglnLandAnswerRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callinglnCityRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callinglnProvinceRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callinglnLandRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> answerlnCityRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> answerlnProvinceRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> answerlnLandRecords = new ArrayList<CallRecord>(); ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); void addCallinglnCityRecords(CallRecord callRecord) { callinglnCityRecords.add(callRecord); } void addCallinglnProvinceRecords(CallRecord callRecord) { callinglnProvinceRecords.add(callRecord); } void addCallinglnLandRecords(CallRecord callRecord) { callinglnLandRecords.add(callRecord); } void addAnswerlnCityRecords(CallRecord answerRecord) { answerlnCityRecords.add(answerRecord); } void addAnswerlnProvinceRecords(CallRecord answerRecord) { answerlnProvinceRecords.add(answerRecord); } void addAnswerlnLandRecords(CallRecord answerRecord) { answerlnLandRecords.add(answerRecord); } void addSendMessageRecords(MessageRecord sendMessageRecord) { sendMessageRecords.add(sendMessageRecord); } void addReceiveMessageRecords(MessageRecord receiveMessageRecord) { receiveMessageRecords.add(receiveMessageRecord); } public ArrayList<CallRecord> getCallinglnCityRecords() { return callinglnCityRecords; } public void setCallinglnCityRecords(ArrayList<CallRecord> callinglnCityRecords) { this.callinglnCityRecords = callinglnCityRecords; } public ArrayList<CallRecord> getCallinglnProvinceRecords() { return callinglnProvinceRecords; } public void setCallinglnProvinceRecords(ArrayList<CallRecord> callinglnProvinceRecords) { this.callinglnProvinceRecords = callinglnProvinceRecords; } public ArrayList<CallRecord> getCallinglnLandRecords() { return callinglnLandRecords; } public void setCallinglnLandRecords(ArrayList<CallRecord> callinglnLandRecords) { this.callinglnLandRecords = callinglnLandRecords; } public ArrayList<CallRecord> getAnswerlnCityRecords() { return answerlnCityRecords; } public void setAnswerlnCityRecords(ArrayList<CallRecord> answerlnCityRecords) { this.answerlnCityRecords = answerlnCityRecords; } public ArrayList<CallRecord> getAnswerlnProvinceRecords() { return answerlnProvinceRecords; } public void setAnswerlnProvinceRecords(ArrayList<CallRecord> answerlnProvinceRecords) { this.answerlnProvinceRecords = answerlnProvinceRecords; } public ArrayList<CallRecord> getAnswerlnLandRecords() { return answerlnLandRecords; } public void setAnswerlnLandRecords(ArrayList<CallRecord> answerlnLandRecords) { this.answerlnLandRecords = answerlnLandRecords; } public ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { this.sendMessageRecords = sendMessageRecords; } public ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { this.receiveMessageRecords = receiveMessageRecords; } public ArrayList<CallRecord> getCallinglnCityToCityRecords() { return callinglnCityToCityRecords; } public ArrayList<CallRecord> getCallinglnCityToProvinceRecords() { return callinglnCityToProvinceRecords; } public ArrayList<CallRecord> getCallinglnCityToLandRecords() { return callinglnCityToLandRecords; } public ArrayList<CallRecord> getCallinglnProvinceToCityRecords() { return callinglnProvinceToCityRecords; } public ArrayList<CallRecord> getCallinglnProvinceToProvinceRecords() { return callinglnProvinceToProvinceRecords; } public ArrayList<CallRecord> getCallinglnProvinceToLandRecords() { return callinglnProvinceToLandRecords; } public ArrayList<CallRecord> getCallinglnLandToCityRecords() { return callinglnLandToCityRecords; } public ArrayList<CallRecord> getCallinglnLandToProvinceRecords() { return callinglnLandToProvinceRecords; } public ArrayList<CallRecord> getCallinglnLandToLandRecords() { return callinglnLandToLandRecords; } public ArrayList<CallRecord> getCallinglnLandAnswerRecords() { return callinglnLandAnswerRecords; } public void addCallinglnLandAnswerRecords(CallRecord callRecord) { callinglnLandAnswerRecords.add(callRecord); } public void addCallinglnCityToCityRecords(CallRecord callRecord) { callinglnCityToCityRecords.add(callRecord); } public void addCallinglnCityToProvinceRecords(CallRecord callRecord) { callinglnCityToProvinceRecords.add(callRecord); } public void addCallinglnCityToLandRecords(CallRecord callRecord) { callinglnCityToLandRecords.add(callRecord); } public void addCallinglnProvinceToCityRecords(CallRecord callRecord) { callinglnProvinceToCityRecords.add(callRecord); } public void addCallinglnProvinceToProvinceRecords(CallRecord callRecord) { callinglnProvinceToProvinceRecords.add(callRecord); } public void addCallinglnProvinceToLandRecords(CallRecord callRecord) { callinglnProvinceToLandRecords.add(callRecord); } public void addCallinglnLandToCityRecords(CallRecord callRecord) { callinglnLandToCityRecords.add(callRecord); } public void addCallinglnLandToProvinceRecords(CallRecord callRecord) { callinglnLandToProvinceRecords.add(callRecord); } public void addCallinglnLandToLandRecords(CallRecord callRecord) { callinglnLandToLandRecords.add(callRecord); } } abstract class CommunicationRecord{ String callingNumber; String answerNumber; abstract String getCallingNumber(); abstract void setCallingNumber(String callingNumber); abstract String getAnswerNumber(); abstract void setAnswerNumber(String answerNumber); } class CallRecord extends CommunicationRecord{ Date startTime; Date endTime; String callingAddressAreaCode; String answerAddressAreaCode; public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public String getAnswerAddressAreaCode() { return answerAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } @Override String getCallingNumber() { return null; } @Override void setCallingNumber(String callingNumber) { } @Override String getAnswerNumber() { return null; } @Override void setAnswerNumber(String answerNumber) { } } class MessageRecord extends CommunicationRecord{ String message; String getMessage() { return null; } void setMessage(String message) { } @Override String getCallingNumber() { return null; } @Override void setCallingNumber(String callingNumber) { } @Override String getAnswerNumber() { return null; } @Override void setAnswerNumber(String answerNumber) { } } abstract class ChargeRule{ abstract public double calCost(ArrayList<CallRecord> callinglnCityRecords); } abstract class CallChargeRule extends ChargeRule{ abstract public double calCost(ArrayList<CallRecord> callRecords); } class LandPhonelnCityRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.1 * ((double)(second / 60)+1.0); }else{ sum += 0.1 * (double)(second / 60); } } return sum; } } class LandPhonelnProvinceRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.3 * ((double)(second / 60)+1.0); }else{ sum += 0.3 * (double)(second / 60); } } return sum; } } class LandPhonelnLandRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.6 * ((double)(second / 60)+1.0); }else{ sum += 0.6 * (double)(second / 60); } } return sum; } } // abstract class MessageChargeRule extends ChargeRule{ // abstract public double calCost(ArrayList<MessageRecord> messageRecords); // } // class SendMessageRule extends MessageChargeRule{ // // @Override // public double calCost(ArrayList<MessageRecord> messageRecords) { // return 0; // } // } class MobilePhonelnCityToCityRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.1 * ((double)(second / 60)+1.0); }else{ sum += 0.1 * (double)(second / 60); } } return sum; } } class MobilePhonelnCityToProvinceRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.2 * ((double)(second / 60)+1.0); }else{ sum += 0.2 * (double)(second / 60); } } return sum; } } class MobilePhonelnCityToLandRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.3 * ((double)(second / 60)+1.0); }else{ sum += 0.3 * (double)(second / 60); } } return sum; } }class MobilePhonelnProvinceToCityRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.3 * ((double)(second / 60)+1.0); }else{ sum += 0.3 * (double)(second / 60); } } return sum; } } class MobilePhonelnProvinceToProvinceRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.3 * ((double)(second / 60)+1.0); }else{ sum += 0.3 * (double)(second / 60); } } return sum; } } class MobilePhonelnProvinceToLandRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.3 * ((double)(second / 60)+1.0); }else{ sum += 0.3 * (double)(second / 60); } } return sum; } } class MobilePhonelnLandToProvinceRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.6 * ((double)(second / 60)+1.0); }else{ sum += 0.6 * (double)(second / 60); } } return sum; } } class MobilePhonelnLandToLandRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.6 * ((double)(second / 60)+1.0); }else{ sum += 0.6 * (double)(second / 60); } } return sum; } } class MobilePhonelnLandToCityRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.6 * ((double)(second / 60)+1.0); }else{ sum += 0.6 * (double)(second / 60); } } return sum; } } class MobliePhonelnLandAnswerRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double sum=0; for (int i = 0; i < callRecords.size(); i++) { long time1 = callRecords.get(i).getStartTime().getTime(); long time2 = callRecords.get(i).getEndTime().getTime(); long second = (time2 - time1) / 1000; if (second % 60 != 0) { sum += 0.3 * ((double)(second / 60)+1.0); }else{ sum += 0.3 * (double)(second / 60); } } return sum; } }View Code
这次的代码改进了大量重复的部分,提高了代码的质量,基本实现了功能,只有几个测试点没过。
7-2 sdut-Collection-sort--C~K的班级(II) 分数 10 作者 周雪芹 单位 山东理工大学
经过不懈的努力,C~K终于当上了班主任。
现在他要统计班里学生的名单,但是C~K在教务系统中导出班级名单时出了问题,发现会有同学的信息重复,现在他想把重复的同学信息删掉,只保留一个,
但是工作量太大了,所以找到了会编程的你,你能帮他解决这个问题吗?
import java.util.*; public class Main{ public static void main(String []args){ Scanner in=new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); ArrayList<String> a = new ArrayList<String>(); while(n!=0){ String s = in.nextLine(); if(!a.contains(s)){ a.add(s); } n--; } a.sort(new Comparator<String>(){ @Override public int compare(String s1,String s2) { return Integer.parseInt(s1.substring(0,4))-Integer.parseInt(s2.substring(0,4)); } }); System.out.println(a.size()); for(String i:a){ System.out.println(i); } } }View Code 7-3 阅读程序,按照题目需求修改程序 分数 10 作者 肖斌 单位 西南石油大学
功能需求:
使用集合存储3个员工的信息(有序);
通过迭代器依次找出所有的员工。
提示:学生复制以下代码到编程区,并按需求进行调试修改。
// 1、导入相关包
//定义员工类
class Employee {
private String name;
private int age;
public Employee() {
super();
}
public Employee(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//主函数
public class Main {
public static void main(String[] args) {
// 1、创建有序集合对象
Collection c ;
// 创建3个员工元素对象
for (int i = 0; i < 3; i++) {
Scanner sc = new Scanner(System.in);
String employeeName = sc.nextLine();
int employeeAge = sc.nextInt();
Employee employee = new Employee(employeeName, employeeAge);
c.add(employee);
}
// 2、创建迭代器遍历集合
Iterator it;
//3、遍历
while (it.hasnext) {
//4、集合中对象未知,向下转型
Employee e = it.next();
System.out.println(e.getName() + "---" + e.getAge());
}
}
}
import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Scanner; //1、导入相关包 //定义员工类 class Employee { private String name; private int age; public Employee() { super(); } public Employee(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } //主函数 public class Main { public static void main(String[] args) { // 1、创建有序集合对象 Collection c = new ArrayList<Employee>(); // 创建3个员工元素对象 Scanner sc = new Scanner(System.in); for (int i = 0; i < 3; i++) { String employeeName = sc.next(); int employeeAge = sc.nextInt(); Employee employee = new Employee(employeeName, employeeAge); c.add(employee); } // 2、创建迭代器遍历集合 Iterator it = c.iterator(); //3、遍历 while (it.hasNext()) { //4、集合中对象未知,向下转型 Employee e = (Employee) it.next(); System.out.println(e.getName() + "---" + e.getAge()); } } }View Code
这题pta出了问题,改了半天还是答案错误,提交过不过得碰运气,什么()网站。
7-1 电信计费系列3-短信计费 分数 50 作者 蔡轲 单位 南昌航空大学
实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
1、接收短信免费,发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。
2、如果一次发送短信的字符数量超过10个,按每10个字符一条短信进行计算。
import java.util.*; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.text.ParseException; public class Main { public static void main(String[] args) { Outputtool outputtool = new Outputtool(); Inputdeal inputdeal = new Inputdeal(); ArrayList<User> users = new ArrayList<>(); Scanner in = new Scanner(System.in); String input = in.nextLine(); while (!input.equals("end")) { if (1 == inputdeal.check(input)) { inputdeal.writeUser(users, input); } else if (2 == inputdeal.check(input)) { inputdeal.writeRecord(users, input); } input = in.nextLine(); } users.sort(new Comparator<User>() { @Override public int compare(User u1, User u2) { if (u1.getNumber().charAt(0) == '0' && u2.getNumber().charAt(0) != '0') { return -1; } else if (u1.getNumber().charAt(0) != '0' && u2.getNumber().charAt(0) == '0') { return 1; } if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) { return 1; } else { return -1; } } }); for (User u : users) { System.out.print(u.getNumber() + " "); outputtool.output(u.calCost()); System.out.print(" "); outputtool.output(u.calBalance()); System.out.println(); } } } abstract class ChargeMode { protected ArrayList<ChargeRule> chargeRules = new ArrayList<>(); public abstract double calCost(UserRecords userRecords); public abstract double getMonthlyRent(); public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } } class UserRecords { private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); public void addCallingInCityRecords(CallRecord callRecord) { callingInCityRecords.add(callRecord); } public void addCallingInProvinceRecords(CallRecord callRecord) { callingInProvinceRecords.add(callRecord); } public void addCallingInLandRecords(CallRecord callRecord) { callingInLandRecords.add(callRecord); } public void addAnswerInCityRecords(CallRecord callRecord) { answerInCityRecords.add(callRecord); } public void aaddAnswerInProvinceRecords(CallRecord callRecord) { answerInProvinceRecords.add(callRecord); } public void addAnswerInLandRecords(CallRecord callRecord) { answerInLandRecords.add(callRecord); } public void addSendMessageRecords(MessageRecord callRecord) { sendMessageRecords.add(callRecord); } public void addReceiveMessageRecords(MessageRecord callRecord) { receiveMessageRecords.add(callRecord); } public ArrayList<CallRecord> getCallingInCityRecords() { return callingInCityRecords; } public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) { this.callingInCityRecords = callingInCityRecords; } public ArrayList<CallRecord> getCallingInProvinceRecords() { return callingInProvinceRecords; } public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) { this.callingInProvinceRecords = callingInProvinceRecords; } public ArrayList<CallRecord> getCallingInLandRecords() { return callingInLandRecords; } public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) { this.callingInLandRecords = callingInLandRecords; } public ArrayList<CallRecord> getAnswerInCityRecords() { return answerInCityRecords; } public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) { this.answerInCityRecords = answerInCityRecords; } public ArrayList<CallRecord> getAnswerInProvinceRecords() { return answerInProvinceRecords; } public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) { this.answerInProvinceRecords = answerInProvinceRecords; } public ArrayList<CallRecord> getAnswerInLandRecords() { return answerInLandRecords; } public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) { this.answerInLandRecords = answerInLandRecords; } public ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { this.sendMessageRecords = sendMessageRecords; } public ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { this.receiveMessageRecords = receiveMessageRecords; } } class LandlinePhoneCharging extends ChargeMode { private double monthlyRent = 20; public LandlinePhoneCharging() { super(); chargeRules.add(new LandPhoneInCityRule()); chargeRules.add(new LandPhoneInProvinceRule()); chargeRules.add(new LandPhoneInlandRule()); } @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (ChargeRule rule : chargeRules) { sumCost += rule.calCost(userRecords); } return sumCost; } @Override public double getMonthlyRent() { return monthlyRent; } } class MobilePhoneCharging extends ChargeMode { private double monthlyRent = 15; public MobilePhoneCharging() { super(); chargeRules.add(new MobilePhoneInCityRule()); chargeRules.add(new MobilePhoneInProvinceRule()); chargeRules.add(new MobilePhoneInlandRule()); } @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (ChargeRule rule : chargeRules) { sumCost += rule.calCost(userRecords); } return sumCost; } @Override public double getMonthlyRent() { return monthlyRent; } } class MobilePhoneMassageCharging extends ChargeMode { private double monthlyRent = 0; public MobilePhoneMassageCharging() { super(); chargeRules.add(new MobilePhoneMessageRule()); } @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (ChargeRule rule : chargeRules) { sumCost += rule.calCost(userRecords); } return sumCost; } @Override public double getMonthlyRent() { return monthlyRent; } } class Inputdeal { public int check(String input) { if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[13]")) { return 1; } else if (input.matches("[m]-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+")) { return 2; } return 0; } public void writeUser(ArrayList<User> users, String input) { User usernew = new User(); String[] inputs = input.split(" "); String num = inputs[0].substring(2); for (User i : users) { if (i.getNumber().equals(num)) { return; } } usernew.setNumber(num); int mode = Integer.parseInt(inputs[1]); if (mode == 0) { usernew.setChargeMode(new LandlinePhoneCharging()); } else if (mode == 1) { usernew.setChargeMode(new MobilePhoneCharging()); } else if (mode == 3) { usernew.setChargeMode(new MobilePhoneMassageCharging()); } users.add(usernew); } public void writeRecord(ArrayList<User> users, String input) { String[] inputs = input.split(" "); inputs[0] = inputs[0].substring(2); User callu = null, answeru = null; String out = inputs[0]; String in = ""; if (inputs.length == 6) { in = inputs[1]; } else if (inputs.length == 7) { in = inputs[1]; } else if (inputs.length == 8) { in = inputs[2]; } else { in = inputs[1]; } for (User i : users) { if (i.getNumber().equals(out)) { callu = i; } if (i.getNumber().equals(in)) { answeru = i; } if (callu != null && answeru != null) { break; } } if (input.charAt(0) == 'm') { MessageRecord messageRecord = new MessageRecord(input); if (callu != null) { callu.getUserRecords().addSendMessageRecords(messageRecord); ; } if (answeru != null) { callu.getUserRecords().addReceiveMessageRecords(messageRecord); } } } } abstract class CommunicationRecord { protected String callingNumber; protected String answerNumbe; public String getCallingNumber() { return callingNumber; } public void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } public String getAnswerNumbe() { return answerNumbe; } public void setAnswerNumbe(String answerNumbe) { this.answerNumbe = answerNumbe; } } abstract class ChargeRule { abstract public double calCost(UserRecords userRecords); } class CallRecord extends CommunicationRecord { private Date startTime; private Date endTime; private String callingAddressAreaCode; private String answerAddressAreaCode; public String getCallType() { String type = ""; if (callingAddressAreaCode.equals("0791")) { type = type.concat("1"); } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) { type = type.concat("2"); } else { type = type.concat("3"); } if (answerAddressAreaCode.equals("0791")) { type = type.concat("1"); } else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) { type = type.concat("2"); } else { type = type.concat("3"); } return type; } public CallRecord(String[] inputs) { super(); char type = inputs[0].charAt(0); String sd = null, st = null, ed = null, et = null; if (type == 't') { if (inputs.length == 6) { sd = inputs[2]; st = inputs[3]; ed = inputs[4]; et = inputs[5]; callingAddressAreaCode = inputs[0].substring(0, 4); answerAddressAreaCode = inputs[1].substring(0, 4); } else if (inputs.length == 7) { sd = inputs[3]; st = inputs[4]; ed = inputs[5]; et = inputs[6]; if (inputs[0].charAt(0) != '0') { if (inputs[2].length() == 10) { answerAddressAreaCode = inputs[2].substring(0, 3); } else { answerAddressAreaCode = inputs[2].substring(0, 4); } callingAddressAreaCode = inputs[1]; } else { if (inputs[0].length() == 10) { callingAddressAreaCode = inputs[0].substring(0, 3); } else { callingAddressAreaCode = inputs[0].substring(0, 4); } answerAddressAreaCode = inputs[2]; } } else if (inputs.length == 8) { sd = inputs[4]; st = inputs[5]; ed = inputs[6]; et = inputs[7]; callingAddressAreaCode = inputs[1]; answerAddressAreaCode = inputs[3]; } } else if (type == 'm') { } SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()); try { startTime = simpleDateFormat.parse(sd + " " + st); endTime = simpleDateFormat.parse(ed + " " + et); } catch (ParseException e) { } } public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) { super(); this.startTime = startTime; this.endTime = endTime; this.callingAddressAreaCode = callingAddressAreaCode; this.answerAddressAreaCode = answerAddressAreaCode; } public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public String getAnswerAddressAreaCode() { return answerAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } } abstract class CallChargeRule extends ChargeRule { } class LandPhoneInCityRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (CallRecord call : userRecords.getCallingInCityRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } if (call.getCallType().equals("11")) { sumCost += distanceM * 0.1; } else if (call.getCallType().equals("12")) { sumCost += distanceM * 0.2; } else if (call.getCallType().equals("13")) { sumCost += distanceM * 0.6; } } return sumCost; } } class LandPhoneInlandRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (CallRecord call : userRecords.getCallingInLandRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } sumCost += distanceM * 0.6; } return sumCost; } } class LandPhoneInProvinceRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (CallRecord call : userRecords.getCallingInProvinceRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } sumCost += distanceM * 0.3; } return sumCost; } } class MobilePhoneInCityRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (CallRecord call : userRecords.getCallingInCityRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } if (call.getCallType().equals("11")) { sumCost += distanceM * 0.1; } else if (call.getCallType().equals("12")) { sumCost += distanceM * 0.2; } else if (call.getCallType().equals("13")) { sumCost += distanceM * 0.3; } } return sumCost; } } class MobilePhoneInlandRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (CallRecord call : userRecords.getCallingInLandRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } sumCost += distanceM * 0.6; } for (CallRecord call : userRecords.getAnswerInLandRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } sumCost += distanceM * 0.3; } return sumCost; } } class MobilePhoneInProvinceRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (CallRecord call : userRecords.getCallingInProvinceRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } if (call.getCallType().equals("21")) { sumCost += distanceM * 0.3; } else if (call.getCallType().equals("22")) { sumCost += distanceM * 0.3; } else if (call.getCallType().equals("23")) { sumCost += distanceM * 0.3; } } return sumCost; } } class MobilePhoneMessageRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; int number = 0; for (MessageRecord m : userRecords.getSendMessageRecords()) { int length = m.getMessage().length(); if (length <= 10) { number++; } else { number += length / 10; if (length % 10 != 0) { number++; } } } if (number <= 3) { sumCost = number * 0.1; } else if (number <= 5) { sumCost = 0.3 + 0.2 * (number - 3); } else { sumCost = 0.7 + 0.3 * (number - 5); } return sumCost; } } class MessageRecord extends CommunicationRecord { private String message; public MessageRecord(String input) { super(); this.message = input.substring(26); } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } class User { private UserRecords userRecords = new UserRecords(); private double balance = 100; private ChargeMode chargeMode; private String number; public double calCost() { return chargeMode.calCost(userRecords); } public double calBalance() { return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords); } public UserRecords getUserRecords() { return userRecords; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } public ChargeMode getChargeMode() { return chargeMode; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } } class Outputtool { @SuppressWarnings("deprecation") public void output(double out) { BigDecimal numb = new BigDecimal(out); out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.print(out); } }View Code
这题相比前两次难度更低,所有测试点都过了。
7-2 编写一个类Shop(商店)、内部类InnerCoupons(内部购物券) 分数 30 作者 吴光生 单位 新余学院
编写一个类Shop
(商店),该类中有一个成员内部类InnerCoupons
(内部购物券),可以用于购买该商店的牛奶(假设每箱牛奶售价为50元)。要求如下:
(1)Shop类中有私有属性milkCount
(牛奶的箱数,int类型)、公有的成员方法setMilkCount( )
和getMilkCount( )
分别用于设置和获取牛奶的箱数。
(2)成员内部类InnerCoupons,有公有属性value
(面值,int类型),一个带参数的构造方法可以设定购物券的面值value,一个公有的成员方法buy( )
要求输出使用了面值为多少的购物券进行支付,同时使商店牛奶的箱数减少value/50。
(3)Shop类中还有成员变量coupons50
(面值为50元的内部购物券,类型为InnerCoupons)、coupons100
(面值为100元的内部购物券,类型为InnerCoupons)。
(4)在Shop类的构造方法中,调用内部类InnerCoupons的带参数的构造方法分别创建上面的购物券coupons50、coupons100。
在测试类Main
中,创建一个Shop类的对象myshop,从键盘输入一个整数(大于或等于3),将其设置为牛奶的箱数。假定有顾客分别使用了该商店面值为50的购物券、面值为100的购物券各消费一次,分别输出消费后商店剩下的牛奶箱数。
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); Shop myshop=new Shop(sc.nextInt()); Shop.InnerCoupons coupons50=myshop.new InnerCoupons(50); Shop.InnerCoupons coupons100=myshop.new InnerCoupons(100); coupons50.buy(); coupons100.buy(); } } class Shop{ static int milkCount; InnerCoupons coupons50; InnerCoupons coupons100; public Shop(int milkCount) { this.milkCount=milkCount; } public void setMilkCount(int milkCount) { this.milkCount=milkCount; } public int getMilkCount() { return milkCount; } public class InnerCoupons{ public int value; public InnerCoupons(int value){ this.value=value; } public void buy(){ Shop.this.milkCount=Shop.this.milkCount-value/50; System.out.println("使用了面值为"+value+"的购物券进行支付"); System.out.println("牛奶还剩"+Shop.this.milkCount+"箱"); } } }View Code 7-3 动物发声模拟器(多态) 分数 20 作者 刘凤良 单位 天津仁爱学院
设计一个动物发生模拟器,用于模拟不同动物的叫声。比如狮吼、虎啸、狗旺旺、猫喵喵……。
定义抽象类Animal,包含两个抽象方法:获取动物类别getAnimalClass()、动物叫shout();
然后基于抽象类Animal定义狗类Dog、猫类Cat和山羊Goat,用getAnimalClass()方法返回不同的动物类别(比如猫,狗,山羊),用shout()方法分别输出不同的叫声(比如喵喵、汪汪、咩咩)。
最后编写AnimalShoutTest类测试,输出:
猫的叫声:喵喵
狗的叫声:汪汪
山羊的叫声:咩咩
其中,在AnimalShoutTestMain类中,用speak(Animal animal){}方法输出动物animal的叫声,在main()方法中调用speak()方法,分别输出猫、狗和山羊对象的叫声。
请在下面的【】处添加代码。
//动物发生模拟器. 请在下面的【】处添加代码。
public class AnimalShoutTest2 {
public static void main(String[] args) {
Cat cat = new Cat();
Dog dog = new Dog();
Goat goat = new Goat();
speak(cat);
speak(dog);
speak(goat);
}
//定义静态方法speak()
【】
}
//定义抽象类Animal
【】class Animal{
【】
}
//基于Animal类,定义猫类Cat,并重写两个抽象方法
class Cat 【】{
【】
【】
}
//基于Animal类,定义狗类Dog,并重写两个抽象方法
class Dog 【】{
【】
【】
}
//基于Animal类,定义山羊类Goat,并重写两个抽象方法
class Goat 【】{
【】
【】
}
//动物发生模拟器. 请在下面的【】处添加代码。 public class Main { public static void main(String[] args) { Cat cat = new Cat(); Dog dog = new Dog(); Goat goat = new Goat(); speak(cat); speak(dog); speak(goat); } // 定义静态方法speak() private static void speak(Animal animal) { System.out.println(animal.getAnimalClass() + "的叫声:" + animal.shout()); } } //定义抽象类Animal abstract class Animal { abstract public String getAnimalClass(); abstract public String shout(); } //基于Animal类,定义猫类Cat,并重写两个抽象方法 class Cat extends Animal { @Override public String getAnimalClass() { return "猫"; } public String shout() { return "喵喵"; } } //基于Animal类,定义狗类Dog,并重写两个抽象方法 class Dog extends Animal { @Override public String getAnimalClass() { return "狗"; } @Override public String shout() { return "汪汪"; } } //基于Animal类,定义山羊类Goat,并重写两个抽象方法 class Goat extends Animal { @Override public String getAnimalClass() { return "山羊"; } @Override public String shout() { return "咩咩"; } }View Code
(三)总结
本次大作业综合性地复习了之前所学的知识,如.ArrayList类的使用和SimpleDateFormat类实现输入时间格式化处理等。编码量太少,花的时间也不够,参考网上代码也未能完成所有任务。学校的这门Java课程到这就结束了,但是我学的并不好,很多需要自己花时间自学的东西都没学。想要学好Java,之后必须要多看网课,多看多写代码。
标签:return,String,double,ArrayList,BLOG,new,public From: https://www.cnblogs.com/cdpr/p/16972474.html