SQL Server對比兩字串的相似度(函式演算法)
一、前言
這次blog主要是為了總結第7-9次pta作業的知識點和心得體會,這次總結的題目是最後三次作業的內容,學習java已經一學期,這次題目的難度對我來說很大,題目也更偏向實際應用,就要求我們更加全面、細節的考慮,程式碼的行數也突破到了幾百行,對我們的知識儲備和編碼能力來說都是極大的挑戰,雖然每次作業都只有一道題或兩道題目,但我幾乎都需要花費幾天的時間設計和寫出程式碼,再利用剩下的時間不斷除錯,更改細節問題。大量的時間和精力沒有白費,我得到了自己較滿意的成果,接下來是我對於這幾次題目的具體分析和完成情況以及遇到的問題和我是如何解決的。
二、設計與分析
①題目集7(7-1)、(7-2)兩道題目的遞進式設計分析總結
掌握類的繼承、多型性使用方法以及介面的應用。詳見作業指導書2020-OO第07次作業-1指導書V1.0.pdf
輸入格式:
- 首先,在一行上輸入一串數字(1~4,整數),其中,1代表圓形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各數字之間以一個或多個空格分隔,以“0”結束。例如:
1 3 4 2 1 3 4 2 1 3 0
- 然後根據第一行數字所代表的卡片圖形型別,依次輸入各圖形的相關引數,例如:圓形卡片需要輸入圓的半徑,矩形卡片需要輸入矩形的寬和長,三角形卡片需要輸入三角形的三條邊長,梯形需要輸入梯形的上底、下底以及高。各資料之間用一個或多個空格分隔。
輸出格式:
- 如果圖形數量非法(小於0)或圖形屬性值非法(數值小於0以及三角形三邊不能組成三角形),則輸出
Wrong Format
。 - 如果輸入合法,則正常輸出,所有數值計算後均保留小數點後兩位即可。輸出內容如下:
- 排序前的各圖形型別及面積,格式為
圖形名稱1:面積值1圖形名稱2:面積值2 …圖形名稱n:面積值n
,注意,各圖形輸出之間用空格分開,且輸出最後存在一個用於分隔的空格; - 排序後的各圖形型別及面積,格式同排序前的輸出;
- 所有圖形的面積總和,格式為
Sum of area:總面積值
。
輸入樣例1:
在這裡給出一組輸入。例如:
1 5 3 2 0
結尾無空行
輸出樣例1:
在這裡給出相應的輸出。例如:
Wrong Format
結尾無空行
輸入樣例2:
在這裡給出一組輸入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5
結尾無空行
輸出樣例2:
在這裡給出相應的輸出。例如:
The original list:
Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02
The sorted list:
Circle:98.52 Triangle:4.02 Rectangle:3.22 Trapezoid:1.14
Sum of area:106.91
結尾無空行
輸入樣例3:
在這裡給出一組輸入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 8.4
結尾無空行
輸出樣例3:
在這裡給出相應的輸出。例如:
Wrong Format
源程式:
import java.util.ArrayList; import java.util.Scanner; interface Comparable { } abstract class Shape { private String shapeName; public Shape() { super(); } // 無參構造方法 public Shape(String shapeName) { 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 abstract String toString(); // 輸出圖形的面積資訊 } class Circle extends Shape { private double radius = 0; public Circle() { super(); super.setShapeName("Circle"); } public Circle(double radius) { this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } @Override public boolean validate() { return (radius > 0 ? true : false); } @Override public String toString() { return null; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } } class Rectangle extends Shape { private double width; private double length; public Rectangle() { super(); super.setShapeName("Rectangle"); } public Rectangle(double width, double length) { this.width = width; this.length = length; } @Override public double getArea() { return width * length; } @Override public boolean validate() { return (width >= 0 && length >= 0 ? true : false); } @Override public String toString() { return null; } 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; } } class Triangle extends Shape { private double side1 = 0; private double side2 = 0; private double side3 = 0; public Triangle() { super(); super.setShapeName("Triangle"); } public Triangle(double side1, double side2, double side3) { this.side1 = side1; this.side2 = side2; this.side3 = side3; } @Override public double getArea() { return 0.25 * Math.sqrt( (side1 + side2 + side3) * (side1 + side2 - side3) * (side1 + side3 - side2) * (side2 + side3 - side1)); } @Override public boolean validate() { return ((side1 >= 0 && side2 >= 0 && side3 >= 0 && (side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1)) ? true : false); } @Override public String toString() { return null; } 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; } } class Trapezoid extends Shape { private double topSide; private double bottomSide; private double height; public Trapezoid() { super(); super.setShapeName("Trapezoid"); } public Trapezoid(double topSide, double bottomSide, double height) { this.topSide = topSide; this.bottomSide = bottomSide; this.height = height; } @Override public double getArea() { return (topSide + bottomSide) * height / 2; } @Override public boolean validate() { return (topSide >= 0 && bottomSide >= 0 && height >= 0 ? true : false); } @Override public String toString() { return null; } public double getTopSide() { return topSide; } public void setTopSide(double topSide) { this.topSide = topSide; } public double getBottomSide() { return bottomSide; } public void setBottomSide(double bottomSide) { this.bottomSide = bottomSide; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } } class Card implements Comparable { private Shape shape; public Card() { super(); } public Card(Shape shape) { this.shape = shape; } public Shape getShape() { return shape; } public void setShape(Shape shape) { this.shape = shape; } public int compareTo(Card card) { return 0; } } class DealCardList { ArrayList<Card> cardList = new ArrayList<Card>(); public DealCardList() { super(); } public DealCardList(ArrayList<Integer> list) { Card card; for(int i = 0; i < list.size();i++) { switch(list.get(i)) { case 1: Circle circle = new Circle(); circle.setRadius(Main.input.nextDouble()); if(!circle.validate()) { //判斷圓形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(circle); cardList.add(card); break; case 2: Rectangle rectangle = new Rectangle(); rectangle.setWidth(Main.input.nextDouble()); rectangle.setLength(Main.input.nextDouble()); if(!rectangle.validate()) { //判斷矩形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(rectangle); cardList.add(card); break; case 3: Triangle triangle = new Triangle(); triangle.setSide1(Main.input.nextDouble()); triangle.setSide2(Main.input.nextDouble()); triangle.setSide3(Main.input.nextDouble()); if(!triangle.validate()) { //判斷三角形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(triangle); cardList.add(card); break; case 4: Trapezoid trapezoid = new Trapezoid(); trapezoid.setTopSide(Main.input.nextDouble()); trapezoid.setBottomSide(Main.input.nextDouble()); trapezoid.setHeight(Main.input.nextDouble()); if(!trapezoid.validate()) { //判斷梯形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(trapezoid); cardList.add(card); break; default: System.out.println("Wrong Format"); System.exit(0); } } } public boolean validate() { return true; } public void cardSort() { for(int i=0;i<cardList.size();i++) { for(int j=i+1;j<cardList.size();j++) { if(cardList.get(i).getShape().getArea()<cardList.get(j).getShape().getArea()) java.util.Collections.swap(cardList, i, j); } } } public double getAllArea() { double sumArea = 0; for(int i = 0; i<cardList.size(); i++) { sumArea += cardList.get(i).getShape().getArea(); } return sumArea; } public void showResult() { int k = 0; System.out.println("The original list:"); for( k=0; k<cardList.size(); k++) { System.out.print(cardList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", cardList.get(k).getShape().getArea()) + " "); } System.out.println(); cardSort(); System.out.println("The sorted list:"); for(k=0; k<cardList.size(); k++) { System.out.print(cardList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", cardList.get(k).getShape().getArea()) + " "); } System.out.println(); System.out.println("Sum of area:" + String.format("%.2f", getAllArea())); } } public class Main { // 其它類中如果想要使用該物件進行輸入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(); } }結果測試:
結果正確
根據SourceMonitor的生成報表:
7-2 圖形卡片分組遊戲 (60 分)掌握類的繼承、多型性使用方法以及介面的應用。 具體需求參考作業指導書。
輸入格式:
- 在一行上輸入一串數字(1~4,整數),其中,1代表圓形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各數字之間以一個或多個空格分隔,以“0”結束。例如:
1 3 4 2 1 3 4 2 1 3 0
- 根據第一行數字所代表的卡片圖形型別,依次輸入各圖形的相關引數,例如:圓形卡片需要輸入圓的半徑,矩形卡片需要輸入矩形的寬和長,三角形卡片需要輸入三角形的三條邊長,梯形需要輸入梯形的上底、下底以及高。各資料之間用一個或多個空格分隔。
輸出格式:
- 如果圖形數量非法(<=0)或圖形屬性值非法(數值<0以及三角形三邊不能組成三角形),則輸出
Wrong Format
。 - 如果輸入合法,則正常輸出,所有數值計算後均保留小數點後兩位即可。輸出內容如下:
- 排序前的各圖形型別及面積,格式為
[圖形名稱1:面積值1圖形名稱2:面積值2 …圖形名稱n:面積值n ]
,注意,各圖形輸出之間用空格分開,且輸出最後存在一個用於分隔的空格,在結束符“]”之前; - 輸出分組後的圖形型別及面積,格式為
[圓形分組各圖形型別及面積][矩形分組各圖形型別及面積][三角形分組各圖形型別及面積][梯形分組各圖形型別及面積]
,各組內格式為圖形名稱:面積值
。按照“Circle、Rectangle、Triangle、Trapezoid”的順序依次輸出; - 各組內圖形排序後的各圖形型別及面積,格式同排序前各組圖形的輸出;
- 各組中面積之和的最大值輸出,格式為
The max area:面積值
。
輸入樣例1:
在這裡給出一組輸入。例如:
1 5 3 2 0
結尾無空行
輸出樣例1:
在這裡給出相應的輸出。例如:
Wrong Format
結尾無空行
輸入樣例2:
在這裡給出一組輸入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5
結尾無空行
輸出樣例2:
在這裡給出相應的輸出。例如:
The original list:
[Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02 ]
The Separated List:
[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]
The Separated sorted List:
[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]
The max area:98.52
結尾無空行
輸入樣例3:
在這裡給出一組輸入。例如:
2 1 2 1 1 3 3 4 4 1 1 1 2 1 0
2.3 3.5 2.5 4.5 2.1 2.6 8.5 3.2 3.1 3.6 8.5 7.5 9.1245 6.5 3.4 10.2 11.2 11.6 15.4 5.8 2.13 6.2011 2.5 6.4 18.65
結尾無空行
輸出樣例3:
在這裡給出相應的輸出。例如:
The original list:
[Rectangle:8.05 Circle:19.63 Rectangle:9.45 Circle:21.24 Circle:226.98 Triangle:4.65 Triangle:29.80 Trapezoid:50.49 Trapezoid:175.56 Circle:105.68 Circle:14.25 Circle:120.81 Rectangle:16.00 Circle:1092.72 ]
The Separated List:
[Circle:19.63 Circle:21.24 Circle:226.98 Circle:105.68 Circle:14.25 Circle:120.81 Circle:1092.72 ][Rectangle:8.05 Rectangle:9.45 Rectangle:16.00 ][Triangle:4.65 Triangle:29.80 ][Trapezoid:50.49 Trapezoid:175.56 ]
The Separated sorted List:
[Circle:1092.72 Circle:226.98 Circle:120.81 Circle:105.68 Circle:21.24 Circle:19.63 Circle:14.25 ][Rectangle:16.00 Rectangle:9.45 Rectangle:8.05 ][Triangle:29.80 Triangle:4.65 ][Trapezoid:175.56 Trapezoid:50.49 ]
The max area:1601.31
結尾無空行
輸入樣例4:
在這裡給出一組輸入。例如:
1 1 3 0
6.5 12.54 3.6 5.3 6.4
結尾無空行
輸出樣例4:
在這裡給出相應的輸出。例如:
The original list:
[Circle:132.73 Circle:494.02 Triangle:9.54 ]
The Separated List:
[Circle:132.73 Circle:494.02 ][][Triangle:9.54 ][]
The Separated sorted List:
[Circle:494.02 Circle:132.73 ][][Triangle:9.54 ][]
The max area:626.75
源程式:
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; interface Comparable { } abstract class Shape { private String shapeName; public Shape() { super(); } // 無參構造方法 public Shape(String shapeName) { 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 abstract String toString(); // 輸出圖形的面積資訊 } class Circle extends Shape { private double radius = 0; public Circle() { super(); super.setShapeName("Circle"); } public Circle(double radius) { this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } @Override public boolean validate() { return (radius > 0 ? true : false); } @Override public String toString() { return null; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } } class Rectangle extends Shape { private double width; private double length; public Rectangle() { super(); super.setShapeName("Rectangle"); } public Rectangle(double width, double length) { this.width = width; this.length = length; } @Override public double getArea() { return width * length; } @Override public boolean validate() { return (width >= 0 && length >= 0 ? true : false); } @Override public String toString() { return null; } 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; } } class Triangle extends Shape { private double side1 = 0; private double side2 = 0; private double side3 = 0; public Triangle() { super(); super.setShapeName("Triangle"); } public Triangle(double side1, double side2, double side3) { this.side1 = side1; this.side2 = side2; this.side3 = side3; } @Override public double getArea() { return 0.25 * Math.sqrt( (side1 + side2 + side3) * (side1 + side2 - side3) * (side1 + side3 - side2) * (side2 + side3 - side1)); } @Override public boolean validate() { return ((side1 >= 0 && side2 >= 0 && side3 >= 0 && (side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1)) ? true : false); } @Override public String toString() { return null; } 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; } } class Trapezoid extends Shape { private double topSide; private double bottomSide; private double height; public Trapezoid() { super(); super.setShapeName("Trapezoid"); } public Trapezoid(double topSide, double bottomSide, double height) { this.topSide = topSide; this.bottomSide = bottomSide; this.height = height; } @Override public double getArea() { return (topSide + bottomSide) * height / 2; } @Override public boolean validate() { return (topSide >= 0 && bottomSide >= 0 && height >= 0 ? true : false); } @Override public String toString() { return null; } public double getTopSide() { return topSide; } public void setTopSide(double topSide) { this.topSide = topSide; } public double getBottomSide() { return bottomSide; } public void setBottomSide(double bottomSide) { this.bottomSide = bottomSide; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } } class Card implements Comparable { private Shape shape; private double cardAllArea = 0; public Card() { super(); } public Card(Shape shape) { this.shape = shape; } public Shape getShape() { return shape; } public void setShape(Shape shape) { this.shape = shape; } public int compareTo() { return 0; } public double getCardAllArea() { return cardAllArea; } public void setCardAllArea(double area) { this.cardAllArea = this.cardAllArea + area; } } class DealCardList { ArrayList<Card> cardList = new ArrayList<Card>(); ArrayList<Card> cirList = new ArrayList<Card>(); ArrayList<Card> recList = new ArrayList<Card>(); ArrayList<Card> triList = new ArrayList<Card>(); ArrayList<Card> traList = new ArrayList<Card>(); public DealCardList() { super(); } public DealCardList(ArrayList<Integer> list) { Card card; for(int i = 0; i < list.size();i++) { switch(list.get(i)) { case 1: Circle circle = new Circle(); circle.setRadius(Main.input.nextDouble()); if(!circle.validate()) { //判斷圓形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(circle); cardList.add(card); cirList.add(card); break; case 2: Rectangle rectangle = new Rectangle(); rectangle.setWidth(Main.input.nextDouble()); rectangle.setLength(Main.input.nextDouble()); if(!rectangle.validate()) { //判斷矩形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(rectangle); cardList.add(card); recList.add(card); break; case 3: Triangle triangle = new Triangle(); triangle.setSide1(Main.input.nextDouble()); triangle.setSide2(Main.input.nextDouble()); triangle.setSide3(Main.input.nextDouble()); if(!triangle.validate()) { //判斷三角形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(triangle); cardList.add(card); triList.add(card); break; case 4: Trapezoid trapezoid = new Trapezoid(); trapezoid.setTopSide(Main.input.nextDouble()); trapezoid.setBottomSide(Main.input.nextDouble()); trapezoid.setHeight(Main.input.nextDouble()); if(!trapezoid.validate()) { //判斷梯形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(trapezoid); cardList.add(card); traList.add(card); break; default: System.out.println("Wrong Format"); System.exit(0); } } } public boolean validate() { return true; } public void cardSort(ArrayList<Card> list) { for(int i=0;i<list.size();i++) { for(int j=i+1;j<list.size();j++) { if(list.get(i).getShape().getArea()<list.get(j).getShape().getArea()) java.util.Collections.swap(list, i, j); } } } public double getMaxArea() { double cirArea = 0, recArea = 0, triArea = 0, traArea = 0; for(int i = 0; i<cirList.size(); i++) { cirArea += cirList.get(i).getShape().getArea(); } for(int i = 0; i<recList.size(); i++) { recArea += recList.get(i).getShape().getArea(); } for(int i = 0; i<triList.size(); i++) { triArea += triList.get(i).getShape().getArea(); } for(int i = 0; i<traList.size(); i++) { traArea += traList.get(i).getShape().getArea(); } double[] arr = {cirArea, recArea, triArea, traArea}; java.util.Arrays.sort(arr); return arr[3]; } public void show() { int k = 0; System.out.print("["); for( k=0; k<cirList.size(); k++) { System.out.print(cirList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", cirList.get(k).getShape().getArea()) + " "); } System.out.print("]"); System.out.print("["); for( k=0; k<recList.size(); k++) { System.out.print(recList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", recList.get(k).getShape().getArea()) + " "); } System.out.print("]"); System.out.print("["); for( k=0; k<triList.size(); k++) { System.out.print(triList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", triList.get(k).getShape().getArea()) + " "); } System.out.print("]"); System.out.print("["); for( k=0; k<traList.size(); k++) { System.out.print(traList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", traList.get(k).getShape().getArea()) + " "); } System.out.print("]"); System.out.println(); } public void showResult() { int k = 0; System.out.println("The original list:"); System.out.print("["); for( k=0; k<cardList.size(); k++) { System.out.print(cardList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", cardList.get(k).getShape().getArea()) + " "); } System.out.print("]"); System.out.println(); System.out.println("The Separated List:"); show(); System.out.println("The Separated sorted List:"); cardSort(cirList); cardSort(recList); cardSort(triList); cardSort(traList); show(); System.out.println("The max area:" + String.format("%.2f", getMaxArea())); } } public class Main { // 其它類中如果想要使用該物件進行輸入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(); } }
結果測試:
結果正確
根據SourceMonitor的生成報表: 從類圖以及SourceMonitor的生成報表上可以看出兩程式的類結構非常相似,甚至複雜度都基本一樣。 分析兩題的不同遞進設計。在題目集7(7-1)中是並不需要將卡片進行分類的,據題目給出的類圖,分別定義了:Shape類作為父類,而圓形(Circle)、矩形(Rectangle)、三角形(Triangle)及梯形(Trapezoid)四個類作為子類,子類只需要利用多型重構Shape中的方法。 而在題目集7(7-2)中是要求卡片分組的,在進行分組處理時ArrayList陣列,因此在DealCardList類中與題目集7(7-1)中不同。 ②題目集8和題目集9兩道ATM機模擬題目的設計思路分析總結 7-3 ATM機類結構設計(一) (100 分)設計ATM模擬系統,具體要求參見作業說明。OO作業8-1題目說明.pdf
輸入格式:
每一行輸入一次業務操作,可以輸入多行,最終以字元#終止。具體每種業務操作輸入格式如下:
- 存款、取款功能輸入資料格式:
卡號 密碼 ATM機編號 金額
(由一個或多個空格分隔), 其中,當金額大於0時,代表取款,否則代表存款。 - 查詢餘額功能輸入資料格式:
卡號
輸出格式:
①輸入錯誤處理
- 如果輸入卡號不存在,則輸出
Sorry,this card does not exist.
。 - 如果輸入ATM機編號不存在,則輸出
Sorry,the ATM's id is wrong.
。 - 如果輸入銀行卡密碼錯誤,則輸出
Sorry,your password is wrong.
。 - 如果輸入取款金額大於賬戶餘額,則輸出
Sorry,your account balance is insufficient.
。 - 如果檢測為跨行存取款,則輸出
Sorry,cross-bank withdrawal is not supported.
。
②取款業務輸出
輸出共兩行,格式分別為:
[使用者姓名]在[銀行名稱]的[ATM編號]上取款¥[金額]
當前餘額為¥[金額]
其中,[]說明括起來的部分為輸出屬性或變數,金額均保留兩位小數。
③存款業務輸出
輸出共兩行,格式分別為:
[使用者姓名]在[銀行名稱]的[ATM編號]上存款¥[金額]
當前餘額為¥[金額]
其中,[]說明括起來的部分為輸出屬性或變數,金額均保留兩位小數。
④查詢餘額業務輸出
¥[金額]
金額保留兩位小數。
輸入樣例1:
在這裡給出一組輸入。例如:
6222081502001312390 88888888 06 -500.00
#
結尾無空行
輸出樣例1:
在這裡給出相應的輸出。例如:
張無忌在中國工商銀行的06號ATM機上存款¥500.00
當前餘額為¥10500.00
結尾無空行
輸入樣例2:
在這裡給出一組輸入。例如:
6217000010041315709 88888888 02 3500.00
#
結尾無空行
輸出樣例2:
在這裡給出相應的輸出。例如:
楊過在中國建設銀行的02號ATM機上取款¥3500.00
當前餘額為¥6500.00
結尾無空行
輸入樣例3:
在這裡給出一組輸入。例如:
6217000010041315715
#
結尾無空行
輸出樣例3:
在這裡給出相應的輸出。例如:
¥10000.00
結尾無空行
輸入樣例4:
在這裡給出一組輸入。例如:
6222081502001312390 88888888 06 -500.00
6222081502051320786 88888888 06 1200.00
6217000010041315715 88888888 02 1500.00
6217000010041315709 88888888 02 3500.00
6217000010041315715
#
結尾無空行
輸出樣例4:
在這裡給出相應的輸出。例如:
張無忌在中國工商銀行的06號ATM機上存款¥500.00 當前餘額為¥10500.00 韋小寶在中國工商銀行的06號ATM機上取款¥1200.00 當前餘額為¥8800.00 楊過在中國建設銀行的02號ATM機上取款¥1500.00 當前餘額為¥8500.00 楊過在中國建設銀行的02號ATM機上取款¥3500.00 當前餘額為¥5000.00 ¥5000.00
源程式:
import java.util.ArrayList; import java.util.Scanner; class find { } class ChinaUnionPay { private ArrayList<Bank> banks = new ArrayList<Bank>(); private ArrayList<Card> cards = new ArrayList<Card>(); public ChinaUnionPay() { } public ArrayList<Bank> getBank() { return banks; } public void addBank(Bank bank) { // 向銀行列表中新增銀行 banks.add(bank); } public void addcards(Card card) { cards.add(card); } public Card getCard(String cardNum) { for (Card o : this.cards) { if (cardNum.equals(o.getNumber())) { return o; } } return null; } public void setCards(ArrayList<Card> cards) { this.cards = cards; } public boolean validate(String cardNum) { for (Card o : cards) { if (cardNum.equals(o.getNumber())) { return true; } } return false; } public ATM getATM(String atmid) { for (Bank bank : this.getBank()) { for (ATM atm : bank.getATMid()) { if (atmid.equals(atm.ATMid)) { return atm; } } } return null; } public User getUser(String cardNum) { for (Bank bank : this.getBank()) { for (User user : bank.getUserName()) { for (Account account : user.getAccount()) { for (Card card : account.getCard()) { if (card.getNumber().equals(cardNum)) { return user; } } } } } return null; } public Bank getBank(String cardNum) { for (Bank bank : this.getBank()) { for (User user : bank.getUserName()) { for (Account account : user.getAccount()) { for (Card card : account.getCard()) { if (card.getNumber().equals(cardNum)) { return bank; } } } } } return null; } public Account getAccount(String cardNum) { for (Bank bank : this.getBank()) { for (User user : bank.getUserName()) { for (Account account : user.getAccount()) { for (Card card : account.getCard()) { if (card.getNumber().equals(cardNum)) { return account; } } } } } return null; } } class Bank { private String bankName; private ArrayList<User> userNames = new ArrayList<User>(); private ArrayList<ATM> ATMids = new ArrayList<ATM>(); public Bank() { } public Bank(String bankName) { this.bankName = bankName; } public String getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } public ArrayList<User> getUserName() { return userNames; } public void addUserName(User userName) { userNames.add(userName); } public ArrayList<ATM> getATMid() { return ATMids; } public void addATMid(ATM ATMid) { ATMids.add(ATMid); } public static void user(User userName) { } public static void ATM(ATM ATMid) { } } class User { private String userName; private ArrayList<Account> accounts = new ArrayList<Account>(); public User() { } public User(String userName) { this.userName = userName; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public ArrayList<Account> getAccount() { return accounts; } public void addAccount(Account account) { // 向賬戶列表新增一個賬戶 accounts.add(account); } public void Account(Account account) { } } class ATM { String ATMid; public ATM() { } public ATM(String ATMid) { this.ATMid = ATMid; } public void takeOut(Account account, double money) { account.setBalance(account.getBalance() - money); } public void storage(Account account, double money) { account.setBalance(account.getBalance() + money); } public void check() { } } class Account { private String accountName; private double balance; private ArrayList<Card> cards = new ArrayList<Card>(); public Account() { } public Account(String accountName) { this.accountName = accountName; } public String getAccountName() { return accountName; } public void setAccountName(String accountName) { this.accountName = accountName; } public ArrayList<Card> getCard() { return cards; } public void addCard(Card card) { cards.add(card); } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } } class Card { private String number; private String password; public Card() { } public Card(String number) { this.number = number; } public boolean checkPassword(String password) { if (this.password.equals(password)) return true; return false; } public void setPassword(String password) { this.password = password; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } } public class Main { static ChinaUnionPay 中國銀聯 = new ChinaUnionPay(); public static Scanner input = new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub init(); String cardNum = ""; String password = ""; String ATMid = ""; double money = 0; Card card = new Card(); Account account = new Account(); ATM atm = new ATM(); String in = input.nextLine(); while (!(in.equals("#"))) { String[] msg = in.split(" "); cardNum = msg[0]; if (!中國銀聯.validate(cardNum)) { System.out.println("Sorry,this card does not exist."); System.exit(0); } card = 中國銀聯.getCard(cardNum); account = 中國銀聯.getAccount(cardNum); switch (msg.length) { case 1: // 查詢賬戶餘額 card = 中國銀聯.getCard(cardNum); System.out.println("¥" + String.format("%.2f", account.getBalance())); break; case 4: // 存款或取款 password = msg[1]; if (!card.checkPassword(password)) { System.out.println("Sorry,your password is wrong."); System.exit(0); } ATMid = msg[2]; atm = 中國銀聯.getATM(ATMid); if (atm==null) { System.out.println("Sorry,the ATM's id is wrong."); System.exit(0); } money = Double.parseDouble(msg[3]); if (money >= 0) { if (money > account.getBalance()) { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } atm.takeOut(account, money); System.out.println(中國銀聯.getUser(cardNum).getUserName() + "在" + 中國銀聯.getBank(cardNum).getBankName() + "的" + ATMid + "號ATM機上取款¥" + String.format("%.2f", money)); System.out.println("當前餘額為¥" + String.format("%.2f", account.getBalance())); } else { atm.storage(account, (-1)*money); System.out.println(中國銀聯.getUser(cardNum).getUserName() + "在" + 中國銀聯.getBank(cardNum).getBankName() + "的" + ATMid + "號ATM機上存款¥" + String.format("%.2f", (-1)*money)); System.out.println("當前餘額為¥" + String.format("%.2f", account.getBalance())); } break; } in = input.nextLine(); String[] str = in.split(""); if(str[0].equals("#")) { System.exit(0); } } } private static void init() { // 初始化資料庫 // 增加銀行 Bank 中國建設銀行 = new Bank("中國建設銀行"); Bank 中國工商銀行 = new Bank("中國工商銀行"); 中國銀聯.addBank(中國建設銀行); 中國銀聯.addBank(中國工商銀行); // 增加ATM ATM ATM01 = new ATM("01"); ATM ATM02 = new ATM("02"); ATM ATM03 = new ATM("03"); ATM ATM04 = new ATM("04"); 中國建設銀行.addATMid(ATM01); 中國建設銀行.addATMid(ATM02); 中國建設銀行.addATMid(ATM03); 中國建設銀行.addATMid(ATM04); ATM ATM05 = new ATM("05"); ATM ATM06 = new ATM("06"); 中國工商銀行.addATMid(ATM05); 中國工商銀行.addATMid(ATM06); // 增加使用者、賬戶、卡號、初始化密碼和餘額 User 楊過 = new User("楊過"); 中國建設銀行.addUserName(楊過); Account 楊過A1 = new Account("3217000010041315709"); 楊過A1.setBalance(10000.00); 楊過.addAccount(楊過A1); Card 楊過A1C1 = new Card("6217000010041315709"); 楊過A1C1.setPassword("88888888"); 楊過A1.addCard(楊過A1C1); 中國銀聯.addcards(楊過A1C1); Card 楊過A1C2 = new Card("6217000010041315715"); 楊過A1C2.setPassword("88888888"); 楊過A1.addCard(楊過A1C2); 中國銀聯.addcards(楊過A1C2); Account 楊過A2 = new Account("3217000010041315715"); 楊過A2.setBalance(10000.00); 楊過.addAccount(楊過A2); Card 楊過A2C1 = new Card("6217000010041315718"); 楊過A2C1.setPassword("88888888"); 楊過A2.addCard(楊過A2C1); 中國銀聯.addcards(楊過A2C1); User 郭靖 = new User("郭靖"); 中國建設銀行.addUserName(郭靖); Account 郭靖A1 = new Account("3217000010051320007"); 郭靖A1.setBalance(10000.00); 郭靖.addAccount(郭靖A1); Card 郭靖A1C1 = new Card("6217000010051320007"); 郭靖A1C1.setPassword("88888888"); 郭靖A1.addCard(郭靖A1C1); 中國銀聯.addcards(郭靖A1C1); User 張無忌 = new User("張無忌"); 中國工商銀行.addUserName(張無忌); Account 張無忌A1 = new Account("3222081502001312389"); 張無忌A1.setBalance(10000.00); 張無忌.addAccount(張無忌A1); Card 張無忌A1C1 = new Card("6222081502001312389"); 張無忌A1C1.setPassword("88888888"); 張無忌A1.addCard(張無忌A1C1); 中國銀聯.addcards(張無忌A1C1); Account 張無忌A2 = new Account("3222081502001312390"); 張無忌.addAccount(張無忌A2); 張無忌A2.setBalance(10000.00); Card 張無忌A2C1 = new Card("6222081502001312390"); 張無忌A2C1.setPassword("88888888"); 張無忌A2.addCard(張無忌A2C1); 中國銀聯.addcards(張無忌A2C1); Account 張無忌A3 = new Account("3222081502001312399"); 張無忌A3.setBalance(10000.00); 張無忌.addAccount(張無忌A3); Card 張無忌A3C1 = new Card("6222081502001312399"); 張無忌A3C1.setPassword("88888888"); 張無忌A3.addCard(張無忌A3C1); 中國銀聯.addcards(張無忌A3C1); Card 張無忌A3C2 = new Card("6222081502001312400"); 張無忌A3C2.setPassword("88888888"); 張無忌A3.addCard(張無忌A3C2); 中國銀聯.addcards(張無忌A3C2); User 韋小寶 = new User("韋小寶"); 中國工商銀行.addUserName(韋小寶); Account 韋小寶A1 = new Account("3222081502051320785"); 韋小寶A1.setBalance(10000.00); 韋小寶.addAccount(韋小寶A1); Card 韋小寶A1C1 = new Card("6222081502051320785"); 韋小寶A1C1.setPassword("88888888"); 韋小寶A1.addCard(韋小寶A1C1); 中國銀聯.addcards(韋小寶A1C1); Account 韋小寶A2 = new Account("3222081502051320786"); 韋小寶.addAccount(韋小寶A2); 韋小寶A2.setBalance(10000.00); Card 韋小寶A2C1 = new Card("6222081502051320786"); 韋小寶A2C1.setPassword("88888888"); 韋小寶A2.addCard(韋小寶A2C1); 中國銀聯.addcards(韋小寶A2C1); } }
這道題目的得分只有70分,不斷修改之後還是有三個測試點不能通過
因為程式碼做了多次刪改,冗長且複雜,不利於刪改或者增加要求,寫成了一次性程式碼
7-1 ATM機類結構設計(二) (100 分)
設計ATM模擬系統,具體要求參見作業說明。OO作業9-1題目說明.pdf
輸入格式:
每一行輸入一次業務操作,可以輸入多行,最終以字元#終止。具體每種業務操作輸入格式如下:
- 取款功能輸入資料格式:
卡號 密碼 ATM機編號 金額
(由一個或多個空格分隔) - 查詢餘額功能輸入資料格式:
卡號
輸出格式:
①輸入錯誤處理
- 如果輸入卡號不存在,則輸出
Sorry,this card does not exist.
。 - 如果輸入ATM機編號不存在,則輸出
Sorry,the ATM's id is wrong.
。 - 如果輸入銀行卡密碼錯誤,則輸出
Sorry,your password is wrong.
。 - 如果輸入取款金額大於賬戶餘額,則輸出
Sorry,your account balance is insufficient.
。
②取款業務輸出
輸出共兩行,格式分別為:
業務:取款 [使用者姓名]在[銀行名稱]的[ATM編號]上取款¥[金額]
當前餘額為¥[金額]
其中,[]說明括起來的部分為輸出屬性或變數,金額均保留兩位小數。
③查詢餘額業務輸出
業務:查詢餘額 ¥[金額]
金額保留兩位小數。
輸入樣例1:
在這裡給出一組輸入。例如:
6222081502001312390 88888888 06 500.00
#
結尾無空行
輸出樣例1:
在這裡給出相應的輸出。例如:
業務:取款 張無忌在中國工商銀行的06號ATM機上取款¥500.00
當前餘額為¥9500.00
結尾無空行
輸入樣例2:
在這裡給出一組輸入。例如:
6217000010041315709 88888888 06 3500.00
#
結尾無空行
輸出樣例2:
在這裡給出相應的輸出。例如:
業務:取款 楊過在中國工商銀行的06號ATM機上取款¥3500.00
當前餘額為¥6395.00
結尾無空行
輸入樣例3:
在這裡給出一組輸入。例如:
6217000010041315715
#
結尾無空行
輸出樣例3:
在這裡給出相應的輸出。例如:
業務:查詢餘額 ¥10000.00
結尾無空行
輸入樣例4:
在這裡給出一組輸入。例如:
6222081502001312390 88888888 01 500.00
6222081502051320786 88888888 06 1200.00
6217000010041315715 88888888 02 1500.00
6217000010041315709 88888888 02 3500.00
6217000010041315715
#
結尾無空行
輸出樣例4:
在這裡給出相應的輸出。例如:
業務:取款 張無忌在中國建設銀行的01號ATM機上取款¥500.00
當前餘額為¥9490.00
業務:取款 韋小寶在中國工商銀行的06號ATM機上取款¥1200.00
當前餘額為¥8800.00
業務:取款 楊過在中國建設銀行的02號ATM機上取款¥1500.00
當前餘額為¥8500.00
業務:取款 楊過在中國建設銀行的02號ATM機上取款¥3500.00
當前餘額為¥5000.00
業務:查詢餘額 ¥5000.00
結尾無空行
輸入樣例5:
在這裡給出一組輸入。例如:
6640000010045442002 88888888 09 3000
6640000010045442002 88888888 06 8000
6640000010045442003 88888888 01 10000
6640000010045442002
#
結尾無空行
輸出樣例5:
在這裡給出相應的輸出。例如:
業務:取款 張三丰在中國農業銀行的09號ATM機上取款¥3000.00
當前餘額為¥6880.00
業務:取款 張三丰在中國工商銀行的06號ATM機上取款¥8000.00
當前餘額為¥-1416.00
業務:取款 張三丰在中國建設銀行的01號ATM機上取款¥10000.00
當前餘額為¥-11916.00
業務:查詢餘額 ¥-11916.00
源程式:
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Org chinaUnionPay = new ChinaUnionPay(); //中國建設銀行 Bank CCB = new Bank(chinaUnionPay,"中國建設銀行",0.02); //中國工商銀行 Bank ICBC = new Bank(chinaUnionPay,"中國工商銀行",0.03); //中國農業銀行 Bank ABC = new Bank(chinaUnionPay,"中國農業銀行",0.04); //新增ATM機 for (int i = 1; i <= 11; i++) { ATM atm = new ATM(); if (i < 5){ atm.setNO("0"+i); CCB.getATMs().add(atm); continue; } if (i < 7){ atm.setNO("0"+i); ICBC.getATMs().add(atm); continue; } if (i < 10){ atm.setNO("0"+i); ABC.getATMs().add(atm); continue; } atm.setNO(""+i); ABC.getATMs().add(atm); } //建立賬戶 //楊過 List<Card> cards1 = new ArrayList<>(); cards1.add(new Card("6217000010041315709","88888888")); cards1.add(new Card("6217000010041315715","88888888")); Account account1 = new Account("3217000010041315709", "借記賬號", cards1, CCB, 10000); List<Card> cards2 = new ArrayList<>(); cards2.add(new Card("6217000010041315718","88888888")); Account account2 = new Account("3217000010041315715", "借記賬號", cards2, CCB, 10000); ArrayList<Account> accounts = new ArrayList<>(); accounts.add(account1); accounts.add(account2); User yg = new User("楊過", accounts); //郭靖 List<Card> cards3 = new ArrayList<>(); cards3.add(new Card("6217000010051320007","88888888")); Account account3 = new Account("3217000010051320007", "借記賬號", cards3, CCB, 10000); ArrayList<Account> accounts1 = new ArrayList<>(); accounts1.add(account3); User gj = new User("郭靖", accounts1); //張無忌 List<Card> cards4 = new ArrayList<>(); cards4.add(new Card("6222081502001312389","88888888")); Account account4 = new Account("3222081502001312389", "借記賬號", cards4, ICBC, 10000); List<Card> cards5 = new ArrayList<>(); cards5.add(new Card("6222081502001312390","88888888")); Account account5 = new Account("3222081502001312390", "借記賬號", cards5, ICBC, 10000); List<Card> cards6 = new ArrayList<>(); cards6.add(new Card("6222081502001312399","88888888")); cards6.add(new Card("6222081502001312400","88888888")); Account account6 = new Account("3222081502001312399", "借記賬號", cards6, ICBC, 10000); ArrayList<Account> accounts2 = new ArrayList<>(); accounts2.add(account4); accounts2.add(account5); accounts2.add(account6); User zwj = new User("張無忌", accounts2); //韋小寶 List<Card> cards7 = new ArrayList<>(); cards7.add(new Card("6222081502051320785","88888888")); Account account7 = new Account("3222081502051320785", "借記賬號", cards7, ICBC, 10000); List<Card> cards8 = new ArrayList<>(); cards8.add(new Card("6222081502051320786","88888888")); Account account8 = new Account("3222081502051320786", "借記賬號", cards8, ICBC, 10000); ArrayList<Account> accounts3 = new ArrayList<>(); accounts3.add(account7); accounts3.add(account8); User wxb = new User("韋小寶", accounts3); //張三丰 List<Card> cards9 = new ArrayList<>(); cards9.add(new Card("6640000010045442002","88888888")); cards9.add(new Card("6640000010045442003","88888888")); Account account9 = new Account("3640000010045442002", "貸記賬號", cards9, CCB, 10000); ArrayList<Account> accounts4 = new ArrayList<>(); accounts4.add(account9); User zsf = new User("張三丰", accounts4); //令狐沖 List<Card> cards10 = new ArrayList<>(); cards10.add(new Card("6640000010045441009","88888888")); Account account10 = new Account("3640000010045441009", "貸記賬號", cards10, ICBC, 10000); ArrayList<Account> accounts5 = new ArrayList<>(); accounts5.add(account10); User lfc = new User("令狐沖", accounts5); //喬峰 List<Card> cards11 = new ArrayList<>(); cards11.add(new Card("6630000010033431001","88888888")); Account account11 = new Account("3630000010033431001", "貸記賬號", cards11, ABC, 10000); ArrayList<Account> accounts6 = new ArrayList<>(); accounts6.add(account11); User qf = new User("喬峰", accounts6); //洪七公 List<Card> cards12 = new ArrayList<>(); cards12.add(new Card("6630000010033431008","88888888")); Account account12 = new Account("3630000010033431008", "貸記賬號", cards12, ABC, 10000); ArrayList<Account> accounts7 = new ArrayList<>(); accounts7.add(account12); User hqg = new User("洪七公", accounts7); List<User> users = new ArrayList<>(); users.add(yg); users.add(gj); users.add(zwj); users.add(wxb); users.add(zsf); users.add(lfc); users.add(qf); users.add(hqg); List<Bank> banks = new ArrayList<>(); banks.add(ABC); banks.add(ICBC); banks.add(CCB); Scanner scanner = new Scanner(System.in); List<String> inputs = new ArrayList<>(); for (;;) { String s = scanner.nextLine(); if (s.equals("#")){ break; } inputs.add(s); } for (String input : inputs) { doAction(users, banks, input); } } private static void doAction(List<User> users, List<Bank> banks, String s) { String[] input = s.split("\\s+"); if (input.length == 4){ String carNo = input[0]; String pwd = input[1]; String ATMNo = input[2];// double amount = Double.parseDouble(input[3]); Account inputAccount = null; double privilegePoundage = 0; Bank atmBank = null; String bankName = null; Card inputCard = null; User inputUser = null; for (User user : users) { List<Account> accountList = user.getAccounts(); for (Account account : accountList) { List<Card> cards = account.getCards(); for (Card card : cards) { if (card.getNO().equals(carNo)) { inputAccount = account; inputCard = card; inputUser = user; break; } } } } if (inputAccount == null){ System.out.println("Sorry,this card does not exist."); return; } boolean hasATM = false; for (Bank bank : banks) { List<ATM> atMs1 = bank.getATMs(); for (ATM atm : atMs1) { if (atm.getNO().equals(ATMNo)) { hasATM = true; bankName = bank.getBankName(); if (bank != inputAccount.getBank()) { privilegePoundage = bank.getPrivilegePoundage(); } break; } } } if (!hasATM) { System.out.println("Sorry,the ATM's id is wrong."); // System.out.println(carNo); return; } if (!inputCard.getPassword().equals(pwd)) { System.out.println("Sorry,your password is wrong."); // System.out.println(carNo+" "+ ATMNo); return; } if (inputAccount.getType().equals("借記賬號")) { if (inputAccount.getBalance() < amount*(1+privilegePoundage)) { System.out.println("Sorry,your account balance is insufficient."); // System.out.println(carNo+" "+ pwd+" "+ATMNo); return; } double v = inputAccount.getBalance() - amount * (1 + privilegePoundage); inputAccount.setBalance(v); System.out.println("業務:取款 "+inputUser.getName()+"在"+bankName+"的"+ATMNo+"號ATM機上取款¥"+String.format("%.2f",amount)+"\n" + "當前餘額為¥"+String.format("%.2f", v)); }else { Org org = inputAccount.getBank().getOrg(); double balance = inputAccount.getBalance(); double overdraftFeeWithdrawal = org.getOverdraftFeeWithdrawal(); double overdraftLimit = org.getOverdraftLimit(); if (balance < amount*(1+privilegePoundage)) { double v = 0; if (balance > 0) { v = (balance-amount) * (1 + overdraftFeeWithdrawal) - amount * privilegePoundage; }else { v = balance - (amount * (1 + overdraftFeeWithdrawal) + amount * privilegePoundage) ; //System.out.println("Aaaaa"); } if (Math.abs(v) > overdraftLimit) { System.out.println("Sorry,your account balance is insufficient."); // System.out.println(carNo+" "+ pwd+" "+ATMNo); return; } inputAccount.setBalance(v); System.out.println("業務:取款 "+inputUser.getName()+"在"+bankName+"的"+ATMNo+"號ATM機上取款¥"+String.format("%.2f",amount)+"\n" + "當前餘額為¥"+String.format("%.2f",v)); }else { double v = inputAccount.getBalance() - amount * (1 + privilegePoundage); inputAccount.setBalance(v); System.out.println("業務:取款 "+inputUser.getName()+"在"+bankName+"的"+ATMNo+"號ATM機上取款¥"+String.format("%.2f",amount)+"\n" + "當前餘額為¥"+String.format("%.2f", v)); } } } else if (input.length == 1){ for (User user : users) { List<Account> accountList = user.getAccounts(); for (Account account : accountList) { for (Card card : account.getCards()) { if (card.getNO().equals(input[0])) { double balance = account.getBalance(); System.out.println("業務:查詢餘額 ¥"+String.format("%.2f",balance)); return; } } } } System.out.println("Sorry,this card does not exist."); } } /** * 銀行賬戶 */ public static class Account { //銀行賬號 private String NO; //賬號種類 private String type; //賬戶擁有的銀行卡 private List<Card> cards; //隸屬銀行 private Bank bank; //餘額 private double balance; public Account(String NO, String type, List<Card> cards, Bank bank, double balance) { this.NO = NO; this.type = type; this.cards = cards; this.bank = bank; this.balance = balance; } public String getNO() { return NO; } public void setNO(String NO) { this.NO = NO; } public String getType() { return type; } public void setType(String type) { this.type = type; } public List<Card> getCards() { return cards; } public void setCards(List<Card> cards) { this.cards = cards; } public Bank getBank() { return bank; } public void setBank(Bank bank) { this.bank = bank; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } } /** * 使用者實體 */ public static class User { //姓名 private String name; //使用者擁有的賬戶 private List<Account> accounts; public User(String name, List<Account> accounts) { this.name = name; this.accounts = accounts; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Account> getAccounts() { return accounts; } public void setAccounts(List<Account> accounts) { this.accounts = accounts; } } /** * ATM機實體類 */ public static class ATM { //編號 private String NO; public String getNO() { return NO; } public void setNO(String NO) { this.NO = NO; } } /** * 銀行實體類 */ public static class Bank { //所屬機構 private Org org; //名稱 private String bankName; //跨行取款手續費 private double privilegePoundage; //銀行擁有的ATM機 private List<ATM> ATMs; public Bank(Org org, String bankName, double privilegePoundage) { this.org = org; this.bankName = bankName; this.privilegePoundage = privilegePoundage; this.ATMs = new ArrayList<>(); } public Org getOrg() { return org; } public void setOrg(Org org) { this.org = org; } public String getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } public double getPrivilegePoundage() { return privilegePoundage; } public void setPrivilegePoundage(double privilegePoundage) { this.privilegePoundage = privilegePoundage; } public List<ATM> getATMs() { return ATMs; } public void setATMs(List<ATM> ATMs) { this.ATMs = ATMs; } } /** * 銀行實體類 */ public static class Card { //銀行卡號 private String NO; //密碼 private String password; public Card(String NO, String password) { this.NO = NO; this.password = password; } public String getNO() { return NO; } public void setNO(String NO) { this.NO = NO; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } //組織介面 public interface Org { public String getName(); double getOverdraftFeeWithdrawal(); double getOverdraftLimit(); } /** * 中國銀聯實體 */ public static class ChinaUnionPay implements Org { //名稱 private String name = "中國銀聯"; //透支取款手續費 private double OverdraftFeeWithdrawal = 0.05; //透支最大限額 private double OverdraftLimit = 50000; @Override public String getName() { return this.name; } @Override public double getOverdraftFeeWithdrawal() { return OverdraftFeeWithdrawal; } @Override public double getOverdraftLimit() { return OverdraftLimit; } } }
這次題目我吸取了上次的教訓,先把類圖設計好,設計方面難點在於如何實現多型。
題目說明著重寫了卡能否貸款取決於賬戶型別,所以賬戶是肯定要繼承於抽象類的,同時也便於再新增新型別的賬戶。
而兩種賬戶唯一的差別就在於能否貸款,所以可以針對能否貸款這一點來構建抽象類。跨行手續費,和貸款手續費就要在相應類裡面增加屬性。
輸出測試:
根據SourceMonitor的生成報表:
三、踩坑心得
這次題目的難點主要在於,有多種複雜功能需要實現,類與類之間的各種關係分析,尤其是第八第九次實驗,老師沒有給出類圖,就需要我們前期先把類的結構設計好,這樣才能保證後期能順暢的寫程式碼。
在我寫第八題的時候,就是因為類的聯絡設計不好,後期要不斷修改以得到正確輸出,導致程式碼結構冗雜混亂,只能一次性使用。
各種類聚合和多型輸入的時候是字串形式而用到檢測或者處理的時候需要數字形式,所以要用到包裝類的方法的設計。可以運用arraylist降低程式複雜度
四、總結
學習java已經一學期,這次題目的難度對我來說很大,題目也更偏向實際應用,就要求我們更加全面、細節的考慮,程式碼的行數也突破到了幾百行,對我們的知識儲備和編碼能力來說都是極大的挑戰,雖然每次作業都只有一道題或兩道題目,但我幾乎都需要花費幾天的時間設計和寫出程式碼。
雖然這次作業對我來說偏難,是最耗時耗力的一次,但我的收穫也是很大的,對類之間的關係有了更深的瞭解,並且更深的明白了物件的意義。以後我在寫程式碼的時候,可以不光為了得到正確的輸出,還可以在程式碼結構的設計上多下功夫,寫出可以重複使用的程式碼。