1,前言:
每次题目集只有只有一两题,题目难度适中,考察Java基础的程序设计方法。根据程序设计原则设计出合理代码。
2,设计与分析:
(2)设计与分析:重点对题目的提交源码进行分析,可参考SourceMonitor的生成报表内容以及PowerDesigner的相应类图,要有相应的解释和心得(做到有图有真相),本次Blog必须分析的内容如下:
①题目集7(7-1)、(7-2)两道题目的递进式设计分析总结
(7-2)在(7-1)的基础上增加了对图片的分组和排序,但主体的各个类还是(7-1)中的
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static Scanner input = new Scanner(System.in); public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<>(); int num = input.nextInt(); if(num == 0){ System.out.println("The original list:"); System.out.println("\nThe sorted list:"); System.out.println("\nSum of area:0.00"); System.exit(0); } while(num != 0){ if(num < 0 || num > 4){ System.out.println("Wrong Format"); System.exit(0); } list.add(num); num = input.nextInt(); } DealCardList dealCardList = new DealCardList(list); // System.out.println(dealCardList.validate()); if(!dealCardList.validates()){ System.out.println("Wrong Format"); System.exit(0); } dealCardList.showResult(); input.close(); } } class DealCardList{ ArrayList<Card> cardList = new ArrayList<>(); DealCardList(ArrayList<Integer> list){ if(!Main.input.hasNext()){ System.out.println("Wrong Format"); System.exit(0); } for(Integer i :list){ switch(i){ case 1: Shape c = new Circle(Main.input.nextDouble()); cardList.add(new Card(c)); break; case 2: Shape r = new Rectangle(Main.input.nextDouble(),Main.input.nextDouble()); cardList.add(new Card(r)); break; case 3: Shape s = new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()); cardList.add(new Card(s)); break; case 4: Shape t = new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()); cardList.add(new Card(t)); break; } } } void showResult(){ System.out.println("The original list:"); for(Card c:cardList){ System.out.print(c.shap.toString()+":"+String.format("%.2f",c.shap.getArea())+" "); } cardSort(); System.out.println("\nThe sorted list:"); for(Card c:cardList){ System.out.print(c.shap.toString()+":"+String.format("%.2f",c.shap.getArea())+" "); } System.out.println("\nSum of area:"+String.format("%.2f",getAllArea())); } double getAllArea(){ double num = 0; for(Card c:cardList){ num = num+c.shap.getArea(); } return num; } void cardSort(){ Collections.sort(cardList); } boolean validates(){ for(Card c:cardList){ if(!c.shap.validata()){ return false; } } return true; } } class Card implements Comparable<Card>{ Shape shap; Card(Shape shap){ this.shap = shap; } Shape getShap(){ return shap; } void setShap(Shape shap){ this.shap = shap; } @Override public int compareTo(Card o) { if(this.shap.getArea() < o.shap.getArea()) return 1; else if(this.shap.getArea() > o.shap.getArea()) return -1; else return 0; } } abstract class Shape{ abstract double getArea(); abstract boolean validata(); @Override public String toString(){ return this.getClass().getName(); } } class Circle extends Shape{ public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } private double radius; Circle(double r){ this.radius = r; } @Override double getArea(){ return Math.PI*radius*radius; } @Override boolean validata(){ return radius >= 0; } } class Rectangle extends Shape{ public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } private double width; private double length; Rectangle(double a,double b){ this.length = a; this.width = b; } @Override double getArea(){ return width*length; } @Override boolean validata(){ return width >= 0 && length >= 0; } } class Triangle extends Shape{ public double getSide1() { return side1; } public void setSide1(double side1) { this.side1 = side1; } public double getSide2() { return side2; } public void setSide2(double side2) { this.side2 = side2; } public double getSide3() { return side3; } public void setSide3(double side3) { this.side3 = side3; } private double side1; private double side2; private double side3; Triangle(double a,double b,double c){ this.side1 = a; this.side2 = b; this.side3 = c; } @Override double getArea(){ double s = (side1+side2+side3)/2.0; return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)); } @Override boolean validata(){ return side1+side2>side3&&side2+side3>side1&&side3+side1>side2; } } class Trapezoid extends Shape{ public void setToside(double toside) { this.toside = toside; } public void setBottomside(double bottomside) { this.bottomside = bottomside; } public void setHeight(double height) { this.height = height; } private double toside; private double bottomside; private double height; Trapezoid(double a,double b,double c){ toside = a; bottomside = b; height = c; } @Override double getArea() { return (toside+bottomside)*height/2.0; } @Override boolean validata() { return toside>0&&bottomside>0&&height>0; } }
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner;
public class 图形卡片排序游戏2 { public static Scanner input = new Scanner(System.in); public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<>(); int num = input.nextInt(); if(num == 0){ System.out.println("Wrong Format"); System.exit(0); } while(num != 0){ if(num < 0 || num > 4){ System.out.println("Wrong Format"); System.exit(0); } list.add(num); num = input.nextInt(); } DealCardList dealCardList = new DealCardList(list); // System.out.println(dealCardList.validate()); if(!dealCardList.validates()){ System.out.println("Wrong Format"); System.exit(0); } dealCardList.showResult(); input.close(); } } class DealCardList{ ArrayList<Card> cardList = new ArrayList<>(); DealCardList(ArrayList<Integer> list){ if(!Main.input.hasNext()){ System.out.println("Wrong Format"); System.exit(0); } for(Integer i :list){ switch(i){ case 1: Shape c = new Circle(Main.input.nextDouble()); cardList.add(new Card(c)); break; case 2: Shape r = new Rectangle(Main.input.nextDouble(),Main.input.nextDouble()); cardList.add(new Card(r)); break; case 3: Shape s = new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()); cardList.add(new Card(s)); break; case 4: Shape t = new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()); cardList.add(new Card(t)); break; } } } void show(ArrayList<Card> card){ System.out.print("["); for(Card c:card){ System.out.print(c.shap.toString()+":"+String.format("%.2f",c.shap.getArea())+" "); } System.out.print("]"); } void showResult(){ ArrayList<Card> circleCardList = new ArrayList<>(); ArrayList<Card> rectangleCardList = new ArrayList<>(); ArrayList<Card> trapezoidCardList = new ArrayList<>(); ArrayList<Card> triangleCardList = new ArrayList<>(); System.out.println("The original list:"); show(cardList); for(Card c:cardList){ if("Circle".equals(c.shap.toString())) circleCardList.add(c); if("Rectangle".equals(c.shap.toString())) rectangleCardList.add(c); if("Trapezoid".equals(c.shap.toString())) trapezoidCardList.add(c); if("Triangle".equals(c.shap.toString())) triangleCardList.add(c); } System.out.println("\nThe Separated List:"); show(circleCardList); show(rectangleCardList); show(triangleCardList); show(trapezoidCardList); System.out.println("\nThe Separated sorted List:"); Collections.sort(circleCardList); show(circleCardList); Collections.sort(rectangleCardList); show(rectangleCardList); Collections.sort(triangleCardList); show(triangleCardList); Collections.sort(trapezoidCardList); show(trapezoidCardList); double max = getAllArea(circleCardList); if(max < getAllArea(rectangleCardList)) max = getAllArea(rectangleCardList); if(max < getAllArea(triangleCardList)) max = getAllArea(triangleCardList); if(max < getAllArea(trapezoidCardList)) max = getAllArea(trapezoidCardList); System.out.println("\nThe max area:"+String.format("%.2f",max)); // System.out.println("The original list:"); // for(Card c:cardList){ // System.out.print(c.shap.toString()+":"+String.format("%.2f",c.shap.getArea())+" "); // } // cardSort(); // System.out.println("\nThe sorted list:"); // for(Card c:cardList){ // System.out.print(c.shap.toString()+":"+String.format("%.2f",c.shap.getArea())+" "); // } // System.out.println("\nSum of area:"+String.format("%.2f",getAllArea())); } double getAllArea(ArrayList<Card> card){ if(card.isEmpty()){ return 0; } else{ double num = 0; for(Card c:card){ num = num+c.shap.getArea(); } return num; } } void cardSort(){ Collections.sort(cardList); } boolean validates(){ for(Card c:cardList){ if(!c.shap.validata()){ return false; } } return true; } } class Card implements Comparable<Card>{ Shape shap; Card(Shape shap){ this.shap = shap; } Shape getShap(){ return shap; } void setShap(Shape shap){ this.shap = shap; } @Override public int compareTo(Card o) { if(this.shap.getArea() < o.shap.getArea()) return 1; else if(this.shap.getArea() > o.shap.getArea()) return -1; else return 0; } } abstract class Shape{ abstract double getArea(); abstract boolean validata(); @Override public String toString(){ return this.getClass().getName(); } } class Circle extends Shape{ public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } private double radius; Circle(double r){ this.radius = r; } @Override double getArea(){ return Math.PI*radius*radius; } @Override boolean validata(){ return radius >= 0; } } class Rectangle extends Shape{ public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } private double width; private double length; Rectangle(double a,double b){ this.length = a; this.width = b; } @Override double getArea(){ return width*length; } @Override boolean validata(){ return width >= 0 && length >= 0; } } class Triangle extends Shape{ public double getSide1() { return side1; } public void setSide1(double side1) { this.side1 = side1; } public double getSide2() { return side2; } public void setSide2(double side2) { this.side2 = side2; } public double getSide3() { return side3; } public void setSide3(double side3) { this.side3 = side3; } private double side1; private double side2; private double side3; Triangle(double a,double b,double c){ this.side1 = a; this.side2 = b; this.side3 = c; } @Override double getArea(){ double s = (side1+side2+side3)/2.0; return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)); } @Override boolean validata(){ return side1+side2>side3&&side2+side3>side1&&side3+side1>side2; } } class Trapezoid extends Shape{ public void setToside(double toside) { this.toside = toside; } public void setBottomside(double bottomside) { this.bottomside = bottomside; } public void setHeight(double height) { this.height = height; } private double toside; private double bottomside; private double height; Trapezoid(double a,double b,double c){ toside = a; bottomside = b; height = c; } @Override double getArea() { return (toside+bottomside)*height/2.0; } @Override boolean validata() { return toside>0&&bottomside>0&&height>0; } }
②题目集8和题目集9两道ATM机仿真题目的设计思路分析总结
将银联、银行、账户、ATM、用户、银行卡分别用各个对象表示,再将取款查询余额等操作单独用别的类实现。
import java.util.ArrayList; import java.util.Scanner; import java.util.Iterator; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Banks { public static void main(String[] args) { Scanner input = new Scanner(System.in); UnionPay unionPay = new UnionPay(); Bank ccb = new Bank("1001","中国建设银行"); Bank icbc = new Bank("1002","中国工商银行"); Bank abc = new Bank("1003","中国农业银行"); unionPay.addBank(ccb); unionPay.addBank(icbc); unionPay.addBank(abc); ATM aTM1 = new ATM("01",ccb); ATM aTM2 = new ATM("02",ccb); ATM aTM3 = new ATM("03",ccb); ATM aTM4 = new ATM("04",ccb); ATM aTM5 = new ATM("05",icbc); ATM aTM6 = new ATM("06",icbc); ATM aTM7 = new ATM("07",abc); ATM aTM8 = new ATM("08",abc); ATM aTM9 = new ATM("09",abc); ATM aTM10 = new ATM("10",abc); ATM aTM11 = new ATM("11",abc); ccb.addATM(aTM1); ccb.addATM(aTM2); ccb.addATM(aTM3); ccb.addATM(aTM4); icbc.addATM(aTM5); icbc.addATM(aTM6); abc.addATM(aTM7); abc.addATM(aTM8); abc.addATM(aTM9); abc.addATM(aTM10); abc.addATM(aTM11); User Yangguo = new User("360101200102122324","杨过","13856223254"); User Guojing = new User("360101200012302552","郭靖","13800021124"); User Zhangwuji = new User("360502199805163221","张无忌","13952110011"); User Weixiaobao = new User("360201200513243326","韦小宝","13025996587"); User Zsf = new User("360201200513243326","张三丰","13025996587"); User Lhc = new User("360201200513243326","令狐冲","13025996587"); User Qf = new User("360201200513243326","乔峰","13025996587"); Account ccbAcc1 = new Account("3217000010041315709",10000.00,Yangguo,ccb,0); Account ccbAcc2 = new Account("3217000010041315715",10000.00,Yangguo,ccb,0); Account ccbAcc3 = new Account("3217000010051320007",10000.00,Guojing,ccb,0); Account ccbAcc4 = new Account("3640000010045442002",10000.00,Zsf,ccb,1); Account icbcAcc1 = new Account("3222081502001312389",10000.00,Zhangwuji,icbc,0); Account icbcAcc2 = new Account("3222081502001312390",10000.00,Zhangwuji,icbc,0); Account icbcAcc3 = new Account("3222081502001312399",10000.00,Zhangwuji,icbc,0); Account icbcAcc4 = new Account("3222081502051320785",10000.00,Weixiaobao,icbc,0); Account icbcAcc5 = new Account("3222081502051320786",10000.00,Weixiaobao,icbc,0); Account icbcAcc6 = new Account("3640000010045441009",10000.00,Lhc,icbc,1); Account abcAcc1 = new Account("3630000010033431001",10000.00,Qf,abc,1); ccb.addAccount(ccbAcc1); ccb.addAccount(ccbAcc2); ccb.addAccount(ccbAcc3); ccb.addAccount(ccbAcc4); icbc.addAccount(icbcAcc1); icbc.addAccount(icbcAcc2); icbc.addAccount(icbcAcc3); icbc.addAccount(icbcAcc4); icbc.addAccount(icbcAcc5); icbc.addAccount(icbcAcc6); abc.addAccount(abcAcc1); Yangguo.addAccount(ccbAcc1); Yangguo.addAccount(ccbAcc2); Guojing.addAccount(ccbAcc3); Zsf.addAccount(ccbAcc4); Zhangwuji.addAccount(icbcAcc1); Zhangwuji.addAccount(icbcAcc2); Zhangwuji.addAccount(icbcAcc3); Weixiaobao.addAccount(icbcAcc4); Weixiaobao.addAccount(icbcAcc5); Lhc.addAccount(icbcAcc6); Qf.addAccount(abcAcc1); Card ccbCard1 = new Card("6217000010041315709","88888888",ccbAcc1); Card ccbCard2 = new Card("6217000010041315715","88888888",ccbAcc1); Card ccbCard3 = new Card("6217000010041315718","88888888",ccbAcc2); Card ccbCard4 = new Card("6217000010051320007","88888888",ccbAcc3); Card icbcCard5 = new Card("6222081502001312389","88888888",icbcAcc1); Card icbcCard6 = new Card("6222081502001312390","88888888",icbcAcc2); Card icbcCard7 = new Card("6222081502001312399","88888888",icbcAcc3); Card icbcCard8 = new Card("6222081502001312400","88888888",icbcAcc3); Card icbcCard9 = new Card("6222081502051320785","88888888",icbcAcc4); Card icbcCard10 = new Card("6222081502051320786","88888888",icbcAcc5); Card ccbcCard11 = new Card("6640000010045442002","88888888",ccbAcc4); Card ccbcCard12 = new Card("6640000010045442003","88888888",ccbAcc4); Card icbcCard13 = new Card("6640000010045441009","88888888",icbcAcc6); Card acbcCard14 = new Card("6630000010033431001","88888888",abcAcc1); ccbAcc1.addCard(ccbCard1); ccbAcc1.addCard(ccbCard2); ccbAcc2.addCard(ccbCard3); ccbAcc3.addCard(ccbCard4); ccbAcc4.addCard(ccbcCard11); ccbAcc4.addCard(ccbcCard12); icbcAcc1.addCard(icbcCard5); icbcAcc2.addCard(icbcCard6); icbcAcc3.addCard(icbcCard7); icbcAcc3.addCard(icbcCard8); icbcAcc4.addCard(icbcCard9); icbcAcc5.addCard(icbcCard10); icbcAcc6.addCard(icbcCard13); abcAcc1.addCard(acbcCard14); StringBuilder sb = new StringBuilder(); String data; while(!((data = input.nextLine()).equals("#"))) { sb.append(data).append("\n"); } String[] dt = sb.toString().split("\n"); for (String dt1 : dt) { String[] dataLine = dt1.toString().split("\\s+"); if(dataLine.length == 1) { GetBalance gb = new GetBalance(unionPay); System.out.println(String.format("业务:查询余额 ¥%.2f", gb.getBalance(dataLine[0]))); }else { Withdraw wd = new Withdraw(unionPay,dataLine[0],dataLine[1],dataLine[2],Double.parseDouble(dataLine[3])); wd.withdraw(); } } } } class Bank { private String bankNO; private String bankName; private double interest; //利息 public double getInterest() { return interest; } private ArrayList<Account> accountList = new ArrayList<>(); private ArrayList<ATM> ATMList = new ArrayList<>(); public Bank() { super(); // TODO Auto-generated constructor stub } public Bank(String bankNO, String bankName) { super(); this.bankNO = bankNO; this.bankName = bankName; if(bankNO.equals("1001")){ interest = 0.02; } if(bankNO.equals("1002")){ interest = 0.03; } if(bankNO.equals("1003")){ interest = 0.04; } } public String getBankNO() { return bankNO; } public void setBankNO(String bankNO) { this.bankNO = bankNO; } public String getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } public void addAccount(Account account) { this.accountList.add(account); } public void removeAccount(Account account) { this.accountList.remove(account); } public void addATM(ATM aTM) { this.ATMList.add(aTM); } public void removeATM(ATM aTM) { this.ATMList.remove(aTM); } public ArrayList<Account> getAccountList() { return accountList; } public void setAccountList(ArrayList<Account> accountList) { this.accountList = accountList; } public ArrayList<ATM> getATMList() { return ATMList; } public void setATMList(ArrayList<ATM> aTMList) { ATMList = aTMList; } } class User { private String ID; private String name; private String Phone; ArrayList<Account> list = new ArrayList<>(); public User() { super(); // TODO Auto-generated constructor stub } public User(String iD, String name, String phone) { super(); ID = iD; this.name = name; Phone = phone; } public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getPhone() { return Phone; } public void setPhone(String phone) { Phone = phone; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void addAccount(Account account) { this.list.add(account); } public void removeAccount(Account account) { this.list.remove(account); } } class Account { private String accountNO; private double balance = 0; private User user = null; private Bank bank = null; private boolean isBorrow; private static ArrayList<Card> list = new ArrayList<>(); public Account() { super(); // TODO Auto-generated constructor stub } public Account(String accountNO, double balance, User user, Bank bank,int i) { super(); this.accountNO = accountNO; this.balance = balance; this.user = user; this.bank = bank; if(i == 1){ isBorrow = true; } else{ isBorrow = false; } } public void addCard(Card card) { list.add(card); } public void removeCard(Card card) { list.remove(card); } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public String getAccountNO() { return accountNO; } public void setAccountNO(String accountNO) { this.accountNO = accountNO; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Bank getBank() { return bank; } public void setBank(Bank bank) { this.bank = bank; } public ArrayList<Card> getList() { return list; } public void setList(ArrayList<Card> list) { Account.list = list; } public boolean isIsBorrow() { return isBorrow; } public static Account getAmountbyCardNO(String cardNO) { Iterator<Card> cardItr = Account.list.iterator(); Card card = null; while(cardItr.hasNext()) { card = cardItr.next(); if(card.getCardNO().equals(cardNO)) { return card.getAccount(); } } return null; } } class Card { private String cardNO; private String cardPassword; private Account account = null; public Card() { super(); // TODO Auto-generated constructor stub } public Card(String cardNO, String cardPassword, Account account) { super(); this.cardNO = cardNO; this.cardPassword = cardPassword; this.account = account; } public String getCardNO() { return cardNO; } public void setCardNO(String cardNO) { this.cardNO = cardNO; } public String getCardPassword() { return cardPassword; } public Account getAccount() { return account; } public void setAccount(Account account) { this.account = account; } public void setCardPassword(String cardPassword) { this.cardPassword = cardPassword; } public boolean checkCard() { Pattern p = Pattern.compile("\\d{16}+"); Matcher m = p.matcher(this.cardNO); return m.matches(); } public boolean checkPassword(String password) { return this.cardPassword.equals(password); } } class ATM { private String ATMID; private Bank bank = null; public ATM() { super(); // TODO Auto-generated constructor stub } public ATM(String aTMID, Bank bank) { super(); ATMID = aTMID; this.bank = bank; } public String getATMID() { return ATMID; } public void setATMID(String aTMID) { ATMID = aTMID; } public Bank getBank() { return bank; } public void setBank(Bank bank) { this.bank = bank; } } class Withdraw { private UnionPay unionPay; private String cardNO; private String cardPassword; private String ATMID; private double amount; public Withdraw() { super(); // TODO Auto-generated constructor stub } public Withdraw(UnionPay unionPay, String cardNO, String cardPassword, String aTMID, double amount) { super(); this.unionPay = unionPay; this.cardNO = cardNO; this.cardPassword = cardPassword; ATMID = aTMID; this.amount = amount; } public Withdraw(String cardNO, String cardPassword, String aTMID, double amount) { super(); this.cardNO = cardNO; this.cardPassword = cardPassword; ATMID = aTMID; this.amount = amount; } public void withdraw() { //取款方法 /** * 校验该卡是否存在 */ Card card = ValidateData.getCardbyCardNO(unionPay, cardNO); if(card == null) { System.out.println("Sorry,this card does not exist."); System.exit(0); } /** * 校验ATM是否存在 */ ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID); if(aTM == null) { System.out.println("Sorry,the ATM's id is wrong."); System.exit(0); } Account account = Account.getAmountbyCardNO(cardNO); double balance = account.getBalance(); /** * 校验卡密码是否正确 */ if(!card.getCardPassword().equals(cardPassword)) { System.out.println("Sorry,your password is wrong."); System.exit(0); } /** * 校验取款 */ if(!account.isIsBorrow()){ //非信用卡 if (account.getBank().getBankNO() != aTM.getBank().getBankNO()) { //跨行 if (amount + aTM.getBank().getInterest()*amount > balance) { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } else{ account.setBalance(balance - amount+ aTM.getBank().getInterest()*amount); } } else{ if (amount > balance && account.isIsBorrow()) { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } else{ account.setBalance(balance - amount); } } } else{ if (account.getBank().getBankNO() != aTM.getBank().getBankNO()) { //跨行 if(balance - amount - aTM.getBank().getInterest()*amount - (balance>0?(amount-balance>0?amount-balance:0):amount)*0.05 >= -50000){ account.setBalance(balance - amount - aTM.getBank().getInterest()*amount - (balance>0?(amount-balance>0?amount-balance:0):amount)*0.05); } else{ System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } } else{ if(balance - amount - (balance>0?(amount-balance>0?amount-balance:0):amount)*0.05 >= -50000){ account.setBalance(balance - amount - (balance>0?(amount-balance>0?amount-balance:0):amount)*0.05); } else{ System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } } } /** * 透支 */ /** * 校验是否为跨行取款 */ //取款更新余额操作 if(amount<0){ showResult(account,0); } else{ showResult(account,1); } } public void showResult(Account account,int flag) { String type = ""; if(flag == 1) { type = "取款"; }else { type = "存款"; amount *= -1; } String userName = account.getUser().getName(); String bankName = account.getBank().getBankName(); System.out.println("业务:"+type+" "+userName + "在" + bankName + "的" + ATMID + "号ATM机上" + type + String.format("¥%.2f", amount)); System.out.println("当前余额为" + String.format("¥%.2f", account.getBalance())); } } class ValidateData { /** * 校验卡号是否存在 * @param unionPay * @param cardNO * @return */ public static Card getCardbyCardNO(UnionPay unionPay,String cardNO) { Card card = null; Iterator<Bank> bankItr = unionPay.getBankList().iterator(); while(bankItr.hasNext()) { ArrayList<Account> accountList = bankItr.next().getAccountList(); Iterator<Account> accountItr = accountList.iterator(); while(accountItr.hasNext()) { ArrayList<Card> cardList = accountItr.next().getList(); Iterator<Card> cardItr = cardList.iterator(); while(cardItr.hasNext()) { card = cardItr.next(); if(card.getCardNO().equals(cardNO)) { return card; } } } } return null; } /** * 校验ATM ID是否存在 * @param unionPay * @param ATMID * @return */ public static ATM getATMbyATMID(UnionPay unionPay,String ATMID) { Iterator<Bank> bankItr = unionPay.getBankList().iterator(); Bank bank = null; ATM aTM = null; while(bankItr.hasNext()) { bank = bankItr.next(); Iterator<ATM> aTMItr = bank.getATMList().iterator(); while(aTMItr.hasNext()) { aTM = aTMItr.next(); if(aTM.getATMID().equals(ATMID)) { return aTM; } } } return null; } } class UnionPay { private ArrayList<Bank> bankList = new ArrayList<Bank>(); public UnionPay() { super(); // TODO Auto-generated constructor stub } public UnionPay(ArrayList<Bank> bankList) { super(); this.bankList = bankList; } public ArrayList<Bank> getBankList() { return bankList; } public void setBankList(ArrayList<Bank> bankList) { this.bankList = bankList; } public void addBank(Bank bank) { this.bankList.add(bank); } public void removeBank(Bank bank) { this.bankList.remove(bank); } } class GetBalance { private UnionPay unionPay; public GetBalance() { super(); // TODO Auto-generated constructor stub } public GetBalance(UnionPay unionPay) { super(); this.unionPay = unionPay; } public double getBalance(String cardNO) { return ValidateData.getCardbyCardNO(unionPay, cardNO).getAccount().getBalance(); } }
3,采坑心得:
4,改进建议:
5,总结: