前言:
这次的题目集难度较以往而言提升了很多,突然提升的难度难免会造成些许的不适应。但是题目虽然有难度,同样的收获也与难度成正比增长。而这次题目集总结主要以三道大题来进行分析。
第四次作业:
题目7-1 菜单计价程序-3:
需求分析:
设计菜单点单计价程序,根据输入的信息,计算并输出相应的价格。菜单记录所有菜品,每个菜品包括菜名、单价属性,由输入决定。订单包括桌号信息、点菜信息属性。菜品会有规定时间折扣,折扣通过点菜时间判断。在点菜时还可以通过delete删除之前的点菜记录。只能在营业时间之内点餐。
题目分析:
应设置四个类,菜品类Dish,包含菜名和单价属性,包括getPrice计算不同份额的价格的方法;菜单类Menu,包含菜品列表,包含添加菜品和查找菜品的方法;点菜记录类Record,包含点菜序号,菜品,份额,包含计价方法;点单类Order, 包含点菜记录,包含计价,包含增减查三种方法。设置一个DealData类来处理数据。
我的代码:
package DISH; import java.time.LocalDate; import java.util.ArrayList; import java.util.Scanner; class Dish { public String name;//菜品名称 public int unit_price; //单价 public Dish() { super(); // TODO 自动生成的构造函数存根 } public Dish(String name, int unit_price) { super(); this.name = name; this.unit_price = unit_price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getUnit_price() { return unit_price; } public void setUnit_price(int unit_price) { this.unit_price = unit_price; } public int getPrice(int portion) { // 不同份额的价格转化 switch(portion) { case 1: unit_price *= 1; break; case 2: unit_price *= 1.5; break; case 3: unit_price *= 2; break; } return (int)unit_price; } } class Menu { public ArrayList<Dish> dishs = new ArrayList<Dish>();//菜品数组,保存所有菜品信息 public Dish searchDish(String dishName){ Dish dish = null; for(Dish t:dishs) { if(dishName.equals(t.getName())) { dish = t; } } if(dish == null) { System.out.println(dishName +"does not exist"); } return dish; } public void addDish(String dishName,int unit_price){ Dish t = new Dish(); t.setName(dishName); t.setUnit_price(unit_price); dishs.add(t); } } class Record { public int orderNum; //序号 public Dish d = new Dish(); //菜品 public int portion; //份额 public int num; // 份数 public Record() { super(); // TODO 自动生成的构造函数存根 } public Record(int orderNum, Dish d, int portion, int num) { super(); this.orderNum = orderNum; this.d = d; this.portion = portion; this.num = num; } public int getOrderNum() { return orderNum; } public void setOrderNum(int orderNum) { this.orderNum = orderNum; } public Dish getDish() { return d; } public void setDish(Dish d) { this.d = d; } public int getPortion() { return portion; } public void setPortion(int portion) { this.portion = portion; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public int getPrice(){ return d.getPrice(portion) * num; } } class Order { public static int differenceMoney; // 要删除菜品的价钱 public static int errorDeleteTime; //删除错误的次数 public ArrayList<Record> records = new ArrayList<Record>(); public int getTotalPrice(){ int sum = 0; for(Record r:records) { sum += r.getPrice(); } return sum; } public int getDifferenceMoney() { return differenceMoney; } public void setDifferenceMoney(int differenceMoney) { this.differenceMoney = differenceMoney; } public Record addARecord(int orderNum, String dishName, int portion, int num, Menu menu) { Dish dish = menu.searchDish(dishName); if (dish == null) { System.out.println(dishName + " does not exist"); return null; } Record r = new Record(); r.setOrderNum(orderNum); r.setDish(dish); r.setPortion(portion); r.setNum(num); records.add(r); return r; } public void delARecordByOrderNum(int orderNum) { // 根据序号删除一条记录 for(int i = 0; i < records.size(); i++) { if(records.get(i).getOrderNum() == orderNum) { differenceMoney += records.get(i).getPrice(); return; } } System.out.println("delete error;"); // 序号不存在 } public Record findRecordByNum(int orderNum) { // 根据序号查找 for(Record record : records) { if (record.getOrderNum() == orderNum) { return record; } } return null; } } class DealData { StringBuilder sb = new StringBuilder(); public DealData() { super(); // TODO 自动生成的构造函数存根 } public DealData(StringBuilder sb) { super(); this.sb = sb; } public void setSb(StringBuilder sb) { this.sb = sb; } public void dealData() { Scanner input = new Scanner(System.in); ArrayList<String> outPut = new ArrayList<String>(); String allInput = ""; allInput = input.nextLine(); if(allInput.equals("exit")) { System.exit(0); } while(!allInput.equals("end")) { sb.append(allInput); sb.append("\n"); allInput = input.nextLine(); } sb.append("end"); Menu menu = new Menu(); double discount = 1.0; int i = 0; String[] lines = sb.toString().split("\\s+"); while (!lines[i].equals("end")) { if (lines[i].startsWith("table")) { String[] parts = lines[i].split(" "); int tableNum = Integer.parseInt(parts[1]); System.out.println("table " + tableNum + ": "); String date = parts[2]; LocalDate currentDate = LocalDate.of(Integer.parseInt(parts[2].split("/")[0]), Integer.parseInt(parts[2].split("/")[1]), Integer.parseInt(parts[2].split("/")[2])); String week = String.valueOf(currentDate.getDayOfWeek()); String time = parts[3]; Order order = new Order(); int totalPrice = 0; boolean isDiscount = false; boolean outTime = false; while (true){ lines[i] = input.nextLine(); if (lines[i].contains("end")) { break; } if (lines[i].startsWith("table")) { break; } else if (lines[i].contains("delete")) { int deleteNum = Integer.parseInt(lines[i].split("\\s+")[0]); order.delARecordByOrderNum(deleteNum); } else { String[] parts2 = lines[i].split("\\s+"); if (parts2.length > 2 && parts2.length <= 4) { int orderNum = Integer.parseInt(parts2[0]); String dishName = parts2[1]; int portion = Integer.parseInt(parts2[2]); int num = Integer.parseInt(parts2[3]); Record record = order.addARecord(orderNum, dishName, portion, num, menu); if (record != null) { totalPrice += record.getPrice(); System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice()); } } else if (parts2.length > 4) { int dNUm = Integer.parseInt(parts2[0]); int orderNum = Integer.parseInt(parts2[1]); String dishName = parts2[2]; int portion = Integer.parseInt(parts2[3]); int num = Integer.parseInt(parts2[4]); Record record = order.addARecord(orderNum, dishName, portion, num, menu); if (record != null) { totalPrice += record.getPrice(); System.out.println(record.orderNum + " table " + tableNum + " pay for table " + dNUm + " " + record.getPrice()); } } } } int hour = Integer.parseInt(time.split("/")[0]); int minute = Integer.parseInt(time.split("/")[1]); if ( week.equals("SATURDAY") || week.equals("SUNDAY")) { if ( (hour > 9 && hour <= 21) || (hour == 9 && minute >= 30) ) { isDiscount = true; discount = 1.0; outTime = false; } else { outTime = true; } } else { if ( hour > 10 && hour < 14 || (hour == 10 && minute >= 30) || (hour == 14 && minute <= 30)) { isDiscount = true; discount = 0.6; } else if ( (hour >= 17 && hour < 20) ||(hour == 20 && minute <= 30) ) { isDiscount = true; discount = 0.8; } else { outTime = true; } } if (isDiscount) { totalPrice = (int)((totalPrice- Order.differenceMoney) * discount + 0.5); } if(outTime) { outPut.add("table " + String.valueOf(tableNum) + " out of opening hours"); } else { outPut.add("table " + String.valueOf(tableNum) + ": " + totalPrice); } } else { String[] parts = lines[i].split(" "); if(parts.length == 2) { String dishName = parts[0]; int unit_price = Integer.parseInt(parts[1]); menu.addDish(dishName, unit_price); } } i++; } for (i = 0; i <outPut.size();i++) { System.out.println(outPut.get(i)); } } } public class Main { public static void main(String[] args) { DealData d = new DealData(); d.dealData(); } }
sourceMonitor:
可以看出不是很好,太复杂了,因为判断和循环嵌套太多。
类图:
第六次作业:
题目7-4 ATM机类结构设计(一):
需求分析:
编写一个银行 ATM 机的模拟程序,能够完成用户的存款、取款以及查询余额功能。相关概念分别有:中国银联(China UnionPay)、银行(Bank)、银行用户(User)、银行账户(Account)、银行卡(Card)、ATM(Automated Teller Machine)。其中,中国银联包含多家银行机构,银行则有银行名称属性。银行用户则持有银行账户,银行账户包含银行卡(可多张)、账户余额属性,且隶属于某家银行,为某人所持有。银行卡包含银行卡号、银行卡密码属性。ATM机比较复杂,可提取现金、查询存款余额、余额查询等工作,并且隶属于某一银行机构。
题目分析:
设计要求:尝试使用面向对象技术对银行用户在 ATM 机上进行相关操作的模拟系统设 计,上述的相关概念均要设计为实体类,业务(控制)类请自行根据实际需要进 行扩充和完善。本次作业限定银行卡均为借记卡(不能透支),且不允许跨行办理相关业务(例如在中国建设银行的 ATM 机上对其他银行的账户进行操作)。所以根据题目要求,我设置了ChinaUnionPay类,Bank类(继承ChinaUnionPay类),ATM类,User类,Account类和Card类六个主要的实体类。在数据处理方面设置AllInfo类进行几个人的数据初始化,用CheckData类来分割和检查数据(这里没做好,没有实现单一职责),用Controller类来主控数据。
我的代码:
1 import java.util.ArrayList; 2 import java.util.*; 3 4 class ChinaUnionPay { 5 private ArrayList<Bank> bank = new ArrayList<Bank>(); 6 7 public ChinaUnionPay() { 8 super(); 9 // TODO 自动生成的构造函数存根 10 } 11 12 public ChinaUnionPay(ArrayList<Bank> bank) { 13 super(); 14 this.bank = bank; 15 } 16 17 public ArrayList<Bank> getBank() { 18 return bank; 19 } 20 21 public void setBank(ArrayList<Bank> bank) { 22 this.bank = bank; 23 } 24 } 25 26 /** 27 * 银行有名字 28 * 银行可以有很多账户 29 * 银行可以有很多用户 30 * 银行可以有很多ATM机 31 * @author 龚良堃 32 * 33 */ 34 35 class Bank extends ChinaUnionPay{ 36 37 protected String bankName; 38 protected ArrayList<ATM> atm; 39 40 public Bank() { 41 super(); 42 // TODO 自动生成的构造函数存根 43 } 44 45 public Bank(String bankName, ArrayList<ATM> atm) { 46 super(); 47 this.bankName = bankName; 48 this.atm = atm; 49 } 50 51 public String getBankName() { 52 return bankName; 53 } 54 55 public void setBankName(String bankName) { 56 this.bankName = bankName; 57 } 58 59 public ArrayList<ATM> getAtm() { 60 return atm; 61 } 62 63 public void setAtm(ArrayList<ATM> atm) { 64 this.atm = atm; 65 } 66 67 public void Name(String atmNum) { 68 if(atmNum.matches("[0][1-4]")) { 69 this.bankName = "中国建设银行"; 70 } 71 if(atmNum.matches("[0][5-6]")) { 72 this.bankName = "中国工商银行"; 73 } 74 } 75 } 76 77 class ATM { 78 79 private String atmNum; 80 81 public ATM() { 82 super(); 83 // TODO 自动生成的构造函数存根 84 } 85 86 public ATM(String atmNum) { 87 super(); 88 this.atmNum = atmNum; 89 90 } 91 92 public String getAtmNum() { 93 return atmNum; 94 } 95 96 public void setAtmNum(String atmNum) { 97 this.atmNum = atmNum; 98 } 99 100 public boolean isLegalATM() { 101 if(this.atmNum.matches("[0][1-6]")) 102 return true; 103 return false; 104 } 105 106 } 107 108 /** 109 * 用户应该有名字 110 * 用户应该有账户 111 * 用户可以有多个账户 (数组对象) 112 * @author 龚良堃 113 * 114 */ 115 116 class User { 117 118 protected String name; 119 protected ArrayList<Account> accounts; 120 // private Card[] cards; 121 122 public User() { 123 super(); 124 // TODO 自动生成的构造函数存根 125 } 126 127 public User(String name, ArrayList<Account> accounts) { 128 super(); 129 this.name = name; 130 this.accounts = accounts; 131 } 132 133 public String getName() { 134 return name; 135 } 136 137 public void setName(String name) { 138 this.name = name; 139 } 140 141 public ArrayList<Account> getAccounts() { 142 return accounts; 143 } 144 145 public void setAccounts(ArrayList<Account> accounts) { 146 this.accounts = accounts; 147 } 148 } 149 150 /** 151 * 账户里有银行卡 152 * 一个账户可以有多张银行卡 (数组对象) 153 * 银行卡的钱是账户共享的 154 * 所以应该把关于钱的属性放账户里 155 * @author 龚良堃 156 * 157 */ 158 159 class Account { 160 161 private ArrayList<Card> cards; 162 private Bank bank; 163 private double allMoney = 10000.00; 164 165 public Account() { 166 super(); 167 // TODO 自动生成的构造函数存根 168 } 169 170 public Account(ArrayList<Card> cards, Bank bank) { 171 super(); 172 this.cards = cards; 173 this.bank = bank; 174 } 175 176 public ArrayList<Card> getCards() { 177 return cards; 178 } 179 180 public void setCards(ArrayList<Card> cards) { 181 this.cards = cards; 182 } 183 184 public Bank getBank() { 185 return bank; 186 } 187 188 public void setBank(Bank bank) { 189 this.bank = bank; 190 } 191 192 public double getAllMoney() { 193 return allMoney; 194 } 195 196 public void setAllMoney(double allMoney) { 197 this.allMoney = allMoney; 198 } 199 200 public void changeValue(double changeValue) { 201 if(canTakeMoney(changeValue)) { 202 this.allMoney -= changeValue; 203 } 204 } 205 206 public boolean canTakeMoney(double takeValue) { 207 if(takeValue > this.allMoney) { 208 return false; 209 } 210 return true; 211 } 212 213 public boolean isCardExist(String cardID) { 214 for(int i = 0; i < cards.size(); i++) { 215 for(int j = 0; j < getCards().size(); j++) { // 存疑 216 if (cards.get(i).getCardID().equals(cardID)) { 217 return true; 218 } 219 } 220 } 221 return false; 222 } 223 224 public boolean isRightPassword(String password) { 225 if(password.matches("88888888")) { 226 return true; 227 } 228 return false; 229 } 230 } 231 232 /** 233 * 我认为卡里的属性应该有: 234 * 卡号 cardID 235 * 卡密 password 236 * 卡内余额 allMoney (初始10000.00) (错误的) 237 * 要取走的金额 takeMoney (初始0.00) (错误的) 238 * 要存入的金额 addMoney (初始0.00) (错误的) 239 * @author 龚良堃 240 * 241 */ 242 class Card { 243 244 private String cardID; 245 private String password; 246 247 public Card() { 248 super(); 249 // TODO 自动生成的构造函数存根 250 } 251 252 public Card(String cardID, String password) { 253 super(); 254 this.cardID = cardID; 255 this.password = password; 256 } 257 258 public String getCardID() { 259 return cardID; 260 } 261 262 public void setCardID(String cardID) { 263 this.cardID = cardID; 264 } 265 266 public String getPassword() { 267 return password; 268 } 269 270 public void setPassword(String password) { 271 this.password = password; 272 } 273 274 public boolean isMyCard(String cardID) { 275 if(this.cardID.matches(cardID)) { 276 return true; 277 } 278 return false; 279 } 280 281 public boolean isRightPassword(String password) { 282 if(password.matches("88888888")) { 283 return true; 284 } 285 return false; 286 } 287 } 288 289 class AllInfo { 290 291 public AllInfo() { 292 super(); 293 // TODO 自动生成的构造函数存根 294 } 295 296 public ArrayList<User> info() { 297 ArrayList<ATM> atmNum01 = new ArrayList<ATM>(); // 建行ATM 298 ATM atm01 = new ATM("01"); 299 ATM atm02 = new ATM("02"); 300 ATM atm03 = new ATM("03"); 301 ATM atm04 = new ATM("04"); 302 atmNum01.add(atm01); 303 atmNum01.add(atm02); 304 atmNum01.add(atm03); 305 atmNum01.add(atm04); 306 307 ArrayList<ATM> atmNum02 = new ArrayList<ATM>(); // 工行ATM 308 ATM atm05 = new ATM("05"); 309 ATM atm06 = new ATM("06"); 310 atmNum02.add(atm05); 311 atmNum02.add(atm06); 312 313 Bank bank01 = new Bank("中国建设银行", atmNum01); // 两个银行的初始化 314 Bank bank02 = new Bank("中国工商银行", atmNum02); 315 316 ArrayList<Card> cards01 = new ArrayList<Card>(); // 杨过一个账户的卡 317 Card card01 = new Card("6217000010041315709","88888888"); // 杨过的卡 318 Card card02 = new Card("6217000010041315715","88888888"); 319 cards01.add(card01); 320 cards01.add(card02); 321 ArrayList<Card> cards02 = new ArrayList<Card>(); // 杨过一个账户的卡 322 Card card03 = new Card("6217000010041315718","88888888"); 323 cards02.add(card03); 324 ArrayList<Card> cards03 = new ArrayList<Card>(); // 郭靖账户的卡 325 Card card04 = new Card("6217000010051320007", "8888888"); 326 cards03.add(card04); 327 ArrayList<Card> cards04 = new ArrayList<Card>(); // 张无忌一个账户的卡 328 Card card05 = new Card("6222081502001312389","88888888"); 329 cards04.add(card05); 330 ArrayList<Card> cards05 = new ArrayList<Card>(); // 张无忌一个账户的卡 331 Card card06 = new Card("6222081502001312390","88888888"); 332 cards05.add(card06); 333 ArrayList<Card> cards06 = new ArrayList<Card>(); // 张无忌一个账户的卡 334 Card card07 = new Card("6222081502001312399","88888888"); 335 Card card08 = new Card("6222081502001312400","88888888"); 336 cards06.add(card07); 337 cards06.add(card08); 338 ArrayList<Card> cards07 = new ArrayList<Card>(); // 韦小宝一个账户的卡 339 Card card09 = new Card("6222081502051320785","88888888"); 340 cards07.add(card09); 341 ArrayList<Card> cards08 = new ArrayList<Card>(); // 韦小宝一个账户的卡 342 Card card10 = new Card("6222081502051320786","88888888"); 343 cards08.add(card10); 344 345 ArrayList<Account> accounts01 = new ArrayList<Account>(); // 杨过的账户 346 Account account01 = new Account(cards01, bank01); // 杨过的建行账户一卡 347 Account account02 = new Account(cards02, bank01); // 杨过的建行账户二卡 348 accounts01.add(account01); 349 accounts01.add(account02); 350 ArrayList<Account> accounts02 = new ArrayList<Account>(); // 郭靖的账户 351 Account account03 = new Account(cards03, bank01); // 郭靖的建行账户一卡 352 accounts02.add(account03); 353 ArrayList<Account> accounts03 = new ArrayList<Account>(); // 张无忌的账户 354 Account account04 = new Account(cards04, bank02); // 张无忌的建行账户一卡 355 Account account05 = new Account(cards05, bank02); // 张无忌的建行账户二卡 356 Account account06 = new Account(cards06, bank02); // 张无忌的建行账户三卡 357 accounts03.add(account04); 358 accounts03.add(account05); 359 accounts03.add(account06); 360 ArrayList<Account> accounts04 = new ArrayList<Account>(); // 韦小宝的账户 361 Account account07 = new Account(cards07, bank02); 362 Account account08 = new Account(cards08, bank02); 363 accounts04.add(account07); 364 accounts04.add(account08); 365 366 ArrayList<User> users = new ArrayList<User>(); 367 User user01 = new User("杨过", accounts01); // 初始化杨过的信息 368 users.add(user01); 369 User user02 = new User("郭靖", accounts02); // 初始化郭靖 370 users.add(user02); 371 User user03 = new User("张无忌", accounts03); // 初始化张无忌 372 users.add(user03); 373 User user04 = new User("韦小宝", accounts04); // 初始化韦小宝 374 users.add(user04); 375 376 return users; 377 } 378 } 379 380 class CheckData { 381 382 private String data; // 一行的数据 383 384 385 public CheckData() { 386 super(); 387 // TODO 自动生成的构造函数存根 388 } 389 390 public CheckData(String data) { 391 super(); 392 this.data = data; 393 } 394 395 public void setData(String data) { 396 this.data = data; 397 } 398 399 public String[] getSplitedData() { // 将一行的数据经过分割后返回 400 String[] datas = data.split("\\s+"); 401 if(datas.length >= 1) { // 如果长度正确则返回分割后的数组, 否则返回空 402 return datas; 403 } 404 return null; 405 } 406 407 public boolean isLegalInput() { 408 if(getSplitedData() == null) { 409 return false; 410 } 411 return true; 412 } 413 } 414 415 class Controller { 416 417 StringBuilder sb = new StringBuilder(); 418 protected AllInfo allInfo = new AllInfo(); 419 protected View view = new View(); 420 421 422 public Controller() { 423 super(); 424 // TODO 自动生成的构造函数存根 425 } 426 427 public Controller(StringBuilder sb) { 428 super(); 429 this.sb = sb; 430 } 431 432 public StringBuilder getSb() { 433 return sb; 434 } 435 436 public void setSb(StringBuilder sb) { 437 this.sb = sb; 438 } 439 440 public void in() { 441 Scanner in = new Scanner(System.in); 442 String allInput = ""; 443 allInput = in.nextLine(); 444 if(allInput.equals("#")) { 445 System.exit(0); 446 } 447 while(!allInput.equals("#")) { 448 sb.append(allInput); 449 sb.append("\n"); 450 allInput = in.nextLine(); 451 452 } 453 } 454 455 public void play() { 456 ArrayList<User> users = allInfo.info(); 457 in(); 458 String[] datas = sb.toString().split("\n"); // 输入数据分行分割 459 460 int i = 0; 461 for(i = 0; i < datas.length; i ++) { 462 CheckData checkData = new CheckData(datas[i]); 463 String[] d = checkData.getSplitedData(); // 一条数据的分割 464 465 if(d.length > 1) { 466 ATM atm = new ATM(d[2]); 467 ArrayList<ATM> atmd = new ArrayList<ATM>(); 468 Bank bankd = new Bank("name", atmd); 469 bankd.Name(d[2]); 470 int usersNum = 0; 471 int accountsNum = 0; 472 double money = 0.0; 473 if(d.length == 4) { 474 money = Double.parseDouble(d[3]); 475 } 476 boolean flag = false; 477 for(int j = 0; j < users.size(); j ++) { 478 for(int k = 0; k < users.get(j).accounts.size(); k ++) { 479 if(users.get(j).accounts.get(k).isCardExist(d[0])) { 480 usersNum = j; 481 accountsNum = k; 482 flag = true; 483 } 484 } 485 } 486 if(flag == true) { // 卡号存在 487 if(users.get(usersNum).accounts.get(accountsNum).isRightPassword(d[1])) { // 密码正确 488 if(atm.isLegalATM()) { // ATM正确 489 if(users.get(usersNum).accounts.get(accountsNum).getBank().getBankName().equals(bankd.getBankName())) { // 对应银行正确 490 if(users.get(usersNum).accounts.get(accountsNum).canTakeMoney(money)) { // 能够取钱 491 users.get(usersNum).accounts.get(accountsNum).changeValue(money); 492 if(money > 0) { 493 view.rightTakeOutput(users.get(usersNum).getName(), 494 users.get(usersNum).accounts.get(accountsNum).getBank().getBankName(), 495 d[2], money); 496 view.showBalance(users.get(usersNum).accounts.get(accountsNum).getAllMoney()); 497 } else { 498 view.rightAddOutput(users.get(usersNum).getName(), 499 users.get(usersNum).accounts.get(accountsNum).getBank().getBankName(), 500 d[2], 0-money); 501 view.showBalance(users.get(usersNum).accounts.get(accountsNum).getAllMoney()); 502 } 503 } else { 504 view.balanceWrong(); 505 } 506 } else { 507 view.crossBankWrong(); 508 } 509 } else { 510 view.ATMWrong(); 511 } 512 } else { 513 view.passwordWrong(); 514 } 515 } else { 516 view.cardWrong(); 517 } 518 } else if(d.length == 1){ // 查询余额 519 boolean flag = false; 520 int usersNum = 0; 521 int accountsNum = 0; 522 for(int j = 0; j < users.size(); j ++) { 523 for(int k = 0; k < users.get(j).accounts.size(); k ++) { 524 if(users.get(j).accounts.get(k).isCardExist(d[0])) { 525 usersNum = j; 526 accountsNum = k; 527 flag = true; 528 } 529 } 530 } 531 if(flag) { 532 view.onlyShowBalance(users.get(usersNum).accounts.get(accountsNum).getAllMoney()); 533 } else { 534 view.cardWrong(); 535 } 536 } 537 } 538 } 539 } 540 541 class View { 542 543 public View() { 544 super(); 545 // TODO 自动生成的构造函数存根 546 } 547 548 // 错误输出汇总 549 public void cardWrong() { 550 System.out.println("Sorry,this card does not exist."); 551 } 552 553 public void ATMWrong() { 554 System.out.println("Sorry,the ATM's id is wrong."); 555 } 556 557 public void passwordWrong() { 558 System.out.println("Sorry,your password is wrong."); 559 } 560 561 public void balanceWrong() { 562 System.out.println("Sorry,your account balance is insufficient."); 563 } 564 565 public void crossBankWrong() { 566 System.out.println("Sorry,cross-bank withdrawal is not supported."); 567 } 568 569 // 正确输出 570 public void rightTakeOutput(String userName, String bankName, String atmNum, double takeMoney) { // 取钱的输出 571 System.out.print(userName + "在" + bankName + "的" + atmNum + "号ATM机上取款¥"); 572 System.out.printf("%.2f", takeMoney); 573 System.out.println(); 574 } 575 576 public void rightAddOutput(String userName, String bankName, String atmNum, double addMoney) { // 存钱的输出 577 System.out.print(userName + "在" + bankName + "的" + atmNum + "号ATM机上存款¥"); 578 System.out.printf("%.2f", addMoney); 579 System.out.println(); 580 581 } 582 583 public void showBalance(double allMoney) { 584 System.out.printf("当前余额为¥%.2f", allMoney); 585 System.out.println(); 586 } 587 588 public void onlyShowBalance(double allMoney) { 589 System.out.printf("¥%.2f", allMoney); 590 System.out.println(); 591 } 592 } 593 594 public class Main { 595 596 public static void main(String[] args) { 597 598 Controller co = new Controller(); 599 co.play(); 600 } 601 }
其实这次的代码我自认为不是很满意,因为太复杂了。复杂的体现在于其中的AllInfo类里添加所有人的信息,以及Controller类里的play方法种的if判断,非常长且判断层数太多。先放上类图和sourceMonitor吧:
sourceMonitor:
这是最复杂的Controllor类的图,可以看出复杂度和深度很高。
类图:
相应输出:
题目7-5 ATM机类结构设计(二):
需求分析:
编写一个银行 ATM 机的模拟程序,能够完成用户的取款以及查询余额功能。与上题不同的是:
1.银行账户:客户在银行开立的存款账户、贷款账户、往来账户 的总称。银行账户分为借记账户和贷记账户两种,其中,借记账户不能够透支取款,而贷记账户可以透支取款(可能需要支付手续费)。一个银行用户可以拥有多个银行账户。
2.银行卡(Card):经批准由商业银行(含邮政金融机构) 向社会发行的具有消费信用、转账结算、存取现金等全部或部分功能的信用支付工具。根据账户 的不同,银行卡一般分为借记卡(针对借记账户)和信用卡(针对贷记账户) 两类。一个银行账户可以发行多张银行卡。
3.允许跨行办理相关业务务(例如在中国建设银行的 ATM 机上使用中国工商银行的银行卡进行业务操作)。
题目分析:
这题在上题基础上改变了很多需求,要求可以跨行操作业务,但要收取手续费,手续费根据银行不同而设立不同手续费;要求新增信用卡类;要求账户新增贷记账户且可以透支取款。
我的代码:
import java.util.*; import java.util.ArrayList; class ChinaUnionPay { private ArrayList<Bank> bank = new ArrayList<Bank>(); public ChinaUnionPay() { super(); // TODO 自动生成的构造函数存根 } public ChinaUnionPay(ArrayList<Bank> bank) { super(); this.bank = bank; } public ArrayList<Bank> getBank() { return bank; } public void setBank(ArrayList<Bank> bank) { this.bank = bank; } // 暂时放这 public boolean isCrossBank(String bankName1, String bankName2) { if(bankName1.matches(bankName2)) { return true; } return false; } } class Bank { private String bankName; private ArrayList<ATM> atms = new ArrayList<ATM>(); private double premium; // 手续费 public Bank() { super(); // TODO 自动生成的构造函数存根 } public Bank(String bankName, ArrayList<ATM> atms, double premium) { super(); this.bankName = bankName; this.atms = atms; this.premium = premium; } public String getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } public ArrayList<ATM> getAtms() { return atms; } public void setAtms(ArrayList<ATM> atms) { this.atms = atms; } public double getPremium() { return premium; } public void setPremium(double premium) { this.premium = premium; } } class ATM { private String atmNum; public ATM() { super(); // TODO 自动生成的构造函数存根 } public ATM(String atmNum) { super(); this.atmNum = atmNum; } public String getAtmNum() { return atmNum; } public void setAtmNum(String atmNum) { this.atmNum = atmNum; } public boolean isRightNum(String atmNum) { // 判断是否存在当前编号的ATM机 if(atmNum.matches("([0][1-9])|([1][0-1])")) { return true; } return false; } } class User { private String name; private ArrayList<Account> accounts = new ArrayList<Account>(); public User() { super(); // TODO 自动生成的构造函数存根 } public User(String name, ArrayList<Account> accounts) { super(); this.name = name; this.accounts = accounts; } public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList<Account> getAccounts() { return accounts; } public void setAccounts(ArrayList<Account> accounts) { this.accounts = accounts; } } class CreditUser { private String name; private ArrayList<CreditAccount> accounts = new ArrayList<CreditAccount>(); public CreditUser() { super(); // TODO 自动生成的构造函数存根 } public CreditUser(String name, ArrayList<CreditAccount> accounts) { super(); this.name = name; this.accounts = accounts; } public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList<CreditAccount> getAccounts() { return accounts; } public void setAccounts(ArrayList<CreditAccount> accounts) { this.accounts = accounts; } } class Account { private Bank bank; private ArrayList<Card> cards = new ArrayList<Card>(); private double money = 10000.00; public Account() { super(); // TODO 自动生成的构造函数存根 } public Account(ArrayList<Card> cards, Bank bank) { super(); this.bank = bank; this.cards = cards; } public Bank getBank() { return bank; } public void setBank(Bank bank) { this.bank = bank; } public ArrayList<Card> getCards() { return cards; } public void setCards(ArrayList<Card> cards) { this.cards = cards; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public void changeMoney(double cMoney) { this.money -= cMoney; } } class CreditAccount { private Bank bank; private ArrayList<Card> cards = new ArrayList<Card>(); private double money = 50000.00; public CreditAccount() { super(); // TODO 自动生成的构造函数存根 } public CreditAccount(ArrayList<Card> cards, Bank bank) { super(); this.bank = bank; this.cards = cards; } public Bank getBank() { return bank; } public void setBank(Bank bank) { this.bank = bank; } public ArrayList<Card> getCards() { return cards; } public void setCards(ArrayList<Card> cards) { this.cards = cards; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } } class Card { private String cardID; private String password = "88888888"; public Card() { super(); // TODO 自动生成的构造函数存根 } public Card(String cardID, String password) { super(); this.cardID = cardID; this.password = password; } public String getCardID() { return cardID; } public void setCardID(String cardID) { this.cardID = cardID; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } class AllUsers { public ArrayList<User> info() { ArrayList<ATM> atmNum01 = new ArrayList<ATM>(); // 建行ATM ATM atm01 = new ATM("01"); ATM atm02 = new ATM("02"); ATM atm03 = new ATM("03"); ATM atm04 = new ATM("04"); atmNum01.add(atm01); atmNum01.add(atm02); atmNum01.add(atm03); atmNum01.add(atm04); ArrayList<ATM> atmNum02 = new ArrayList<ATM>(); // 工行ATM ATM atm05 = new ATM("05"); ATM atm06 = new ATM("06"); atmNum02.add(atm05); atmNum02.add(atm06); ArrayList<ATM> atmNum03 = new ArrayList<ATM>(); // 农行ATM ATM atm07 = new ATM("07"); ATM atm08 = new ATM("08"); ATM atm09 = new ATM("09"); ATM atm10 = new ATM("10"); ATM atm11 = new ATM("11"); atmNum03.add(atm07); atmNum03.add(atm08); atmNum03.add(atm09); atmNum03.add(atm10); atmNum03.add(atm11); Bank bank01 = new Bank("中国建设银行", atmNum01, 0.02); // 两个银行的初始化 Bank bank02 = new Bank("中国工商银行", atmNum02, 0.03); Bank bank03 = new Bank("中国农商银行", atmNum03, 0.04); ArrayList<Card> cards01 = new ArrayList<Card>(); // 杨过一个账户的卡 Card card01 = new Card("6217000010041315709","88888888"); // 杨过的卡 Card card02 = new Card("6217000010041315715","88888888"); cards01.add(card01); cards01.add(card02); ArrayList<Card> cards02 = new ArrayList<Card>(); // 杨过一个账户的卡 Card card03 = new Card("6217000010041315718","88888888"); cards02.add(card03); ArrayList<Card> cards03 = new ArrayList<Card>(); // 郭靖账户的卡 Card card04 = new Card("6217000010051320007", "8888888"); cards03.add(card04); ArrayList<Card> cards04 = new ArrayList<Card>(); // 张无忌一个账户的卡 Card card05 = new Card("6222081502001312389","88888888"); cards04.add(card05); ArrayList<Card> cards05 = new ArrayList<Card>(); // 张无忌一个账户的卡 Card card06 = new Card("6222081502001312390","88888888"); cards05.add(card06); ArrayList<Card> cards06 = new ArrayList<Card>(); // 张无忌一个账户的卡 Card card07 = new Card("6222081502001312399","88888888"); Card card08 = new Card("6222081502001312400","88888888"); cards06.add(card07); cards06.add(card08); ArrayList<Card> cards07 = new ArrayList<Card>(); // 韦小宝一个账户的卡 Card card09 = new Card("6222081502051320785","88888888"); cards07.add(card09); ArrayList<Card> cards08 = new ArrayList<Card>(); // 韦小宝一个账户的卡 Card card10 = new Card("6222081502051320786","88888888"); cards08.add(card10); ArrayList<Account> accounts01 = new ArrayList<Account>(); // 杨过的账户 Account account01 = new Account(cards01, bank01); // 杨过的建行账户一卡 Account account02 = new Account(cards02, bank01); // 杨过的建行账户二卡 accounts01.add(account01); accounts01.add(account02); ArrayList<Account> accounts02 = new ArrayList<Account>(); // 郭靖的账户 Account account03 = new Account(cards03, bank01); // 郭靖的建行账户一卡 accounts02.add(account03); ArrayList<Account> accounts03 = new ArrayList<Account>(); // 张无忌的账户 Account account04 = new Account(cards04, bank02); // 张无忌的建行账户一卡 Account account05 = new Account(cards05, bank02); // 张无忌的建行账户二卡 Account account06 = new Account(cards06, bank02); // 张无忌的建行账户三卡 accounts03.add(account04); accounts03.add(account05); accounts03.add(account06); ArrayList<Account> accounts04 = new ArrayList<Account>(); // 韦小宝的账户 Account account07 = new Account(cards07, bank02); Account account08 = new Account(cards08, bank02); accounts04.add(account07); accounts04.add(account08); ArrayList<User> users = new ArrayList<User>(); User user01 = new User("杨过", accounts01); // 初始化杨过的信息 users.add(user01); User user02 = new User("郭靖", accounts02); // 初始化郭靖 users.add(user02); User user03 = new User("张无忌", accounts03); // 初始化张无忌 users.add(user03); User user04 = new User("韦小宝", accounts04); // 初始化韦小宝 users.add(user04); return users; } public ArrayList<CreditUser> info1(){ ArrayList<ATM> atmNum01 = new ArrayList<ATM>(); // 建行ATM ATM atm01 = new ATM("01"); ATM atm02 = new ATM("02"); ATM atm03 = new ATM("03"); ATM atm04 = new ATM("04"); atmNum01.add(atm01); atmNum01.add(atm02); atmNum01.add(atm03); atmNum01.add(atm04); ArrayList<ATM> atmNum02 = new ArrayList<ATM>(); // 工行ATM ATM atm05 = new ATM("05"); ATM atm06 = new ATM("06"); atmNum02.add(atm05); atmNum02.add(atm06); ArrayList<ATM> atmNum03 = new ArrayList<ATM>(); // 农行ATM ATM atm07 = new ATM("07"); ATM atm08 = new ATM("08"); ATM atm09 = new ATM("09"); ATM atm10 = new ATM("10"); ATM atm11 = new ATM("11"); atmNum03.add(atm07); atmNum03.add(atm08); atmNum03.add(atm09); atmNum03.add(atm10); atmNum03.add(atm11); Bank bank01 = new Bank("中国建设银行", atmNum01, 0.02); // 两个银行的初始化 Bank bank02 = new Bank("中国工商银行", atmNum02, 0.03); Bank bank03 = new Bank("中国农商银行", atmNum03, 0.04); ArrayList<Card> cards09 = new ArrayList<Card>(); // 张三丰的卡们 Card card11 = new Card("6640000010045442002", "88888888"); Card card12 = new Card("6640000010045442003", "88888888"); cards09.add(card11); cards09.add(card12); ArrayList<Card> cards10 = new ArrayList<Card>(); // 令狐冲的卡 Card card13 = new Card("6640000010045441009", "88888888"); ArrayList<Card> cards11 = new ArrayList<Card>(); Card card14 = new Card("6630000010033431001", "88888888"); cards11.add(card14); ArrayList<Card> cards12 = new ArrayList<Card>(); Card card15 = new Card("6630000010033431008", "88888888"); cards12.add(card15); ArrayList<CreditAccount> accounts05 = new ArrayList<CreditAccount>(); // 张三丰 CreditAccount account09 = new CreditAccount(cards09, bank01); accounts05.add(account09); ArrayList<CreditAccount> accounts06 = new ArrayList<CreditAccount>(); // 令狐冲 CreditAccount account10 = new CreditAccount(cards10, bank02); accounts06.add(account10); ArrayList<CreditAccount> accounts07 = new ArrayList<CreditAccount>(); // 乔峰 CreditAccount account11 = new CreditAccount(cards11, bank03); accounts07.add(account11); ArrayList<CreditAccount> accounts08 = new ArrayList<CreditAccount>(); // 洪七公 CreditAccount account12 = new CreditAccount(cards12, bank03); accounts08.add(account12); ArrayList<CreditUser> users = new ArrayList<CreditUser>(); CreditUser user05 = new CreditUser("张三丰", accounts05); CreditUser user06 = new CreditUser("令狐冲", accounts06); CreditUser user07 = new CreditUser("乔峰", accounts07); CreditUser user08 = new CreditUser("洪七公", accounts08); users.add(user05); users.add(user06); users.add(user07); users.add(user08); return users; } } class Check1 { // 检查借记账户输入的正确性 User users = new User(); private String cardID; private String password; private String atmNum; private double balance; View v = new View(); public Check1() { super(); // TODO 自动生成的构造函数存根 } public Check1(User users, String cardID, String password, String atmNum, double balance) { super(); this.users = users; this.cardID = cardID; this.password = password; this.atmNum = atmNum; this.balance = balance; } public boolean check() { int flag = 0; int i = 0; int userNum = 0; int j = 0; int accoNum = 0; int k = 0; int cardNum = 0; for(i = 0; j < users.getAccounts().size(); j ++){ for(j = 0; j < users.getAccounts().get(i).getCards().size(); j ++) { if (cardID.equals(users.getAccounts().get(i).getCards().get(j).getCardID())){ flag=1; userNum = i; // 定位 accoNum = j; break; } } } if(flag == 1) { // 检查密码 if(password.equals(users.getAccounts().get(accoNum).getCards().get(cardNum).getPassword())) { flag = 2; } else { v.passwordWrong(); return false; } } else { v.cardWrong(); return false; } if(flag == 2) { // 检查ATM if(atmNum.matches("([0][1-9])|([1][0-1])")) { flag = 3; } } if(flag == 3) { flag = 4; } else { v.ATMWrong(); return false; } if(flag == 4) { // 检查是否可取钱 if(balance > users.getAccounts().get(accoNum).getMoney()) { return false; } else { return true; } } else { v.balanceWrong(); return false; } } public int crossWhitchBank(int bankNum, String atmNum) { if (bankNum == 0&& atmNum.matches("0[1-4]")) { return 1; // 建行 } else if (bankNum == 1 && atmNum.matches("[0][56]")) { return 2; // 工行 } else if (bankNum == 2 && atmNum.matches("([0][789])|([1][01])")) { return 3; // 农行 } else { return 0; // 非跨银行 } } } class View { public View() { super(); // TODO 自动生成的构造函数存根 } // 错误输出汇总 public void cardWrong() { System.out.println("Sorry,this card does not exist."); } public void ATMWrong() { System.out.println("Sorry,the ATM's id is wrong."); } public void passwordWrong() { System.out.println("Sorry,your password is wrong."); } public void balanceWrong() { System.out.println("Sorry,your account balance is insufficient."); } // 正确输出 public void rightTakeOutput(String userName, String bankName, String atmNum, double takeMoney) { // 取钱的输出 System.out.print(userName + "在" + bankName + "的" + atmNum + "号ATM机上取款¥"); System.out.printf("%.2f", takeMoney); System.out.println(); } public void rightAddOutput(String userName, String bankName, String atmNum, double addMoney) { // 存钱的输出 System.out.print(userName + "在" + bankName + "的" + atmNum + "号ATM机上存款¥"); System.out.printf("%.2f", addMoney); System.out.println(); } public void showBalance(double allMoney) { System.out.printf("当前余额为¥%.2f", allMoney); System.out.println(); } public void onlyShowBalance(double allMoney) { System.out.printf("¥%.2f", allMoney); System.out.println(); } } class DealData { StringBuilder sb = new StringBuilder(); AllUsers all = new AllUsers(); public DealData() { super(); // TODO 自动生成的构造函数存根 } public DealData(StringBuilder sb) { super(); this.sb = sb; } public StringBuilder getSb() { return sb; } public void setSb(StringBuilder sb) { this.sb = sb; } public void input() { Scanner in = new Scanner(System.in); String allInput = ""; allInput = in.nextLine(); while(!allInput.equals("#")) { sb.append(allInput); sb.append("\n"); allInput = in.nextLine(); } } public String[] lines() { input(); String[] lines = sb.toString().split("\n"); // 输入数据分行分割 return lines; } public String[] datas(String line) { String[] datas = line.split("\\s+"); if(datas.length >= 1) { // 如果长度正确则返回分割后的数组, 否则返回空 return datas; } return null; } public void play() { ArrayList<User> users = all.info(); View v = new View(); input(); String[] lines = lines(); for(int i = 0; i < lines.length; i ++) { String[] datas = datas(lines[i]); if(datas.length == 4) { double money = Double.parseDouble(datas[3]); for(int j = 0; j < users.size(); j ++) { for(int k = 0; k < users.get(j).getAccounts().size(); k ++) { Check1 check = new Check1(users.get(j), datas[0], datas[1], datas[2], money); check.check(); if(check.check()) { if(check.crossWhitchBank(k, datas[3]) == 0) { // 非跨行 users.get(i).getAccounts().get(j).changeMoney(money); if(money > 0) { v.rightTakeOutput(users.get(i).getName(), users.get(i).getAccounts().get(j).getBank().getBankName(), datas[2], money); v.showBalance(users.get(i).getAccounts().get(j).getMoney()); } else { v.rightAddOutput(users.get(i).getName(), users.get(i).getAccounts().get(j).getBank().getBankName(), datas[2], 0-money); v.showBalance(users.get(i).getAccounts().get(j).getMoney()); v.rightTakeOutput(users.get(j).getName(), users.get(j).getAccounts().get(k).getBank().getBankName(), datas[2], money); } } } } } } } } public class Main { public static void main(String[] args) { DealData d = new DealData(); d.play(); } } }
这题一直在修改,到现在也还有点问题,不能保证正确。前前后后重新写了三次,我觉得自己写的太复杂了,不能完全掌握这题。先放上类图和sourceMonitor:
sourceMonitor:
类图:
总结:
这三次的题目集做的并不是很好,与其他人相比有很大差距。记忆犹新的是第六次作业7-5 在结束的前一秒上交的,但是是不完整的。当时如果再有半个小时就可以写完,这是时间安排上的问题,由于上周没有飞机仪表项目的检查,而我却把时间更多地放在了飞机仪表项目上,导致时间上没有安排好,这是我吸取的教训之一。另外就是在写代码之前一定要先设计好、想清楚,而且设计好之后尽量不要更改,按照设计的来。在两题ATM机的题目里,我重写的次数是六次,这是对时间的最大程度的浪费,在写代码时发现这个属性放这会更好,比如money我一开始放在了Card类里,但这是不对的。因为一个账户里可以有很多张卡,但一个账户里的钱是所有卡通用的。这也与我的常识认知有关,我习惯性地认为钱是在银行卡里的,所以不假思索地将money放在了Card里。经过讨论和仔细读题我才最终确定我错了。另一个就是Bank类,我一直在修改Bank类里的属性,首先ATM是有的,但是Account和User我不知道要不要或者说有无必要放入Bank类里,这里也是一直在修改的地方。这种修改导致的牵一发而动全身最终带来的结果是重写。现在想来还有后怕,我因为这种问题浪费了很多时间,也最终导致我没有写完,这真是非常要命的,无论是对心理还是生理。会出现这些问题也说明我的能力不足,对面向对象的设计不到位。警钟长鸣,我会吸取这惨痛的教训,放到未来。