Blog作业03

------------恢复内容开始------------

目    录

一、前言

二、设计与分析

三、踩坑心得

四、改进建议

五、总结

一、前言

题目集七主要考察的知识点有类的继承、多态性使用方法、接口的应用,要求答题者掌握类的继承、多态性使用方法以及接口的应用,本题目集题量小,一共就两题,第一题难度较小,第二题难度较大;

题目集八主要考察的知识点有类的继承、多态性使用方法、接口的应用,要求答题者掌握并运用类的继承、多态性使用方法以及接口的应用到现实社会中,设计一个ATM仿真系统,本题目集题量更小,只有一题,但难度大,考验了答题者的综合素质;

题目集九主要考察的知识点有类的继承、多态性使用方法、接口的应用,要求答题者掌握并运用类的继承、多态性使用方法以及接口的应用到现实社会中,设计一个更加符合当前社会上的ATM功能的加强版ATM仿真系统,本题目集的特征与上一个题目集相似,可看作上一个题目集的升级版本。

二、设计与分析

题目集7(7-1)图形卡片排序游戏 

在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束,然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。

如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边不能组成三角形),则输出Wrong Format。

  • 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
  1. 排序前的各图形类型及面积,格式为图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格;
  2. 排序后的各图形类型及面积,格式同排序前的输出;
  3. 所有图形的面积总和,格式为Sum of area:总面积值。

代码类图如下:

 

 

 

 

 

 设计思路:首先把各个类的基本框架写好,然后把除主类以外的各个类的基本功能,变量,方法编写完整,最后在主类中输入要创建的图形对象代号,并调用其它类中的方法。

 代码分析:

DealCardList类:这个类主要是把主类中输入的数据进行判断与分类,若输入数字合法,则创建各数字对应的形状对象。里面包含输出结果的方法。

class DealCardList{
    ArrayList<Card> cardList=new ArrayList<>();

    public DealCardList(ArrayList<Integer> card) {
        for (Integer integer : card) {
            if (integer==0)break;
            switch (integer){
                case 1:
                    Card card1=new Card(new Circle(Main.input.nextDouble()));
                    card1.getShape().setShapeName("Circle");
                    cardList.add(card1);
                    break;
                case 2:
                    Card card2=new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble()));
                    card2.getShape().setShapeName("Rectangle");
                    cardList.add(card2);
                    break;
                case 3:
                    Card card3=new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
                    card3.getShape().setShapeName("Triangle");
                    cardList.add(card3);
                    break;
                case 4:
                    Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
                    card4.getShape().setShapeName("Trapezoid");
                    cardList.add(card4);
                    break;
            }

        }
    }
    public boolean validate(){
        boolean ret=true;
        for (Card card : cardList) {
            if (card.getShape().validate()==false){
                ret=false;
                break;
            }
        }
        return ret;
    }
    public void cardSort(){
        TreeSet<Card> cards = new TreeSet<>(cardList);
        for (Card card : cards) {
            System.out.print(card.getShape());
        }
    }
    public double getAllArea(){
        double sum=0;
        for (Card card : cardList) {
            sum+=card.getShape().getArea();
        }
        return sum;
    }
    public void showResult(){
        System.out.println("The original list:");
        for (Card card : cardList) {
            System.out.print(card.getShape());
        }
        System.out.println();
        System.out.println("The sorted list:");
        cardSort();
        System.out.println();
        System.out.printf("Sum of area:%.2f\n",getAllArea());
    }
}

抽象形状类:这个类里面有所有形状的共同特点,以这个类为父类通过继承的方式可以节省很多时间与空间。

abstract class Shape{
    private String shapeName;

    public Shape() {
    }

    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 String toString() {
        return getShapeName()+":"+String.format("%.2f ",getArea());
    }
}

以下是各个图形类:这些类的基本结构框架相似,只是有些特征不同,例如三角形的构成条件对边有一定限制需要做出判断,各个图形求面积的方法不同等。

class Circle extends Shape{
    private double radius;
    public Circle() {
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI*radius*radius;
    }

    @Override
    
    public boolean validate() {
        return this.radius>0;
    }
}

class Rectangle extends Shape{
    private double width,height;
    
   
   
   

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

   
    public void setWidth(double width) {
        this.width = width;
    }

   

    public void setHeight(double height) {
        this.height = height;
    }

    @Override
    public double getArea() {
        return height*width;
    }

    @Override
    public boolean validate() {
        if( width>0&&height>0) {
            return true;
        }
        else
            return false;
            
    }
}
class Triangle extends Shape{
    private double side1,side2,side3;
    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    @Override
    public double getArea() {
        double p=(side1+side2+side3)/2;
        return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
    }

    @Override
    public boolean validate() {
       
        if ((!(side1>0&&side3>0&&side2>0))|| (!(side1+side2>side3&&side1+side3>side2&&side2+side3>side1)))
            
        
        return false;
    
    else
        return true;
}
}

class Trapezoid extends Shape{
    private double height;
    private double upSide;
    private double downSide;

    public boolean validate() {
        if( upSide>0&&height>0&&downSide>0) {
            return true;
        }
        else
            return false;
    }

    public Trapezoid(double upSide, double downSide, double height) {
        this.upSide = upSide;
        this.downSide = downSide;
        this.height = height;
    }

   
    public double getArea() {
        return (upSide+downSide)*height/2;
    }    
}

这是接口类:

class Card implements Comparable<Card>{
    private Shape shape;

    public Card() {
    }

    public Card(Shape shape) {
        this.shape = shape;
    }

    public Shape getShape() {
        return shape;
    }

    public void setShape(Shape shape) {
        this.shape = shape;
    }

    @Override
    public int compareTo(Card card) {
        return -(int)(shape.getArea()-card.getShape().getArea());
    }
}

最后是主类:主类功能比较简单,主要是判断输入的数字是否是在1和4之间以及在输入0时判断输入结束,若输入数据合法,则创建DealCardList对象,然后通过DealCardList类中的方法完成程序的正常运行。


public class Main {
  
    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()==false){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        dealCardList.showResult();
        input.close();
        
    }
}

题目集7(7-2)图形卡片分组游戏

这题是上一题的拓展,大体上基本相同,增加了一些功能,输出时要按图形类型分组排序输出,以下是这题的输出方法

 public void showResult() {
        System.out.println("The original list:");
        for(int i=0;i<cardList.size();i++) {
            if(i==0)
                System.out.print("[");
            System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
            if(i==cardList.size()-1)
                System.out.print("]");
        }

        System.out.println();
        System.out.println("The Separated List:");

        for(int i=0;i<cardList1.size();i++) {
            if(i==0)
                System.out.print("[");
            System.out.print(cardList1.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList1.get(i).getShape().getArea())+" ");
            if(i==cardList1.size()-1)
                System.out.print("]");
        }
        if(cardList1.size() == 0)
            System.out.print("[]");
        for(int i=0;i<cardList2.size();i++) {
            if(i==0)
                System.out.print("[");
            System.out.print(cardList2.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList2.get(i).getShape().getArea())+" ");
            if(i==cardList2.size()-1)
                System.out.print("]");
        }
        if(cardList2.size() == 0)
            System.out.print("[]");
        for(int i=0;i<cardList3.size();i++) {
            if(i==0)
                System.out.print("[");
            System.out.print(cardList3.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList3.get(i).getShape().getArea())+" ");
            if(i==cardList3.size()-1)
                System.out.print("]");
        }
        if(cardList3.size() == 0)
            System.out.print("[]");
        for(int i=0;i<cardList4.size();i++) {
            if(i==0)
                System.out.print("[");
            System.out.print(cardList4.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList4.get(i).getShape().getArea())+" ");
            if(i==cardList4.size()-1)
                System.out.print("]");
        }
        if(cardList4.size() == 0)
            System.out.print("[]");
        System.out.println();
        cardSort();
        System.out.println("The Separated sorted List:");
        for(int i=0;i<cardList1.size();i++) {
            if(i==0)
                System.out.print("[");
            System.out.print(cardList1.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList1.get(i).getShape().getArea())+" ");
            if(i==cardList1.size()-1)
                System.out.print("]");
        }
        if(cardList1.size() == 0)
            System.out.print("[]");
        for(int i=0;i<cardList2.size();i++) {
            if(i==0)
                System.out.print("[");
            System.out.print(cardList2.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList2.get(i).getShape().getArea())+" ");
            if(i==cardList2.size()-1)
                System.out.print("]");
        }
        if(cardList2.size() == 0)
            System.out.print("[]");
        for(int i=0;i<cardList3.size();i++) {
            if(i==0)
                System.out.print("[");
            System.out.print(cardList3.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList3.get(i).getShape().getArea())+" ");
            if(i==cardList3.size()-1)
                System.out.print("]");
        }
        if(cardList3.size() == 0)
            System.out.print("[]");
        for(int i=0;i<cardList4.size();i++) {
            if(i==0)
                System.out.print("[");
            System.out.print(cardList4.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList4.get(i).getShape().getArea())+" ");
            if(i==cardList4.size()-1)
                System.out.print("]");
        }
        if(cardList4.size() == 0)
            System.out.print("[]");
        System.out.println();
        System.out.print("The max area:");
        System.out.println(String.format("%.2f",getAllArea()));
    }

这是我编写的代码的类图:

 

 还有代码复杂度:

 

 习题集8 ATM机类结构设计(一)

题目分析:

随着学习的深入,我们做的作业也更加接近现实生活,题目集8的ATM机仿真设计就是一个典例,通过这个题目可以让学生认识到自己将来的职业方向,让答题者对自己的专业有一个更深刻的认识的同时提高学习兴趣。这道题要求设计一个简单的ATM系统,包含了一些基础的简单的功能,如查询、取款、存款等。具体分析如下:

首先要把代码的基本框架先写好,结合题目要求与自身理解我用了四个类来完成,其中主类用于存储题目中要求的初始化数据、从键盘输入命令、判断输入是否结束、判断从键盘输入的内容分别是什么东西,然后分别创建对应的对象,并调用对象所对应的类中的方法实现题目输出要求。

class User {
    Account account[]=new Account[10];
    int a =0;
    private String bank;

    public String getBank() {
        return bank;
    }

    public void setBank(String bank) {
        this.bank = bank;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;

}

class Account
{
    Card card[]=new Card[10];
    private double balbace=10000;
    private String account;

    public void setAccount(String account) {
        this.account = account;
    }

    public double getBalbace() {
        return balbace;
    }

    public void setBalbace(double balbace) {
        this.balbace = balbace;
    }
    private int password=88888888;

    public int getPassword() {
        return password;
    }
}
class Card
{
    private String card;
    public String getCard() {
        return card;
    }
    public void setCard(String card) {
        this.card = card;
    }
}

User、Account、Card类中都是包含该类所具有的属性,无实际作用,这里就不仔细说明了。User类里包含的属性有:账户对象数组、银行名称、用户名称;Account类里包含的属性有:卡对象数组、卡初始余额、卡密码。

主类包含的内容就比较多了,首先是储存初始化的数据如下面的部分代码所示,这部分代码就是用来储存题目要求的初始化数据的,这部分代码虽然没什么难度,但很容易出错,稍不注意就把数据存错了,在编写这一部分代码时,我也是非常仔细,生怕一不小心存错了数据找错误都找不到,影响后面的步骤,初始化数据储存部分代码如下:

User user[] = new User[4];

        user[0]=new User();
        user[1]=new User();
        user[2]=new User();
        user[3]=new User();
        user[0].account[0]=new Account();
        user[0].account[1]=new Account();
        user[1].account[0]=new Account();
        user[2].account[0]=new Account();
        user[2].account[1]=new Account();
        user[2].account[2]=new Account();
        user[3].account[0]=new Account();
        user[3].account[1]=new Account();
        user[0].account[0].card[0]=new Card();
        user[0].account[0].card[1]=new Card();
        user[0].account[1].card[0]=new Card();
        user[1].account[0].card[0]=new Card();
        user[2].account[0].card[0]=new Card();
        user[2].account[1].card[0]=new Card();
        user[2].account[2].card[0]=new Card();
        user[2].account[2].card[1]=new Card();
        user[3].account[0].card[0]=new Card();
        user[3].account[1].card[0]=new Card();

        user[0].setName("杨过");
        user[0].setBank("中国建设银行");
        user[0].account[0].setAccount("3217000010041315709 ");
        user[0].account[0].card[0].setCard("6217000010041315709");
        user[0].account[0].card[1].setCard("6217000010041315715");
        user[0].account[1].setAccount("3217000010041315715");
        user[0].account[1].card[0].setCard("6217000010041315718");
        user[1].setName("郭靖");
        user[1].setBank("中国建设银行");
        user[1].account[0].setAccount("3217000010051320007");
        user[1].account[0].card[0].setCard("6217000010051320007");
        user[2].setName("张无忌");
        user[2].setBank("中国工商银行");
        user[2].account[0].setAccount("3222081502001312389");
        user[2].account[0].card[0].setCard("6222081502001312389");
        user[2].account[1].setAccount("3222081502001312390");
        user[2].account[1].card[0].setCard("6222081502001312390");
        user[2].account[2].setAccount("3222081502001312399");
        user[2].account[2].card[0].setCard("6222081502001312399");
        user[2].account[2].card[1].setCard("6222081502001312400");
        user[3].setName("韦小宝 ");
        user[3].setBank("中国工商银行");
        user[3].account[0].setAccount("3222081502051320785");
        user[3].account[0].card[0].setCard("6222081502051320785");
        user[3].account[1].setAccount("3222081502051320786");
        user[3].account[1].card[0].setCard("6222081502051320786");

储存完数据,接下来就开始完善各个功能方法了,要判断从键盘输入的内容分别是什么东西,就要先把从键盘输入的数据都分别储存然后逐个拿出来判断

while(true) {
            a = input.nextLine();
            if(a.equals("#")) {
                break;
            }
            stringbuilder.append(a).append("\n");
        }
        String text1=stringbuilder.toString();
        String []d=text1.split("\n");

这段代码就是将输入的数据分开并储存在数组中,知道编译器读到符号“#”时结束;

接下来就是重头戏了,这段代码为整个代码功能实现的主要部分,运用for循环和switch-case、if-else语法,结合上面那部分判断数据对应的含义的代码,完成最后的输出。

for (int k=0;k<d.length;k++) {
            String[] e =d[k].split("\\s{1,3}");

            for (flag = 1; flag < 5; flag++) {
                if (flag == 1) {
                    for (int z = 0; z < 4; z++) {
                        switch (z) {
                            case 0:
                                if (e[0].equals( user[0].account[0].card[0].getCard() )|| e[0].equals(  user[0].account[0].card[1].getCard())) {
                                    sign1 = 0;
                                    sign2 = 0;
                                    f1=1;
                                }
                                if (e[0].equals( user[0].account[1].card[0].getCard())) {
                                    sign1 = 0;
                                    sign2 = 1;
                                    f1++;
                                }
                                break;
                            case 1:
                                if (e[0].equals(user[1].account[0].card[0].getCard())) {
                                    sign1 = 1;
                                    sign2 = 0;
                                    f1++;
                                }
                                break;
                            case 2:
                                if (e[0].equals( user[2].account[0].card[0].getCard())){
                                    sign1 = 2;
                                    sign2 = 0;
                                    f1++;
                                }
                                if (e[0].equals(user[2].account[1].card[0].getCard()) ){
                                    sign1 = 2;
                                    sign2 = 1;
                                    f1++;
                                }
                                if (e[0].equals( user[2].account[2].card[0].getCard()) || e[0].equals( user[2].account[2].card[1].getCard())) {
                                    sign1 = 2;
                                    sign2 = 2;
                                    f1++;
                                }
                                break;
                            case 3:
                                if (e[0].equals( user[3].account[0].card[0].getCard())) {
                                    sign1 = 3;
                                    sign2 = 0;
                                    f1++;
                                }
                                if (e[0].equals( user[3].account[1].card[0].getCard())) {
                                    sign1 = 3;
                                    sign2 = 1;
                                    f1++;
                                }
                                break;
                        }
                        if (f1 == 1)
                            break;
                    }
                }
                if(e.length==1)
                {
                    if(f1!=0)
                        System.out.println("¥"+df.format(user[sign1].account[sign2].getBalbace()));
                    else
                        System.out.println("Sorry,this card does not exist.");
                    f1=0;
                    break;
                }
                if (flag == 2) {
                    if (Double.parseDouble(e[1]) == user[sign1].account[sign2].getPassword())
                        f2++;
                }
                if (flag == 3) {
                    if ((e[2].equals("06") || e[2].equals("05")) && user[sign1].getBank().equals("中国工商银行")){
                        f3++;
                        bianhao = e[2];
                    }
                    if ((e[2].equals("01") || e[2].equals("02")||e[2].equals("03") || e[2].equals("04") ) && user[sign1].getBank().equals("中国建设银行")) {
                        f3++;
                        bianhao = e[2];
                    }
                    if (e[2].compareTo("01") < 0 || e[2].compareTo("06") > 0)
                        flag31++;

                }
                double b = Double.parseDouble(e[3]);
                if (flag == 4) {

                    if (b > 0 && b <= user[sign1].account[sign2].getBalbace()) {
                        user[sign1].account[sign2].setBalbace(user[sign1].account[sign2].getBalbace() - b);
                        f4++;
                    } else {
                        flag41++;
                    }
                    if (b < 0) {
                        user[sign1].account[sign2].setBalbace(user[sign1].account[sign2].getBalbace() - b);
                        f4++;
                    }
                }
                if (flag ==4) {
                    if (f1 == 0)
                        System.out.println("Sorry,this card does not exist.");
                    if (f2 == 0)
                        System.out.println("Sorry,your password is wrong.");
                    if (f3 == 0 && flag31 == 0)
                        System.out.println("Sorry,cross-bank withdrawal is not supported.");
                    if (flag31 != 0)
                        System.out.println("Sorry,the ATM's id is wrong.");
                    if (flag41 != 0&&b>0)
                        System.out.println("Sorry,your account balance is insufficient.");
                    if (f1 != 0 && f2 != 0 && f3 != 0 && f4 != 0) {
                        System.out.print(user[sign1].getName() + "在" + user[sign1].getBank() + "的" + bianhao+"号ATM机");
                        if (b > 0)
                            System.out.println("上取款¥" + df.format(b));
                        else
                            System.out.println("上存款¥" + df.format(-b));
                        System.out.println("当前余额为¥" + df.format(user[sign1].account[sign2].getBalbace()));
                    }
                    f1 = 0;
                    f2 = 0;
                    f3 = 0;
                    flag31 = 0;
                    flag41 = 0;
                }
            }
        }
        Account account=new Account();
        String str=input.nextLine();
        char iuy[]=new char[str.length()];
        for(int i=0;i<=iuy.length;i++){        //mao pao pai xv
            for(int j=0;j<iuy.length-i-1;j++){
                if(iuy[j]>iuy[j+1]){
                    char temp=iuy[j+1];
                    iuy[j+1]=iuy[j];
                    iuy[j]=temp;
                }
            }
        }
   

 

 上图为代码复杂度,下图为类图:

 

 就代码复杂度和类图来看,这个题目没有用到类的继承、多态性使用方法、接口的应用,因为我发现不用那些技术也能顺利完成这道题,但以这样的方式编写代码,不利于后续的修改与拓展,这一点在题目集9中就能很好地体现:

题目集9 ATM机类结构设计(二)

题目分析:

这题作为题目集8的拓展,完美的体现了出题者在习题可持续性特征方面做出的思考,这题相较于上一题,新添了一些功能,更加接近现实生活中的真实ATM机系统,例如:银行账户分为借记账户和贷记账户两种,其中,借记账户不能够透支 取款,而贷记账户可以透支取款(可能需要支付手续费)在上一题就没有作要求,并且根据账户 的不同,银行卡一般分为借记卡(针对借记账户)和信用卡(针对贷记账户) 两类;就这两点来看,想要在我上一题写的代码的基础上进行修改从而达到这一题的题目要求是比较困难的,所以在编写代码时最好要确保代码具有一定的可拓展性,不要当作一次性任务,这样可以让自己在以后的事情中节省很多时间精力。这题题目要求允许跨行业务办理,且透支取款手续费为取款的百分之五 ,透支最大限额为五万元,本题还新增了一个中国农业银行,新添加五个ATM机,不同银行跨行取款手续费不同,建设、工商、农业银行分别为取款的百分之二、百分之三、百分之四,最后初始化数据较上一题也多了四组,输出格式也稍有不同。以上就是该题目集较上一题目集的改进点,概括来说,这一题目集更加符合当前生活现实,接下来就是代码设计与分析:

本来这道题是可以在上道题的基础上进行修改完成的,但由于我上一题没有使用最合适的方法,所以这题需要重新做,大体设计思路就如下面的类图所示:

 

 主类中存储数据分割输入数据和上题基本相同这里就不多说了,GetBalance类,Bank类,ATM类,User类,UnionPay类也比较简单,主要就是获取和返回相应值;

Withdraw类中有一个判断各种合法性的方法如下:

public boolean verify() {
        /**
         * 校验该卡是否存在
         */
        Card card = ValidateData.getCardbyCardNO(unionPay, cardNO);
        if(card == null) {
            System.out.println("Sorry,this card does not exist.");
            return false;
        }

        /**
         * 校验ATM是否存在
         */
        ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID);
        if(aTM == null) {
            System.out.println("Sorry,the ATM's id is wrong.");
            return false;
        }

        Account account = Account.getAmountbyCardNO(cardNO);
        double balance = account.getBalance();

        /**
         * 校验卡密码是否正确
         */
        if(!card.getCardPassword().equals(cardPassword)) {
            System.out.println("Sorry,your password is wrong.");
            return false;
        }

        /**
         * 校验取款金额是否大于余额
         */if(!account.maxwithdrawal(amount, aTM.getBank())){
            System.out.println("Sorry,your account balance is insufficient.");
            return false;
        }



        return true;
    }

还有一个专门校检数据合法性的类:

class ValidateData {
    /**
     * 校验卡号是否存在
     * @param unionPay
     * @param cardNO
     * @return
     */
    public static Card getCardbyCardNO(UnionPay unionPay,String cardNO) {
        Card card = null;
        Iterator<Bank> bankItr = unionPay.getBankList().iterator();

        while(bankItr.hasNext()) {
            ArrayList<Account> accountList = bankItr.next().getAccountList();
            Iterator<Account> accountItr = accountList.iterator();
            while(accountItr.hasNext()) {
                ArrayList<Card> cardList = accountItr.next().getList();
                Iterator<Card> cardItr = cardList.iterator();
                while(cardItr.hasNext()) {
                    card = cardItr.next();
                    if(card.getCardNO().equals(cardNO)) {
                        return card;
                    }
                }
            }
        }
        return null;
    }

    /**
     * 校验ATM ID是否存在
     * @param unionPay
     * @param ATMID
     * @return
     */
    public static ATM getATMbyATMID(UnionPay unionPay,String ATMID) {
        Iterator<Bank> bankItr = unionPay.getBankList().iterator();
        Bank bank = null;
        ATM aTM = null;

        while(bankItr.hasNext()) {
            bank = bankItr.next();
            Iterator<ATM> aTMItr = bank.getATMList().iterator();

            while(aTMItr.hasNext()) {
                aTM = aTMItr.next();
                if(aTM.getATMID().equals(ATMID)) {
                    return aTM;
                }
            }
        }
        return null;
    }
}

存款、取款输出方法:

 public void showResult(Account account,int flag, ATM aTM) {
        String type = "";
        if(flag == 1) {
            type = "取款";
        }else {
            type = "存款";
            amount *= -1;
        }
        String userName = account.getUser().getName();
        String bankName = aTM.getBank().getBankName();
        System.out.printf("业务:%s ",type);
        System.out.println(userName + "在" +
                bankName + "的" + ATMID + "号ATM机上" + type + String.format("¥%.2f", amount));
        System.out.println("当前余额为" + String.format("¥%.2f", account.getBalance()));
    }

借记账户是本题新添的要求,这个类的参考代码如下:

class DebitAccount extends Account {//借记账户

    final String name = "借记账户";
    DebitAccount (String accountNO, double balance, User user, Bank bank){
        super(accountNO,balance,user,bank);
    }

    public void withdrawal(double money,Bank bank) {    //取款函数
        if(bank.getBankName().equals(getBank().getBankName())) {
            super.setBalance(super.getBalance() - money);
        }else {
            super.setBalance(super.getBalance() - money * (1 + bank.getServicecharge()));
        }

    }

    public void deposit(double money) {    //存款函数
        super.setBalance(super.getBalance() + money);
    }

    public boolean maxwithdrawal(double money,Bank bank) {

        double money1 = super.getBalance();
        if(super.getBank().getBankName().equals(bank.getBankName())) {
            if(money > money1) {
                return false;
            }
        }else {
            if(money *(1 + bank.getServicecharge()) > money1) {
                return false;
            }
        }

        return true;
    }
}

最后贷记账户类是整个题目中较为复杂的一个类

class CreditAccount extends Account {

    /**
     *贷记账户
     */

    private double credit = 50000;        //信用,默认为五万
    final String name = "贷记账户";

    public double getBalance(){
        if(super.getBalance() > 0) {
            return super.getBalance();
        }
        else {
            return credit - 50000;
        }

    }
    public boolean maxwithdrawal(double money,Bank bank) {

        double money_1 = super.getBalance();
        if(bank.getBankName().equals(getBank().getBankName())) {
            if(money > money_1) {
                if(credit - (money - money_1) * 1.05 < 0) {
                    return false;
                }
            }
            else {
                if(money_1 - money  < 0) {
                    return false;
                }

            }
        }else {
            double sum = 0;
            if(money > money_1) {
                sum = money + (money - money_1)*0.05 + money * bank.getServicecharge();
                if(credit - (sum - money_1) < 0)
                    return false;
            }else {
                sum = money + money * bank.getServicecharge();
                if(sum > money_1) {
                    if(credit - (sum - money_1) < 0)
                        return false;
                }else {
                    if((money_1 - sum) < 0){
                        return false;
                    }
                }
            }
        }
        return true;

    }

    public CreditAccount(String accountNO, double balance, User user, Bank bank){
        super(accountNO,balance,user,bank);
    }


    public void withdrawal(double money,Bank bank) {    //取款函数
        // TODO 自动生成的方法存根
        double money_1 = super.getBalance();
        if(bank.getBankName().equals(getBank().getBankName())) {
            if(money > money_1) {
                super.setBalance(0);
                this.setCredit(credit - (money - money_1) * 1.05);
            }else {
                super.setBalance( money_1 - money );
            }
        }else {
            double sum = 0;
            if(money > money_1) {
                sum = money + (money - money_1)*0.05 + money * bank.getServicecharge();
                super.setBalance(0);
                this.setCredit(credit - (sum - money_1));
            }else {
                sum = money + money * bank.getServicecharge();
                if(sum > money_1) {
                    super.setBalance(0);
                    this.setCredit(credit - (sum - money_1));
                }else {
                    super.setBalance((money_1 - sum));
                }
            }

        }
    }

    public void setCredit(double credit) {
        this.credit = credit;
    }

}

如果你上一题用对了方法的话,那么这一题和上一题的解题思路应该是差不多的,这两题的详细总结在上面已经说过了就不再重复说明了。

代码复杂度如下:

 

 

三、踩坑心得


编写代码的时候一定要养成备份和保存的好习惯,不然辛苦敲打的代码一不小心就会要重新再打一遍;然后就是编写代码尽量使其具有标准的结构框架,具有一定的可拓展性,增强代码的可持续性,这样在下次遇到相同类型的习题时就可、以直接复制黏贴过来套用,节省很多时间,具有清晰的结构框架的代码也便于今后的学习、借鉴。在设计涉及一定数学运算的程序时最好在草稿纸上先打好草稿,不然很容易出现程序输出答案错误,例如题目集9中的计算量较大,直接心算然后编写代码就很容易出错。

四、改进建议


习题集9可以增加一些更加接近现实生活的人性化功能,例用增加一些引导用户使用的文字,增加密码修改功能,增加用户数据添加功能并增加数据储存功能,将数据储存到记事本中,下次运行时可直接调用,增加界面类等使作业一步步现实化。

五、总结


最近做的这几个题目工作量都有点大,不能很快的完成,具有很强的考察类的继承、多态性使用方法、接口的应用的特征;通过对这三个题目集的学习,我加强学习了类的继承、多态性使用方法、接口的应用,认识到在编写代码时使用正确的方法的重要性,但这些技术我使用起来还不是很习惯,每次写代码时我还是会不由自主的直接写而不会用这些技术,所以在课余时间还需在这些地方进行进一步的学习研究。

 

 

posted @ 2021-06-15 19:20  gjl1  阅读(71)  评论(0)    收藏  举报