OO第三次博客

              OO第三次Blog

(1)    前言  本阶段的知识点就是掌握java中的Fianl关键字的使用

抽象类的使用

接口的基本概念使用的基本语法 还有Map, List类的使用方法。

1 接口的定义:Java中接口是一系列方法的声明,是特征方法的集合。接口并不是类,编写接口的方式和类很相似,但是它们属于不同的概念。类描述对象的属性和方法。接口则包含类要实现的方法。

接口与类的区别

  • · 接口不能用于实例化对象。
  • · 接口没有构造方法。
  • · 接口中所有的方法必须是抽象方法。
  • · 接口不能包含成员变量,除了 static 和 final 变量。
  • · 接口不是被类继承了,而是要被类实现。
  • · 接口支持多继承。

 

2.fianl 的作用

用来修饰一个类

用来修饰引用

用来修饰方法

Final的使用注意事项:修饰引用使。如果引用的是数据基本类型,那么数值是常量,不可更改

修饰方法 无法被子类重写

3.使用面向对象技术设计ATM仿真取款机系统。

(2)设计与分析:

 

题目集的第一题的sourcemonitor的图为  ,发现复杂度并不高

 

 

 

第二题:这里显示有100个类和方法,说明复杂度还是蛮高的

 

 

 

题目集7的第一二题目的类图为:

 

 

 

这里解释一下,Shape类是抽象类,而其他五个图形类都是继承他的具体子类,方法进行了重写,每个子类求面积都有自己的公式方法。

题目集7-1 7-2 的方法设计的思路:

考虑一款适合于小学生的卡片(Card)排序游戏,其规则为随机发放一些卡片给学生,卡片分为 四种形状:圆形(Circle)、矩形(Rectangle)、三角形(Triangle)及梯形(Trapezoid),并给出各种 卡片的相应参数,要求学生能够迅速求出各卡片的面积大小然后将卡片按照其面积值从大到小进行排 序,同时求出所有卡片的面积之和。

对卡片排序时使用 Comparable 接口, 即 Card 类需要实现 Comparable 接口中的 CompareTo()方法。

代码如下;

题目集8的类图如下:

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

 

public class Main {

             

              public static Scanner input = new Scanner(System.in);

              public static void main(String[] args){

              ArrayList<Integer> list = new ArrayList<Integer>();

              int k = input.nextInt();

              while(k != 0){

              if(k>4|| k <0 ){

              System.out.println("Wrong Format");

              System.exit(0);

              }

               list.add(k);

               k = input.nextInt();

              }

              DealCardList dealCardList = new DealCardList(list);

              if(!dealCardList.validate()){

              System.out.println("Wrong Format");

              System.exit(0);

              }

              dealCardList.showResult();

              input.close();

              }

       }

class Card{

        Shape shape;

       Card(){

             

       }

       Card(Shape shape){

              this.shape=shape;

       }

       public void setShape(Shape Shape) {

              this.shape=shape;

       }

       public Shape getShape() {

              return shape;

       }

      

      

}

class DealCardList{

       ArrayList<Card> cardList=new ArrayList<Card>();

       DealCardList(){

             

       }

       DealCardList(ArrayList<Integer> list){

              for(int k=0;k<list.size();k++)

              {

                     if(list.get(k)==1)

                     {

                            double r=Main.input.nextDouble();

                            Circle circle=new Circle(r);

                            Card j=new Card(circle);

                            j.getShape().setShapeName("Circle");

                            cardList.add(j);     

                     }

                     if(list.get(k)==2) {

                            double a=Main.input.nextDouble();

                            double b=Main.input.nextDouble();

                            Rectangle rectangle=new Rectangle(a,b);

                            Card card2=new Card(rectangle);

                            card2.getShape().setShapeName("Rectangle");

                            cardList.add(card2);

                     }

                     if(list.get(k)==3) {

                            double a=Main.input.nextDouble();

                            double b=Main.input.nextDouble();

                            double c=Main.input.nextDouble();

                            Triangle triangle=new Triangle(a,b,c);

                            Card cc=new Card(triangle);

                            cc.getShape().setShapeName("Triangle");

                            cardList.add(cc);

                     }

                     if(list.get(k)==4) {

                            double a=Main.input.nextDouble();

                            double b=Main.input.nextDouble();

                            double c=Main.input.nextDouble();

                            Traperoid traperoid=new Traperoid(a,b,c);

                            Card card4=new Card(traperoid);

                            card4.getShape().setShapeName("Trapezoid");

                            cardList.add(card4);

                     }

                     }

       }

       public boolean validate() {

              for(int i=0;i<cardList.size();i++)

                     {if(!cardList.get(i).getShape().vaildate()) {

                                   return false;

                     }

                     }

              return true;

       }

       public void cardSort() {

              for(int k=0;k<cardList.size();k++)

                     for(int m=k+1;m<cardList.size();m++)

                     {

                            if(cardList.get(k).getShape().getArea()<cardList.get(m).getShape().getArea())

                                   Collections.swap(cardList, k, m);

                     }

             

             

       }

       public double getAllArea() {

              double sum=0;

              for(int ic=0;ic<cardList.size();ic++)

              sum=sum+cardList.get(ic).getShape().getArea();

              return sum;

       }

       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<cardList.size();i++) {

 

              if(cardList.get(i).getShape().getShapeName()=="Circle") {

              System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

             

              }

              }

              System.out.print("]");

              System.out.print("[");

              for(int i=0;i<cardList.size();i++) {

 

                     if(cardList.get(i).getShape().getShapeName()=="Rectangle") {

                     System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                    

                     }

                     }

              System.out.print("]");

              System.out.print("[");

              for(int i=0;i<cardList.size();i++) {

 

                     if(cardList.get(i).getShape().getShapeName()=="Triangle") {

                     System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                    

                     }

                     }

              System.out.print("][");

             

              for(int i=0;i<cardList.size();i++) {

 

                     if(cardList.get(i).getShape().getShapeName()=="Trapezoid") {

                     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 sorted List:");

              System.out.print("[");

              cardSort();

      

              for(int i=0;i<cardList.size();i++) {

 

                     if(cardList.get(i).getShape().getShapeName()=="Circle") {

                     System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                     }

                     }

              System.out.print("][");

              for(int i=0;i<cardList.size();i++) {

 

                     if(cardList.get(i).getShape().getShapeName()=="Rectangle") {

                     System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                     }

                     }

              System.out.print("][");

              for(int i=0;i<cardList.size();i++) {

 

                     if(cardList.get(i).getShape().getShapeName()=="Triangle") {

                     System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                     }

                     }

              System.out.print("][");

              for(int i=0;i<cardList.size();i++) {

 

                     if(cardList.get(i).getShape().getShapeName()=="Trapezoid") {

                     System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                     }

                     }

              System.out.print("]");

              System.out.println("");

               

              System.out.print("The max area:"+String.format("%.2f",cardList.get(0).getShape().getArea()));

 

 

 

             

       }

}

 class Shape {

       private String shapeName;

       Shape(){

             

       }

       Shape(String shapeName){

              this.shapeName=shapeName;

       }

       public String getShapeName() {

              return shapeName;

       }

      

       public boolean vaildate() {

              return true;

       }

       public void setShapeName(String shapeName) {

              this.shapeName=shapeName;

       }

       public double getArea() {

              return 0.0;

       }

      

}

class Circle extends Shape{

       private double radius;

       Circle(){

             

       }

       Circle(double radius){

              this.radius=radius;

       }

       public double getArea() {

              return Math.PI*Math.pow(radius,2);

       }

       public boolean vaildate() {

              if(radius>0)

              return true;

              else return false;

       }

}

class Rectangle extends Shape{

       private double width,length;

       Rectangle (double width,double length){

              this.width=width;

              this.length=length;

       }

       public double getArea() {

              return width*length;

       }

       public boolean vaildate() {

              if(width>0&&length>0)

              return true;

              else return false;

       }

      

}

 

class Traperoid extends Shape{

       private double top,bottomSide,height;

       Traperoid(){

             

       }

       Traperoid(double topSide,double bottomSide,double height){

              this.bottomSide=bottomSide;

              this.top=topSide;

              this.height=height;

      

       }

       public boolean validate() {

              if(top>0&&bottomSide>0&&height>0)

                     return true;

              else return false;

       }

       public double getArea() {

              return (top+bottomSide)*height/2;

       }

      

}

class Triangle extends Shape{

        double side1,side2,side3;

        Triangle(double x,double y,double z){

               this.side1=x;

               this.side2=y;

               this.side3=z;

        }

        public boolean vaildate() {

                     if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1)

                     return true;

                     else

                            return false;

              }

 

        public double getArea() {

               double c=(side1+side2+side3)/2;

               double s=Math.sqrt(c*(c-side2)*(c-side1)*(c-side3));

                     return s;

              }

      

      

}

 

 

1)这里解释说明一下:

 

 

Account 类聚合Bank类和User类   Card类聚合Account 类 ,ATM聚合Bank类。Withdraw聚合UnionPay类,GetBalance聚合UnionPay类

 

 

2)设计思路和总结:先要在Main类中存入人和账户,身份证,账户余额,账户密码的信息,然后依次设计好自己所设计好的类,明白自己所设计的类之间的关系,条理要清晰,尽量把类之间的封装性控制好。

 

 

(3)踩坑心得

如;要把方法写对。改为getCardbyCardNO

 

抽象方法是不能带有大括号的

如:

解决方案就是把大括号去掉

Java 实现类的实现方法前面必须加上public.

(4)改进建议

如实验四的源码:

public class ObjectExistRule  extends AbstractRule{

    Animal animal2 =new Animal();

    Plant plant = new Plant();

   

   

 

    boolean objectExistRule = false;{

   

    if(animal2.isAlive==true &&plant.isAlive==true) {

        objectExistRule= true;

    }

    else {

        objectExistRule = false;

    }

   

   

 

}

 

}

接口的方法建议不加public

还有Comparable接口

要想一个类的对象是可比较的,就需要这个类实现 Comparable 接口,该接口可以使用泛型。

 

需要实现 int cpmpareTo(T other) 方法,如果这个对象小于 other 对象,则返回一个负整数;

修改后:题目集8的源码:‘import java.util.ArrayList;

import java.util.Iterator;

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

/**

 * 账户抽象类

 *

 * @author Administrator

 * @date

 */

abstract class AbstractAccount {

    private String accountNO;// 账号

    private double balance = 0;// 余额

    private User user = null;// 用户

    private Bank bank = null;// 所属银行

    private double ratio = 0;// 透支取款手续费

    private double overdrawAmount = 0;// 可透支金额

    private static ArrayList<Card> list = new ArrayList<Card>();

 

    public AbstractAccount() {

        super();

        // TODO Auto-generated constructor stub

    }

 

    public AbstractAccount(String accountNO, double balance, User user, Bank bank, double ratio,

        double overdrawAmount) {

        super();

        this.accountNO = accountNO;

        this.balance = balance;

        this.user = user;

        this.bank = bank;

        this.ratio = ratio;

        this.overdrawAmount = overdrawAmount;

    }

 

    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 double getOverdrawAmount() {

        return overdrawAmount;

    }

 

    public void setOverdrawAmount(double overdrawAmount) {

        this.overdrawAmount = overdrawAmount;

    }

 

    public Bank getBank() {

        return bank;

    }

 

    public void setBank(Bank bank) {

        this.bank = bank;

    }

 

    public double getRatio() {

        return ratio;

    }

 

    public void setRatio(double ratio) {

        this.ratio = ratio;

    }

 

    public static ArrayList<Card> getList() {

        return list;

    }

 

    public static void setList(ArrayList<Card> list) {

        AbstractAccount.list = list;

    }

 

    public void addCard(Card card) {

        list.add(card);

icbc.addATM(aTM5);

        icbc.addATM(aTM6);

        abc.addATM(aTM7);// new

        abc.addATM(aTM8);// new

        abc.addATM(aTM9);// new

        abc.addATM(aTM10);// new

        abc.addATM(aTM11);// new

 

        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 Zhangsanfeng = new User("360102199713459810", "张三丰", "13677871290");// new

        User Linghuchong = new User("360503190299878473", "令狐冲", "13609899238");// new

        User Qiaofeng = new User("360607194313666711", "乔峰", "13677871290");// new

        User Hongqigong = new User("363220987809872234", "洪七公", "13609009454");// new

 

        AbstractAccount ccbAcc1 = new DebitAccount("3217000010041315709", 10000.00, Yangguo, ccb, 0, 0);

        AbstractAccount ccbAcc2 = new DebitAccount("3217000010041315715", 10000.00, Yangguo, ccb, 0, 0);

        AbstractAccount ccbAcc3 = new DebitAccount("3217000010051320007", 10000.00, Guojing, ccb, 0, 0);

        AbstractAccount ccbAcc4 = new CreditAccount("3640000010045442002", 10000.00, Zhangsanfeng, ccb, 0.05, 50000.00);// new

 

        AbstractAccount icbcAcc1 = new DebitAccount("3222081502001312389", 10000.00, Zhangwuji, icbc, 0, 0);

        AbstractAccount icbcAcc2 = new DebitAccount("3222081502001312390", 10000.00, Zhangwuji, icbc, 0, 0);

        AbstractAccount icbcAcc3 = new DebitAccount("3222081502001312399", 10000.00, Zhangwuji, icbc, 0, 0);

        AbstractAccount icbcAcc4 = new DebitAccount("3222081502051320785", 10000.00, Weixiaobao, icbc, 0, 0);

        AbstractAccount icbcAcc5 = new DebitAccount("3222081502051320786", 10000.00, Weixiaobao, icbc, 0, 0);

        AbstractAccount icbcAcc6 =

            new CreditAccount("3640000010045441009", 10000.00, Linghuchong, icbc, 0.05, 50000.00);// new

 

        AbstractAccount abcAcc1 = new CreditAccount("3630000010033431001", 10000.00, Qiaofeng, abc, 0.05, 50000.00);// new

        AbstractAccount abcAcc2 = new CreditAccount("3630000010033431008", 10000.00, Hongqigong, abc, 0.05, 50000.00);// new

 

        ccb.addAccount(ccbAcc1);

        ccb.addAccount(ccbAcc2);

        ccb.addAccount(ccbAcc3);

        ccb.addAccount(ccbAcc4);// new

 

        icbc.addAccount(icbcAcc1);

        icbc.addAccount(icbcAcc2);

        icbc.addAccount(icbcAcc3);

        icbc.addAccount(icbcAcc4);

        icbc.addAccount(icbcAcc5);

        icbc.addAccount(icbcAcc6);// new

 

        abc.addAccount(abcAcc1);// new

        abc.addAccount(abcAcc2);// new

 

        Yangguo.addAccount(ccbAcc1);

        Yangguo.addAccount(ccbAcc2);

        Guojing.addAccount(ccbAcc3);

        Zhangsanfeng.addAccount(ccbAcc4);// new

 

        Zhangwuji.addAccount(icbcAcc1);

        Zhangwuji.addAccount(icbcAcc2);

        Zhangwuji.addAccount(icbcAcc3);

        Weixiaobao.addAccount(icbcAcc4);

        Weixiaobao.addAccount(icbcAcc5);

        Linghuchong.addAccount(icbcAcc6);// new

 

        Qiaofeng.addAccount(abcAcc1);// new

        Hongqigong.addAccount(abcAcc2);// new

 

        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);// new

        Card ccbCard12 = new Card("6640000010045442003", "88888888", ccbAcc4);// new

 

        Card icbcCard13 = new Card("6640000010045441009", "88888888", icbcAcc6);// new

 

        Card abcCard14 = new Card("6630000010033431001", "88888888", abcAcc1);// new

        Card abcCard15 = new Card("6630000010033431008", "88888888", abcAcc2);// new

 

        ccbAcc1.addCard(ccbCard1);

        ccbAcc1.addCard(ccbCard2);

        ccbAcc2.addCard(ccbCard3);

        ccbAcc3.addCard(ccbCard4);

        ccbAcc4.addCard(ccbCard11);// new

        ccbAcc4.addCard(ccbCard12);// new

 

        icbcAcc1.addCard(icbcCard5);

        icbcAcc2.addCard(icbcCard6);

        icbcAcc3.addCard(icbcCard7);

        icbcAcc3.addCard(icbcCard8);

        icbcAcc4.addCard(icbcCard9);

        icbcAcc5.addCard(icbcCard10);

        icbcAcc6.addCard(icbcCard13);// new

 

        abcAcc1.addCard(abcCard14);// new

        abcAcc2.addCard(abcCard15);// new

 

        return unionPay;

    }

}

 

/**

 * 银联类

 *

 * @author Administrator

 * @date

 */

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);

    }

}

 

/**

 * 用户类

 *

 * @author Administrator

 * @date

 */

class User {

    private String ID;// 身份证号

    private String name;// 姓名

    private String Phone;// 电话号码

    ArrayList<AbstractAccount> list = new ArrayList<AbstractAccount>();// 该用户拥有的账户列表(注意,账户可能隶属于不同银行

 

    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(AbstractAccount ccbAcc1) {

        this.list.add(ccbAcc1);

    }

 

    public void removeAccount(DebitAccount account) {

        this.list.remove(account);

    }

}

 

/**

 * 业务类,取款类

 *

 * @author Administrator

 * @date

 */

class Withdraw {

    // 以下属性为取款需要输入的信息

    private UnionPay unionPay;// 银联,一切业务操作的入口

    private String cardNO;// 卡号

    private String cardPassword;// 密码

    private String ATMID;// ATM编号

    private double amount;// 取款金额

 

    // 根据卡号获取卡对象

    private Card card = null;

    // 根据ATM编号获取ATM对象

    private ATM aTM = null;

    // 根据卡号获取对应账号

    private AbstractAccount account = null;

    // 根据ATM或ATM所属银行对象

    private Bank bank = null;

 

    public Withdraw() {

        super();

        // TODO Auto-generated constructor stub

    }

 

    /**

     *

     * @param unionPay

     * @param cardNO

     * @param cardPassword

     * @param aTMID

     * @param amount

     */

    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;

    }

 

    /**

     * 求跨行业务手续费

     *

     * @return

     */

    public double getCrossBankCommission(double amount) {

        return amount * bank.getCommission();

    }

 

    /**

     * 求透支取款手续费

     *

     * @param amount

     * @return

     */

    public double getOverdrawCommission(double amount) {

        return amount * account.getRatio();

    }

 

    /**

     * 校验是否为跨行取款

     */

    public boolean isCrossBank() {

        if (account.getBank().getBankNO() != aTM.getBank().getBankNO()) {

            return true;

        }

        return false;

    }

 

    /**

     * 判断是否为信用卡

     *

     * @return

     */

    public boolean isCreditAccount() {

        if (account instanceof CreditAccount) {

            return true;

        }

        return false;

    }

 

    /**

     * 校验输入的数据是否正确 注意:该方法不支持开闭原则,大家可以思考如何设计才能针对各种银行卡均能够实现开闭原则

     *

     * @return

     */

    public boolean checkData() {

 

        // 获取银行卡对象

        card = ATMUtil.getCardbyCardNO(unionPay, cardNO);

 

        // 银行卡不存在(卡号错误)

        if (card == null) {

            System.out.println("Sorry,this card does not exist.");

            return false;

        }

 

        // 获取ATM机对象

        aTM = ATMUtil.getATMbyATMID(unionPay, ATMID);

 

        // ATM机不存在(ATM ID错误)

        if (aTM == null) {

            System.out.println("Sorry,the ATM's id is wrong.");

            return false;

        }

 

        // 获取ATM机所隶属银行对象

        bank = aTM.getBank();

 

        // 用户密码错误

        if (!card.getCardPassword().equals(cardPassword)) {

            System.out.println("Sorry,your password is wrong.");

            return false;

        }

 

        // 根据卡号获取用户账号

        account = ATMUtil.getAccountbyCardNO(cardNO);

 

        // 如果为借记卡

        if (!isCreditAccount()) {

            double commission = 0;

 

            // 如果为跨行业务,则收取跨行费用

            if (this.isCrossBank()) {

                commission += this.getCrossBankCommission(amount);

            }

            // 取款超出余额

            if (amount + commission > account.getBalance()) {

                System.out.println("Sorry,your account balance is insufficient.");

                return false;

            }

        }

 

        // 如果为信用卡

        if (isCreditAccount()) {

            double commission = 0; // 手续费总额

            double overdrawAmount = 0; // 透支金额

            double balance = 0; // 当前卡上余额

 

            balance = account.getBalance(); // 获取当前卡余额

 

            // 有余额,则透支金额为取款金额与余额的差值

            if (balance >= 0) {

                overdrawAmount = amount - account.getBalance();

            } else {// 余额为负值(已经透支),则透支金额为取款金额

                overdrawAmount = amount;

            }

 

            // 收取跨行业务费用

            if (this.isCrossBank()) {

                commission += this.getCrossBankCommission(amount);

            }

            // 收取透支费用

            if (overdrawAmount > 0) {

                commission += this.getOverdrawCommission(overdrawAmount);

            }

 

            // 如果取款金额与手续费金额之和大于卡片余额与可透支最大金额之和,无法取款

            if (commission + amount > account.getBalance() + account.getOverdrawAmount()) {

                System.out.println("Sorry,your account balance is insufficient.");

                return false;

            }

        }

 

        return true;

    }

 

    /**

     * 该方法支持开闭原则

     */

    public void withdraw() {

        double commission = 0;

        double overdrawAmount = 0;

        double balance = 0;

 

        balance = account.getBalance();

 

        if (balance >= 0) {

            overdrawAmount = amount - account.getBalance();

        } else {

            overdrawAmount = amount;

        }

 

        // 收取跨行费用

        if (this.isCrossBank()) {

            commission += this.getCrossBankCommission(amount);

        }

        // 收取透支费用

        if (overdrawAmount > 0) {

            commission += this.getOverdrawCommission(overdrawAmount);

        }

 

        if (commission + amount > balance + account.getOverdrawAmount()) {

            System.out.println("Sorry,your account balance is insufficient.");

            System.exit(0);

        }

 

        this.account.setBalance(balance - amount - commission);

        showResult(this.account);

    }

 

    /**

     * 显示

     *

     * @param account

     */

    public void showResult(AbstractAccount account) {

        String userName = account.getUser().getName();

        String bankName = aTM.getBank().getBankName();

        System.out

            .println("业务:取款 " + userName + "在" + bankName + "的" + ATMID + "号ATM机上取款" + String.format("¥%.2f", amount));

 

        System.out.println("当前余额为" + String.format("¥%.2f", account.getBalance()));

    }

}

 

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

 

        UnionPay unionPay = InitData.initData();// 初始化程序数据

 

        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.println("业务:查询余额 " + String.format("¥%.2f", gb.getBalance(dataLine[0])));

            } else {// 取款功能

                Withdraw wd =

                    new Withdraw(unionPay, dataLine[0], dataLine[1], dataLine[2], Double.parseDouble(dataLine[3]));

                if (wd.checkData()) {

                    wd.withdraw();

                } else {

                    System.exit(0);

                }

            }

        }

    }

}

 

(5)总结:

知识点:接口的定义和使用,Map类的使用和List类的使用

泛化的使用,抽像类的一些注意事项。

接口的使用注意事项。

自己还需要的注意事项:

Java的接口的使用,Map类的使用还不够熟练,在程序的debug 过程中容易不注意一些细节的东西。

对于课程的建议:

课程的讲解内容可以详细一些,最好用一些简单实例讲解。

 

posted @ 2021-06-16 21:06  fxdcn  阅读(67)  评论(0)    收藏  举报