java第7-9次作业总结

一.前言:

  我认为这三次的题目整体来说有点难度,老师讲java面对对象一直都是围绕着一个词:变化,用户的需求发生变化、程序实现的功能也在发送变化,而在题目集8到题目集9的升级,就是需求的一个变化。

 

 

二.设计与分析:

   题目集7:第一题是所有卡片排序,第二题是卡片类型分组排序。

 

 

 代码如下:

  1 import java.util.ArrayList;
  2 import java.util.Collections;
  3 import java.util.Scanner;
  4 
  5 public class Main{
  6     public static Scanner input = new Scanner(System.in);
  7     public static void main(String[] args){
  8         ArrayList<Integer> list = new ArrayList<Integer>(); 
  9     
 10         int num = input.nextInt();
 11     
 12         while(num != 0){
 13             if(num < 0 || num > 4){
 14                 System.out.println("Wrong Format");
 15                 System.exit(0);
 16             }
 17             list.add(num);
 18             num = input.nextInt();
 19         }
 20         DealCardList dealCardList = new DealCardList(list);
 21         if(!dealCardList.validate()){
 22             System.out.println("Wrong Format");
 23             System.exit(0);
 24         }
 25         dealCardList.showResult();
 26         input.close();
 27     }
 28 }
 29 /*
 30  * 卡片组类
 31  */
 32 class DealCardList{
 33     private ArrayList<Card> cardList = new ArrayList<Card>();
 34     
 35     DealCardList(){
 36     }
 37     DealCardList(ArrayList<Integer> list){
 38         Card card;
 39         for(Integer i: list) {
 40             switch(i) {
 41             case 1:card = new Card(new Circle(Main.input.nextDouble()));
 42                 cardList.add(card);
 43                 break;
 44             case 2:card = new Card(new Rectangle(Main.input.nextDouble(), Main.input.nextDouble()));
 45                 cardList.add(card);
 46                 break;
 47             case 3:card = new Card(new Triangle(Main.input.nextDouble(), Main.input.nextDouble(), Main.input.nextDouble()));
 48                 cardList.add(card);
 49                 break;
 50             case 4:card = new Card(new Trapezoid(Main.input.nextDouble(), Main.input.nextDouble(), Main.input.nextDouble()));
 51                 cardList.add(card);
 52                 break;
 53             }
 54         }
 55     }
 56     boolean validate() {//合法性校验
 57         for(Card i: cardList) {
 58             if(!i.getShape().validate())
 59                 return false;
 60         }
 61         return true;
 62     }
 63     void cardSort() {
 64         Collections.sort(this.cardList);
 65     }
 66     double getAllArea() {
 67         double sum = 0;
 68         for(Card i: cardList) {
 69             sum += i.getShape().getArea();
 70         }
 71         return sum;
 72     }
 73     void showResult() {
 74         System.out.println("The original list:");
 75         for(Card i: cardList) {
 76             System.out.printf("%s:%.2f ", i.getShape().toString(), i.getShape().getArea());
 77         }
 78         this.cardSort();
 79         System.out.println("\nThe sorted list:");
 80         for(Card i: cardList) {
 81             System.out.printf("%s:%.2f ", i.getShape().toString(), i.getShape().getArea());
 82         }
 83         System.out.printf("\nSum of area:%.2f", this.getAllArea());
 84     }
 85 }
 86 /*
 87  * 卡片类
 88  */
 89 class Card implements Comparable<Card> {
 90     private Shape shape = new Shape();
 91     
 92     Card(){
 93     }
 94     Card(Shape shape){
 95         this.shape = shape;
 96     }
 97     Shape getShape() {
 98         return shape;
 99     }
100     public int compareTo(Card card){
101         if(this.shape.getArea()<card.shape.getArea())
102             return 1;
103         else if(this.shape.getArea()>card.shape.getArea())
104             return -1;
105         else
106             return 0;
107     }
108 }
109 /*
110  * 图形类
111  */
112 class Shape{
113     private String shapeName;
114     
115     Shape(){
116     }
117     Shape(String shapeName){
118         this.shapeName = shapeName;
119     }
120     
121     String getShapeName() {
122         return shapeName;
123     }
124     void setShapeName(String shapeName) {
125         this.shapeName = shapeName;
126     }
127     
128     double getArea() {//图形面积
129         return 0;
130     }
131     boolean validate() {//合法性校验
132         return true;
133     }
134     public String toString() {
135         return getClass().getName();
136     }
137 }
138 /*
139  * 圆形类,继承图形类
140  */
141 class Circle extends Shape{
142     private double radius;
143     
144     Circle(double radius){
145         this.radius = radius;
146     }
147     boolean validate() {//合法性校验
148         if(this.radius>0)
149             return true;
150         else
151             return false;
152     }
153     double getArea() {//图形面积
154         return Math.PI*this.radius*this.radius;
155     }
156 }
157 /*
158  * 矩形类,继承图形类
159  */
160 class Rectangle extends Shape{
161     private double width;
162     private double length;
163     
164     Rectangle(double width, double length){
165         this.length = length;
166         this.width = width;
167     }
168     boolean validate() {//合法性校验
169         if(this.length>0&&this.width>0)
170             return true;
171         else
172             return false;
173     }
174     double getArea() {//图形面积
175         return this.length*this.width;
176     }
177 }
178 /*
179  * 三角形类,继承图形类
180  */
181 class Triangle extends Shape{
182     private double side1;
183     private double side2;
184     private double side3;
185     
186     Triangle(double side1, double side2, double side3){
187         this.side1 = side1;
188         this.side2 = side2;
189         this.side3 = side3;
190     }
191     boolean validate() {//合法性校验
192         if(this.side1>0&&this.side2>0&&this.side3>0) {
193             if(this.side1+this.side2>this.side3
194                     && this.side2+this.side3>this.side1
195                     && this.side1+this.side3>this.side2)
196                 return true;
197             else
198                 return false;
199         }
200         else
201             return false;
202     }
203     double getArea() {//图形面积
204         double l = (this.side1+this.side2+this.side3)/2;
205         return Math.sqrt(l*(l-this.side1)*(l-this.side2)*(l-this.side3));
206     }
207 }
208 /*
209  * 梯形类,继承图形类
210  */
211 class Trapezoid extends Shape{
212     private double topSide;
213     private double bottomSide;
214     private double height;
215     
216     Trapezoid(double topSide, double bottomSide, double height){
217         this.topSide = topSide;
218         this.bottomSide = bottomSide;
219         this.height = height;
220     }
221     boolean validate() {//合法性校验
222         if(this.topSide>0&&this.bottomSide>0&&this.height>0)
223             return true;
224         else
225             return false;
226     }
227     double getArea() {//图形面积
228         return (this.topSide+this.bottomSide)*this.height/2;
229     }
230 }
View Code

类图如下:

 

 圈复杂度分析:

 

 

 

 

=============

 

 代码如下:

  1 import java.util.ArrayList;
  2 import java.util.Arrays;
  3 import java.util.Collections;
  4 import java.util.Scanner;
  5 
  6 public class Main{
  7     public static Scanner input = new Scanner(System.in);
  8     public static void main(String[] args){
  9         ArrayList<Integer> list = new ArrayList<Integer>(); 
 10     
 11         int num = input.nextInt();
 12     
 13         while(num != 0){
 14             if(num < 0 || num > 4){
 15                 System.out.println("Wrong Format");
 16                 System.exit(0);
 17             }
 18             list.add(num);
 19             num = input.nextInt();
 20         }
 21         if(list.size()==0){
 22             System.out.println("Wrong Format");
 23             System.exit(0);
 24         }
 25         DealCardList dealCardList = new DealCardList(list);
 26         if(!dealCardList.validate()){
 27             System.out.println("Wrong Format");
 28             System.exit(0);
 29         }
 30         dealCardList.showResult();
 31         input.close();
 32     }
 33 }
 34 /*
 35  * 卡片组类
 36  */
 37 class DealCardList{
 38     private ArrayList<Card> cardList = new ArrayList<Card>();
 39     
 40     DealCardList(){
 41     }
 42     DealCardList(ArrayList<Integer> list){
 43         Card card;
 44         for(Integer i: list) {
 45             switch(i) {
 46             case 1:card = new Card(new Circle(Main.input.nextDouble()));
 47                 cardList.add(card);
 48                 break;
 49             case 2:card = new Card(new Rectangle(Main.input.nextDouble(), Main.input.nextDouble()));
 50                 cardList.add(card);
 51                 break;
 52             case 3:card = new Card(new Triangle(Main.input.nextDouble(), Main.input.nextDouble(), Main.input.nextDouble()));
 53                 cardList.add(card);
 54                 break;
 55             case 4:card = new Card(new Trapezoid(Main.input.nextDouble(), Main.input.nextDouble(), Main.input.nextDouble()));
 56                 cardList.add(card);
 57                 break;
 58             }
 59         }
 60     }
 61     boolean validate() {//合法性校验
 62         for(Card i: cardList) {
 63             if(!i.getShape().validate())
 64                 return false;
 65         }
 66         return true;
 67     }
 68     void cardSort() {
 69         Collections.sort(this.cardList);
 70     }
 71     double getTypeMaxArea() {
 72         double[] sum = new double[]{0,0,0,0};
 73         for(Card i: cardList) {
 74             switch(i.getShape().toString()) {
 75             case "Circle":
 76                 sum[0] += i.getShape().getArea();
 77                 break;
 78             case "Rectangle":
 79                 sum[1] += i.getShape().getArea();
 80                 break;
 81             case "Triangle":
 82                 sum[2] += i.getShape().getArea();
 83                 break;
 84             case "Trapezoid":
 85                 sum[3] += i.getShape().getArea();
 86                 break;
 87             }
 88         }
 89         Arrays.sort(sum);
 90         return sum[sum.length-1];
 91     }
 92     void showCircle() {
 93         System.out.print("[");
 94         for(Card i: cardList) {
 95             if(i.getShape().toString().equals("Circle"))
 96                 System.out.printf("%s:%.2f ", i.getShape().toString(), i.getShape().getArea());
 97         }
 98         System.out.print("]");
 99     }
100     void showRectangle() {
101         System.out.print("[");
102         for(Card i: cardList) {
103             if(i.getShape().toString().equals("Rectangle"))
104                 System.out.printf("%s:%.2f ", i.getShape().toString(), i.getShape().getArea());
105         }
106         System.out.print("]");
107     }
108     void showTriangle() {
109         System.out.print("[");
110         for(Card i: cardList) {
111             if(i.getShape().toString().equals("Triangle"))
112                 System.out.printf("%s:%.2f ", i.getShape().toString(), i.getShape().getArea());
113         }
114         System.out.print("]");
115     }
116     void showTrapezoid() {
117         System.out.print("[");
118         for(Card i: cardList) {
119             if(i.getShape().toString().equals("Trapezoid"))
120                 System.out.printf("%s:%.2f ", i.getShape().toString(), i.getShape().getArea());
121         }
122         System.out.print("]");
123     }
124     void showType() {
125         this.showCircle();
126         this.showRectangle();
127         this.showTriangle();
128         this.showTrapezoid();
129     }
130     void showResult() {
131         System.out.println("The original list:");
132         System.out.print("[");
133         for(Card i: cardList) {
134             System.out.printf("%s:%.2f ", i.getShape().toString(), i.getShape().getArea());
135         }
136         System.out.print("]");
137         
138         System.out.println("\nThe Separated List:");
139         this.showType();
140         
141         this.cardSort();
142         System.out.println("\nThe Separated sorted List:");
143         this.showType();
144         
145         System.out.printf("\nThe max area:%.2f", this.getTypeMaxArea());
146     }
147 }
148 /*
149  * 卡片类
150  */
151 class Card implements Comparable<Card> {
152     private Shape shape = new Shape();
153     
154     Card(){
155     }
156     Card(Shape shape){
157         this.shape = shape;
158     }
159     Shape getShape() {
160         return shape;
161     }
162     public int compareTo(Card card){
163         if(this.shape.getArea()<card.shape.getArea())
164             return 1;
165         else if(this.shape.getArea()>card.shape.getArea())
166             return -1;
167         else
168             return 0;
169     }
170 }
171 /*
172  * 图形类
173  */
174 class Shape{
175     private String shapeName;
176     
177     Shape(){
178     }
179     Shape(String shapeName){
180         this.shapeName = shapeName;
181     }
182     
183     String getShapeName() {
184         return shapeName;
185     }
186     void setShapeName(String shapeName) {
187         this.shapeName = shapeName;
188     }
189     
190     double getArea() {//图形面积
191         return 0;
192     }
193     boolean validate() {//合法性校验
194         return true;
195     }
196     public String toString() {
197         return getClass().getName();
198     }
199 }
200 /*
201  * 圆形类,继承图形类
202  */
203 class Circle extends Shape{
204     private double radius;
205     
206     Circle(double radius){
207         this.radius = radius;
208     }
209     boolean validate() {//合法性校验
210         if(this.radius>0)
211             return true;
212         else
213             return false;
214     }
215     double getArea() {//图形面积
216         return Math.PI*this.radius*this.radius;
217     }
218 }
219 /*
220  * 矩形类,继承图形类
221  */
222 class Rectangle extends Shape{
223     private double width;
224     private double length;
225     
226     Rectangle(double width, double length){
227         this.length = length;
228         this.width = width;
229     }
230     boolean validate() {//合法性校验
231         if(this.length>0&&this.width>0)
232             return true;
233         else
234             return false;
235     }
236     double getArea() {//图形面积
237         return this.length*this.width;
238     }
239 }
240 /*
241  * 三角形类,继承图形类
242  */
243 class Triangle extends Shape{
244     private double side1;
245     private double side2;
246     private double side3;
247     
248     Triangle(double side1, double side2, double side3){
249         this.side1 = side1;
250         this.side2 = side2;
251         this.side3 = side3;
252     }
253     boolean validate() {//合法性校验
254         if(this.side1>0&&this.side2>0&&this.side3>0) {
255             if(this.side1+this.side2>this.side3
256                     && this.side2+this.side3>this.side1
257                     && this.side1+this.side3>this.side2)
258                 return true;
259             else
260                 return false;
261         }
262         else
263             return false;
264     }
265     double getArea() {//图形面积
266         double l = (this.side1+this.side2+this.side3)/2;
267         return Math.sqrt(l*(l-this.side1)*(l-this.side2)*(l-this.side3));
268     }
269 }
270 /*
271  * 梯形类,继承图形类
272  */
273 class Trapezoid extends Shape{
274     private double topSide;
275     private double bottomSide;
276     private double height;
277     
278     Trapezoid(double topSide, double bottomSide, double height){
279         this.topSide = topSide;
280         this.bottomSide = bottomSide;
281         this.height = height;
282     }
283     boolean validate() {//合法性校验
284         if(this.topSide>0&&this.bottomSide>0&&this.height>0)
285             return true;
286         else
287             return false;
288     }
289     double getArea() {//图形面积
290         return (this.topSide+this.bottomSide)*this.height/2;
291     }
292 }
View Code

类图如下:

 

 圈复杂度分析:

 

 

 

题目集8:设计思路:一个中国银联(两个银行对象),包括两种银行(设计一个银行类),银行有账号和ATM机,设计一个账号类,账号有各种属性,账号包含银行卡信息(设计了一个银行卡类)。我这就是在套娃。

 

 

 

 

 代码如下:

  1 import java.util.ArrayList;
  2 import java.util.Arrays;
  3 import java.util.Collections;
  4 import java.util.Scanner;
  5 
  6 public class Main{
  7     public static void main(String[] args){
  8         Scanner input = new Scanner(System.in);
  9         
 10         ChinaUnionPay CUP = new ChinaUnionPay();
 11         CUP.Bank_Init();
 12         
 13         String s;
 14         while(!(s=input.nextLine()).trim().equals("#")) {
 15             String[] str = s.trim().split("\\s+");
 16             switch(str.length) {
 17             case 1:
 18                 if(CUP.isCardId_CCB(str[0])||CUP.isCardId_ICCB(str[0])) {
 19                     System.out.printf("¥%.2f\n", CUP.balance(str[0]));
 20                 }
 21                 else {
 22                     System.out.println("Sorry,this card does not exist.");
 23                 }
 24                 break;
 25             case 4:
 26                 if(!CUP.isCardId_CCB(str[0])&&!CUP.isCardId_ICCB(str[0])) {
 27                     System.out.println("Sorry,this card does not exist.");
 28                 }
 29                 else if(!CUP.isCardPassId(str[1])) {
 30                     System.out.println("Sorry,your password is wrong.");
 31                 }
 32                 else if(!CUP.isATMId_CCB(str[2])&&!CUP.isATMId_ICCB(str[2])) {
 33                     System.out.println("Sorry,the ATM's id is wrong.");
 34                 }
 35                 else if(Double.valueOf(str[3])>CUP.balance(str[0])) {
 36                     System.out.println("Sorry,your account balance is insufficient.");
 37                 }
 38                 else if(CUP.isCardId_CCB(str[0])!=CUP.isATMId_CCB(str[2])) {
 39                     System.out.println("Sorry,cross-bank withdrawal is not supported.");
 40                 }
 41                 else {
 42                     CUP.setbalance(str[0], Double.valueOf(str[3]));
 43                     CUP.show(str[0], str[2], Double.valueOf(str[3]));
 44                 }
 45                 break;
 46             default:
 47                 System.exit(0);
 48             }
 49         }
 50         
 51         input.close();
 52     }
 53 }
 54 /*
 55  * 中国银联
 56  */
 57 class ChinaUnionPay{
 58     private Bank CCB = new Bank();
 59     private Bank ICCB = new Bank();
 60     
 61     void Bank_Init(){
 62         Account a = new Account("杨过", "3217000010041315709");
 63         a.addCard(new Card("6217000010041315709"));
 64         a.addCard(new Card("6217000010041315715"));
 65         this.CCB.addAccountList(a);
 66         
 67         a = new Account("杨过", "3217000010041315715");
 68         a.addCard(new Card("6217000010041315718"));
 69         this.CCB.addAccountList(a);
 70         
 71         a = new Account("郭靖", "3217000010051320007");
 72         a.addCard(new Card("6217000010051320007"));
 73         this.CCB.addAccountList(a);
 74         
 75         a = new Account("张无忌", "3222081502001312389");
 76         a.addCard(new Card("6222081502001312389"));
 77         this.ICCB.addAccountList(a);
 78         
 79         a = new Account("张无忌", "3222081502001312390");
 80         a.addCard(new Card("6222081502001312390"));
 81         this.ICCB.addAccountList(a);
 82         
 83         a = new Account("张无忌", "3222081502001312399");
 84         a.addCard(new Card("6222081502001312399"));
 85         a.addCard(new Card("6222081502001312400"));
 86         this.ICCB.addAccountList(a);
 87         
 88         a = new Account("韦小宝", "3222081502001320785");
 89         a.addCard(new Card("6222081502001320785"));
 90         this.ICCB.addAccountList(a);
 91         
 92         a = new Account("韦小宝", "3222081502001320786");
 93         a.addCard(new Card("6222081502001320786"));
 94         this.ICCB.addAccountList(a);
 95         
 96         this.CCB.addAtmList("01");
 97         this.CCB.addAtmList("02");
 98         this.CCB.addAtmList("03");
 99         this.CCB.addAtmList("04");
100         this.ICCB.addAtmList("05");
101         this.ICCB.addAtmList("06");
102     }
103     String name(String s) {//返回用户姓名
104         for(Account i: this.CCB.getAccountList()) {
105             for(Card j: i.getCard()) {
106                 if(j.getId().equals(s)) {
107                     return i.getName();
108                 }
109             }
110         }
111         for(Account i: this.ICCB.getAccountList()) {
112             for(Card j: i.getCard()) {
113                 if(j.getId().equals(s)) {
114                     return i.getName();
115                 }
116             }
117         }
118         return null;
119     }
120     Double balance(String s) {//返回账户余额
121         for(Account i: this.CCB.getAccountList()) {
122             for(Card j: i.getCard()) {
123                 if(j.getId().equals(s)) {
124                     return i.getBalance();
125                 }
126             }
127         }
128         for(Account i: this.ICCB.getAccountList()) {
129             for(Card j: i.getCard()) {
130                 if(j.getId().equals(s)) {
131                     return i.getBalance();
132                 }
133             }
134         }
135         return 0.0;
136     }
137     boolean isCardId_CCB(String s) {//卡号是否存在
138         for(Account i: this.CCB.getAccountList()) {
139             for(Card j: i.getCard()) {
140                 if(j.getId().equals(s)) {
141                     return true;
142                 }
143             }
144         }
145         return false;
146     }
147     boolean isCardId_ICCB(String s) {//卡号是否存在
148         for(Account i: this.ICCB.getAccountList()) {
149             for(Card j: i.getCard()) {
150                 if(j.getId().equals(s)) {
151                     return true;
152                 }
153             }
154         }
155         return false;
156     }
157     boolean isCardPassId(String s) {//密码是否正确
158         if(s.equals("88888888"))
159             return true;
160         return false;
161     }
162     boolean isATMId_CCB(String s) {//ATM机编号是否存在
163         for(String i: this.CCB.getAtmList()) {
164             if(i.equals(s)) {
165                 return true;
166             }
167         }
168         return false;
169     }
170     boolean isATMId_ICCB(String s) {//ATM机编号是否存在
171         for(String i: this.ICCB.getAtmList()) {
172             if(i.equals(s)) {
173                 return true;
174             }
175         }
176         return false;
177     }
178     void setbalance(String s, double num) {//存取款
179         for(Account i: this.CCB.getAccountList()) {
180             for(Card j: i.getCard()) {
181                 if(j.getId().equals(s)) {
182                     i.setBalance(num);
183                     return;
184                 }
185             }
186         }
187         for(Account i: this.ICCB.getAccountList()) {
188             for(Card j: i.getCard()) {
189                 if(j.getId().equals(s)) {
190                     i.setBalance(num);
191                     return;
192                 }
193             }
194         }
195     }
196     void show(String cardId, String atmId, double num) {
197         System.out.print(this.name(cardId));
198         if(this.isATMId_CCB(atmId)) {
199             System.out.print("在中国建设银行的");
200         }
201         if(this.isATMId_ICCB(atmId)) {
202             System.out.print("在中国工商银行的");
203         }
204         System.out.print(atmId+"号ATM机上");
205         if(num>0) {
206             System.out.print("取款");
207         }
208         else {
209             System.out.print("存款");
210         }
211         System.out.printf("¥%.2f\n", Math.abs(num));
212         System.out.printf("当前余额为¥%.2f\n", this.balance(cardId));
213     }
214 }
215 /*
216  * 银行类
217  */
218 class Bank{
219     private ArrayList<Account> accountList = new ArrayList<Account>();
220     private ArrayList<String> atmList = new ArrayList<String>();
221     
222     Bank(){
223         
224     }
225     void addAccountList(Account a) {
226         this.accountList.add(a);
227     }
228     void addAtmList(String s) {
229         this.atmList.add(s);
230     }
231     ArrayList<Account> getAccountList() {
232         return accountList;
233     }
234     ArrayList<String> getAtmList() {
235         return atmList;
236     }
237 }
238 /*
239  * 账号类
240  */
241 class Account{
242     private String id = new String();
243     private String name = new String();
244     private Double balance = 10000.00;
245     private ArrayList<Card> card = new ArrayList<Card>();
246     
247     Account(String name, String id){
248         this.name = name;
249         this.id = id;
250     }
251     void addCard(Card card) {
252         this.card.add(card);
253     }
254     void setBalance(double num) {
255         this.balance -= num;
256     }
257     String getId() {
258         return id;
259     }
260     String getName() {
261         return name;
262     }
263     Double getBalance() {
264         return balance;
265     }
266     ArrayList<Card> getCard() {
267         return card;
268     }
269 }
270 /*
271  * 卡号类
272  */
273 class Card{
274     private String id = new String();
275     private String passId = "88888888";
276     
277     Card(String id){
278         this.id = id;
279     }
280     String getId() {
281         return id;
282     }
283     void setId(String id) {
284         this.id = id;
285     }
286     String getPassId() {
287         return passId;
288     }
289     void setPassId(String passId) {
290         this.passId = passId;
291     }
292 }
View Code

类图如下:

 

 

圈复杂度分析:

 

 

 

==========

 题目集9:根据题目集8增加了内容:银行账户分为借记账户和贷记账户两种,其中,借记账户不能够透支 取款,而贷记账户可以透支取款(可能需要支付手续费)。整体来讲我对类结构没有什么改变,所以我认为我的类设计的还行,不算太差。不过在算法方面没有想好,改动比较多。

 

 

 

 

 代码如下:

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 public class Main{
  5     public static void main(String[] args){
  6         Scanner input = new Scanner(System.in);
  7         
  8         ChinaUnionPay CUP = new ChinaUnionPay();
  9         CUP.Bank_Init();
 10         
 11         String s;
 12         while(!(s=input.nextLine()).trim().equals("#")) {
 13             String[] str = s.trim().split("\\s+");
 14             switch(str.length) {
 15             case 1:
 16                 if(CUP.isCardId_CCB(str[0])||CUP.isCardId_ICCB(str[0])||CUP.isCardId_ABC(str[0])) {
 17                     System.out.printf("业务:查询余额 ¥%.2f\n", CUP.balance(str[0]));
 18                 }
 19                 else {
 20                     System.out.println("Sorry,this card does not exist.");
 21                 }
 22                 break;
 23             case 4:
 24                 if(!CUP.isCardId_CCB(str[0])&&!CUP.isCardId_ICCB(str[0])&&!CUP.isCardId_ABC(str[0])) {
 25                     System.out.println("Sorry,this card does not exist.");
 26                 }
 27                 else if(!CUP.isCardPassId(str[1])) {
 28                     System.out.println("Sorry,your password is wrong.");
 29                 }
 30                 else if(!CUP.isATMId_CCB(str[2])&&!CUP.isATMId_ICCB(str[2])&&!CUP.isATMId_ABC(str[2])) {
 31                     System.out.println("Sorry,the ATM's id is wrong.");
 32                 }
 33                 else if( (CUP.isCardId_CCB(str[0])&&CUP.isATMId_CCB(str[2]))
 34                         ||(CUP.isCardId_ICCB(str[0])&&CUP.isATMId_ICCB(str[2]))
 35                         ||(CUP.isCardId_ABC(str[0])&&CUP.isATMId_ABC(str[2])) ) {//同银行取款
 36                     double num = Double.valueOf(str[3]);//取款金额
 37                     double num1 = CUP.balance(str[0]);//余额
 38                     if(CUP.type(str[0]).equals("2")) {//是否为贷记
 39                         if(CUP.balance(str[0])>0&&num1>=num) {
 40                             CUP.setbalance(str[0], num);
 41                             CUP.show(str[0], str[2], num);
 42                         }
 43                         else if(CUP.balance(str[0])>0&&num1<num){
 44                             if( (num+(num-num1)*0.05)<=num1+50000 ) {
 45                                 CUP.setbalance(str[0], num+(num-num1)*0.05);
 46                                 CUP.show(str[0], str[2], num);
 47                             }
 48                             else {
 49                                 System.out.println("Sorry,your account balance is insufficient.");
 50                             }
 51                         }
 52                         else {
 53                             if(num*1.05<=num1+50000) {
 54                                 CUP.setbalance(str[0], num*1.05);
 55                                 CUP.show(str[0], str[2], num);
 56                             }
 57                             else {
 58                                 System.out.println("Sorry,your account balance is insufficient.");
 59                             }
 60                         }
 61                     }
 62                     else {
 63                         if(CUP.balance(str[0])>=num) {
 64                             CUP.setbalance(str[0], num);
 65                             CUP.show(str[0], str[2], num);
 66                         }
 67                         else {
 68                             System.out.println("Sorry,your account balance is insufficient.");
 69                         }
 70                     }
 71                 }
 72                 else {//转行取款
 73                     double num = Double.valueOf(str[3]);//取款金额
 74                     double num1 = CUP.balance(str[0]);//余额
 75                     double num2 = num*CUP.rate(str[2]);//手续费
 76                     if(CUP.type(str[0]).equals("2")) {//是否为贷记
 77                         if(CUP.balance(str[0])>0&&num1>=num2+num) {
 78                             CUP.setbalance(str[0], num2+num);
 79                             CUP.show(str[0], str[2], num);
 80                         }
 81                         else if(CUP.balance(str[0])>0&&num1<num2+num){
 82                             if( (num2+num+(num-num1)*0.05)<=num1+50000 ) {
 83                                 CUP.setbalance(str[0], num2+num+(num-num1)*0.05);
 84                                 CUP.show(str[0], str[2], num);
 85                             }
 86                             else {
 87                                 System.out.println("Sorry,your account balance is insufficient.");
 88                             }
 89                         }
 90                         else {
 91                             if(num2+num*1.05<=num1+50000) {
 92                                 CUP.setbalance(str[0], num2+num*1.05);
 93                                 CUP.show(str[0], str[2], num);
 94                             }
 95                             else {
 96                                 System.out.println("Sorry,your account balance is insufficient.");
 97                             }
 98                         }
 99                     }
100                     else {
101                         if(CUP.balance(str[0])>=num2+num) {
102                             CUP.setbalance(str[0], num2+num);
103                             CUP.show(str[0], str[2], num);
104                         }
105                         else {
106                             System.out.println("Sorry,your account balance is insufficient.");
107                         }
108                     }
109                 }
110                 break;
111             default:
112                 System.exit(0);
113             }
114         }
115         
116         input.close();
117     }
118 }
119 /*
120  * 中国银联
121  */
122 class ChinaUnionPay{
123     private Bank CCB = new Bank();//中国建设银行
124     private Bank ICCB = new Bank();//中国工商银行
125     private Bank ABC = new Bank();//中国农业银行
126     
127     void Bank_Init(){
128         CCB.setCommissionRates(0.02);
129         ICCB.setCommissionRates(0.03);
130         ABC.setCommissionRates(0.04);
131         
132         Account a = new Account("杨过", "3217000010041315709");
133         a.addCard(new Card("6217000010041315709"));
134         a.addCard(new Card("6217000010041315715"));
135         this.CCB.addAccountList(a);
136         
137         a = new Account("杨过", "3217000010041315715");
138         a.addCard(new Card("6217000010041315718"));
139         this.CCB.addAccountList(a);
140         
141         a = new Account("郭靖", "3217000010051320007");
142         a.addCard(new Card("6217000010051320007"));
143         this.CCB.addAccountList(a);
144         
145         a = new Account("张无忌", "3222081502001312389");
146         a.addCard(new Card("6222081502001312389"));
147         this.ICCB.addAccountList(a);
148         
149         a = new Account("张无忌", "3222081502001312390");
150         a.addCard(new Card("6222081502001312390"));
151         this.ICCB.addAccountList(a);
152         
153         a = new Account("张无忌", "3222081502001312399");
154         a.addCard(new Card("6222081502001312399"));
155         a.addCard(new Card("6222081502001312400"));
156         this.ICCB.addAccountList(a);
157         
158         a = new Account("韦小宝", "3222081502051320785");
159         a.addCard(new Card("6222081502051320785"));
160         this.ICCB.addAccountList(a);
161         
162         a = new Account("韦小宝", "3222081502051320786");
163         a.addCard(new Card("6222081502051320786"));
164         this.ICCB.addAccountList(a);
165         
166         this.CCB.addAtmList("01");
167         this.CCB.addAtmList("02");
168         this.CCB.addAtmList("03");
169         this.CCB.addAtmList("04");
170         this.ICCB.addAtmList("05");
171         this.ICCB.addAtmList("06");
172         
173         //新纪录
174         a = new Account("张三丰", "3640000010045442002");
175         a.setType("2");
176         a.addCard(new Card("6640000010045442002"));
177         a.addCard(new Card("6640000010045442003"));
178         this.CCB.addAccountList(a);
179         
180         a = new Account("令狐冲", "3640000010045441009");
181         a.setType("2");
182         a.addCard(new Card("6640000010045441009"));
183         this.ICCB.addAccountList(a);
184         
185         a = new Account("乔峰", "3630000010033431001");
186         a.setType("2");
187         a.addCard(new Card("6630000010033431001"));
188         this.ABC.addAccountList(a);
189         
190         a = new Account("洪七公", "3630000010033431008");
191         a.setType("2");
192         a.addCard(new Card("6630000010033431008"));
193         this.ABC.addAccountList(a);
194         
195         this.ABC.addAtmList("07");
196         this.ABC.addAtmList("08");
197         this.ABC.addAtmList("09");
198         this.ABC.addAtmList("10");
199         this.ABC.addAtmList("11");
200     }
201     String type(String s) {//返回账号类型
202         for(Account i: this.CCB.getAccountList()) {
203             for(Card j: i.getCard()) {
204                 if(j.getId().equals(s)) {
205                     return i.getType();
206                 }
207             }
208         }
209         for(Account i: this.ICCB.getAccountList()) {
210             for(Card j: i.getCard()) {
211                 if(j.getId().equals(s)) {
212                     return i.getType();
213                 }
214             }
215         }
216         for(Account i: this.ABC.getAccountList()) {
217             for(Card j: i.getCard()) {
218                 if(j.getId().equals(s)) {
219                     return i.getType();
220                 }
221             }
222         }
223         return null;
224     }
225     Double rate(String s) {//返回各银行的利率
226         if(isATMId_CCB(s)) {
227             return this.CCB.getCommissionRates();
228         }
229         else if(isATMId_ICCB(s)) {
230             return this.ICCB.getCommissionRates();
231         }
232         else if(isATMId_ABC(s)) {
233             return this.ABC.getCommissionRates();
234         }
235         else {
236             return 0.0;
237         }
238     }
239     String name(String s) {//返回用户姓名
240         for(Account i: this.CCB.getAccountList()) {
241             for(Card j: i.getCard()) {
242                 if(j.getId().equals(s)) {
243                     return i.getName();
244                 }
245             }
246         }
247         for(Account i: this.ICCB.getAccountList()) {
248             for(Card j: i.getCard()) {
249                 if(j.getId().equals(s)) {
250                     return i.getName();
251                 }
252             }
253         }
254         for(Account i: this.ABC.getAccountList()) {
255             for(Card j: i.getCard()) {
256                 if(j.getId().equals(s)) {
257                     return i.getName();
258                 }
259             }
260         }
261         return null;
262     }
263     Double balance(String s) {//返回账户余额
264         for(Account i: this.CCB.getAccountList()) {
265             for(Card j: i.getCard()) {
266                 if(j.getId().equals(s)) {
267                     return i.getBalance();
268                 }
269             }
270         }
271         for(Account i: this.ICCB.getAccountList()) {
272             for(Card j: i.getCard()) {
273                 if(j.getId().equals(s)) {
274                     return i.getBalance();
275                 }
276             }
277         }
278         for(Account i: this.ABC.getAccountList()) {
279             for(Card j: i.getCard()) {
280                 if(j.getId().equals(s)) {
281                     return i.getBalance();
282                 }
283             }
284         }
285         return 0.0;
286     }
287     boolean isCardId_CCB(String s) {//卡号是否存在
288         for(Account i: this.CCB.getAccountList()) {
289             for(Card j: i.getCard()) {
290                 if(j.getId().equals(s)) {
291                     return true;
292                 }
293             }
294         }
295         return false;
296     }
297     boolean isCardId_ICCB(String s) {//卡号是否存在
298         for(Account i: this.ICCB.getAccountList()) {
299             for(Card j: i.getCard()) {
300                 if(j.getId().equals(s)) {
301                     return true;
302                 }
303             }
304         }
305         return false;
306     }
307     boolean isCardId_ABC(String s) {//卡号是否存在
308         for(Account i: this.ABC.getAccountList()) {
309             for(Card j: i.getCard()) {
310                 if(j.getId().equals(s)) {
311                     return true;
312                 }
313             }
314         }
315         return false;
316     }
317     boolean isCardPassId(String s) {//密码是否正确
318         if(s.equals("88888888"))
319             return true;
320         return false;
321     }
322     boolean isATMId_CCB(String s) {//ATM机编号是否存在
323         for(String i: this.CCB.getAtmList()) {
324             if(i.equals(s)) {
325                 return true;
326             }
327         }
328         return false;
329     }
330     boolean isATMId_ICCB(String s) {//ATM机编号是否存在
331         for(String i: this.ICCB.getAtmList()) {
332             if(i.equals(s)) {
333                 return true;
334             }
335         }
336         return false;
337     }
338     boolean isATMId_ABC(String s) {//ATM机编号是否存在
339         for(String i: this.ABC.getAtmList()) {
340             if(i.equals(s)) {
341                 return true;
342             }
343         }
344         return false;
345     }
346     void setbalance(String s, double num) {//存取款
347         for(Account i: this.CCB.getAccountList()) {
348             for(Card j: i.getCard()) {
349                 if(j.getId().equals(s)) {
350                     i.setBalance(num);
351                     return;
352                 }
353             }
354         }
355         for(Account i: this.ICCB.getAccountList()) {
356             for(Card j: i.getCard()) {
357                 if(j.getId().equals(s)) {
358                     i.setBalance(num);
359                     return;
360                 }
361             }
362         }
363         for(Account i: this.ABC.getAccountList()) {
364             for(Card j: i.getCard()) {
365                 if(j.getId().equals(s)) {
366                     i.setBalance(num);
367                     return;
368                 }
369             }
370         }
371     }
372     void show(String cardId, String atmId, double num) {
373         System.out.print("业务:");
374         if(num>0) {
375             System.out.print("取款 ");
376         }
377         else {
378             System.out.print("存款 ");
379         }
380         System.out.print(this.name(cardId));
381         if(this.isATMId_CCB(atmId)) {
382             System.out.print("在中国建设银行的");
383         }
384         if(this.isATMId_ICCB(atmId)) {
385             System.out.print("在中国工商银行的");
386         }
387         if(this.isATMId_ABC(atmId)) {
388             System.out.print("在中国农业银行的");
389         }
390         System.out.print(atmId+"号ATM机上");
391         if(num>0) {
392             System.out.print("取款");
393         }
394         else {
395             System.out.print("存款");
396         }
397         System.out.printf("¥%.2f\n", Math.abs(num));
398         System.out.printf("当前余额为¥%.2f\n", this.balance(cardId));
399     }
400 }
401 /*
402  * 银行类
403  */
404 class Bank{
405     private ArrayList<Account> accountList = new ArrayList<Account>();
406     private ArrayList<String> atmList = new ArrayList<String>();
407     private double commissionRates = 0;
408     
409     Bank(){
410         
411     }
412     void addAccountList(Account a) {
413         this.accountList.add(a);
414     }
415     void addAtmList(String s) {
416         this.atmList.add(s);
417     }
418     ArrayList<Account> getAccountList() {
419         return accountList;
420     }
421     ArrayList<String> getAtmList() {
422         return atmList;
423     }
424     double getCommissionRates() {
425         return commissionRates;
426     }
427     void setCommissionRates(double commissionRates) {
428         this.commissionRates = commissionRates;
429     }
430 }
431 /*
432  * 账号类
433  */
434 class Account{
435     private String id = new String();
436     private String type = "1";//1为借记,2为贷记,默认为借记
437     private String name = new String();
438     private Double balance = 10000.00;
439     private ArrayList<Card> card = new ArrayList<Card>();
440     
441     Account(String name, String id){
442         this.name = name;
443         this.id = id;
444     }
445     void addCard(Card card) {
446         this.card.add(card);
447     }
448     void setBalance(double num) {
449         this.balance -= num;
450     }
451     String getId() {
452         return id;
453     }
454     String getName() {
455         return name;
456     }
457     Double getBalance() {
458         return balance;
459     }
460     ArrayList<Card> getCard() {
461         return card;
462     }
463     String getType() {
464         return type;
465     }
466     void setType(String type) {
467         this.type = type;
468     }
469 }
470 /*
471  * 卡号类
472  */
473 class Card{
474     private String id = new String();
475     private String passId = "88888888";
476     
477     Card(String id){
478         this.id = id;
479     }
480     String getId() {
481         return id;
482     }
483     void setId(String id) {
484         this.id = id;
485     }
486     String getPassId() {
487         return passId;
488     }
489     void setPassId(String passId) {
490         this.passId = passId;
491     }
492 }
View Code

 

类图如下:

 

 圈复杂度分析:

 

 

 

 

 

 

三.采坑心得:

   题目银行卡初始化打错了账号,所以debug的时候可太难受了。(一定要细心啊)

 

四.改进建议:

    感觉挺好的,不用改了吧

五.总结:

  面向对象类的设计真的感觉挺难的,如果有了一个好的设计,编写代码优化代码都会很舒服。

  对于继承、多态、以及接口都能比较熟练的掌握。

posted @ 2021-12-18 21:25  漓尘  阅读(39)  评论(0)    收藏  举报