1.前言
这三次的题目集题目很少,只有四题,但难度剧增,属于爬坡阶段,题目集七要求我们掌握类的继承、多态性使用方法以及接口的应用,而题目集八则是考验类的设计,和以往不同的是,题目并没有直接给出类图,需要我们自己进行设计,题目集九则是在题目集八的基础上进行扩展,其实就是考验继承与多态,虽然过程比较困难,但还是在自己的努力下都拿到了满分。
2.设计与分析
(1)
题目集7(7-1)以及(7-2)的代码、类图、圈复杂度如下:
import java.util.ArrayList; import java.util.Scanner; import java.util.Collections; public class Main { //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接 //使用Main.input.next…即可(避免采坑) public static Scanner input = new Scanner(System.in); public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<Integer>(); int num = input.nextInt(); 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); if(!dealCardList.validate()){ System.out.println("Wrong Format"); System.exit(0); } dealCardList.showResult(); input.close(); } } class Card implements Comparable<Card>{ private Shape shape; public Card() { // TODO Auto-generated constructor stub } public int compareTo(Card card) { return -(int)(shape.getArea()-card.getShape().getArea()); } public Card(Shape shape) { this.shape=shape; } public Shape getShape(){ return shape; } public void setShape(Shape shape) { this.shape=shape; } } class Circle extends Shape{ private double radius; public Circle() { // TODO Auto-generated constructor stub } public Circle(double radius) { // TODO Auto-generated constructor stub this.radius=radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double getArea() { return Math.PI*radius*radius; } public boolean validate() { if(radius>0) { return true; } else { return false; } } } interface Comparable<Card> { public abstract int compareTo(Card card); } class DealCardList { ArrayList<Card> cardList=new ArrayList<Card>(); public DealCardList() { // TODO Auto-generated constructor stub } public DealCardList(ArrayList<Integer>list) { // TODO Auto-generated constructor stub for(int i=0;i<list.size();i++) { switch(list.get(i)) { case 1: double radius=Main.input.nextDouble(); Circle circle=new Circle(radius); Card card1=new Card(circle); card1.getShape().setShapeName("Circle"); cardList.add(card1); break; case 4: double topSide=Main.input.nextDouble(); double bottomSide=Main.input.nextDouble(); double height=Main.input.nextDouble(); Trapezoid trapezoid=new Trapezoid(topSide,bottomSide,height); Card card2=new Card(trapezoid); card2.getShape().setShapeName("Trapezoid"); cardList.add(card2); break; case 3: double side1=Main.input.nextDouble(); double side2=Main.input.nextDouble(); double side3=Main.input.nextDouble(); Triangle triangle=new Triangle(side1,side2,side3); Card card3=new Card(triangle); card3.getShape().setShapeName("Triangle"); cardList.add(card3); break; case 2: double width=Main.input.nextDouble(); double length=Main.input.nextDouble(); Rectangle rectangle=new Rectangle(width,length); Card card4=new Card(rectangle); card4.getShape().setShapeName("Rectangle"); cardList.add(card4); break; } } } public boolean validate() { for(int i=0;i<cardList.size();i++) { if(!cardList.get(i).getShape().validate()) { return false; } } return true; } public double getAllArea() { double AllArea=0; for(int i=0;i<cardList.size();i++) { AllArea=AllArea+cardList.get(i).getShape().getArea(); } return AllArea; } public void cardSort() { for(int k=0;k<cardList.size();k++) for(int i=k+1;i<cardList.size();i++) { if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea()) Collections.swap(cardList, k, i); } } public void showResult() { System.out.println("The original list:"); for(int i=0;i<cardList.size();i++) System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" "); System.out.println(); System.out.println("The sorted list:"); cardSort(); for(int i=0;i<cardList.size();i++) System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" "); System.out.println(); System.out.println("Sum of area:"+String.format("%.2f",getAllArea())); } } class Rectangle extends Shape{ private double width; private double length; public Rectangle() { // TODO Auto-generated constructor stub } public Rectangle(double width,double length) { // TODO Auto-generated constructor stub this.length=length; this.width=width; } 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; } public double getArea() { return width*length; } public boolean validate() { if(width>0&&length>0) { return true; } else { return false; } } } abstract class Shape { private String shapeName; public Shape() { // TODO Auto-generated constructor stub } public Shape(String shapeName) { // TODO Auto-generated constructor stub this.shapeName=shapeName; } public String getShapeName() { return shapeName; } public void setShapeName(String shapeName) { this.shapeName = shapeName; } public abstract double getArea(); public abstract boolean validate(); public String toString() { return getShapeName()+":"+String.format("%.2f ",getArea()); } } class Trapezoid extends Shape { private double topSide; private double bottomSide; private double height; public Trapezoid() { // TODO Auto-generated constructor stub } public Trapezoid(double topSide,double bottomSide,double height) { // TODO Auto-generated constructor stub this.bottomSide=bottomSide; this.topSide=topSide; this.height=height; } public double getArea() { return (topSide+bottomSide)*height/2; } public boolean validate() { if(topSide>0&&bottomSide>0&&height>0) { return true; } else { return false; } } } class Triangle extends Shape { private double side1; private double side2; private double side3; public Triangle() { // TODO Auto-generated constructor stub } public Triangle(double side1,double side2,double side3) { // TODO Auto-generated constructor stub this.side1=side1; this.side2=side2; this.side3=side3; } public double getArea() { double p = (side1+side2+side3)/2; double Area=Math.sqrt(p*(p-side1)*(p-side2)*(p-side3)); return Area; } public boolean validate() { if(side1>0&&side2>0&&side3>0&&(side1+side2>side3)&&(side2+side3>side1)&&(side1+side3>side2)&&(Math.abs(side1-side2)<side3)&&(Math.abs(side1-side3)<side2)&&(Math.abs(side2-side3)<side1)) { return true; } else { return false; } } }
import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; public class Main { //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接 //使用Main.input.next…即可(避免采坑) public static Scanner input = new Scanner(System.in); public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<Integer>(); 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); if(!dealCardList.validate()){ System.out.println("Wrong Format"); System.exit(0); } dealCardList.showResult(); input.close(); } } class Card implements Comparable<Card>{ private Shape shape; public Card() { // TODO Auto-generated constructor stub } public int compareTo(Card card) { return (int)(-shape.getArea()+card.getShape().getArea()); } public Card(Shape shape) { this.shape=shape; } public Shape getShape(){ return shape; } public void setShape(Shape shape) { this.shape=shape; } } class Circle extends Shape{ private double radius; public Circle() { // TODO Auto-generated constructor stub } public Circle(double radius) { // TODO Auto-generated constructor stub this.radius=radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double getArea() { return Math.PI*radius*radius; } public boolean validate() { if(radius>0) { return true; } else { return false; } } } class DealCardList { ArrayList<Card> cardList=new ArrayList<Card>(); ArrayList<Card> circleList=new ArrayList<Card>(); ArrayList<Card> trapezoidList=new ArrayList<Card>(); ArrayList<Card> triangleList=new ArrayList<Card>(); ArrayList<Card> rectangleList=new ArrayList<Card>(); public DealCardList() { // TODO Auto-generated constructor stub } public DealCardList(ArrayList<Integer>list) { // TODO Auto-generated constructor stub for(int i=0;i<list.size();i++) { switch(list.get(i)) { case 1: double radius=Main.input.nextDouble(); Circle circle=new Circle(radius); Card card1=new Card(circle); card1.getShape().setShapeName("Circle"); circleList.add(card1); cardList.add(card1); break; case 4: double topSide=Main.input.nextDouble(); double bottomSide=Main.input.nextDouble(); double height=Main.input.nextDouble(); Trapezoid trapezoid=new Trapezoid(topSide,bottomSide,height); Card card2=new Card(trapezoid); card2.getShape().setShapeName("Trapezoid"); trapezoidList.add(card2); cardList.add(card2); break; case 3: double side1=Main.input.nextDouble(); double side2=Main.input.nextDouble(); double side3=Main.input.nextDouble(); Triangle triangle=new Triangle(side1,side2,side3); Card card3=new Card(triangle); card3.getShape().setShapeName("Triangle"); triangleList.add(card3); cardList.add(card3); break; case 2: double width=Main.input.nextDouble(); double length=Main.input.nextDouble(); Rectangle rectangle=new Rectangle(width,length); Card card4=new Card(rectangle); card4.getShape().setShapeName("Rectangle"); rectangleList.add(card4); cardList.add(card4); break; } } } public boolean validate() { for(int i=0;i<cardList.size();i++) { if(!cardList.get(i).getShape().validate()) { return false; } } return true; } public double getAllArea() { double AllArea=0; for(int i=0;i<cardList.size();i++) { AllArea=AllArea+cardList.get(i).getShape().getArea(); } return AllArea; } public double getmaxArea() { double AllcircleArea=0; for(int i=0;i<circleList.size();i++) { AllcircleArea=AllcircleArea+circleList.get(i).getShape().getArea(); } double AllrectangleArea=0; for(int i=0;i<rectangleList.size();i++) { AllrectangleArea=AllrectangleArea+rectangleList.get(i).getShape().getArea(); } double AlltriangleArea=0; for(int i=0;i<triangleList.size();i++) { AlltriangleArea=AlltriangleArea+triangleList.get(i).getShape().getArea(); } double AlltrapezoidArea=0; for(int i=0;i<trapezoidList.size();i++) { AlltrapezoidArea=AlltrapezoidArea+trapezoidList.get(i).getShape().getArea(); } double[] arr = { AllcircleArea,AllrectangleArea, AlltriangleArea, AlltrapezoidArea}; double s=0; for(int k = 0; k < arr.length; k++) { s = s > arr[k] ? s : arr[k]; } return s; } public void cardSort() { Collections.sort(circleList); Collections.sort(rectangleList); Collections.sort(triangleList); Collections.sort(trapezoidList); } /* public void cardSort() { for(int k=0;k<cardList.size();k++) for(int i=k+1;i<cardList.size();i++) { if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea()) Collections.swap(cardList, k, i); } }*/ public void showResult() { System.out.println("The original list:"); System.out.print("["); for(int i=0;i<cardList.size();i++) System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" "); System.out.print("]"); System.out.println(); System.out.println("The Separated List:"); System.out.print("["); for(int i=0;i<circleList.size();i++) System.out.print(circleList.get(i).getShape().getShapeName()+":"+String.format("%.2f",circleList.get(i).getShape().getArea())+" "); System.out.print("]"); System.out.print("["); for(int i=0;i<rectangleList.size();i++) System.out.print(rectangleList.get(i).getShape().getShapeName()+":"+String.format("%.2f",rectangleList.get(i).getShape().getArea())+" "); System.out.print("]"); System.out.print("["); for(int i=0;i<triangleList.size();i++) System.out.print(triangleList.get(i).getShape().getShapeName()+":"+String.format("%.2f",triangleList.get(i).getShape().getArea())+" "); System.out.print("]"); System.out.print("["); for(int i=0;i<trapezoidList.size();i++) System.out.print(trapezoidList.get(i).getShape().getShapeName()+":"+String.format("%.2f",trapezoidList.get(i).getShape().getArea())+" "); System.out.print("]"); System.out.println(); System.out.println("The Separated sorted List:"); cardSort(); System.out.print("["); for(int i=0;i<circleList.size();i++) System.out.print(circleList.get(i).getShape().getShapeName()+":"+String.format("%.2f",circleList.get(i).getShape().getArea())+" "); System.out.print("]"); System.out.print("["); for(int i=0;i<rectangleList.size();i++) System.out.print(rectangleList.get(i).getShape().getShapeName()+":"+String.format("%.2f",rectangleList.get(i).getShape().getArea())+" "); System.out.print("]"); System.out.print("["); for(int i=0;i<triangleList.size();i++) System.out.print(triangleList.get(i).getShape().getShapeName()+":"+String.format("%.2f",triangleList.get(i).getShape().getArea())+" "); System.out.print("]"); System.out.print("["); for(int i=0;i<trapezoidList.size();i++) System.out.print(trapezoidList.get(i).getShape().getShapeName()+":"+String.format("%.2f",trapezoidList.get(i).getShape().getArea())+" "); System.out.print("]"); System.out.println(); /* for(int i=0;i<cardList.size();i++) System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" "); System.out.println();*/ System.out.println("The max area:"+String.format("%.2f",getmaxArea())); } } class Rectangle extends Shape{ private double width; private double length; public Rectangle() { // TODO Auto-generated constructor stub } public Rectangle(double width,double length) { // TODO Auto-generated constructor stub this.length=length; this.width=width; } 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; } public double getArea() { return width*length; } public boolean validate() { if(width>0&&length>0) { return true; } else { return false; } } } abstract class Shape { private String shapeName; public Shape() { // TODO Auto-generated constructor stub } public Shape(String shapeName) { // TODO Auto-generated constructor stub this.shapeName=shapeName; } public String getShapeName() { return shapeName; } public void setShapeName(String shapeName) { this.shapeName = shapeName; } public abstract double getArea(); public abstract boolean validate(); public String toString() { return getShapeName()+":"+String.format("%.2f ",getArea()); } } class Trapezoid extends Shape { private double topSide; private double bottomSide; private double height; public Trapezoid() { // TODO Auto-generated constructor stub } public Trapezoid(double topSide,double bottomSide,double height) { // TODO Auto-generated constructor stub this.bottomSide=bottomSide; this.topSide=topSide; this.height=height; } public double getArea() { return (topSide+bottomSide)*height/2; } public boolean validate() { if(topSide>0&&bottomSide>0&&height>0) { return true; } else { return false; } } } class Triangle extends Shape { private double side1; private double side2; private double side3; public Triangle() { // TODO Auto-generated constructor stub } public Triangle(double side1,double side2,double side3) { // TODO Auto-generated constructor stub this.side1=side1; this.side2=side2; this.side3=side3; } public double getArea() { double p = (side1+side2+side3)/2; double Area=Math.sqrt(p*(p-side1)*(p-side2)*(p-side3)); return Area; } public boolean validate() { if(side1>0&&side2>0&&side3>0&&(side1+side2>side3)&&(side2+side3>side1)&&(side1+side3>side2)&&(Math.abs(side1-side2)<side3)&&(Math.abs(side1-side3)<side2)&&(Math.abs(side2-side3)<side1)) { return true; } else { return false; } } }



题目集7(7-1)、(7-2)两道题目的递进式设计分析总结:
从上面的类图不难看出,这种递进式设计的特点,就是由抽象到具体,就拿本题举例,卡牌游戏,就先把卡作为一个实体类,而卡里面有图像类型,图像类型是不确定的,就作为一个抽象类,并且卡中包含图像类型这个属性,再根据有三角形,梯形,正方形,圆形,依次做成实体类,而它们均是继承与图像类,就是它的子类,最后在做一个deal类处理数据,特别的情况可以根据题目要求向卡类加接口,例如本题的Comparable,这就是递进式设计的一个具体思路,从抽象到具体。
(2)
题目集八(8-1)以及题目集9(9-1)的代码、类图、圈复杂度如下:
import java.util.Iterator; import java.util.ListIterator; import java.util.Scanner; import java.util.ArrayList; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub ChinaUnionPay chinaUnionPay=new ChinaUnionPay(); Bank gsbank=new Bank("中国工商银行"); Bank jsbank=new Bank("中国建设银行"); chinaUnionPay.addBank(gsbank); chinaUnionPay.addBank(jsbank); ATM atm1=new ATM("01"); ATM atm2=new ATM("02"); ATM atm3=new ATM("03"); ATM atm4=new ATM("04"); ATM atm5=new ATM("05"); ATM atm6=new ATM("06"); jsbank.addATM(atm1); jsbank.addATM(atm2); jsbank.addATM(atm3); jsbank.addATM(atm4); gsbank.addATM(atm5); gsbank.addATM(atm6); User user1=new User("杨过"); User user2=new User("郭靖"); User user3=new User("张无忌"); User user4=new User("韦小宝"); jsbank.addUser(user1); jsbank.addUser(user2); gsbank.addUser(user3); gsbank.addUser(user4); Account account1=new Account("3217000010041315709"); Account account2=new Account("3217000010041315715"); Account account3=new Account("3217000010051320007"); Account account4=new Account("3222081502001312389"); Account account5=new Account("3222081502001312390"); Account account6=new Account("3222081502001312399"); Account account7=new Account("3222081502001320785"); Account account8=new Account("3222081502051320786"); user1.addAccount(account1); user1.addAccount(account2); user2.addAccount(account3); user3.addAccount(account4); user3.addAccount(account5); user3.addAccount(account6); user4.addAccount(account7); user4.addAccount(account8); Card card1=new Card("6217000010041315709"); Card card2=new Card("6217000010041315715"); Card card3=new Card("6217000010041315718"); Card card4=new Card("6217000010051320007"); Card card5=new Card("6222081502001312389"); Card card6=new Card("6222081502001312390"); Card card7=new Card("6222081502001312399"); Card card8=new Card("6222081502001312400"); Card card9=new Card("6222081502051320785"); Card card10=new Card("6222081502051320786"); account1.addCard(card1); account1.addCard(card2); account2.addCard(card3); account3.addCard(card4); account4.addCard(card5); account5.addCard(card6); account6.addCard(card7); account6.addCard(card8); account7.addCard(card9); account8.addCard(card10); Scanner input=new Scanner(System.in); String mark = "#"; StringBuilder k = new StringBuilder(); String k1 = input.nextLine(); while(k1!=null) { if(k1.equals("#")) { break; } k.append(k1); k.append('\n'); k1 = input.nextLine(); } String k2 = k.toString().replaceAll(" +"," "); String[] a=k2.split("\n"); for(int b=a.length,i=0;b>0;b--,i++){ String[] c=a[i].split(" "); String answer = "88888888"; boolean findcard = true; boolean right = true; String account = c[0]; String password = null; if(c.length==4) { password = c[1]; } else { password = "no"; } //System.out.println(account); if(password.equals("no")) { Iterator <Bank> bankltr=chinaUnionPay.getBanklist().iterator(); while(bankltr.hasNext()) { Iterator <User> userltr=bankltr.next().getUserlist().iterator(); while(userltr.hasNext()) { ListIterator <Account> accountltr=userltr.next().getAccountlist().listIterator(); while(accountltr.hasNext()) { Iterator <Card> cardltr=accountltr.next().getCardlist().iterator(); while(cardltr.hasNext()) { if(account.equals(cardltr.next().getNumber())) { System.out.print("¥"); String s=accountltr.previous().getNumber(); System.out.printf("%.2f",accountltr.next().getMoney()); findcard=false; System.out.print("\n"); } } } } } if(findcard) { System.out.print("Sorry,this card does not exist."); System.exit(0); } } else { if(password.equals(answer)) { String number = c[2]; String money4 = c[3]; double money = Double.valueOf(money4); boolean findATM = true; ListIterator <Bank> bankltr=chinaUnionPay.getBanklist().listIterator(); while(bankltr.hasNext()) { ListIterator <ATM> atmltr=bankltr.next().getATMlist().listIterator(); while(atmltr.hasNext()){ if(atmltr.next().getNumber().equals(number)) { findATM=false; String s = bankltr.previous().getName(); ListIterator <User> userltr=bankltr.next().getUserlist().listIterator(); while(userltr.hasNext()) { ListIterator <Account> accountltr=userltr.next().getAccountlist().listIterator(); while(accountltr.hasNext()) { ListIterator <Card> cardltr=accountltr.next().getCardlist().listIterator(); while(cardltr.hasNext()) { if(account.equals(cardltr.next().getNumber())) { right=false; double s1 = accountltr.previous().getMoney(); double money1 = accountltr.next().getMoney(); double s2 = accountltr.previous().getMoney(); accountltr.next().setMoney(money1-money); String name = userltr.previous().getName(); String s3 = bankltr.previous().getName(); if(money>0) { if(money1-money<0) { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } System.out.print(userltr.next().getName()+"在"+bankltr.next().getName()+"的"+number+"号ATM机上取款¥"); System.out.printf("%.2f", money); System.out.print("\n"); System.out.print("当前余额为¥"); System.out.printf("%.2f", money1-money); } else { System.out.print(userltr.next().getName()+"在"+bankltr.next().getName()+"的"+number+"号ATM机上存款¥"); System.out.printf("%.2f", -money); System.out.print("\n"); System.out.print("当前余额为¥"); System.out.printf("%.2f", money1-money); } } } } } } } ListIterator <Bank> allbankltr=chinaUnionPay.getBanklist().listIterator(); while(allbankltr.hasNext()) { Iterator <User> alluserltr=allbankltr.next().getUserlist().iterator(); while(alluserltr.hasNext()) { Iterator <Account> allaccountltr=alluserltr.next().getAccountlist().iterator(); while(allaccountltr.hasNext()) { Iterator <Card> allcardltr=allaccountltr.next().getCardlist().iterator(); while(allcardltr.hasNext()) { if(account.equals(allcardltr.next().getNumber())) { findcard=false; } } } } } } if(findcard) { System.out.print("Sorry,this card does not exist."); System.exit(0); } if(findATM) { System.out.print("Sorry,the ATM's id is wrong."); System.exit(0); } if(right) { System.out.print("Sorry,cross-bank withdrawal is not supported."); System.exit(0); } } else { System.out.print("Sorry,your password is wrong."); System.exit(0); } System.out.print("\n"); } } } } class ChinaUnionPay { private ArrayList<Bank> Banklist=new ArrayList<Bank>(); public ChinaUnionPay() { // TODO Auto-generated constructor stub } public void addBank(Bank bank) { Banklist.add(bank); } public void removeBank(Bank bank) { Banklist.remove(bank); } public ArrayList<Bank> getBanklist() { return Banklist; } public void setBanklist(ArrayList<Bank> banklist) { Banklist = banklist; } } class User { private String name; private ArrayList<Account> Accountlist=new ArrayList<Account>(); public User() { super(); // TODO Auto-generated constructor stub } public User(String name) { this.name=name; } public void remove(Account account){ Accountlist.remove(account); } public void addAccount(Account account) { Accountlist.add(account); } public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList<Account> getAccountlist() { return Accountlist; } public void setAccountlist(ArrayList<Account> accountlist) { Accountlist = accountlist; } } class Card { private String number; public Card() { // TODO Auto-generated constructor stub } public Card(String number) { this.number=number; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } } class Bank { private String name; private ArrayList<User> Userlist=new ArrayList<User>(); private ArrayList<ATM> ATMlist=new ArrayList<ATM>(); public Bank() { super(); // TODO Auto-generated constructor stub } public Bank(String name) { this.name=name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void addATM(ATM atm) { ATMlist.add(atm); } public void remove(ATM atm){ ATMlist.remove(atm); } public void addUser(User user) { Userlist.add(user); } public void remove(User user) { Userlist.remove(user); } public ArrayList<User> getUserlist() { return Userlist; } public void setUserlist(ArrayList<User> userlist) { Userlist = userlist; } public ArrayList<ATM> getATMlist() { return ATMlist; } public void setATMlist(ArrayList<ATM> aTMlist) { ATMlist = aTMlist; } } class ATM { private String number; public ATM() { super(); // TODO Auto-generated constructor stub } public ATM(String number) { this.number=number; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } } class Account { private double money=10000; private String number; private ArrayList<Card> Cardlist=new ArrayList<Card>(); public Account() { super(); // TODO Auto-generated constructor stub } public Account(String number) { this.number=number; } public void addCard(Card card) { Cardlist.add(card); } public void remove(Card card) { Cardlist.remove(card); } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public ArrayList<Card> getCardlist() { return Cardlist; } public void setCardlist(ArrayList<Card> cardlist) { Cardlist = cardlist; } }
import java.util.Scanner; import java.util.ArrayList; import java.util.Iterator; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { 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 zhangsangfeng = new User("360201200513243325","张三丰","13025946587"); User linghuchong = new User("360201200513243324","令狐冲","13025996587"); User qiaofeng = new User("360201200513243323","乔峰","13025596587"); User hongqigong =new User("360201200513243322","洪七公","13015996587"); Account ccbAcc1 = new DebitAccount("3217000010041315709",10000.00,Yangguo,ccb); Account ccbAcc2 = new DebitAccount("3217000010041315715",10000.00,Yangguo,ccb); Account ccbAcc3 = new DebitAccount("3217000010051320007",10000.00,Guojing,ccb); Account icbcAcc1 = new DebitAccount("3222081502001312389",10000.00,Zhangwuji,icbc); Account icbcAcc2 = new DebitAccount("3222081502001312390",10000.00,Zhangwuji,icbc); Account icbcAcc3 = new DebitAccount("3222081502001312399",10000.00,Zhangwuji,icbc); Account icbcAcc4 = new DebitAccount("3222081502051320785",10000.00,Weixiaobao,icbc); Account icbcAcc5 = new DebitAccount("3222081502051320786",10000.00,Weixiaobao,icbc); Account ccbAcc4 = new CreditAccount("3640000010045442002",10000.00,zhangsangfeng,ccb); Account icbcAcc6 = new CreditAccount("3640000010045441009",10000.00,linghuchong,icbc); Account abcAcc1 = new CreditAccount("3630000010033431001",10000.00,qiaofeng,abc); Account abcAcc2 = new CreditAccount("3630000010033431008",10000.00,hongqigong,abc); 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); abc.addAccount(abcAcc2); Yangguo.addAccount(ccbAcc1); Yangguo.addAccount(ccbAcc2); Guojing.addAccount(ccbAcc3); Zhangwuji.addAccount(icbcAcc1); Zhangwuji.addAccount(icbcAcc2); Zhangwuji.addAccount(icbcAcc3); Weixiaobao.addAccount(icbcAcc4); Weixiaobao.addAccount(icbcAcc5); zhangsangfeng.addAccount(ccbAcc4); linghuchong.addAccount(icbcAcc6); qiaofeng.addAccount(abcAcc1); hongqigong.addAccount(abcAcc2); 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 ccbCard11 = new Card("6640000010045442002","88888888",ccbAcc4); Card ccbCard12 = new Card("6640000010045442003","88888888",ccbAcc4); Card icbcCard13 = new Card("6640000010045441009","88888888",icbcAcc6); Card abcCard14 = new Card("6630000010033431001","88888888",abcAcc1); Card abaCard15 = new Card("6630000010033431008","88888888",abcAcc2); ccbAcc1.addCard(ccbCard1); ccbAcc1.addCard(ccbCard2); ccbAcc2.addCard(ccbCard3); ccbAcc3.addCard(ccbCard4); ccbAcc4.addCard(ccbCard11); ccbAcc4.addCard(ccbCard12); icbcAcc1.addCard(icbcCard5); icbcAcc2.addCard(icbcCard6); icbcAcc3.addCard(icbcCard7); icbcAcc3.addCard(icbcCard8); icbcAcc4.addCard(icbcCard9); icbcAcc5.addCard(icbcCard10); icbcAcc6.addCard(icbcCard13); abcAcc1.addCard(abcCard14); abcAcc2.addCard(abaCard15); StringBuilder sb = new StringBuilder(); String data; while(!((data = input.nextLine()).equals("#"))) { sb.append(data + "\n"); } String[] dt = sb.toString().split("\n"); for(int i = 0; i < dt.length; i++) { String[] dataLine = dt[i].toString().split("\\s+"); if(dataLine.length == 1) { GetBalance gb = new GetBalance(unionPay); System.out.print("业务:查询余额 "); 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(); } } } } abstract class Account { private String accountNO; private double balance = 0; private User user = null; private Bank bank = null; private static ArrayList<Card> list = new ArrayList<Card>(); public Account() { super(); // TODO Auto-generated constructor stub } public Account(String accountNO, double balance, User user, Bank bank) { super(); this.accountNO = accountNO; this.balance = balance; this.user = user; this.bank = bank; } 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 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; } public abstract String getType(); } 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 Bank { private String bankNO; private String bankName; private ArrayList<Account> accountList = new ArrayList<Account>(); private ArrayList<ATM> ATMList = new ArrayList<ATM>(); public Bank() { super(); // TODO Auto-generated constructor stub } public Bank(String bankNO, String bankName) { super(); this.bankNO = bankNO; this.bankName = bankName; } 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 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 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); } public String getCardNO() { return cardNO; } public void setCardNO(String cardNO) { this.cardNO = cardNO; } public String getCardPassword() { return cardPassword; } public void setCardPassword(String cardPassword) { this.cardPassword = cardPassword; } public Account getAccount() { return account; } public void setAccount(Account account) { this.account = account; } } class CreditAccount extends Account { private String accountNO; private double balance = 0; private User user = null; private Bank bank = null; public CreditAccount() { // TODO Auto-generated constructor stub } public CreditAccount(String accountNO, double balance, User user, Bank bank) { super(); this.accountNO = accountNO; this.balance = balance; this.user = user; this.bank = bank; } public String getAccountNO() { return accountNO; } public void setAccountNO(String accountNO) { this.accountNO = accountNO; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } 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 String getType() { return "Credit"; } } class DebitAccount extends Account { private String accountNO; private double balance = 0; private User user = null; private Bank bank = null; public DebitAccount() { // TODO Auto-generated constructor stub } public DebitAccount(String accountNO, double balance, User user, Bank bank) { super(); this.accountNO = accountNO; this.balance = balance; this.user = user; this.bank = bank; } public String getAccountNO() { return accountNO; } public void setAccountNO(String accountNO) { this.accountNO = accountNO; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } 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 String getType() { return "Debit"; } } 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(); } } 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 User { private String ID; private String name; private String Phone; ArrayList<Account> list = new ArrayList<Account>(); 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 ValidateData { 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; } 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 Withdraw { private UnionPay unionPay; private String cardNO; private String cardPassword; private String ATMID; private double amount; private String Type; private double tax=0; private String Bankname; private double part; 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); } Bankname=aTM.getBank().getBankName(); Account account = Account.getAmountbyCardNO(cardNO); double balance = account.getBalance(); Type=account.getType(); if(balance>0) { part=amount-balance; } else { part=amount; } /** * 校验卡密码是否正确 */ if(!card.getCardPassword().equals(cardPassword)) { System.out.println("Sorry,your password is wrong."); System.exit(0); } /** * 校验取款金额是否大于余额 */ if (amount > 0) { tax=0; if (account.getBank().getBankNO() != aTM.getBank().getBankNO()) { if(aTM.getBank().getBankName().equals("中国建设银行")) { tax=0.02; } else if(aTM.getBank().getBankName().equals("中国工商银行")) { tax=0.03; } else { tax=0.04; } if(amount+amount*tax>balance) { if(Type.equals("Credit")) { if((amount+part*0.05+amount*tax)<(50000+balance)) { account.setBalance(balance - amount-part*0.05-amount*tax); } else { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } } else { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } } else { account.setBalance(balance - amount-amount*tax); } } else { if(amount>balance) { if(Type.equals("Credit")) { if((amount+part*0.05)<(50000+balance)) { account.setBalance(balance - amount-part*0.05); } else { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } } else { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } } else { account.setBalance(balance - amount); } } } else { account.setBalance(balance - amount); } if(amount >= 0) { showResult(account,1); }else { showResult(account,0); } } public void showResult(Account account,int flag) { String type = ""; if(flag == 1) { type = "取款"; }else { type = "存款"; amount *= -1; } String userName = account.getUser().getName(); System.out.print("业务:"+type+" "); System.out.println(userName + "在" + Bankname + "的" + ATMID + "号ATM机上" + type + String.format("¥%.2f", amount)); System.out.println("当前余额为" + String.format("¥%.2f", account.getBalance())); } }





题目集八并没有 给出类图,所以需要我们自己进行类设计,我的思路其实很简单,首先就是按照单一职责原则把所有的实体类做出来,一共有银联、银行、账户、用户、卡、ATM机六个实体类,理清它们之间的关系,银联很简单只有一个,它包含着银行,就把银行作为它的属性,而银行中有着账户和ATM机,就再把账户和ATM机作为银行的属性,账户又拥有卡和用户,就接着把卡和用户作为它的属性,最后在根据要求增加业务类来处理题目的要求,整体的思路就是如此,根据生活中的常识来进行类的设计,题目集九中是在老师给的代码的基础上进行修改,题目集九就多了个跨行取款和透支取款的手续费问题,这只需要把账户做成抽象类,而它的子类就是有两个,贷记账户和借记账户,在把业务类的功能稍加修改,就完成了题目集九。
3.踩坑心得
1.在9-1的题目集中对计算余额出了些错误,首先是对透支取款的方法判断出了问题,一开始把超出部分需要的手续费记为(amount-balance)*0.05,这样的写法造成了在余额为负数的时候就出现了错误,所以需要分情况讨论,还有一个错误就是在跨行且透支取款时的手续费问题,一开始以为只是透支的百分之5,后来才知道是透支的百分之五再加上跨行的手续费,最终我的算法如下:
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); } Bankname=aTM.getBank().getBankName(); Account account = Account.getAmountbyCardNO(cardNO); double balance = account.getBalance(); Type=account.getType(); if(balance>0) { part=amount-balance; } else { part=amount; } /** * 校验卡密码是否正确 */ if(!card.getCardPassword().equals(cardPassword)) { System.out.println("Sorry,your password is wrong."); System.exit(0); } /** * 校验取款金额是否大于余额 */ if (amount > 0) { tax=0; if (account.getBank().getBankNO() != aTM.getBank().getBankNO()) { if(aTM.getBank().getBankName().equals("中国建设银行")) { tax=0.02; } else if(aTM.getBank().getBankName().equals("中国工商银行")) { tax=0.03; } else { tax=0.04; } if(amount+amount*tax>balance) { if(Type.equals("Credit")) { if((amount+part*0.05+amount*tax)<(50000+balance)) { account.setBalance(balance - amount-part*0.05-amount*tax); } else { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } } else { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } } else { account.setBalance(balance - amount-amount*tax); } } else { if(amount>balance) { if(Type.equals("Credit")) { if((amount+part*0.05)<(50000+balance)) { account.setBalance(balance - amount-part*0.05); } else { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } } else { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } } else { account.setBalance(balance - amount); } } } else { account.setBalance(balance - amount); } if(amount >= 0) { showResult(account,1); }else { showResult(account,0); } }
2.第二个踩坑其实是个小问题,但也不容忽略

这是题目对输入有误的要求,在发现错误就要立即停止运行,就等于在报错的语句后面加上System.exit(0);使程序强制停止,我再一开始就忘了这一点,直到测试点没过时才发现是这里出了错,最后我的改正如下:

3.迭代器问题,在一开始做ATM类设计时,由于缺乏经验,没有类中对象做成双向的,导致用迭代器时,显得非常麻烦,因为这个迭代器再找到你想要的东西时它就会自动往下跳一个,当你想要读取它时,它已经不是你要找的那个东西了,所以我在一开始提交时就报错,数组越界,后来我把Iterator换成了ListIterator,这个Listlterator比lterator多了向前遍历的功能,就等于我在找到我想要的东西后,我让它向前遍历一遍,就刚好抵消了之前的向后遍历,虽然整体看起来有点繁琐,但是也解决了这个问题,最终修改后我的代码如下:
Iterator <Bank> bankltr=chinaUnionPay.getBanklist().iterator(); while(bankltr.hasNext()) { Iterator <User> userltr=bankltr.next().getUserlist().iterator(); while(userltr.hasNext()) { ListIterator <Account> accountltr=userltr.next().getAccountlist().listIterator(); while(accountltr.hasNext()) { Iterator <Card> cardltr=accountltr.next().getCardlist().iterator(); while(cardltr.hasNext()) { if(account.equals(cardltr.next().getNumber())) { System.out.print("¥"); String s=accountltr.previous().getNumber(); System.out.printf("%.2f",accountltr.next().getMoney()); findcard=false; }
4.对象数组的排序问题,在题目集八中需要对图形按面积大小进行排序,一开始想的很简单,直接用冒泡排序排,但后来发现这只是把面积值进行了排序,并没有把对象数组排序,再后来就用了集合中的方法进行冒泡排序,最后的代码如下:
public void cardSort() { for(int k=0;k<cardList.size();k++) for(int i=k+1;i<cardList.size();i++) { if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea()) Collections.swap(cardList, k, i); }
虽然这成功解决了对象数组的排序,但其实还有更好的方法,就是用comparable接口,这一点的使用就写在后面的
改进建议中
4.改进建议
1.首先就是对题目集八中对象数组排序方法的改进,当时我用的方法为:
public void cardSort() { for(int k=0;k<cardList.size();k++) for(int i=k+1;i<cardList.size();i++) { if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea()) Collections.swap(cardList, k, i); }
就是一个冒泡排序,但其实利用comparable接口可以更轻松的解决这个问题,我改进后的代码如下:
先是重写这个接口方法
public int compareTo(Card card) { return (int)(-shape.getArea()+card.getShape().getArea()); }
在把接口重写后就只需要用Collections.sort就可以将对象数组排序了
public void cardSort() { Collections.sort(circleList); Collections.sort(rectangleList); Collections.sort(triangleList); Collections.sort(trapezoidList); }
非常的简洁
2.接下来就是为了解决遍历器的单向遍历问题,需要在类中设计双向的指针,例如在账户中既要有卡,也要有银行
private String accountNO;
private double balance = 0;
private User user = null;
private Bank bank = null;
private static ArrayList<Card> list = new ArrayList<Card>();
这样在遍历中就会非常轻松了
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;
}
5.总结
写完这三次题目集之后,给我的帮助非常大,首先最直接的就是类的设计,原来都是题目给出类图,自己照葫芦画瓢,一步一步按题目要求写,而这次确需要自己动脑设计,确实是一个不小的挑战,另外就是在写代码过程中学到了许多以前没见过的东西,比如在题目集八对对象数组排序时,一开始自己只会冒泡排序,后来才了解到要用上comparble接口进行排序,还有就是ATM中找对象用到的迭代器,比循环简洁清晰,但也存在很多不足,题目提交后拿自己的代码与老师给的代码做对比后,也发现自己的设计有着许多不合理的地方,都需要自己日后加强学习。
浙公网安备 33010602011771号