我的第三次Blog作业
1.前言
2.设计与分析
3.采坑心得
4.改进建议
5.总结
(1)前言
在题目集7-1和7-2中,有很多关于继承、多态和接口的用法,题目量不多,难度适中,但是很要求我们细心去完成每一道题目,不然很容易踩坑。
而在题目集8和9中,各自一道题目,设计ATM类,难度适中,没有特别难的点,也是很考验我们的耐心的题目,需要足够细心才能找到自己错误的点。
(2)设计与分析
题目集7-1
输入格式:
- 首先,在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如:
1 3 4 2 1 3 4 2 1 3 0 - 然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
输出格式:
- 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边不能组成三角形),则输出
Wrong Format。 - 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
- 排序前的各图形类型及面积,格式为
图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格; - 排序后的各图形类型及面积,格式同排序前的输出;
- 所有图形的面积总和,格式为
Sum of area:总面积值。
输入样例1:
在这里给出一组输入。例如:
1 5 3 2 0
输出样例1:
在这里给出相应的输出。例如:
Wrong Format
代码如下:
1 import java.util.*; 2 3 public class Main { 4 // 在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接 5 // 使用Main.input.next…即可(避免采坑) 6 public static Scanner input = new Scanner(System.in); 7 8 public static void main(String[] args) { 9 ArrayList<Integer> list = new ArrayList<Integer>(); 10 int num = input.nextInt(); 11 while (num != 0) { 12 if (num < 0 || num > 4) { 13 System.out.println("Wrong Format"); 14 System.exit(0); 15 } 16 list.add(num); 17 num = input.nextInt(); 18 } 19 DealCardList dealCardList = new DealCardList(list); 20 dealCardList.inputNum(); 21 if (!dealCardList.validate()) { 22 System.out.println("Wrong Format"); 23 System.exit(0); 24 } 25 dealCardList.showResult(); 26 input.close(); 27 } 28 } 29 class DealCardList { 30 ArrayList<Integer> list= new ArrayList<Integer>(); 31 ArrayList<Double> num= new ArrayList<Double>(); 32 public DealCardList(ArrayList<Integer> list) { 33 this.list=list; 34 } 35 void inputNum() { 36 for(Integer i:list) { 37 switch(i) { 38 case 1: 39 double radius=Main.input.nextDouble(); 40 num.add(radius); 41 break; 42 case 2: 43 double width=Main.input.nextDouble(); 44 double length=Main.input.nextDouble(); 45 num.add(width); 46 num.add(length); 47 break; 48 case 3: 49 double a=Main.input.nextDouble(); 50 double b=Main.input.nextDouble(); 51 double c=Main.input.nextDouble(); 52 num.add(a); 53 num.add(b); 54 num.add(c); 55 break; 56 case 4: 57 double upper=Main.input.nextDouble(); 58 double downer=Main.input.nextDouble(); 59 double taller=Main.input.nextDouble(); 60 num.add(upper); 61 num.add(downer); 62 num.add(taller); 63 break; 64 } 65 } 66 } 67 void showResult() { 68 StringBuilder sb = new StringBuilder(); 69 ArrayList<Shape> list1= new ArrayList<Shape>(); 70 int j=0; 71 for(int i=0;i<list.size();i++) { 72 switch(list.get(i)) { 73 case 1: 74 double radius=num.get(j); 75 j++; 76 Circle cir=new Circle(); 77 cir.setRadius(radius); 78 sb.append(cir.show()); 79 sb.append(String.format("%.2f", cir.getArea())); 80 sb.append(" "); 81 num.add(cir.getArea()); 82 list1.add(new Shape(cir.getArea(),"Circle:")); 83 break; 84 case 2: 85 double width=num.get(j); 86 j++; 87 double length=num.get(j); 88 j++; 89 Rectangle rec=new Rectangle(); 90 rec.setLength(length); 91 rec.setWidth(width); 92 sb.append(rec.show()); 93 sb.append(String.format("%.2f", rec.getArea())); 94 sb.append(" "); 95 num.add(rec.getArea()); 96 list1.add(new Shape(rec.getArea(),"Rectangle:")); 97 break; 98 case 3: 99 double a=num.get(j); 100 j++; 101 double b=num.get(j); 102 j++; 103 double c=num.get(j); 104 j++; 105 Triangle tri=new Triangle(); 106 tri.setRadius(a, b, c); 107 sb.append(tri.show()); 108 sb.append(String.format("%.2f", tri.getArea())); 109 sb.append(" "); 110 num.add(tri.getArea()); 111 list1.add(new Shape(tri.getArea(),"Triangle:")); 112 break; 113 case 4: 114 double upper=num.get(j); 115 j++; 116 double downer=num.get(j); 117 j++; 118 double taller=num.get(j); 119 j++; 120 Trapezoid tra=new Trapezoid(upper,downer,taller); 121 sb.append(tra.show()); 122 sb.append(String.format("%.2f", tra.getArea())); 123 sb.append(" "); 124 num.add(tra.getArea()); 125 list1.add(new Shape(tra.getArea(),"Trapezoid:")); 126 break; 127 } 128 } 129 System.out.println("The original list:"); 130 System.out.println(sb); 131 System.out.println("The sorted list:"); 132 Collections.sort(list1); 133 double sum=0; 134 for(Shape i:list1) { 135 sum=sum+i.area; 136 System.out.print(i.str+String.format("%.2f", i.getArea())+" "); 137 } 138 System.out.println(); 139 System.out.println("Sum of area:"+String.format("%.2f", sum)); 140 } 141 boolean validate() { 142 for(Integer i:list) { 143 if(i<0) { 144 return false; 145 } 146 } 147 return true; 148 } 149 150 } 151 class Circle extends Shape { 152 private double radius; 153 double pai=3.141593; 154 Circle(){ 155 } 156 public double getRadius() { 157 return radius; 158 } 159 public void setRadius(double radius) { 160 if(validate(radius)==false) { 161 System.out.print("Wrong Format"); 162 System.exit(0); 163 } 164 this.radius = radius; 165 } 166 public double getArea() { 167 super.area=radius*radius*Math.PI; 168 return radius*radius*Math.PI; 169 } 170 public boolean validate(double n) { 171 if(n<0) { 172 return false; 173 } 174 else 175 return true; 176 } 177 public String show() { 178 return "Circle:"; 179 } 180 } 181 class Rectangle extends Shape { 182 private double width,length; 183 Rectangle(){ 184 } 185 public double getWidth() { 186 return width; 187 } 188 189 public void setWidth(double width) { 190 if(validate(width)==false) { 191 System.out.print("Wrong Format"); 192 System.exit(0); 193 } 194 this.width = width; 195 } 196 197 public double getLength() { 198 return length; 199 } 200 201 public void setLength(double length) { 202 if(validate(length)==false) { 203 System.out.print("Wrong Format"); 204 System.exit(0); 205 } 206 this.length = length; 207 } 208 public double getArea() { 209 super.area=width*length; 210 return width*length; 211 } 212 public boolean validate(double n) { 213 if(n<0) { 214 return false; 215 } 216 else 217 return true; 218 } 219 public String show() { 220 return "Rectangle:"; 221 } 222 } 223 class Shape implements Comparable{ 224 double area=1; 225 String str; 226 Shape(double area,String str){ 227 this.setStr(str); 228 this.area=area; 229 } 230 public String getStr() { 231 return str; 232 } 233 public void setStr(String str) { 234 this.str = str; 235 } 236 Shape(){ 237 } 238 public double getArea() { 239 return area; 240 } 241 public String show() { 242 return "Shape:"; 243 } 244 @Override 245 public int compareTo(Object o) { 246 // TODO 自动生成的方法存根 247 Shape a=(Shape)o; 248 if(this.area<a.area) return 1; 249 if(this.area>a.area) return -1; 250 return 0; 251 } 252 } 253 class Trapezoid extends Shape{ 254 private double upper,downer,taller; 255 Trapezoid(){ 256 257 } 258 Trapezoid(double upper,double downer,double taller){ 259 this.setTaller(taller); 260 this.setUpper(upper); 261 this.setDowner(downer); 262 } 263 public double getUpper() { 264 return upper; 265 } 266 void setUpper(double upper) { 267 if(validate(upper)==false) { 268 System.out.print("Wrong Format"); 269 System.exit(0); 270 } 271 this.upper = upper; 272 } 273 public double getDowner() { 274 return downer; 275 } 276 public void setDowner(double downer) { 277 if(validate(downer)==false) { 278 System.out.print("Wrong Format"); 279 System.exit(0); 280 } 281 this.downer = downer; 282 } 283 public double getTaller() { 284 return taller; 285 } 286 public void setTaller(double taller) { 287 if(validate(taller)==false) { 288 System.out.print("Wrong Format"); 289 System.exit(0); 290 } 291 this.taller = taller; 292 } 293 public double getArea() { 294 super.area=(upper+downer)*taller/2; 295 return (upper+downer)*taller/2; 296 } 297 boolean validate(double n) { 298 if(n<0) { 299 return false; 300 } 301 else 302 return true; 303 } 304 public String show() { 305 return "Trapezoid:"; 306 } 307 } 308 class Triangle extends Shape { 309 double a,b,c; 310 Triangle(){ 311 } 312 public void setRadius(double a,double b,double c) { 313 if(validate(a)==false||validate(b)==false||validate(c)==false||a-b>=c||a-c>=b||b-c>=a||a+b<=c||b+c<=a||a+c<=b) { 314 System.out.print("Wrong Format"); 315 System.exit(0); 316 } 317 this.a=a; 318 this.b=b; 319 this.c=c; 320 } 321 public double getArea() { 322 double p=(a+b+c)/2; 323 super.area=Math.pow(p*(p-a)*(p-b)*(p-c), 0.5); 324 return Math.pow(p*(p-a)*(p-b)*(p-c), 0.5); 325 } 326 boolean validate(double n) { 327 if(n<0) { 328 return false; 329 } 330 else 331 return true; 332 } 333 public String show() { 334 return "Triangle:"; 335 } 336 }
代码圈复杂度图如下:

如图所示,代码的平均复杂度在绿色范围内,说明代码的圈复杂度适中,代码的最大圈复杂度比较高
代码的类图如下:

分析:在这次题目中,按照题目集所给的要求写代码,然后再按照实际需求对题目所给的类图进行修改,使其更加合理。
题目集7-2
输入格式:
- 在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如:
1 3 4 2 1 3 4 2 1 3 0 - 根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
输出格式:
- 如果图形数量非法(<=0)或图形属性值非法(数值<0以及三角形三边不能组成三角形),则输出
Wrong Format。 - 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
- 排序前的各图形类型及面积,格式为
[图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前; - 输出分组后的图形类型及面积,格式为
[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出; - 各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
- 各组中面积之和的最大值输出,格式为
The max area:面积值。
输入样例1:
在这里给出一组输入。例如:
1 5 3 2 0
输出样例1:
在这里给出相应的输出。例如:
Wrong Format
1 import java.util.*; 2 3 public class Main { 4 // 在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接 5 // 使用Main.input.next…即可(避免采坑) 6 public static Scanner input = new Scanner(System.in); 7 8 public static void main(String[] args) { 9 ArrayList<Integer> list = new ArrayList<Integer>(); 10 int num = input.nextInt(); 11 while (num != 0) { 12 if (num < 0 || num > 4) { 13 System.out.println("Wrong Format"); 14 System.exit(0); 15 } 16 list.add(num); 17 num = input.nextInt(); 18 } 19 DealCardList dealCardList = new DealCardList(list); 20 dealCardList.showResult(); 21 if (dealCardList.validate()==false||list.size()==0) { 22 System.out.println("Wrong Format"); 23 System.exit(0); 24 } 25 dealCardList.show(); 26 } 27 } 28 class DealCardList { 29 ArrayList<Integer> list= new ArrayList<Integer>(); 30 ArrayList<Circle> list1= new ArrayList<Circle>(); 31 ArrayList<Rectangle> list2= new ArrayList<Rectangle>(); 32 ArrayList<Triangle> list3= new ArrayList<Triangle>(); 33 ArrayList<Trapezoid> list4= new ArrayList<Trapezoid>(); 34 35 36 DealCardList(ArrayList<Integer> list) { 37 this.list=list; 38 } 39 void showResult() { 40 for(Integer i:list) { 41 switch(i) { 42 case 1: 43 list1.add(new Circle(Main.input.nextDouble())); 44 break; 45 case 2: 46 list2.add(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble())); 47 break; 48 case 3: 49 list3.add(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 50 break; 51 case 4: 52 list4.add(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 53 break; 54 } 55 } 56 } 57 void show() { 58 System.out.println("The original list:"); 59 int i1=0,i2=0,i3=0,i4=0; 60 System.out.print("["); 61 for(Integer i:list) { 62 switch(i) { 63 case 1: 64 System.out.print(Circle.show()+String.format("%.2f", list1.get(i1).getArea())+" "); 65 i1++; 66 break; 67 case 2: 68 System.out.print(Rectangle.show()+String.format("%.2f", list2.get(i2).getArea())+" "); 69 i2++; 70 break; 71 case 3: 72 System.out.print(Triangle.show()+String.format("%.2f", list3.get(i3).getArea())+" "); 73 i3++; 74 break; 75 case 4: 76 System.out.print(Trapezoid.show()+String.format("%.2f", list4.get(i4).getArea())+" "); 77 i4++; 78 break; 79 } 80 } 81 System.out.println("]"); 82 System.out.println("The Separated List:"); 83 System.out.print("["); 84 for (Circle i : list1) { 85 System.out.print(Circle.show()+String.format("%.2f", i.getArea())+" "); 86 } 87 System.out.print("]"); 88 System.out.print("["); 89 for (Rectangle i : list2) { 90 System.out.print(Rectangle.show()+String.format("%.2f", i.getArea())+" "); 91 } 92 System.out.print("]"); 93 System.out.print("["); 94 for (Triangle i : list3) { 95 System.out.print(Triangle.show()+String.format("%.2f", i.getArea())+" "); 96 } 97 System.out.print("]"); 98 System.out.print("["); 99 for (Trapezoid i : list4) { 100 System.out.print(Trapezoid.show()+String.format("%.2f", i.getArea())+" "); 101 } 102 System.out.println("]"); 103 System.out.println("The Separated sorted List:"); 104 Collections.sort(list1); 105 Collections.sort(list2); 106 Collections.sort(list3); 107 Collections.sort(list4); 108 double max1=0,max2=0,max3=0,max4=0; 109 System.out.print("["); 110 for (Circle i : list1) { 111 max1=max1+i.getArea(); 112 System.out.print(Circle.show()+String.format("%.2f", i.getArea())+" "); 113 } 114 System.out.print("]"); 115 System.out.print("["); 116 for (Rectangle i : list2) { 117 max2=max2+i.getArea(); 118 System.out.print(Rectangle.show()+String.format("%.2f", i.getArea())+" "); 119 } 120 System.out.print("]"); 121 System.out.print("["); 122 for (Triangle i : list3) { 123 max3=max3+i.getArea(); 124 System.out.print(Triangle.show()+String.format("%.2f", i.getArea())+" "); 125 } 126 System.out.print("]"); 127 System.out.print("["); 128 for (Trapezoid i : list4) { 129 max4=max4+i.getArea(); 130 System.out.print(Trapezoid.show()+String.format("%.2f", i.getArea())+" "); 131 } 132 System.out.println("]"); 133 double max=max1; 134 if(max2>max) { 135 max=max2; 136 } 137 if(max3>max) { 138 max=max3; 139 } 140 if(max4>max) { 141 max=max4; 142 } 143 System.out.println("The max area:"+String.format("%.2f",max)); 144 } 145 boolean validate() { 146 for(Circle i:list1) { 147 if(i.getRadius()<0) { 148 return false; 149 } 150 } 151 for(Rectangle i:list2) { 152 if(i.getLength()<0||i.getWidth()<0) { 153 return false; 154 } 155 } 156 for(Triangle i:list3) { 157 if(i.validate()==false) { 158 return false; 159 } 160 } 161 for(Trapezoid i:list4) { 162 if(i.getDowner()<0||i.getTaller()<0||i.getUpper()<0) { 163 return false; 164 } 165 } 166 return true; 167 } 168 } 169 170 class Shape{ 171 double area=1; 172 String str; 173 Shape(){ 174 } 175 Shape(double area,String str){ 176 this.setStr(str); 177 this.area=area; 178 } 179 public String getStr() { 180 return str; 181 } 182 public void setStr(String str) { 183 this.str = str; 184 } 185 public double getArea() { 186 return area; 187 } 188 // @Override 189 // public int compareTo(Object o) { 190 // // TODO 自动生成的方法存根 191 // Shape a=(Shape)o; 192 // if(this.area<a.area) return 1; 193 // if(this.area>a.area) return -1; 194 // return 0; 195 // } 196 } 197 class Circle extends Shape implements Comparable<Circle>{ 198 private double radius; 199 Circle(){ 200 } 201 Circle(double radius){ 202 this.setRadius(radius); 203 } 204 public double getRadius() { 205 return radius; 206 } 207 public void setRadius(double radius) { 208 this.radius = radius; 209 } 210 public double getArea() { 211 super.area=radius*radius*Math.PI; 212 return radius*radius*Math.PI; 213 } 214 public static String show() { 215 return "Circle:"; 216 } 217 public int compareTo(Circle o) { 218 // TODO 自动生成的方法存根 219 if(this.area<o.area) return 1; 220 if(this.area>o.area) return -1; 221 return 0; 222 } 223 } 224 class Rectangle extends Shape implements Comparable<Rectangle>{ 225 private double width,length; 226 Rectangle(){ 227 } 228 Rectangle(double width,double length){ 229 this.setLength(length); 230 this.setWidth(width); 231 } 232 public double getWidth() { 233 return width; 234 } 235 236 public void setWidth(double width) { 237 this.width = width; 238 } 239 240 public double getLength() { 241 return length; 242 } 243 244 public void setLength(double length) { 245 this.length = length; 246 } 247 public double getArea() { 248 super.area=width*length; 249 return width*length; 250 } 251 public static String show() { 252 return "Rectangle:"; 253 } 254 @Override 255 public int compareTo(Rectangle o) { 256 // TODO 自动生成的方法存根 257 if(this.area<o.area) return 1; 258 if(this.area>o.area) return -1; 259 return 0; 260 } 261 } 262 class Triangle extends Shape implements Comparable<Triangle>{ 263 private double a,b,c; 264 public double getA() { 265 return a; 266 } 267 public void setA(double a) { 268 this.a = a; 269 } 270 public double getB() { 271 return b; 272 } 273 public void setB(double b) { 274 this.b = b; 275 } 276 public double getC() { 277 return c; 278 } 279 public void setC(double c) { 280 this.c = c; 281 } 282 Triangle(){ 283 } 284 Triangle(double a,double b,double c) { 285 this.a=a; 286 this.b=b; 287 this.c=c; 288 } 289 public double getArea() { 290 double p=(a+b+c)/2; 291 super.area=Math.pow(p*(p-a)*(p-b)*(p-c), 0.5); 292 return Math.pow(p*(p-a)*(p-b)*(p-c), 0.5); 293 } 294 boolean validate() { 295 if(a<0||b<0||c<0) { 296 return false; 297 } 298 if(a-b>=c||a-c>=b||b-c>=a||a+b<=c||b+c<=a||a+c<=b) { 299 return false; 300 } 301 return true; 302 } 303 public static String show() { 304 return "Triangle:"; 305 } 306 @Override 307 public int compareTo(Triangle o) { 308 // TODO 自动生成的方法存根 309 if(this.area<o.area) return 1; 310 if(this.area>o.area) return -1; 311 return 0; 312 } 313 } 314 class Trapezoid extends Shape implements Comparable<Trapezoid>{ 315 private double upper,downer,taller; 316 Trapezoid(){ 317 318 } 319 Trapezoid(double upper,double downer,double taller){ 320 this.setTaller(taller); 321 this.setUpper(upper); 322 this.setDowner(downer); 323 } 324 public double getUpper() { 325 return upper; 326 } 327 void setUpper(double upper) { 328 this.upper = upper; 329 } 330 public double getDowner() { 331 return downer; 332 } 333 public void setDowner(double downer) { 334 this.downer = downer; 335 } 336 public double getTaller() { 337 return taller; 338 } 339 public void setTaller(double taller) { 340 this.taller = taller; 341 } 342 public double getArea() { 343 super.area=(upper+downer)*taller/2; 344 return (upper+downer)*taller/2; 345 } 346 public static String show() { 347 return "Trapezoid:"; 348 } 349 @Override 350 public int compareTo(Trapezoid o) { 351 // TODO 自动生成的方法存根 352 if(this.area<o.area) return 1; 353 if(this.area>o.area) return -1; 354 return 0; 355 } 356 }
代码的圈复杂度如下:

如图所示,代码的平均圈复杂度很小,但是代码中的最大圈复杂度超出绿色范围很多,说明某一部分代码过于复杂,但是在此图中,除了最大圈复杂度很大之外,其余都在很好的区间内,尽管代码最大复杂度很复杂,但是总体来说,代码的质量还是很好的。
代码的类图如下所示:

分析:在一开始题目所给的类图中填写代码,然后通过修改类之间的关系,来优化代码,使其更加合理。
题目集8:
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
- 存款、取款功能输入数据格式:
卡号 密码 ATM机编号 金额(由一个或多个空格分隔), 其中,当金额大于0时,代表取款,否则代表存款。 - 查询余额功能输入数据格式:
卡号
输出格式:
①输入错误处理
- 如果输入卡号不存在,则输出
Sorry,this card does not exist.。 - 如果输入ATM机编号不存在,则输出
Sorry,the ATM's id is wrong.。 - 如果输入银行卡密码错误,则输出
Sorry,your password is wrong.。 - 如果输入取款金额大于账户余额,则输出
Sorry,your account balance is insufficient.。 - 如果检测为跨行存取款,则输出
Sorry,cross-bank withdrawal is not supported.。
②取款业务输出
输出共两行,格式分别为:
[用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
③存款业务输出
输出共两行,格式分别为:
[用户姓名]在[银行名称]的[ATM编号]上存款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
④查询余额业务输出
¥[金额]
金额保留两位小数。
输入样例1:
在这里给出一组输入。例如:
6222081502001312390 88888888 06 -500.00
#
输出样例1:
在这里给出相应的输出。例如:
张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00
代码如下:
1 import java.util.*; 2 public class Main { 3 public static void main(String args[]) { 4 ArrayList<User> u=new ArrayList<User>(); 5 u.add(new User("杨过","中国建设银行",new String[]{"6217000010041315709","6217000010041315715"})); 6 u.add(new User("杨过","中国建设银行",new String[]{"6217000010041315718"})); 7 u.add(new User("郭靖","中国建设银行",new String[]{"6217000010051320007"})); 8 u.add(new User("张无忌","中国工商银行",new String[]{"6222081502001312389",})); 9 u.add(new User("张无忌","中国工商银行",new String[]{"6222081502001312390"})); 10 u.add(new User("张无忌","中国工商银行",new String[]{"6222081502001312399","6222081502001312400"})); 11 u.add(new User("韦小宝","中国工商银行",new String[]{"6222081502051320785"})); 12 u.add(new User("韦小宝","中国工商银行",new String[]{"6222081502051320786"})); 13 14 Scanner input=new Scanner(System.in); 15 boolean flag=false; 16 boolean flag1=false; 17 String str=input.nextLine().trim(); 18 while(str.equals("#")==false){ 19 String[] str1=str.split("\\s+"); 20 // for(int i=0;i<str1.length;i++){ 21 // System.out.println(str1[i]); 22 // } 23 // int len=str1.length; 24 // for(int i=0;i<str1.length;i++){ 25 // if(str1[i]==""){ 26 // for(int j=i;j<str1.length-1;j++){ 27 // str1[j]=str1[j+1]; 28 // } 29 // len--; 30 // } 31 // } 32 if(str1.length==4){ 33 User j = new User(); 34 int i=0; 35 for(i=0;i<u.size();i++){ 36 j=u.get(i); 37 if(j.checkUsernum(str1[0])==true){ 38 flag=true; 39 break; 40 } 41 } 42 if(flag==false){ 43 System.out.println("Sorry,this card does not exist."); 44 System.exit(0); 45 } 46 if(str1[2].equals("01")==true||str1[2].equals("02")==true||str1[2].equals("03")==true||str1[2].equals("04")==true||str1[2].equals("05")==true||str1[2].equals("06")==true){ 47 if(str1[1].equals("88888888")==false){ 48 System.out.println("Sorry,your password is wrong."); 49 System.exit(0); 50 } 51 52 double num=Double.parseDouble(str1[3]); 53 if(num>0&&j.getMoney()<num){ 54 System.out.println("Sorry,your account balance is insufficient."); 55 System.exit(0); 56 } 57 for(i=0;i<j.bankNum.length;i++){ 58 if(j.bankNum[i].equals(str1[2])==true){ 59 flag1=true; 60 break; 61 } 62 } 63 if(flag1==false){ 64 System.out.println("Sorry,cross-bank withdrawal is not supported."); 65 System.exit(0); 66 } 67 68 //所有输入都正确的操作 69 j.setMoney(num,str1[2]); 70 } 71 else{ 72 System.out.println("Sorry,the ATM's id is wrong."); 73 System.exit(0); 74 } 75 } 76 else 77 if(str1.length==1){ 78 int i=0; 79 User j = new User(); 80 for(i=0;i<u.size();i++){ 81 j=u.get(i); 82 if(j.checkUsernum(str1[0])==true){ 83 flag1=true; 84 break; 85 } 86 } 87 if(flag1==false){ 88 System.out.println("Sorry,this card does not exist."); 89 System.exit(0); 90 } 91 System.out.println("¥"+String.format("%.2f",j.getMoney())); 92 } 93 else{ 94 System.exit(0); 95 } 96 str=input.nextLine().trim(); 97 } 98 // System.out.println(str1); 99 100 } 101 } 102 class User { 103 private String userName; 104 private String bankName; 105 private double money=10000; 106 String[] userNum; 107 String[] bankNum; 108 User(){ 109 110 } 111 User(String userName,String bankName,String[] userNum){ 112 this.userName=userName; 113 this.bankName=bankName; 114 this.userNum=userNum; 115 if(bankName.equals("中国建设银行")==true){ 116 this.bankNum=new String[]{"01","02","03","04"}; 117 } 118 else{ 119 this.bankNum=new String[]{"05","06"}; 120 } 121 } 122 public double getMoney(){ 123 return money; 124 } 125 public void setMoney(double money,String bankNum){ 126 if(money>0){ 127 this.money=this.money-money; 128 System.out.println(userName+"在"+bankName+"的"+bankNum+"号ATM机上取款¥"+String.format("%.2f",money)); 129 System.out.println("当前余额为¥"+String.format("%.2f",this.money)); 130 } 131 else{ 132 this.money=this.money-money; 133 System.out.println(userName+"在"+bankName+"的"+bankNum+"号ATM机上存款¥"+String.format("%.2f",-money)); 134 System.out.println("当前余额为¥"+String.format("%.2f",this.money)); 135 } 136 } 137 public boolean checkUsernum(String str){ 138 for(int i=0;i<userNum.length;i++){ 139 if(userNum[i].equals(str)==true){ 140 return true; 141 } 142 } 143 return false; 144 } 145 public boolean checkBankNum(String str){ 146 for(int i=0;i<bankNum.length;i++){ 147 if(bankNum[i].equals(str)==true){ 148 return true; 149 } 150 } 151 return false; 152 } 153 public void show(){ 154 System.out.print(this.userName+" "+this.bankName+" "); 155 for(int i=0;i<this.userNum.length;i++){ 156 System.out.print(this.userNum[i]+" "); 157 } 158 System.out.println(); 159 } 160 }
实验圈复杂度图如下所示:

在该图中,代码的平均圈复杂度良好,最大圈复杂度过大,代码的平均深度和最大深度都在绿色区间,说明代码的质量较高,完成度很好。
代码的类图如下:

分析:在这次作业中,我并没有完全按照题目所给的要求去写代码,而是把不必要的部分没有添加到代码里面,这是为了减少自己的工作量,从而也简化了代码,但是也完成了题目的要求的点,虽然这很不好,但是希望我会在下次改正过来。
题目集9:
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
- 取款功能输入数据格式:
卡号 密码 ATM机编号 金额(由一个或多个空格分隔) - 查询余额功能输入数据格式:
卡号
输出格式:
①输入错误处理
- 如果输入卡号不存在,则输出
Sorry,this card does not exist.。 - 如果输入ATM机编号不存在,则输出
Sorry,the ATM's id is wrong.。 - 如果输入银行卡密码错误,则输出
Sorry,your password is wrong.。 - 如果输入取款金额大于账户余额,则输出
Sorry,your account balance is insufficient.。
②取款业务输出
输出共两行,格式分别为:
业务:取款 [用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
③查询余额业务输出
业务:查询余额 ¥[金额]
金额保留两位小数。
输入样例1:
在这里给出一组输入。例如:
6222081502001312390 88888888 06 500.00
#
输出样例1:
在这里给出相应的输出。例如:
业务:取款 张无忌在中国工商银行的06号ATM机上取款¥500.00
当前余额为¥9500.00
1 import java.util.*; 2 import java.util.ArrayList; 3 public class Main { 4 public static void main(String args[]) { 5 ArrayList<User> u=new ArrayList<User>(); 6 u.add(new User("杨过","中国建设银行",1,new String[]{"6217000010041315709","6217000010041315715"})); 7 u.add(new User("杨过","中国建设银行",1,new String[]{"6217000010041315718"})); 8 u.add(new User("郭靖","中国建设银行",1,new String[]{"6217000010051320007"})); 9 u.add(new User("张无忌","中国工商银行",1,new String[]{"6222081502001312389",})); 10 u.add(new User("张无忌","中国工商银行",1,new String[]{"6222081502001312390"})); 11 u.add(new User("张无忌","中国工商银行",1,new String[]{"6222081502001312399","6222081502001312400"})); 12 u.add(new User("韦小宝","中国工商银行",1,new String[]{"6222081502051320785"})); 13 u.add(new User("韦小宝","中国工商银行",1,new String[]{"6222081502051320786"})); 14 u.add(new User("张三丰","中国建设银行",2,new String[]{"6640000010045442002","6640000010045442003"})); 15 u.add(new User("令狐冲","中国工商银行",2,new String[]{"6640000010045441009"})); 16 u.add(new User("乔峰","中国农业银行",2,new String[]{"6630000010033431001"})); 17 u.add(new User("洪七公","中国农业银行",2,new String[]{"6630000010033431008"})); 18 19 Scanner input=new Scanner(System.in); 20 21 String str=input.nextLine().trim(); 22 while(str.equals("#")==false){ 23 boolean flag=false; 24 boolean flag1=false; 25 boolean flag2=false; 26 String[] str1=str.split("\\s+"); 27 if(str1.length==4){ 28 User j = new User(); 29 int i=0; 30 for(i=0;i<u.size();i++){ 31 j=u.get(i); 32 if(j.checkUsernum(str1[0])==true){ 33 flag=true; 34 break; 35 } 36 } 37 if(flag==false){ 38 System.out.println("Sorry,this card does not exist."); 39 System.exit(0); 40 } 41 if(str1[2].equals("01")==true||str1[2].equals("02")==true||str1[2].equals("03")==true||str1[2].equals("04")==true||str1[2].equals("05")==true||str1[2].equals("06")==true|| 42 str1[2].equals("07")==true||str1[2].equals("08")==true||str1[2].equals("09")==true||str1[2].equals("10")==true||str1[2].equals("11")==true){ 43 if(str1[1].equals("88888888")==false){ 44 System.out.println("Sorry,your password is wrong."); 45 System.exit(0); 46 } 47 48 double num=Double.parseDouble(str1[3]); 49 if((num>0&&j.getMoney()<=num)||(j.account==2&&j.getMoney()<0)){ 50 if(j.account==1) { 51 System.out.println("Sorry,your account balance is insufficient."); 52 System.exit(0); 53 } 54 else{ 55 flag2=true; 56 } 57 } 58 for(i=0;i<j.bankNum.length;i++){ 59 if(j.bankNum[i].equals(str1[2])==true){ 60 flag1=true; 61 break; 62 } 63 } 64 //所有输入都正确的操作 65 if(flag2==false) 66 j.setMoney(num,str1[2],flag1); 67 else { 68 j.setmoney1(num, str1[2], flag1); 69 } 70 } 71 else{ 72 System.out.println("Sorry,the ATM's id is wrong."); 73 System.exit(0); 74 } 75 } 76 else 77 if(str1.length==1){ 78 int i=0; 79 User j = new User(); 80 for(i=0;i<u.size();i++){ 81 j=u.get(i); 82 if(j.checkUsernum(str1[0])==true){ 83 flag1=true; 84 break; 85 } 86 } 87 if(flag1==false){ 88 System.out.println("Sorry,this card does not exist."); 89 System.exit(0); 90 } 91 System.out.println("业务:查询余额 ¥"+String.format("%.2f",j.getMoney())); 92 } 93 else{ 94 System.exit(0); 95 } 96 str=input.nextLine().trim(); 97 } 98 // System.out.println(str1); 99 100 } 101 } 102 103 104 class User { 105 private String userName; 106 private String bankName; 107 int account; 108 private double money=10000; 109 private double li; 110 String[] userNum; 111 String[] bankNum; 112 113 User() { 114 115 } 116 117 User(String userName, String bankName, int account, String[] userNum) { 118 this.userName = userName; 119 this.bankName = bankName; 120 this.account = account; 121 this.userNum = userNum; 122 if (bankName.equals("中国建设银行") == true) { 123 this.bankNum = new String[]{"01", "02", "03", "04"}; 124 } else if (bankName.equals("中国工商银行") == true) { 125 this.bankNum = new String[]{"05", "06"}; 126 } else { 127 this.bankNum = new String[]{"07", "08", "09", "10", "11"}; 128 } 129 } 130 public double getMoney(){ 131 return money; 132 } 133 134 public void setMoney(double money, String bankNum, boolean flag) {//flag=false是跨行取款 135 if (flag == true) { 136 if (money > 0) { 137 this.money = this.money - money; 138 System.out.println("业务:取款 " + userName + "在" + bankName + "的" + bankNum + "号ATM机上取款¥" + String.format("%.2f", money)); 139 System.out.println("当前余额为¥" + String.format("%.2f", this.money)); 140 } else { 141 this.money = this.money - money; 142 System.out.println("业务:存款 " + userName + "在" + bankName + "的" + bankNum + "号ATM机上存款¥" + String.format("%.2f", -money)); 143 System.out.println("当前余额为¥" + String.format("%.2f", this.money)); 144 } 145 } else { 146 String bankName1; 147 if (bankNum.equals("01") || bankNum.equals("02") || bankNum.equals("03") || bankNum.equals("04")) { 148 bankName1 = new String("中国建设银行"); 149 li = 0.02; 150 } else if (bankNum.equals("05") || bankNum.equals("06")) { 151 bankName1 = new String("中国工商银行"); 152 li = 0.03; 153 } else { 154 bankName1 = new String("中国农业银行"); 155 li = 0.04; 156 } 157 if (money > 0) { 158 this.money = this.money - money - money * li; 159 if(this.money<0){ 160 System.out.println("Sorry,your account balance is insufficient."); 161 System.exit(0); 162 } 163 System.out.println("业务:取款 " + userName + "在" + bankName1 + "的" + bankNum + "号ATM机上取款¥" + String.format("%.2f", money)); 164 System.out.println("当前余额为¥" + String.format("%.2f", this.money)); 165 } else { 166 this.money = this.money - money - (-money) * li; 167 if(this.money<0){ 168 System.out.println("Sorry,your account balance is insufficient."); 169 System.exit(0); 170 } 171 System.out.println("业务:存款 " + userName + "在" + bankName1 + "的" + bankNum + "号ATM机上存款¥" + String.format("%.2f", -money)); 172 System.out.println("当前余额为¥" + String.format("%.2f", this.money)); 173 } 174 } 175 } 176 177 public void setmoney1(double money, String bankNum, boolean flag) { 178 String bankName1; 179 if (bankNum.equals("01") || bankNum.equals("02") || bankNum.equals("03") || bankNum.equals("04")) { 180 bankName1 = new String("中国建设银行"); 181 li = 0.02; 182 } else if (bankNum.equals("05") || bankNum.equals("06")) { 183 bankName1 = new String("中国工商银行"); 184 li = 0.03; 185 } else { 186 bankName1 = new String("中国农业银行"); 187 li = 0.04; 188 } 189 if (flag == true) { 190 if(this.money>money) { 191 this.money = this.money - money; 192 } 193 else{ 194 if(this.money>0) { 195 this.money = this.money - money - (money - this.money) * 0.05; 196 } 197 else{ 198 this.money = this.money - money - money * 0.05; 199 } 200 } 201 } else { 202 if(this.money>money) { 203 this.money = this.money - money- money * li; 204 } 205 else{ 206 if(this.money>0) { 207 this.money = this.money - money - money * li - (money - this.money) * 0.05; 208 } 209 else{ 210 this.money = this.money - money - money * li - money * 0.05; 211 } 212 } 213 } 214 if(this.money<-50000){ 215 System.out.println("Sorry,your account balance is insufficient."); 216 System.exit(0); 217 } 218 if(flag==false) { 219 System.out.println("业务:取款 " + userName + "在" + bankName1 + "的" + bankNum + "号ATM机上取款¥" + String.format("%.2f", money)); 220 System.out.println("当前余额为¥" + String.format("%.2f", this.money)); 221 } 222 else{ 223 System.out.println("业务:取款 " + userName + "在" + bankName + "的" + bankNum + "号ATM机上取款¥" + String.format("%.2f", money)); 224 System.out.println("当前余额为¥" + String.format("%.2f", this.money)); 225 } 226 } 227 public boolean checkUsernum(String str){ 228 for(int i=0;i<userNum.length;i++){ 229 if(userNum[i].equals(str)==true){ 230 return true; 231 } 232 } 233 return false; 234 } 235 public boolean checkBankNum(String str){ 236 for(int i=0;i<bankNum.length;i++){ 237 if(bankNum[i].equals(str)==true){ 238 return true; 239 } 240 } 241 return false; 242 } 243 }
代码的圈复杂度如下:

在此图中可以看出,代码的平均复杂度和平均深度都超出了绿色范围之外,而且最大深度和最大全复杂度都远远超出绿色范围,说明代码的可读性不高,完成度也不高,从而也给自己一个警响吧,希望下次自己可以完成的更好。
代码类图如下

在这次代码类图和题目集8差别不大,主要多了一个在贷款时的处理的函数。
(3)采坑心得
1.在题目集9和题目集8中,很多时候都是因为填错账号而导致前三个测试点没有对

2.在题目集7-1中,要注意每个图形的取值范围,建议先全部看一遍题目在开始做题目,把每个图形的边的范围弄清楚,这样在做计算的时候不会出错!!!
(4)改进建议
1.在上面的分析中,题目集9中的题目圈复杂度很高,在以后的时间里面,修改代码之间的关系,用一种更合理的方式去写代码,不要过多的使用if之类的增加圈复杂度的代码。
2.在题目集8中,我写的代码并没有完全按照题目的实际情况来进行操作,希望以后可以通过修改类或者增加一些必要的类来贴合题目与实际情况。
(5)总结
总的来说,通过这几次题目集,对于Java继承和正则表达式有了更加深刻的理解,学到了如何通过修改代码之间的关系减少圈复杂度,但是有时候因为自己想要减少工作量而偷懒,希望在以后的Java学习中,我会更加认真和仔细的对待每一次Java作业,不会偷懒,不会偷工减料,以及在每一次Java课后都要做课堂总结和复习课堂所讲的内容,实验出真知,多写写代码提高自己的水平。

浙公网安备 33010602011771号