OO第三次博客作业
前言:
这三次的题目的难度就挺循序渐进的,题目集7是一式两份的渐进式的大题,题目8和9也是拆开的两道渐进式的大题,感觉老师是不是接收到了大家感觉难度跳跃的信号(也可能是本身教学安排就这会就比较科学),对这点就感觉挺快乐的。这一次总的来说题量肯定是比较小,满打满算一共就4道题,。这一次就没有官方类图了,思路都是自己来,同样的思路能否在改变后的题目中也同样适用,可能是个问题,估计也是这三次题目集来锻炼下代码思路和质量这样。
设计与分析:
①题目集7(7-1)、(7-2)两道题目的递进式设计分析总结
掌握类的继承、多态性使用方法以及接口的应用。详见作业指导书 2020-OO第07次作业-1指导书V1.0.pdf
输入格式:
- 首先,在一行上输入一串数字(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 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Scanner; 4 5 public class Main { 6 public static Scanner input = new Scanner(System.in); 7 public static void main(String[] args){ 8 ArrayList<Integer> list = new ArrayList<Integer>(); 9 int num = input.nextInt(); 10 while(num != 0){ 11 if(num < 0 || num > 4){ 12 System.out.println("Wrong Format"); 13 System.exit(0); 14 } 15 list.add(num); 16 num = input.nextInt(); 17 } 18 DealCardList dealCardList = new DealCardList(list); 19 if(!dealCardList.validate()){ 20 System.out.println("Wrong Format"); 21 System.exit(0); 22 } 23 System.out.println("The original list:"); 24 dealCardList.showResult(); 25 System.out.println(); 26 System.out.println("The sorted list:"); 27 dealCardList.cardSort(); 28 dealCardList.showResult(); 29 System.out.println(); 30 System.out.println("Sum of area:"+String.format("%.2f",dealCardList.getAllArea())); 31 input.close(); 32 } 33 } 34 class Card{ 35 Shape shape; 36 Card(){ 37 } 38 Card(Shape shape){ 39 this.shape=shape; 40 } 41 public Shape getShape() { 42 return shape; 43 } 44 public void setShape(Shape Shape) { 45 this.shape=shape; 46 } 47 } 48 class DealCardList { 49 ArrayList<Card> cardList = new ArrayList<Card>(); 50 51 DealCardList() { 52 } 53 54 DealCardList(ArrayList<Integer> list) { 55 for (int i = 0; i < list.size(); i++) { 56 if (list.get(i) == 1) { 57 double r = Main.input.nextDouble(); 58 Circle circle = new Circle(r); 59 Card card = new Card(circle); 60 card.getShape().setShapeName("Circle"); 61 cardList.add(card); 62 } 63 if (list.get(i) == 2) { 64 double a = Main.input.nextDouble(); 65 double b = Main.input.nextDouble(); 66 Rectangle rectangle = new Rectangle(a, b); 67 Card card = new Card(rectangle); 68 card.getShape().setShapeName("Rectangle"); 69 cardList.add(card); 70 } 71 if (list.get(i) == 3) { 72 double a = Main.input.nextDouble(); 73 double b = Main.input.nextDouble(); 74 double c = Main.input.nextDouble(); 75 Triangle triangle = new Triangle(a, b, c); 76 Card card = new Card(triangle); 77 card.getShape().setShapeName("Triangle"); 78 cardList.add(card); 79 } 80 if (list.get(i) == 4) { 81 double a = Main.input.nextDouble(); 82 double b = Main.input.nextDouble(); 83 double c = Main.input.nextDouble(); 84 Traperoid traperoid = new Traperoid(a, b, c); 85 Card card = new Card(traperoid); 86 card.getShape().setShapeName("Trapezoid"); 87 cardList.add(card); 88 } 89 } 90 } 91 public double getAllArea() { 92 double s = 0; 93 for (int i = 0; i < cardList.size(); i++) 94 s = s + cardList.get(i).getShape().getArea(); 95 return s; 96 } 97 public boolean validate() { 98 for (int i = 0; i < cardList.size(); i++) { 99 if (!cardList.get(i).getShape().vaildate()) 100 return false; 101 } 102 return true; 103 } 104 public void cardSort() { 105 for (int k = 0; k < cardList.size(); k++) 106 for (int i = k + 1; i < cardList.size(); i++) { 107 if (cardList.get(k).getShape().getArea() < cardList.get(i).getShape().getArea()) 108 Collections.swap(cardList, k, i); 109 } 110 } 111 public void showResult() { 112 for(int i=0;i<cardList.size();i++) 113 System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" "); 114 } 115 } 116 class Shape { 117 private String shapeName; 118 Shape(){ 119 } 120 Shape(String shapeName){ 121 this.shapeName=shapeName; 122 } 123 public String getShapeName() { 124 return shapeName; 125 } 126 public void setShapeName(String shapeName) { 127 this.shapeName=shapeName; 128 } 129 public double getArea() { 130 return 0.0; 131 } 132 public boolean vaildate() { 133 return true; 134 } 135 } 136 class Circle extends Shape{ 137 private double radius; 138 Circle(){ 139 } 140 Circle(double radius){ 141 this.radius=radius; 142 } 143 public double getArea() { 144 return Math.PI*radius*radius; 145 } 146 public boolean vaildate() { 147 if(radius>0) 148 return true; 149 else 150 return false; 151 } 152 } 153 class Rectangle extends Shape{ 154 private double width,length; 155 Rectangle (double width,double length){ 156 this.width=width; 157 this.length=length; 158 } 159 public double getArea() { 160 return width*length; 161 } 162 public boolean vaildate() { 163 if(width>0&&length>0) 164 return true; 165 else 166 return false; 167 } 168 } 169 class Triangle extends Shape{ 170 double side1,side2,side3; 171 Triangle(double side1,double side2,double side3){ 172 this.side1=side1; 173 this.side2=side2; 174 this.side3=side3; 175 } 176 public double getArea() { 177 double c=(side1+side2+side3)/2; 178 double s=Math.sqrt(c*(c-side1)*(c-side2)*(c-side3)); 179 return s; 180 } 181 public boolean vaildate() { 182 if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1) 183 return true; 184 else 185 return false; 186 } 187 } 188 class Rectanglea extends Shape{ 189 private double width; 190 private double length; 191 public double getArea(){ 192 return width*length; 193 } 194 public double getwidth(){ 195 return width; 196 } 197 public void Rectangle(double width, double length){ 198 this.width = width; 199 this.length = length; 200 } 201 public void setlength(double length){ 202 this.length=length; 203 } 204 public boolean isRectangle() { 205 return width > 0 && length > 0; 206 } 207 } 208 class Trianglea extends Shape{ 209 private double a; 210 private double b; 211 private double c; 212 public void Triangle(double a, double b, double c) { 213 this.a = a; 214 this.b = b; 215 this.c = c; 216 } 217 public double getArea() { 218 double p=(a+b+c)/2; 219 return Math.sqrt(p*(p-a)*(p-b)*(p-c)); 220 } 221 public boolean isTriangle() { 222 if (a < 0 || b < 0 || c < 0) { 223 return false; 224 } 225 double[] sides = new double[3]; 226 sides[0] = a; 227 sides[1] = b; 228 sides[2] = c; 229 return sides[0] + sides[1] > sides[2]; 230 } 231 } 232 class Traperoid extends Shape{ 233 private double topSide,bottomSide,height; 234 Traperoid(){ 235 } 236 Traperoid(double topSide,double bottomSide,double height){ 237 this.bottomSide=bottomSide; 238 this.height=height; 239 this.topSide=topSide; 240 } 241 public double getArea() { 242 return (topSide+bottomSide)*height/2; 243 } 244 public boolean validate() { 245 if(topSide>0&&bottomSide>0&&height>0) 246 return true; 247 else 248 return false; 249 } 250 }
掌握类的继承、多态性使用方法以及接口的应用。 具体需求参考作业指导书。
输入格式:
- 在一行上输入一串数字(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 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Scanner; 4 5 public class Main { 6 public static Scanner input = new Scanner(System.in); 7 public static void main(String[] args){ 8 ArrayList<Integer> list = new ArrayList<Integer>(); 9 int num = input.nextInt(); 10 while(num != 0){ 11 if(num < 0 || num > 4){ 12 System.out.println("Wrong Format"); 13 System.exit(0); 14 } 15 list.add(num); 16 num = input.nextInt(); 17 } 18 while(num==0){ 19 System.out.println("Wrong Format"); 20 System.exit(0); 21 } 22 DealCardList dealCardList = new DealCardList(list); 23 if(!dealCardList.validate()){ 24 System.out.println("Wrong Format"); 25 System.exit(0); 26 } 27 System.out.println("The original list:"); 28 System.out.print("["); 29 dealCardList.showResult1(); 30 System.out.print("]"); 31 System.out.println(); 32 System.out.println("The Separated List:"); 33 dealCardList.cardSort(); 34 dealCardList.showResult2(); 35 System.out.println(); 36 System.out.println("The Separated sorted List:"); 37 dealCardList.cardSort(); 38 dealCardList.showResult2(); 39 System.out.println(); 40 System.out.println("The max area:"+String.format("%.2f",dealCardList.getmaxArea())); 41 input.close(); 42 } 43 } 44 class Card{ 45 Shape shape; 46 Card(){ 47 } 48 Card(Shape shape){ 49 this.shape=shape; 50 } 51 public Shape getShape() { 52 return shape; 53 } 54 public void setShape(Shape Shape) { 55 this.shape=shape; 56 } 57 } 58 class DealCardList { 59 ArrayList<Card> cardList = new ArrayList<Card>(); 60 61 DealCardList() { 62 } 63 64 DealCardList(ArrayList<Integer> list) { 65 for (int i = 0; i < list.size(); i++) { 66 if (list.get(i) == 1) { 67 double r = Main.input.nextDouble(); 68 Circle circle = new Circle(r); 69 Card card = new Card(circle); 70 card.getShape().setShapeName("Circle"); 71 cardList.add(card); 72 } 73 if (list.get(i) == 2) { 74 double a = Main.input.nextDouble(); 75 double b = Main.input.nextDouble(); 76 Rectangle rectangle = new Rectangle(a, b); 77 Card card = new Card(rectangle); 78 card.getShape().setShapeName("Rectangle"); 79 cardList.add(card); 80 } 81 if (list.get(i) == 3) { 82 double a = Main.input.nextDouble(); 83 double b = Main.input.nextDouble(); 84 double c = Main.input.nextDouble(); 85 Triangle triangle = new Triangle(a, b, c); 86 Card card = new Card(triangle); 87 card.getShape().setShapeName("Triangle"); 88 cardList.add(card); 89 } 90 if (list.get(i) == 4) { 91 double a = Main.input.nextDouble(); 92 double b = Main.input.nextDouble(); 93 double c = Main.input.nextDouble(); 94 Traperoid traperoid = new Traperoid(a, b, c); 95 Card card = new Card(traperoid); 96 card.getShape().setShapeName("Trapezoid"); 97 cardList.add(card); 98 } 99 } 100 } 101 public double getmaxArea() { 102 double m = 0; 103 for (int i = 0; i < cardList.size(); i++){ 104 if(m<=cardList.get(i).getShape().getArea()) 105 m=cardList.get(i).getShape().getArea(); 106 } 107 return s; 108 } 109 public boolean validate() { 110 for (int i = 0; i < cardList.size(); i++) { 111 if (!cardList.get(i).getShape().vaildate()) 112 return false; 113 } 114 return true; 115 } 116 public void cardSort() { 117 for (int k = 0; k < cardList.size(); k++) 118 for (int i = k + 1; i < cardList.size(); i++) { 119 if (cardList.get(k).getShape().getArea() < cardList.get(i).getShape().getArea()) 120 Collections.swap(cardList, k, i); 121 } 122 } 123 public void showResult1() { 124 for(int i=0;i<cardList.size();i++) 125 System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" "); 126 } 127 public void showResult2() { 128 for(int i=0;i<cardList.size();i++) 129 System.out.print("["); 130 System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" "); 131 System.out.print("]"); 132 } 133 } 134 class Shape { 135 private String shapeName; 136 Shape(){ 137 } 138 Shape(String shapeName){ 139 this.shapeName=shapeName; 140 } 141 public String getShapeName() { 142 return shapeName; 143 } 144 public void setShapeName(String shapeName) { 145 this.shapeName=shapeName; 146 } 147 public double getArea() { 148 return 0.0; 149 } 150 public boolean vaildate() { 151 return true; 152 } 153 } 154 class Circle extends Shape{ 155 private double radius; 156 Circle(){ 157 } 158 Circle(double radius){ 159 this.radius=radius; 160 } 161 public double getArea() { 162 return Math.PI*radius*radius; 163 } 164 public boolean vaildate() { 165 if(radius>0) 166 return true; 167 else 168 return false; 169 } 170 } 171 class Rectangle extends Shape{ 172 private double width,length; 173 Rectangle (double width,double length){ 174 this.width=width; 175 this.length=length; 176 } 177 public double getArea() { 178 return width*length; 179 } 180 public boolean vaildate() { 181 if(width>0&&length>0) 182 return true; 183 else 184 return false; 185 } 186 } 187 class Triangle extends Shape{ 188 double side1,side2,side3; 189 Triangle(double side1,double side2,double side3){ 190 this.side1=side1; 191 this.side2=side2; 192 this.side3=side3; 193 } 194 public double getArea() { 195 double c=(side1+side2+side3)/2; 196 double s=Math.sqrt(c*(c-side1)*(c-side2)*(c-side3)); 197 return s; 198 } 199 public boolean vaildate() { 200 if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1) 201 return true; 202 else 203 return false; 204 } 205 } 206 class Rectanglea extends Shape{ 207 private double width; 208 private double length; 209 public double getArea(){ 210 return width*length; 211 } 212 public double getwidth(){ 213 return width; 214 } 215 public void Rectangle(double width, double length){ 216 this.width = width; 217 this.length = length; 218 } 219 public void setlength(double length){ 220 this.length=length; 221 } 222 public boolean isRectangle() { 223 return width > 0 && length > 0; 224 } 225 } 226 class Trianglea extends Shape{ 227 private double a; 228 private double b; 229 private double c; 230 public void Triangle(double a, double b, double c) { 231 this.a = a; 232 this.b = b; 233 this.c = c; 234 } 235 public double getArea() { 236 double p=(a+b+c)/2; 237 return Math.sqrt(p*(p-a)*(p-b)*(p-c)); 238 } 239 public boolean isTriangle() { 240 if (a < 0 || b < 0 || c < 0) { 241 return false; 242 } 243 double[] sides = new double[3]; 244 sides[0] = a; 245 sides[1] = b; 246 sides[2] = c; 247 return sides[0] + sides[1] > sides[2]; 248 } 249 } 250 class Traperoid extends Shape{ 251 private double topSide,bottomSide,height; 252 Traperoid(){ 253 } 254 Traperoid(double topSide,double bottomSide,double height){ 255 this.bottomSide=bottomSide; 256 this.height=height; 257 this.topSide=topSide; 258 } 259 public double getArea() { 260 return (topSide+bottomSide)*height/2; 261 } 262 public boolean validate() { 263 if(topSide>0&&bottomSide>0&&height>0) 264 return true; 265 else 266 return false; 267 } 268 }
总的来说这两道题目输入还是一样的,就是输出的格式和内容变了挺多,不过其实都是考察了ArrayList泛型的应用方法和Comparable接口及泛型的应用,第二题在对于按照图形类型的分类输出格式上更加能让我们发现ArrayList动态数组确实很好用,某种意义上也更加凸显其的方便程度,总的说来就挺好的(摸鱼成分也up了)。
②题目集8和题目集9两道ATM机仿真题目的设计思路分析总结
设计ATM仿真系统,具体要求参见作业说明。 OO作业8-1题目说明.pdf
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
- 存款、取款功能输入数据格式:
卡号 密码 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 import java.util.*; 2 class Bank { 3 String bankname; 4 ArrayList<String> ATMList; 5 public Bank(){ 6 } 7 public Bank(String bankname,ArrayList<String> ATMList) 8 { 9 this.bankname=bankname; 10 this.ATMList=ATMList; 11 } 12 public String getBankname() { 13 return bankname; 14 } 15 public ArrayList<String> getATMList() { 16 return ATMList; 17 } 18 public void setBankname(String bankname){ 19 this.bankname=bankname; 20 } 21 public void setATMList(ArrayList<String> tATMList){ 22 this.ATMList=ATMList; 23 } 24 } 25 class Account { 26 private String name; 27 private String account; 28 private String password; 29 private double balance; 30 ArrayList<Bank> banklist; 31 Bank bank; 32 private ArrayList<String> cardList; 33 public Account(){ 34 } 35 public Account(String name,String account,String password,double balance,ArrayList<Bank> banklist,Bank bank,ArrayList<String> cardList){ 36 this.name=name; 37 this.account=account; 38 this.password=password; 39 this.balance=balance; 40 this.bank=bank; 41 this.banklist=banklist; 42 this.cardList=cardList; 43 } 44 public String getName() { 45 return name; 46 } 47 public String getAccount() { 48 return account; 49 } 50 public String getPassword() { 51 return password; 52 } 53 public double getBalance() { 54 return balance; 55 } 56 public ArrayList<String> getCardList() { 57 return cardList; 58 } 59 public void setName(String name) { 60 this.name = name; 61 } 62 public void setAccount(String account) { 63 this.account = account; 64 } 65 public void setPassword(String password) { 66 this.password = password; 67 } 68 public void setBalance(double balance) { 69 this.balance = balance; 70 } 71 public void setCardList(ArrayList<String> cardList) { 72 this.cardList = cardList; 73 } 74 } 75 class Check { 76 ArrayList<Account> accountList; 77 String card; 78 String password; 79 String number; 80 double money; 81 82 public Check(ArrayList<Account> accountList,String card,String password,String number,double money){ 83 this.accountList=accountList; 84 this.card=card; 85 this.password=password; 86 this.number=number; 87 this.money=money; 88 } 89 public boolean check(){ 90 int flag=0; 91 int i,j; 92 int k=0; 93 for(i=0;i<accountList.size();i++){ 94 for(j=0;j<accountList.get(i).getCardList().size();j++){ 95 if (card.equals(accountList.get(i).getCardList().get(j))){ 96 flag=1; 97 k=i; 98 break; 99 } 100 } 101 if(flag==1){ 102 break; 103 } 104 } 105 if(flag==1){ 106 if(password.equals(accountList.get(k).getPassword())){ 107 flag=2; 108 } 109 else{ 110 System.out.println("Sorry,your password is wrong."); 111 return false; 112 } 113 } 114 else{ 115 System.out.println("Sorry,this card does not exist."); 116 return false; 117 } 118 if(flag==2){ 119 for(i=0;i<accountList.get(k).banklist.size();i++){ 120 for(j=0;j<accountList.get(k).banklist.get(i).ATMList.size();j++){ 121 if(number.equals(accountList.get(k).banklist.get(i).ATMList.get(j))){ 122 flag=3; 123 break; 124 } 125 } 126 } 127 } 128 if(flag==3){ 129 if(money<=accountList.get(k).getBalance()){ 130 flag=4; 131 } 132 else{ 133 System.out.println("Sorry,your account balance is insufficient."); 134 return false; 135 } 136 } 137 else{ 138 System.out.println("Sorry,the ATM's id is wrong."); 139 return false; 140 141 } 142 if(flag==4){ 143 for(i=0;i<accountList.get(k).bank.ATMList.size();i++){ 144 if(number.equals(accountList.get(k).bank.ATMList.get(i))){ 145 flag=5; 146 break; 147 } 148 } 149 } 150 if(flag!=5){ 151 System.out.println("Sorry,cross-bank withdrawal is not supported."); 152 return false; 153 } 154 else 155 return true; 156 } 157 158 } 159 class Check1 { 160 ArrayList<Account> accountList; 161 String card; 162 public Check1(ArrayList<Account> accountList, String card){ 163 this.accountList = accountList; 164 this.card = card; 165 } 166 167 public boolean check(){ 168 int i,j; 169 int flag=0; 170 for(i=0;i<accountList.size();i++){ 171 for(j=0;j<accountList.get(i).getCardList().size();j++){ 172 if (card.equals(accountList.get(i).getCardList().get(j))){ 173 flag=1; 174 break; 175 } 176 } 177 if(flag==1){ 178 break; 179 } 180 } 181 if(flag==1) 182 return true; 183 else{ 184 System.out.println("Sorry,this card does not exist."); 185 return false; 186 } 187 } 188 } 189 class Access{ 190 ArrayList<Account> accountList; 191 String card; 192 String password; 193 String number; 194 double money; 195 public Access(ArrayList<Account> accountList,String card,String password,String number,double money){ 196 this.password=password; 197 this.number=number; 198 this.card=card; 199 this.accountList=accountList; 200 this.money=money; 201 } 202 public void access(){ 203 int i,j; 204 int t=0; 205 for(i=0;i<accountList.size();i++){ 206 for(j=0;j<accountList.get(i).getCardList().size();j++){ 207 if(card.equals(accountList.get(i).getCardList().get(j))){ 208 t=i; 209 break; 210 } 211 } 212 } 213 accountList.get(t).setBalance(accountList.get(t).getBalance()-money); 214 } 215 } 216 class Show{ 217 ArrayList<Account> accountList; 218 String card; 219 String password; 220 String number; 221 double money; 222 public Show(ArrayList<Account> accountList,String card,String password,String number,double money){ 223 this.password=password; 224 this.number=number; 225 this.card=card; 226 this.accountList=accountList; 227 this.money=money; 228 } 229 public void show(){ 230 int i,j; 231 int t=0; 232 for(i=0;i<accountList.size();i++){ 233 for(j=0;j<accountList.get(i).getCardList().size();j++){ 234 if(card.equals(accountList.get(i).getCardList().get(j))){ 235 t=i; 236 break; 237 } 238 } 239 } 240 241 if(money>=0){ 242 System.out.printf(accountList.get(t).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上取款¥%.2f\n",money); 243 } 244 else{ 245 money=-money; 246 System.out.printf(accountList.get(t).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上存款¥%.2f\n",money); 247 } 248 System.out.printf("当前余额为¥%.2f\n",accountList.get(t).getBalance()); 249 } 250 } 251 class Show1{ 252 ArrayList<Account> accountList; 253 String card; 254 public Show1(ArrayList<Account> accountList,String card){ 255 this.accountList=accountList; 256 this.card=card; 257 } 258 public void show1(){ 259 int i,j; 260 int t=0; 261 for(i=0;i<accountList.size();i++){ 262 for(j=0;j<accountList.get(i).getCardList().size();j++){ 263 if(card.equals(accountList.get(i).getCardList().get(j))){ 264 t=i; 265 break; 266 } 267 } 268 } 269 System.out.printf("¥%.2f\n",accountList.get(t).getBalance()); 270 } 271 } 272 public class Main { 273 public static void main(String[] args) { 274 ArrayList<String> ATMList1 = new ArrayList<>(); 275 ATMList1.add("01"); 276 ATMList1.add("02"); 277 ATMList1.add("03"); 278 ATMList1.add("04"); 279 Bank jsyh = new Bank("中国建设银行", ATMList1); 280 ArrayList<String> ATMList2 = new ArrayList<>(); 281 ATMList2.add("05"); 282 ATMList2.add("06"); 283 Bank gsyh = new Bank("中国工商银行", ATMList2); 284 ArrayList<Bank> bankList = new ArrayList<>(); 285 bankList.add(jsyh); 286 bankList.add(gsyh); 287 ArrayList<String> cardList1 = new ArrayList<>(); 288 cardList1.add("6217000010041315709"); 289 cardList1.add("6217000010041315715"); 290 Account account1 = new Account("杨过", "3217000010041315709", "88888888", 10000.00, bankList, jsyh,cardList1); 291 ArrayList<String> cardList2 = new ArrayList<>(); 292 cardList2.add("6217000010041315718"); 293 Account account2 = new Account("杨过", "3217000010041315715", "88888888", 10000.00, bankList,jsyh,cardList2); 294 ArrayList<String> cardList3 = new ArrayList<>(); 295 cardList3.add("6217000010051320007"); 296 Account account3 = new Account("郭靖", "3217000010051320007", "88888888", 10000.00, bankList,jsyh,cardList3); 297 ArrayList<String> cardList4 = new ArrayList<>(); 298 cardList4.add("6222081502001312389"); 299 Account account4 = new Account("张无忌", "3222081502001312389", "88888888", 10000.00, bankList,gsyh,cardList4); 300 ArrayList<String> cardList5 = new ArrayList<>(); 301 cardList5.add("6222081502001312390"); 302 Account account5 = new Account("张无忌", "3222081502001312390", "88888888", 10000.00, bankList,gsyh,cardList5); 303 ArrayList<String> cardList6 = new ArrayList<>(); 304 cardList6.add("6222081502001312399"); 305 cardList6.add("6222081502001312400"); 306 Account account6 = new Account("张无忌", "3222081502001312399", "88888888", 10000.00, bankList,gsyh,cardList6); 307 ArrayList<String> cardList7 = new ArrayList<>(); 308 cardList7.add("6222081502051320785"); 309 Account account7 = new Account("韦小宝", "3222081502051320785", "88888888", 10000.00, bankList,gsyh,cardList7); 310 ArrayList<String> cardList8 = new ArrayList<>(); 311 cardList8.add("6222081502051320786"); 312 Account account8 = new Account("韦小宝", "3222081502051320786", "88888888", 10000.00, bankList,gsyh,cardList8); 313 ArrayList<Account> accountList = new ArrayList<>(); 314 accountList.add(account1); 315 accountList.add(account2); 316 accountList.add(account3); 317 accountList.add(account4); 318 accountList.add(account5); 319 accountList.add(account6); 320 accountList.add(account7); 321 accountList.add(account8); 322 Scanner x=new Scanner(System.in); 323 String kp; 324 Check check; 325 Check1 check1; 326 kp=x.nextLine(); 327 while(!kp.equals("#")){ 328 int i,j; 329 String[] shuju=kp.split("\\s+"); 330 if(shuju.length!=1){ 331 check = new Check(accountList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3])); 332 if (check.check()){ 333 Access access = new Access(accountList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3])); 334 access.access(); 335 336 Show show = new Show(accountList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3])); 337 show.show(); 338 } 339 } 340 else { 341 check1=new Check1(accountList,shuju[0]); 342 if(check1.check()){ 343 Show1 show1= new Show1(accountList,shuju[0]); 344 show1.show1(); 345 } 346 } 347 kp=x.nextLine(); 348 } 349 } 350 }
虽然循环部分改善了,但是复杂程度还是很超标,超级加倍ing。
设计ATM仿真系统,具体要求参见作业说明。 OO作业9-1题目说明.pdf
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
- 取款功能输入数据格式:
卡号 密码 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 import java.util.*; 2 class Bank { 3 String bankname; 4 ArrayList<String> ATMList; 5 public Bank(){ 6 } 7 public Bank(String bankname,ArrayList<String> ATMList) 8 { 9 this.bankname=bankname; 10 this.ATMList=ATMList; 11 } 12 public String getBankname() { 13 return bankname; 14 } 15 public ArrayList<String> getATMList() { 16 return ATMList; 17 } 18 public void setBankname(String bankname){ 19 this.bankname=bankname; 20 } 21 public void setATMList(ArrayList<String> tATMList){ 22 this.ATMList=ATMList; 23 } 24 } 25 class Account { 26 private String name; 27 private String account; 28 private String password; 29 private double balance; 30 ArrayList<Bank> banklist; 31 Bank bank; 32 private ArrayList<String> cardList; 33 public Account(){ 34 } 35 public Account(String name,String account,String password,double balance,ArrayList<Bank> banklist,Bank bank,ArrayList<String> cardList){ 36 this.name=name; 37 this.account=account; 38 this.password=password; 39 this.balance=balance; 40 this.bank=bank; 41 this.banklist=banklist; 42 this.cardList=cardList; 43 } 44 public String getName() { 45 return name; 46 } 47 public String getAccount() { 48 return account; 49 } 50 public String getPassword() { 51 return password; 52 } 53 public double getBalance() { 54 return balance; 55 } 56 public ArrayList<String> getCardList() { 57 return cardList; 58 } 59 public void setName(String name) { 60 this.name = name; 61 } 62 public void setAccount(String account) { 63 this.account = account; 64 } 65 public void setPassword(String password) { 66 this.password = password; 67 } 68 public void setBalance(double balance) { 69 this.balance = balance; 70 } 71 public void setCardList(ArrayList<String> cardList) { 72 this.cardList = cardList; 73 } 74 } 75 class Check { 76 ArrayList<Account> accountList; 77 String card; 78 String password; 79 String number; 80 double money; 81 82 public Check(ArrayList<Account> accountList,String card,String password,String number,double money){ 83 this.accountList=accountList; 84 this.card=card; 85 this.password=password; 86 this.number=number; 87 this.money=money; 88 } 89 public boolean check(){ 90 int flag=0; 91 int i,j; 92 int k=0; 93 for(i=0;i<accountList.size();i++){ 94 for(j=0;j<accountList.get(i).getCardList().size();j++){ 95 if (card.equals(accountList.get(i).getCardList().get(j))){ 96 flag=1; 97 k=i; 98 break; 99 } 100 } 101 if(flag==1){ 102 break; 103 } 104 } 105 if(flag==1){ 106 if(password.equals(accountList.get(k).getPassword())){ 107 flag=2; 108 } 109 else{ 110 System.out.println("Sorry,your password is wrong."); 111 return false; 112 } 113 } 114 else{ 115 System.out.println("Sorry,this card does not exist."); 116 return false; 117 } 118 if(flag==2){ 119 for(i=0;i<accountList.get(k).banklist.size();i++){ 120 for(j=0;j<accountList.get(k).banklist.get(i).ATMList.size();j++){ 121 if(number.equals(accountList.get(k).banklist.get(i).ATMList.get(j))){ 122 flag=3; 123 break; 124 } 125 } 126 } 127 } 128 if(flag==3){ 129 if(money<=accountList.get(k).getBalance()){ 130 flag=4; 131 } 132 else{ 133 System.out.println("Sorry,your account balance is insufficient."); 134 return false; 135 } 136 } 137 else{ 138 System.out.println("Sorry,the ATM's id is wrong."); 139 return false; 140 141 } 142 if(flag==4){ 143 for(i=0;i<accountList.get(k).bank.ATMList.size();i++){ 144 if(number.equals(accountList.get(k).bank.ATMList.get(i))){ 145 flag=5; 146 break; 147 } 148 } 149 } 150 if(flag!=5){ 151 System.out.println("Sorry,cross-bank withdrawal is not supported."); 152 return false; 153 } 154 else 155 return true; 156 } 157 158 } 159 class Check1 { 160 ArrayList<Account> accountList; 161 String card; 162 public Check1(ArrayList<Account> accountList, String card){ 163 this.accountList = accountList; 164 this.card = card; 165 } 166 167 public boolean check(){ 168 int i,j; 169 int flag=0; 170 for(i=0;i<accountList.size();i++){ 171 for(j=0;j<accountList.get(i).getCardList().size();j++){ 172 if (card.equals(accountList.get(i).getCardList().get(j))){ 173 flag=1; 174 break; 175 } 176 } 177 if(flag==1){ 178 break; 179 } 180 } 181 if(flag==1) 182 return true; 183 else{ 184 System.out.println("Sorry,this card does not exist."); 185 return false; 186 } 187 } 188 } 189 class Access{ 190 ArrayList<Account> accountList; 191 String card; 192 String password; 193 String number; 194 double money; 195 public Access(ArrayList<Account> accountList,String card,String password,String number,double money){ 196 this.password=password; 197 this.number=number; 198 this.card=card; 199 this.accountList=accountList; 200 this.money=money; 201 } 202 public void access(){ 203 int i,j; 204 int t=0; 205 for(i=0;i<accountList.size();i++){ 206 for(j=0;j<accountList.get(i).getCardList().size();j++){ 207 if(card.equals(accountList.get(i).getCardList().get(j))){ 208 t=i; 209 break; 210 } 211 } 212 } 213 accountList.get(t).setBalance(accountList.get(t).getBalance()-money); 214 } 215 } 216 class Show{ 217 ArrayList<Account> accountList; 218 String card; 219 String password; 220 String number; 221 double money; 222 public Show(ArrayList<Account> accountList,String card,String password,String number,double money){ 223 this.password=password; 224 this.number=number; 225 this.card=card; 226 this.accountList=accountList; 227 this.money=money; 228 } 229 public void show(){ 230 int i,j; 231 int t=0; 232 for(i=0;i<accountList.size();i++){ 233 for(j=0;j<accountList.get(i).getCardList().size();j++){ 234 if(card.equals(accountList.get(i).getCardList().get(j))){ 235 t=i; 236 break; 237 } 238 } 239 } 240 241 if(money>=0){ 242 System.out.printf("业务:取款 "+accountList.get(t).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上取款¥%.2f\n",money); 243 } 244 else{ 245 money=-money; 246 System.out.printf("业务:存款 "+accountList.get(t).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上存款¥%.2f\n",money); 247 } 248 System.out.printf("当前余额为¥%.2f\n",accountList.get(t).getBalance()); 249 } 250 } 251 class Show1{ 252 ArrayList<Account> accountList; 253 String card; 254 public Show1(ArrayList<Account> accountList,String card){ 255 this.accountList=accountList; 256 this.card=card; 257 } 258 public void show1(){ 259 int i,j; 260 int t=0; 261 for(i=0;i<accountList.size();i++){ 262 for(j=0;j<accountList.get(i).getCardList().size();j++){ 263 if(card.equals(accountList.get(i).getCardList().get(j))){ 264 t=i; 265 break; 266 } 267 } 268 } 269 System.out.printf("业务:查询余额 ¥%.2f\n",accountList.get(t).getBalance()); 270 } 271 } 272 public class Main { 273 public static void main(String[] args) { 274 ArrayList<String> ATMList1 = new ArrayList<>(); 275 ATMList1.add("01"); 276 ATMList1.add("02"); 277 ATMList1.add("03"); 278 ATMList1.add("04"); 279 Bank jsyh = new Bank("中国建设银行", ATMList1); 280 ArrayList<String> ATMList2 = new ArrayList<>(); 281 ATMList2.add("05"); 282 ATMList2.add("06"); 283 Bank gsyh = new Bank("中国工商银行", ATMList2); 284 ArrayList<String> ATMList3 = new ArrayList<>(); 285 ATMList3.add("07"); 286 ATMList3.add("08"); 287 ATMList3.add("09"); 288 ATMList3.add("10"); 289 ATMList3.add("11"); 290 Bank nyyh = new Bank("中国农业银行", ATMList3); 291 ArrayList<Bank> bankList = new ArrayList<>(); 292 bankList.add(jsyh); 293 bankList.add(gsyh); 294 bankList.add(nyyh); 295 ArrayList<String> cardList1 = new ArrayList<>(); 296 cardList1.add("6217000010041315709"); 297 cardList1.add("6217000010041315715"); 298 Account account1 = new Account("杨过", "3217000010041315709", "88888888", 10000.00, bankList, jsyh,cardList1); 299 ArrayList<String> cardList2 = new ArrayList<>(); 300 cardList2.add("6217000010041315718"); 301 Account account2 = new Account("杨过", "3217000010041315715", "88888888", 10000.00, bankList,jsyh,cardList2); 302 ArrayList<String> cardList3 = new ArrayList<>(); 303 cardList3.add("6217000010051320007"); 304 Account account3 = new Account("郭靖", "3217000010051320007", "88888888", 10000.00, bankList,jsyh,cardList3); 305 ArrayList<String> cardList4 = new ArrayList<>(); 306 cardList4.add("6222081502001312389"); 307 Account account4 = new Account("张无忌", "3222081502001312389", "88888888", 10000.00, bankList,gsyh,cardList4); 308 ArrayList<String> cardList5 = new ArrayList<>(); 309 cardList5.add("6222081502001312390"); 310 Account account5 = new Account("张无忌", "3222081502001312390", "88888888", 10000.00, bankList,gsyh,cardList5); 311 ArrayList<String> cardList6 = new ArrayList<>(); 312 cardList6.add("6222081502001312399"); 313 cardList6.add("6222081502001312400"); 314 Account account6 = new Account("张无忌", "3222081502001312399", "88888888", 10000.00, bankList,gsyh,cardList6); 315 ArrayList<String> cardList7 = new ArrayList<>(); 316 cardList7.add("6222081502051320785"); 317 Account account7 = new Account("韦小宝", "3222081502051320785", "88888888", 10000.00, bankList,gsyh,cardList7); 318 ArrayList<String> cardList8 = new ArrayList<>(); 319 cardList8.add("6222081502051320786"); 320 Account account8 = new Account("韦小宝", "3222081502051320786", "88888888", 10000.00, bankList,gsyh,cardList8); 321 ArrayList<String> cardList9 = new ArrayList<>(); 322 cardList9.add("6640000010045442002"); 323 cardList9.add("6640000010045442003"); 324 Account account9 = new Account("张三丰", "3640000010045442002", "88888888", 10000.00, bankList,jsyh,cardList9); 325 ArrayList<String> cardList10 = new ArrayList<>(); 326 cardList10.add("6640000010045441009"); 327 Account account10 = new Account("令狐冲", "3640000010045441009", "88888888", 10000.00, bankList,gsyh,cardList10); 328 ArrayList<String> cardList11 = new ArrayList<>(); 329 cardList11.add("6630000010033431001"); 330 Account account11 = new Account("乔峰", "3630000010033431001", "88888888", 10000.00, bankList,nyyh,cardList11); 331 ArrayList<String> cardList12 = new ArrayList<>(); 332 cardList12.add("6630000010033431008"); 333 Account account12 = new Account("洪七公", "3630000010033431008", "88888888", 10000.00, bankList,nyyh,cardList12); 334 ArrayList<Account> accountList = new ArrayList<>(); 335 accountList.add(account1); 336 accountList.add(account2); 337 accountList.add(account3); 338 accountList.add(account4); 339 accountList.add(account5); 340 accountList.add(account6); 341 accountList.add(account7); 342 accountList.add(account8); 343 accountList.add(account9); 344 accountList.add(account10); 345 accountList.add(account11); 346 accountList.add(account12); 347 Scanner x=new Scanner(System.in); 348 String kp; 349 Check check; 350 Check1 check1; 351 kp=x.nextLine(); 352 while(!kp.equals("#")){ 353 int i,j; 354 String[] shuju=kp.split("\\s+"); 355 if(shuju.length!=1){ 356 check = new Check(accountList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3])); 357 if (check.check()){ 358 Access access = new Access(accountList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3])); 359 access.access(); 360 361 Show show = new Show(accountList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3])); 362 show.show(); 363 } 364 } 365 else { 366 check1=new Check1(accountList,shuju[0]); 367 if(check1.check()){ 368 Show1 show1= new Show1(accountList,shuju[0]); 369 show1.show1(); 370 } 371 } 372 kp=x.nextLine(); 373 } 374 } 375 }
这两道题就也写的比较坎坷,第一题这个不同判定要素就很多,银行机器卡号和用户又都是之前的ArrayList动态数组的高光点了,总之写起来就是边写边改,无尽的(bushi循环嵌套还是进行一点的改善的,虽然改善之后复杂度还是一骑绝尘了,但当时修改完的时候还是感觉挺好的。第二题增加的部分就挺多,为什么没有新的SourceMonitor的图呢,因为当时时间上踩线摸鱼,代码基本是添加了银行和账户的新部分,也就是跨行和借记贷记的这一部分,新的银行和账户也添加了,基本上就是改进了这一part。另外应该是重要并且比较繁琐的手续费这个部分就摸了,基本上是把取钱的函数重写一份新的了,又是好几个判断,需要扣除的钱要先进行是否跨行是否透支来决定扣除显示还是返回提示,总的来说还是摸了的,没能get到这一部分的分数。
采坑心得:
1.这次的题目就是两个渐进式的设计,一般第一道做出来了之后第二道都会直接对上一次的代码进行改善来更快的完成作业,这样也有助于在后续自行进行反思对比。
2.还是应该提前开始对题目的解答,让时间不那么紧迫,更多地在答题时去吸收题目中的知识,渐进式题目在第二道时的危机意识不太够(其实就是不要因为是渐进式题目就偷懒kora)。
改进建议:
这次的题目都挺好,两次的循序渐进更加有助于对比改进,整挺好,没什么别的建议了。
总结:
对于本阶段三次题目集的学习,依旧还是学到了很多东西。三次题目一共两道渐进式的大题,虽然题量减小了,但是对于题目的思考不能减少,代码能不能在同类的相似环境要求里同样适用也是比较要紧的能力,代码能力需要自己梳理编写才能得到切实的提高,代码的思路也应该在一次次的对比或者对于他人优秀代码的吸收中改善。对于课程工作的建议也没有什么了,这个也是这学期最后一次的Blog了,就希望大家以后JAVA也快乐学习多多进步(笑)。
来点私货,再来一份我最爱的死盖摸鱼图