JAVA 第三次总结Blog

前言

总结:这几次的作业题量,难度都不大,但都趋近于完成一整个系统,而非只实现部分的功能。题目集八、九也不在给出类图,而是要求自己设计。我认为这是比较好的,不想当码农,而是要自己的设计能力。
题目集七:这主要是关于类的继承,多态,接口的使用,ArrayList泛型的应用方法,Compareble接口及泛型的应用单一职责原则的应用,“开-闭”原则的应用。第一题将输入的数字存入数组中,根据不同的数字即代表了,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片,要我们计算出,各个图形的面积和周长,以及他们的总面积。第二题在第一题的基础上,增加了排序分组的环节。
题目集八 九:要求我们设计ATM机的取款程序,这题主要是敲代码之前要想好初始化的数据要怎么存,存在哪,他们之间的关系要怎么实现,要怎么找到对应的人以及考虑“开闭”原则。以及实体类的单一职责原则,怎样实现读取卡号,再找到对应的账户进行存取款业务,以及对应的密码是不是正确的,以及怎样将字符串中的提取以及存取金额转化为int类型或者double类型,进行余额的加减,都是本次题目集中我们需要考虑的问题,对应的银行有其对应的ATM机的编号,我们在跨行时能否找出对应的ATM机,以及我们设计的ATM机是不是支持跨行取款,这些都是我们在设计中需要考虑到的问题。做题目集九的时候,很容易算错钱数,因为分不清楚利息和贷款利息,在计算余额时候要想清楚。


题目集7(7-1)、(7-2)综合分析

点击查看代码

import java.util.*;
public class Main {
    //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
//使用Main.input.next…即可(避免采坑)
    public static Scanner input = new Scanner(System.in);
    public static void main(String[] args){
        ArrayList<Integer> list = new ArrayList<Integer>();
        int num = input.nextInt();
       
        while(num != 0){
            if(num < 0 || num > 4){
                System.out.println("Wrong Format");
                System.exit(0);
            }
            list.add(num);
            num = input.nextInt();
        }
        DealCardList dealCardList = new DealCardList(list);
        if(!dealCardList.validate()){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        dealCardList.showResult();
        input.close();
    }
}

abstract class Shape{//图形
    String shapeName;
    Shape() {

    }
    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 vaildate();

    public String toString ()
    {
        return toString();
    }
}

class Circle extends Shape{//圆
    private double radius=1;
    private  String Name="Circle";
    Circle()
    {

    }

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

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public double getRadius() {
        return radius;
    }

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

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

    @Override
    public boolean vaildate() {
        if(radius<=0)
            return false;
        else
            return true;
    }
}

class Out{
    public void out(double [] area)
    {
        int i=0;
        double all=0;
        System.out.println("Original area:");
        for(i=0;i<area.length;i++)//总和是约前约后
        {
            System.out.printf("%.2f ",area[i]);
            all=all+area[i];
        }
        System.out.print("\n");
        System.out.print("Sum of area:");
        System.out.printf("%.2f\n",all);
        System.out.println("Sorted area:");
        Arrays.sort(area);
        for (i=0;i<area.length;i++)
        {
            System.out.printf("%.2f ",area[i]);
        }
        System.out.print("\n");
        System.out.print("Sum of area:");
        System.out.printf("%.2f",all);
    }
}

class PrinRule{
    int i;
}

class Rectangle extends Shape{//矩形
    private double width=0,length=0;
    private  String Name="Rectangle";
    Rectangle()
    {

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

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }//矩形

    public double getWidth() {
        return width;
    }

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


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

    @Override
    public boolean vaildate() {
        if(length<0||width<0)
            return false;
        else
            return true;
    }
}

class Triangle extends Shape{//三角
    private double side1=0,side2=0,side3=0;
    private  String Name="Triangle";
    Triangle()
    {

    }
    Triangle(double side1,double side2,double side3)
    {
        this.side1=side1;
        this.side2=side2;
        this.side3=side3;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public double getSide1() {
        return side1;
    }

    public void setSide1(double side1) {
        this.side1 = side1;
    }

    public double getSide2() {
        return side2;
    }

    public void setSide2(double side2) {
        this.side2 = side2;
    }

    public double getSide3() {
        return side3;
    }

    public void setSide3(double side3) {
        this.side3 = side3;
    }

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

    @Override
    public boolean vaildate() {
        double[] arr= {side1,side2,side3};
        Arrays.sort(arr);
        if(arr[0]+arr[1]>arr[2]&&arr[1]-arr[0]<arr[2]) {
            return true;
        }else {
            return false;
        }

    }
}

class Trapezoid extends Shape{
    private double topSide=0,bottomSide=0,height=0;
    private String Name="Trapezoid";
    Trapezoid()
    {

    }
    Trapezoid(double topSide,double bottomSide,double height)
    {
        this.bottomSide=bottomSide;
        this.height=height;
        this.topSide=topSide;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    @Override
    public double getArea() {
        return (topSide+bottomSide)*height/2;
    }

    @Override
    public boolean vaildate() {
        if(topSide>0&&bottomSide>0&&height>0)
        return true;
        else
            return false;
    }
}

class Card extends Shape implements Comparable<Card>{
    private Shape shape;
    Card()
    {

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

    public String getName()
    {
        return shape.getShapeName();
    }

    public Shape getShape() {
        return shape;
    }

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

    @Override
    public double getArea() {
        return shape.getArea();
    }

    @Override
    public boolean vaildate() {
        if(shape.vaildate())
            return true;
        else
            return false;
    }

    @Override
    public int compareTo(Card card) {
        if(this.getArea()>card.getArea())
            return -1;
        else if(this.getArea()==card.getArea())
            return 0;
        else
            return 1;
    }
}

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

    }
    DealCardList(ArrayList<Integer> list)
    {
        int i;
        for(i=0;i<list.size();i++)
        {
            switch(list.get(i))
            {
                case 1:{double radius=Main.input.nextDouble();
                    Circle circle=new Circle(radius);
                    Card card=new Card(circle);
                    circle.setShapeName("Circle");
                    //System.out.println(circle.getShapeName());
                    cardList.add(card);
                    break;
                }
                case 2:
                {
                    double width=Main.input.nextDouble();
                    double length=Main.input.nextDouble();
                    Rectangle rectangle=new Rectangle(width,length);
                    Card card=new Card(rectangle);
                    rectangle.setShapeName("Rectangle");
                    cardList.add(card);
                    break;
                }
                case 3:
                {
                    double side1=Main.input.nextDouble();
                    double side2=Main.input.nextDouble();
                    double side3=Main.input.nextDouble();
                    Triangle triangle=new Triangle(side1,side2,side3);
                    Card card=new Card(triangle);
                    triangle.setShapeName("Triangle");
                    cardList.add(card);
                    break;
                }
                case 4:
                {
                    double top=Main.input.nextDouble();
                    double bottom=Main.input.nextDouble();
                    double height=Main.input.nextDouble();
                    Trapezoid trapezoid=new Trapezoid(top,bottom,height);
                    Card card=new Card(trapezoid);
                    trapezoid.setShapeName("Trapezoid");
                    cardList.add(card);
                    break;
                }
            }
        }
    }

    public boolean validate()
    {
        for(int i=0;i<cardList.size();i++)
        {
            if(!cardList.get(i).vaildate())
            {
                return false;
            }
        }
        return true;
    }

    public void cardSort()
    {
        Collections.sort(cardList);
        for(int i=0;i<cardList.size();i++)
        {
            System.out.print(cardList.get(i).getName()+":");
            System.out.printf("%.2f ",cardList.get(i).getArea());
        }
    }

    public double getAllArea()
    {
        double allArea=0;
        for(int i=0;i<cardList.size();i++)
        {
            allArea +=cardList.get(i).getArea();
        }
        return allArea;
    }

    public void showResult()
    {
        System.out.println("The original list:");
        for(int i=0;i<cardList.size();i++)
        {
            System.out.print(cardList.get(i).getName()+":");
            System.out.printf("%.2f ",cardList.get(i).getArea());
        }
        System.out.print("\n");
        System.out.println("The sorted list:");
        cardSort();
        System.out.print("\n");
        System.out.print("Sum of area:");
        System.out.printf("%.2f",getAllArea());
    }
}

7-1类图

java 名片识别 tesseract_System


首先从类图来分析:Shape是一个抽象类,圆形,三角形,矩形,梯形类继承于Shape抽象类,然后Shape中有两个抽象方法,getArea和validDate,分别用于得到该图型的面积,以及图形合法值的检测,DealCardList用于数据处理,在里面将输入的整型数组便历提出,按照对应的数字类型1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片,进行运算,在类DealCardList里面处理这些数据,再将对应的类型导入到对应形状类,在进行面积的计算后进行输出。所有的形状类都继承于Shape抽象类,然后在各个形状类中进行重写两个方法,因为不同的图形有不同的面积计算方法以及合法值的验算。

此外,通过改写该接口,我还可以直接使用sort方法来排序卡片面积。

7-1复杂度分析

java 名片识别 tesseract_java 名片识别 tesseract_02


从图中可以看出:我的各项指标都还OK,平均深度还有待提高。

7-2复杂度分析

java 名片识别 tesseract_ide_03


从图中可以看出:我这最大复杂度超标了,平均复杂度是小的,代码有待提高。

点击查看7-2代码

import java.util.*;
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();
        if(num==0)
        {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        while(num != 0){
            if(num < 0 || num > 4){
                System.out.println("Wrong Format");
                System.exit(0);
            }
            list.add(num);
            num = input.nextInt();
        }
        DealCardList dealCardList = new DealCardList(list);
        if(!dealCardList.validate()){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        dealCardList.showResult();
        input.close();
    }
}
abstract class Shape{
    String shapeName;
    Shape() {
    }
    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 vaildate();
}
class Circle extends Shape{
    private double radius=1;
    private  String Name="Circle";
    Circle()
    {
    }
    Circle(double radius) {
        this.radius=radius;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    @Override
    public double getArea() {
        return radius*radius*Math.PI;
    }
    @Override
    public boolean vaildate() {
        if(radius<=0)
            return false;
        else
            return true;
    }
}
class Rectangle extends Shape{//矩形
    private double width=0,length=0;
    private  String Name="Rectangle";
    Rectangle()
    {
    }
    Rectangle(double width,double length)
    {
        this.length=length;
        this.width=width;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    @Override
    public double getArea() {
        return width*length;
    }
    @Override
    public boolean vaildate() {
        if(length<0||width<0)
            return false;
        else
            return true;
    }
}
class Triangle extends Shape{
    private double side1=0,side2=0,side3=0;
    private  String Name="Triangle";
    Triangle() {}
    Triangle(double side1,double side2,double side3)
    {
        this.side1=side1;
        this.side2=side2;
        this.side3=side3;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    @Override
    public double getArea() {
        double s=(side1+side2+side3)/2;double area=Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
        return area;
    }
    @Override
    public boolean vaildate() {
        double[] arr= {side1,side2,side3};
        Arrays.sort(arr);
        if(arr[0]+arr[1]>arr[2]&&arr[1]-arr[0]<arr[2]) {
            return true;
        }else {
            return false;
        }
    }
}
class Trapezoid extends Shape{
    private double topSide=0,bottomSide=0,height=0;
    private String Name="Trapezoid";
    Trapezoid() {}
    Trapezoid(double topSide,double bottomSide,double height)
    {
        this.bottomSide=bottomSide;this.height=height;this.topSide=topSide;
    }
    public String getName() {
        return Name;}
    public void setName(String name) {
        Name = name;}
    @Override
    public double getArea() {
        return (topSide+bottomSide)*height/2;
    }
    @Override
    public boolean vaildate() {
        if(topSide>0&&bottomSide>0&&height>0)
        return true;
        else
            return false;
    }
}
class Card extends Shape implements Comparable<Card>{
    private Shape shape;
    Card() {}
    Card(Shape shape){
    this.shape=shape;
    }
    public String getName()
    {return shape.getShapeName();}
    public Shape getShape() {
        return shape;
    }

    public void setShape(Shape shape) {
        this.shape = shape;
    }
    @Override
    public double getArea() {return shape.getArea();}
    @Override
    public boolean vaildate() {
        if(shape.vaildate())
            return true;
        else
            return false;}
    @Override
    public int compareTo(Card card) {
        if(this.getArea()>card.getArea())
            return -1;
        else if(this.getArea()==card.getArea())
            return 0;
        else
            return 1;}
}
class DealCardList{
    ArrayList<Card> cardList=new ArrayList<>();
    ArrayList<Card> tempList=new ArrayList<>();
    DealCardList(ArrayList<Integer> list)
    {
        int i;
        for(i=0;i<list.size();i++)
        {
            switch(list.get(i))
            {
                case 1:{double radius=Main.input.nextDouble();
                    Circle circle=new Circle(radius);
                    Card card=new Card(circle);circle.setShapeName("Circle");cardList.add(card);break;}
                case 2:
                {double width=Main.input.nextDouble();double length=Main.input.nextDouble();
                    Rectangle rectangle=new Rectangle(width,length);Card card=new Card(rectangle);rectangle.setShapeName("Rectangle");cardList.add(card);break;}
                case 3:
                {double side1=Main.input.nextDouble();double side2=Main.input.nextDouble();double side3=Main.input.nextDouble();
                    Triangle triangle=new Triangle(side1,side2,side3);Card card=new Card(triangle);triangle.setShapeName("Triangle");cardList.add(card);break;}
                case 4:
                {double top=Main.input.nextDouble();double bottom=Main.input.nextDouble();double height=Main.input.nextDouble();
                    Trapezoid trapezoid=new Trapezoid(top,bottom,height);
                    Card card=new Card(trapezoid);trapezoid.setShapeName("Trapezoid");cardList.add(card);break;}
            }}}
    public boolean validate()
    {
        for(int i=0;i<cardList.size();i++)
        {
            if(!cardList.get(i).vaildate())
            {return false;}
        }return true;}
    public void cardSort(ArrayList<Card> arrayList)
    {
        Collections.sort(arrayList);
    }
    public double getAllArea(ArrayList<Card> arrayList)
    {
        double allArea=0;
        for(int i=0;i<arrayList.size();i++)
        {allArea +=arrayList.get(i).getArea();}
        return allArea;
    }
    public void separated(String str)
    {
        tempList.clear();
        for(int i=0;i<cardList.size();i++)
        {
            if(str==cardList.get(i).getName())
            {tempList.add(cardList.get(i));}}
    }
    public String separatedSorted(String str)
    {
        StringBuilder stringBuilder = new StringBuilder();String out="",area ="";separated(str);cardSort(tempList);
        for(int i=0;i<tempList.size();i++)
        {area=String.format("%.2f",tempList.get(i).getArea());stringBuilder.append(tempList.get(i).getName()+":"+area+" ");}
        out=stringBuilder.toString();
        return out;
    }
    public void showResult()
    {
        int i,j;String output=null;double arr[]=new double[]{0,0,0,0};
        System.out.println("The original list:");System.out.print("[");
        for( i=0;i<cardList.size();i++)
        {System.out.print(cardList.get(i).getName()+":");System.out.printf("%.2f ",cardList.get(i).getArea());}
        System.out.print("]");System.out.print("\n");System.out.println("The Separated List:");
        separated("Circle");
        System.out.print("[");
        if(!tempList.isEmpty())
        {
            for(i=0;i<tempList.size();i++)
            {System.out.print(tempList.get(i).getName()+":");System.out.printf("%.2f ",tempList.get(i).getArea());}
        } System.out.print("]");
        arr[0]=maxArea(tempList);
        separated("Rectangle");System.out.print("[");
        if(!tempList.isEmpty())
        {
            for(i=0;i<tempList.size();i++)
            {System.out.print(tempList.get(i).getName()+":");System.out.printf("%.2f ",tempList.get(i).getArea());}
        }System.out.print("]");arr[1]=maxArea(tempList);
        separated("Triangle");System.out.print("[");
        if(!tempList.isEmpty())
        {for(i=0;i<tempList.size();i++)
            {System.out.print(tempList.get(i).getName()+":");System.out.printf("%.2f ",tempList.get(i).getArea());}
        }System.out.print("]");arr[2]=maxArea(tempList);
        separated("Trapezoid");System.out.print("[");
        if(!tempList.isEmpty())
        {for(i=0;i<tempList.size();i++)
            {System.out.print(tempList.get(i).getName()+":");System.out.printf("%.2f ",tempList.get(i).getArea());}
        }arr[3]=maxArea(tempList); System.out.print("]");
        System.out.print("\n");System.out.println("The Separated sorted List:");
        output=separatedSorted("Circle");System.out.print("[");
        if(!output.isEmpty())
        {System.out.print(output);}System.out.print("]");
        output=separatedSorted("Rectangle");
        System.out.print("[");
        if(!output.isEmpty())
        {System.out.print(output);}System.out.print("]");
        output=separatedSorted("Triangle");System.out.print("[");
        if(!output.isEmpty())
        {System.out.print(output);}System.out.print("]");
        output=separatedSorted("Trapezoid");System.out.print("[");
        if(!output.isEmpty())
        {System.out.print(output);}System.out.print("]");
        System.out.print("\n");System.out.print("The max area:");Arrays.sort(arr);System.out.printf("%.2f",arr[3]);
    }
    public double maxArea(ArrayList<Card> arrayList)
    {
        double all=0;Collections.sort(arrayList);
        all=getAllArea(arrayList);
        return all;
    }
}

7-2在7-1的基础上加入更多的数据,我们需要将每一组数据进行排序后分类输出。这需要我们将对应的图形类型存入一个数组,四分图形四个数组,然后再传入对应的图形卡片数组,此时的图形卡片数组都是和自己类型一样的图形,然后在输出的时候就可以进行分组输出,这样就达到了分组的目的。大部分代码还是与7-1一样。

总结:将Shape类抽象化,从而提高了程序的拓展性,这样我们不管在之后想要加入不同的图形,我们只需要继承重写,getAtea和validDate这两个方法即可,这两道题对于我们对于面向对象编程思想都有所提高。

题目集八、九综合分析

每个银行中又有着多个用户,多个用户又有着多个账号,多个账号有着多张卡,一级一级从属下去,整个实体类结构就是这样,然后我分出了一个操作类,该类中有着许许多多的处理方法,如:判断输入,进行存取款操作以及余额查询操作,整个程序的结构很简单,进行的操作也很简单,首先对于老师给的初始化数据进行了初始化操作,在进行输入操作后,进行判断,卡号,密码,atm机号是否正确,由于不能跨行取款,所以还判断了是否跨行,进行一系列的判断后,就开始了查找操作。

点击查看8-1代码

import java.util.*;

import static java.lang.Math.abs;

public class Main {
    public static void main(String[] args) {
        Input input=new Input();input.input();
    }
}
class Input{
    Scanner in=new Scanner(System.in);
     List<String> output=new ArrayList<>();
    public void input()
    {
        DateDeal dateDeal=new DateDeal();
        String input=in.nextLine();
        dateDeal.init();
        while(!input.equals("#"))
        {//6222081502001312390 88888887 06 -500.00
            dateDeal.deal(input);
            input=in.nextLine();
        }
        output=dateDeal.getOutput();
        for(int i=0;i<output.size();i++)
        {
            System.out.println(output.get(i));
        }
    }
}
class DateDeal{
    Bank bank1,bank2;double nmoney=0;
    String []message=new String[]{"","","",""};/*银行,用户,账户*/
    private List<String> output=new ArrayList<>();
    public void init()
    {
        List<String> my_card1=new ArrayList<>();List<String> my_card2=new ArrayList<>();
        List<String> my_card3=new ArrayList<>();List<String> my_card4=new ArrayList<>();List<String> my_card5=new ArrayList<>();
        List<String> my_card6=new ArrayList<>();List<String> my_card7=new ArrayList<>();List<String> my_card8=new ArrayList<>();
        Map<String,Double> account1 = new HashMap<String,Double>();Map<String,Double> account2 = new HashMap<String,Double>();Map<String,Double> account3 = new HashMap<String,Double>();Map<String,Double> account4 = new HashMap<String,Double>();
        Map<String,List<String>> card1=new HashMap<String,List<String>>();Map<String,List<String>> card2=new HashMap<String,List<String>>();Map<String,List<String>> card3=new HashMap<String,List<String>>();Map<String,List<String>> card4=new HashMap<String,List<String>>();
        account1.put("3217000010041315709",10000.0);account1.put("3217000010041315715",10000.0);
        my_card1.add("6217000010041315709");my_card1.add("6217000010041315715");
        card1.put("3217000010041315709",my_card1);
        my_card2.add("6217000010041315718");
        card1.put("3217000010041315715",my_card2);
        User user1=new User("杨过",account1,card1,"88888888");
        account2.put("3217000010051320007",10000.0);my_card3.add("6217000010051320007");
        card2.put("3217000010051320007",my_card3);
        User user2=new User("郭靖",account2,card2,"88888888");
        account3.put("3222081502001312389",10000.0);account3.put("3222081502001312390",10000.0);account3.put("3222081502001312399",10000.0);
        my_card4.add("6222081502001312389");card3.put("3222081502001312389",my_card4);
        my_card5.add("6222081502001312390");card3.put("3222081502001312390",my_card5);
        my_card6.add("6222081502001312399");my_card6.add("6222081502001312400");card3.put("3222081502001312399",my_card6);
       User user3=new User("张无忌",account3,card3,"88888888");
        account4.put("3222081502051320785",10000.0);account4.put("3222081502051320786",10000.0);
        my_card7.add("6222081502051320785");card4.put("3222081502051320785",my_card7);
        my_card8.add("6222081502051320786");card4.put("3222081502051320786",my_card8);
        User user4=new User("韦小宝",account4,card4,"88888888");
        List<String> atm1=new ArrayList<>();List<String> atm2=new ArrayList<>();//------------------------------------
        List<User> users1=new ArrayList<>();List<User> users2=new ArrayList<>();
        users1.add(user1);users1.add(user2);
        for(int i=1;i<=4;i++)
            atm1.add("0"+i);
        bank1=new Bank("中国建设银行",atm1,users1);
        atm2.add("05");atm2.add("06");users2.add(user3);users2.add(user4);
        bank2=new Bank("中国工商银行",atm2,users2);
    }
    public List<String> getOutput() {
        return output;
    }
    public void deal(String in)
    {
        StringBuilder out = new StringBuilder("");
        double remoney;
        in=in.trim();
        String [] str=new String[]{"","","",""};/*卡号,密码,atm_id,钱*/
        str=in.split("\\s+");
        if(str.length!=1){
            double a=Double.valueOf(str[3]);
            isRight(str[0],str[1],str[2],a);
            /*修改余额*/
            if(bank1.getName()==message[0])
                remoney=renewMoney(message[1],message[2],bank1,a);
            else
                remoney=renewMoney(message[1],message[2],bank2,a);
            out.append(message[1]+"在"+message[0]+"的"+str[2]+"号ATM机上");
            if(a>0)
                out.append("取款¥");
            else
                out.append("存款¥");
            String  m1 = String.format("%.2f",abs(a));
            out.append(m1);
            output.add(out.toString());
            out.delete(0, out.length());
            out.append("当前余额为¥");
            String  m2 = String.format("%.2f",abs(remoney));
            out.append(m2);
            output.add(out.toString());
            out.delete(0, out.length());
        }
        else
        {
            isRight2(str[0]);
            /*修改余额*/
            if(bank1.getName()==message[0])
                remoney=renewMoney(message[1],message[2],bank1,0);
            else
                remoney=renewMoney(message[1],message[2],bank2,0);
            out.append("¥");
            String  m3 = String.format("%.2f",abs(remoney));
            out.append(m3);
            output.add(out.toString());
            out.delete(0, out.length());
        }
    }
    public double renewMoney(String s2,String s3,Bank bank,double a)
    {
        int i;double remoney=0;
        for(i=0;i<bank.getUser().size();i++)
        {
            if(bank.getUser().get(i).getName()==s2)
            {
                bank.getUser().get(i).getAccount().put(s3,nmoney-a);
                remoney=bank.getUser().get(i).getAccount().get(s3);
            }
        }
        return remoney;
    }
    public String atmRight(String s3, Bank bank)
    {
        if(bank.getAtm().contains(s3))
            return bank.getName();
        return "0";
    }
    public boolean cardIdIsRignt(String s1,Bank bank)
    {
        boolean card_exist=false;
        message[0]="";message[1]="";message[2]="";message[3]="";nmoney=0;
        int i=0;
        for(i=0;i<bank.getUser().size();i++)
        {
            for (Map.Entry<String,List<String>> entry : bank.getUser().get(i).card.entrySet()) {
                if(entry.getValue().contains(s1))
                {   message[1]=bank.getUser().get(i).getName();
                    card_exist = true;
                    message[0]=bank.getName();
                    message[2]= entry.getKey();
                    nmoney=bank.getUser().get(i).account.get(message[2]);
                }
            }
        }
        if(card_exist==false)
            return false;
        else
            return true;
    }
    public boolean isRight2(String s1)
    {
        boolean card_exist=false;
        card_exist=cardIdIsRignt(s1,bank1);
        if(!card_exist){
            card_exist=cardIdIsRignt(s1,bank2);
        }
        if(!card_exist)
        {System.out.println("Sorry,this card does not exist.");System.exit(0);
        }
        return true;
    }
    public boolean isRight(String s1,String s2,String s3,Double money)
    {/*卡号不存在,atm存在,密码错误,金额不够,跨行了*/
        message[0]="";message[1]="";message[2]="";message[3]="";nmoney=0;
        boolean card_exist=false,atm_id=true,password=false,insufficient=false,cross_bank=true;
        /*查找卡号,找到银行,用户姓名,金额,账号*/
        card_exist=cardIdIsRignt(s1,bank1);
        if(!card_exist){
            card_exist=cardIdIsRignt(s1,bank2);
        }
        if(!card_exist)
        {System.out.print("Sorry,this card does not exist.");System.exit(0);
        }
        /*ATM机编号是否存在*/
        String bank11=atmRight(s3,bank1);
        String bank12=atmRight(s3,bank2);
        if(bank11=="0"&&bank12=="0"){
            atm_id=false;System.out.print("Sorry,the ATM's id is wrong.");System.exit(0);
        }
        /*密码是否正确*/
        if(s2.equals("88888888")) {
            password=true;
        }
        else{
            System.out.print("Sorry,your password is wrong."); System.exit(0);
        }
        /*检查金额是否越界*/
        if(nmoney-money<0)
        {

            System.out.println("Sorry,your account balance is insufficient.");System.exit(0);
        }
        /*是否跨行存储*/
        if((message[0]==bank11&&bank12=="0")||(message[0]==bank12&&bank11=="0"))
        {
            cross_bank=false;
        }
        else
        {
            System.out.println("Sorry,cross-bank withdrawal is not supported.");System.exit(0);
        }
        /*错误信息输出*/
        // outputRight(card_exist,atm_id,password,insufficient,cross_bank);
        /*整体返回*/
        if(card_exist&&atm_id==true&&password==true&&insufficient==false&&cross_bank==false)
            return true;
        else
            return false;
    }

}
class User{
    String name,password;
    Map<String,Double> account = new HashMap<String,Double>();/*账户,钱*/
    Map<String,List<String>> card=new HashMap<String,List<String>>();/*账号,卡号*/
    User(String name,Map<String,Double> account,Map<String,List<String>> card,String password)
    {
        this.setAccount(account);
        this.setCard(card);
        this.setName(name);this.password=password;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Map<String, Double> getAccount() {
        return account;
    }
    public void setAccount(Map<String, Double> account) {
        this.account = account;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
         = name;
    }
    public Map<String, List<String>> getCard() {
        return card;
    }
    public void setCard(Map<String, List<String>> card) {
        this.card = card;
    }
}
class Bank{
    private String name;
    private List<String> atm=new ArrayList<>();
    private List<User> user=new ArrayList<>();
    Bank(String name,List<String> atm,List<User> user)
    {
        =name;
        this.setAtm(atm);
        this.setUser(user);
    }
    public List<String> getAtm() {
        return atm;
    }
    public void setAtm(List<String> atm) {
        this.atm = atm;
    }
    public List<User> getUser() {
        return user;
    }
    public void setUser(List<User> user) {
        this.user = user;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
         = name;
    }
}

8-1类图

java 名片识别 tesseract_i++_04


从类图可以看出:我是用DataDeal作为处理函数,进行数据的初始化,错误检查,输出。Bank存储有关银行的信息,同时还用一个ArraryList存储顾客信息,将银行与顾客联系起来。这样方便我们后续根据银行查找对应的顾客。此外,顾客单独作为一个User类,存储有关顾客的信息。我用了一个Map存储顾客的账号,对应的是该卡下的余额,这是因为一张卡下面一个余额。此外,还用了一个Map存储账号与卡号的关系,因为一个账号下可以有多个卡号,所以Map里面嵌套了一个list。

8-1复杂度

java 名片识别 tesseract_java 名片识别 tesseract_05


可以看出:总体上的数据还可以,但对于平均方法数有点多,应该是处理函数包含了太多东西。

8-2

这个题目是相对于第一题难度有所增加,这个题相对于第一题不同的是增加了可以跨行取款的功能以及增加了可以透支的贷记卡,其余的实体类没有发生太大的变化,银联与银行的对应关系以及银行与账户的对应关系,以及账户与卡号的对应关系与第一题的对应关系相同,在这个题目中我是这样设计的,在user里加了一个属性记录每个账户的类型。这样还是要改代码,“开闭”实现的不是很好。

减少圈复杂度的方法:
①将固定的判断的方法不要分开判断,写入一个方法中,不要遇到一次在去写一次判断,每一次循环遍历都提高了圈复杂度;
②尽量将每一次需要处理的数据,尽量的放入到一次遍历当中去,不要遇到一次数据处理就再进行一次遍历,尽可能的写入到一次遍历当中去;
③得到利息的方法我认为不要一直判断,将账户卡号导入到一个方法,通过一个方法来得到利息,不要遇到一次卡号走一次循环,这样也会使圈复杂度提高。

总结:这两道题目还是有一定的难度的,考察的不再是对部分数据的处理,而是将实际的问题变成了我们对于实体类设计,怎样用Java实现具体的功能,对我们的考察有了进一步的提升,在解这两道题目我们最先考虑到的是类的设计方法,怎样设计实体类的功能,以及在 最开始要弄清楚,银联与银行,银行与账户,账户与卡号之间的关系,只有在一开始我们把所有的关系都理清楚之后,我们才能去设计出我们需要的类。


踩坑心得

  1. 7-2要注意的就是分组的格式问题,格式要正确,否则就得不到正确的结果;
  2. 编程的严谨性是不可忽略的问题,前面设计的时候不想仔细后面改的时候只会苦叫;
  3. ATM:输入数据的判断方法应该放在哪一个类中,在第八次程序中体现的很完全,但是后来的程序编写时,我把他们全部放在了一个判断类中,使用静态方法进行设计,从而我每个类都可以直接使用这些静态方法,从而判断输入;
  4. 9-1计算跨行利息是一个需要细心的点,此时要注意区分好跨行利息,看清题目;
  5. 8-1中写的时候对Map还不是很熟,所以花了很久在想如何遍历,如何找到对应用户;
  6. ATM机需要我们考虑到的情况多变,不再局限于解题,而加入了实际问题,这也需要我们在设计的时候想清楚,尽可能的考虑到每一种情况。

可以改进的地方

  1. 后面两个题目我完全没有考虑继承,抽象类,接口等方面,对JAVA的了解还是太过于局限;
  2. 8,9中我是直接将所有的判断,初始化数据,输出都放在一个处理类中实现,这样实际上逻辑过于复杂,功能又太多,如果出现变化不好改代码;
  3. 设计思维的改进,在每次写代码解题之前就想清楚,各个类之间的关系,以及这个类有怎样的功能,该承担怎样的职责,把这些全部想清楚这样就在写代码时大大的提高了我们的效率;
  4. 还有就是要善于用Java中一些功能方便的类,这样可以大大的提高我们的解题效率。

总结心得

对于这三个题目集,学会最多的就是关于程序设计的设计要求及设计原则的运用,其中,运用最多的便是单一原则和开闭原则,在这三次题目集中,这两个原则都运用的很多,其次,则是类的继承和多态,继承则包括了接口继承和实体类继承,在题目集七中,明显的运用了继承和多态,对于许多有着相同属性的类,可以创建一个父类,通过继承,重写,生成一个一个单独的子类,在程序中使用父类的引用和子类的对象可以使得程序更加的灵活多变,利于维护修改。当软件需要变化时,应该尽量通过扩展的方式来实现变化,而不是通过修改已有的代码来实现。因此,在开发过程中需要自己结合具体情况进行考量,是通过修改旧代码还是通过继承使得软件系统更稳定、更灵活,在保证去除不好代码的同时,也保证原有模块的正确性。
此外,不管是课堂上还是题目中,我都明显感觉到一个JAVA程序员最核心的能力还是设计代码的能力,所以程序设计思想上提高更加重要,所以还是要去读一些好的代码。
最后,通过这一个学期对于《Java面向对象设计》课程的学习,虽然困难但是还是坚持了下来,一个学期的时间学习了关于Java的很多很多知识,这里就不一一描述了,最主要的是从原先学习C语言的面向过程的思想,转变到现在也具备了一定的面向过程的思想,以及关于面向对象三大技术特性封装性、继承性、多态性理解的提高,从一开始看到复杂的UML类图不知所措,但后面有自己的独力思考能力可以将他们分为一个一个类去解题,也是一个质的改变。