Blog3

一:前言

1.题目集七的7-1和7-2考察的都是差不多的知识点,也并没有前面最难的几次难,尽管题目集八和题目集九都就是一道题目,但是我觉得很难,这就是少则精炼,浓缩才是精华,然后题目集八的话,就开始进入到了主题就是ATM机的存款,然后题目集九是在老师的发的题目集八代码上修改,满足更多的条件(也就是题目集九在题目集八上添加了一点难度,但这里老师给了题目集八的源码,那也就没那么难),主要是题目集八需要把代码写出来,然后题目集九只是添加功能,并没有题目集八难。

2.知识点考察:

题目集七:

(1):这一题考察的是继承、多态的应用;ArrayList泛型的应用方法,Comparable接口及泛型的应用,单一职责原则的应用“开-闭”原则的应用,掌握类的继承、多态性使用方法以及接口的应用,这道题属于一般难度,并不会很难。

 

(2):这一题考察的知识点是必须在作业7-1的基础上进行进一步改进和完善,考虑面向对象设计的“单一职责原则”,思考该程序是否能够符合“开-闭”原则,掌握类的继承、多态性使用方法以及接口的应用,这道题的本质其实和题目集7-1差不了多少,都是差不多的做法,所以这道题的难度最多比上一题难一点,也不是很难。

 

题目集八:

(1):这道题就一个要求,设计ATM仿真系统,但他考察的知识点却很多,可以说把以前很多的知识点都考察进去了,所以不懂时就会觉得这道题目很难,但是这道题目也是真的很难,他需要把每个数值都赋值对才会正确,所以需要很细心,这处主要考察的就是这个。

题目集九:

(1):这道题就一个要求,设计ATM仿真系统,本题重点考察继承、多态及抽象类的应用,这道题是题目集八的加强版,他有两种卡(信用卡和借记卡),这里是这道题最难的地方,也是比题目集八难的地方,多了几个判断,但是老师给了源码,所以并没有很难,主要是看你题目集八学的怎么样。

二:设计与分析

1.题目集七中的7-1和7-2其实是递进关系的,7-2是在7-1的基础上递进,接下俩看一下题目的要求和源码:

先看一下题目集七7-1的要求及源码:

 

输入格式:

 

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

 

输出格式:

 

  • 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边不能组成三角形),则输出Wrong Format
  • 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:

 

  1. 排序前的各图形类型及面积,格式为图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格;
  2. 排序后的各图形类型及面积,格式同排序前的输出;
  3. 所有图形的面积总和,格式为Sum of area:总面积值

 

输入样例1:

 

在这里给出一组输入。例如:

 

1 5 3 2 0

 

 

 

输出样例1:

 

在这里给出相应的输出。例如:

 

Wrong Format

 

 

 

输入样例2:

 

在这里给出一组输入。例如:

 

4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5

 

 

 

输出样例2:

 

在这里给出相应的输出。例如:

 

The original list:
Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02 
The sorted list:
Circle:98.52 Triangle:4.02 Rectangle:3.22 Trapezoid:1.14 
Sum of area:106.91

 

 

 

输入样例3:

 

在这里给出一组输入。例如:

 

4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 8.4

 

 

 

输出样例3:

 

在这里给出相应的输出。例如:

 

Wrong Format
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
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()){
	        System.out.println("Wrong Format");
	        System.exit(0);
	    }
	    dealCardList.showResult();
	    input.close();
    }
}
interface Comparable{
	
}
class DealCardList{
	ArrayList<Card> cardList = new ArrayList<Card>();
	public DealCardList() {
		super();
	}
	public DealCardList(ArrayList<Integer> list) {
		for(int i = 0;i < list.size();i ++) {
			switch(list.get(i)) {
			    case 1: double r = Main.input.nextDouble();
			    Circle circle = new Circle(r);
			    Card card1 = new Card(circle);
			    card1.getShape().setShapeName("Circle");
			    cardList.add(card1);
			    break;
			    case 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);
				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 card3 = new Card(triangle);
				card3.getShape().setShapeName("Triangle");
				cardList.add(card3);
				break;
			    case 4:double topSide = Main.input.nextDouble();
				double bottomSide = Main.input.nextDouble();
				double height = Main.input.nextDouble();
				Traperoid traperoid = new Traperoid(topSide,bottomSide,height);
				Card card4 = new Card(traperoid);
				card4.getShape().setShapeName("Trapezoid");
				cardList.add(card4);
				break;
			}
		}
	}

	public boolean validate() {
		for(int i = 0;i < cardList.size();i ++){
			if(!cardList.get(i).getShape().validate())
				return false;
		}
	    return true;
	}
	public void cardSort(){
		for(int i = 0;i < cardList.size();i ++)
			for(int j = i+1;j < cardList.size();j ++)
			{
				if(cardList.get(i).getShape().getArea() < cardList.get(j).getShape().getArea())
					Collections.swap(cardList,i,j);
			}
	}

	public void showResult() {
		System.out.println("The original list:");
		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.println();
		System.out.println("The sorted list:");
		cardSort();
		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.println();
		System.out.println("Sum of area:" + String.format("%.2f",getAllArea()));	
	}
	public double getAllArea() {
		double n = 0;
        for (Card card : cardList) {
            n += card.getShape().getArea();
        }
        return n;
	}	
}
class Card{
	Shape shape;
	public Card() {
		super();
		// TODO 自动生成的构造函数存根
	}

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

	public Shape getShape() {
		return shape;
	}

	public void setShape(Shape shape) {
		this.shape = shape;
	}
	public int compareTo(Card card) {
		return 0;		
	}
}
 abstract class Shape{
	String shapeName;
	
	public Shape() {
		super();
		// TODO 自动生成的构造函数存根
	}

	public Shape(String shapeName) {
		super();
		this.shapeName = shapeName;
	}

	public String getShapeName() {
		return shapeName;
	}

	public void setShapeName(String shapeName) {
		this.shapeName = shapeName;
	}
	public double getArea() {		
		    return 0;
		
	}
	public boolean validate(){
		return true;
		
	}

	@Override
	public String toString() {
		return getShapeName() + ":" + String.format("%.2f ",getArea());
	}
	
}
class Circle extends Shape{
	double radius;
	public Circle(double radius) {
		this.radius = radius;
	}
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	public double getArea() {
		return Math.PI * radius * radius;
	}
	public boolean validate() {
		if(radius > 0)
			return true;
		else return false;
	}
}
class Rectangle extends Shape{
	double width;
	double length;
	
	public Rectangle(double width, double length) {
		super();
		this.width = width;
		this.length = length;
	}

	public double getWidth() {
		return width;
	}

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

	public double getLength() {
		return length;
	}

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

	public double getArea(){
		return width * length;
	}
	public boolean validate() {
		if(width > 0&&length > 0)
			return true;
		else return false;
	}
}
class Triangle extends Shape{
	double side1;
	double side2;
	double side3;
	
	public Triangle(double side1, double side2, double side3) {
		this.side1 = side1;
		this.side2 = side2;
		this.side3 = side3;
	}
	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() {		
		return  Math.sqrt((side1 + side2 + side3) * (side1 + side2 - side3)
				* (side1 + side3 -side2) * (side2 + side3 - side1)) / 4;
	}
	public boolean validate() {
		if(side1 + side2 > side3&&side1 + side3 > side2&&side2 + side3 > side1
				&&side1 > 0&&side2 > 0&&side3 > 0)
			return true;
		else return false;
	}
}
class Traperoid extends Shape{
	double topSide;
	double bottomSide;
	double height;
	public Traperoid() {
		super();
		// TODO 自动生成的构造函数存根
	}
	public Traperoid(String shapeName) {
		super(shapeName);
		// TODO 自动生成的构造函数存根
	}
	public Traperoid(double topSide, double bottomSide, double height) {
		super();
		this.topSide = topSide;
		this.bottomSide = bottomSide;
		this.height = height;
	}
	public double getArea() {
		return (topSide + bottomSide) * height / 2.0;
	}
	public boolean validate() {
		if(topSide > 0&&bottomSide > 0&&height > 0)
			return true;
		else return false;
	}
}

  然后看一下题目集题目集七7-2:

输入格式:

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

输出格式:

  • 如果图形数量非法(<=0)或图形属性值非法(数值<0以及三角形三边不能组成三角形),则输出Wrong Format
  • 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
  1. 排序前的各图形类型及面积,格式为[图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前;
  2. 输出分组后的图形类型及面积,格式为[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出;
  3. 各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
  4. 各组中面积之和的最大值输出,格式为The max area:面积值

 

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
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();
    }
}
interface Comparable{
	
}
class DealCardList{
	ArrayList<Card> cardList = new ArrayList<Card>();
	ArrayList<Card> cardList1 = new ArrayList<Card>();
	ArrayList<Card> cardList2 = new ArrayList<Card>();
	ArrayList<Card> cardList3 = new ArrayList<Card>();
	ArrayList<Card> cardList4 = new ArrayList<Card>();
	public DealCardList() {
		super();
	}
	public DealCardList(ArrayList<Integer> list) {
		for(int i = 0;i < list.size();i ++) {
			switch(list.get(i)) {
			    case 1: double r = Main.input.nextDouble();
			    Circle circle = new Circle(r);
			    Card card1 = new Card(circle);
			    card1.getShape().setShapeName("Circle");
			    cardList.add(card1);
			    cardList1.add(card1);
			    break;
			    case 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);
				cardList2.add(card2);
				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 card3 = new Card(triangle);
				card3.getShape().setShapeName("Triangle");
				cardList.add(card3);
				cardList3.add(card3);
				break;
			    case 4:double topSide = Main.input.nextDouble();
				double bottomSide = Main.input.nextDouble();
				double height = Main.input.nextDouble();
				Traperoid traperoid = new Traperoid(topSide,bottomSide,height);
				Card card4 = new Card(traperoid);
				card4.getShape().setShapeName("Trapezoid");
				cardList.add(card4);
				cardList4.add(card4);
				break;
			}
		}
	}

	public boolean validate() {
		for(int i = 0;i < cardList.size();i ++){
			if(!cardList.get(i).getShape().validate())
				return false;
		}
	    return true;
	}
	public void cardSort1(){
		for(int i = 0;i < cardList1.size();i ++)
			for(int j = i+1;j < cardList1.size();j ++)
			{
				if(cardList1.get(i).getShape().getArea() < cardList1.get(j).getShape().getArea())
					Collections.swap(cardList1,i,j);
			}
	}
	public void cardSort2(){
		for(int i = 0;i < cardList2.size();i ++)
			for(int j = i+1;j < cardList2.size();j ++)
			{
				if(cardList2.get(i).getShape().getArea() < cardList2.get(j).getShape().getArea())
					Collections.swap(cardList2,i,j);
			}
	}
	public void cardSort3(){
		for(int i = 0;i < cardList3.size();i ++)
			for(int j = i+1;j < cardList3.size();j ++)
			{
				if(cardList3.get(i).getShape().getArea() < cardList3.get(j).getShape().getArea())
					Collections.swap(cardList3,i,j);
			}
	}
	public void cardSort4(){
		for(int i = 0;i < cardList4.size();i ++)
			for(int j = i+1;j < cardList4.size();j ++)
			{
				if(cardList4.get(i).getShape().getArea() < cardList4.get(j).getShape().getArea())
					Collections.swap(cardList4,i,j);
			}
	}
	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.println("]");		
		System.out.println("The Separated List:");
		System.out.print("[");
		for(int i = 0;i < cardList1.size();i ++)
		System.out.print(cardList1.get(i).getShape().getShapeName() + 
				":" + String.format("%.2f",cardList1.get(i).getShape().getArea()) + " ");
		System.out.print("]");
		System.out.print("[");
		for(int i = 0;i < cardList2.size();i ++)
		System.out.print(cardList2.get(i).getShape().getShapeName() + 
				":" + String.format("%.2f",cardList2.get(i).getShape().getArea()) + " ");
		System.out.print("]");
		System.out.print("[");
		for(int i = 0;i < cardList3.size();i ++)
		System.out.print(cardList3.get(i).getShape().getShapeName() + 
				":" + String.format("%.2f",cardList3.get(i).getShape().getArea()) + " ");
		System.out.print("]");
		System.out.print("[");
		for(int i = 0;i < cardList4.size();i ++)
		System.out.print(cardList4.get(i).getShape().getShapeName() + 
				":" + String.format("%.2f",cardList4.get(i).getShape().getArea()) + " ");
		System.out.println("]");
		System.out.println("The Separated sorted List:");
		System.out.print("[");
		cardSort1();
		for(int i = 0;i < cardList1.size();i ++)
		System.out.print(cardList1.get(i).getShape().getShapeName() + 
				":" + String.format("%.2f",cardList1.get(i).getShape().getArea()) + " ");
		System.out.print("]");
		cardSort2();
		System.out.print("[");
		for(int i = 0;i < cardList2.size();i ++)
		System.out.print(cardList2.get(i).getShape().getShapeName() + 
				":" + String.format("%.2f",cardList2.get(i).getShape().getArea()) + " ");
		System.out.print("]");
		cardSort3();
		System.out.print("[");
		for(int i = 0;i < cardList3.size();i ++)
		System.out.print(cardList3.get(i).getShape().getShapeName() + 
				":" + String.format("%.2f",cardList3.get(i).getShape().getArea()) + " ");
		System.out.print("]");
		cardSort4();
		System.out.print("[");
		for(int i = 0;i < cardList4.size();i ++)
		System.out.print(cardList4.get(i).getShape().getShapeName() + 
				":" + String.format("%.2f",cardList4.get(i).getShape().getArea()) + " ");
		System.out.println("]");
		System.out.println("The max area:" + String.format("%.2f",getAllArea()));	
	}
	public double getAllArea() {
		double[] q = new double[4];
		for(int i = 0;i < 4;i ++) {
			q[i] = 0;
		}
            for (Card card : cardList1) {
                q[0] += card.getShape().getArea();
            }
            for (Card card : cardList2) {
                q[1] += card.getShape().getArea();
            }
            for (Card card : cardList3) {
                q[2] += card.getShape().getArea();
            }
            for (Card card : cardList4) {
                q[3] += card.getShape().getArea();
            }
            Arrays.sort(q);
            return q[3];
	}	
}
class Card{
	Shape shape;
	public Card() {
		super();
		// TODO 自动生成的构造函数存根
	}

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

	public Shape getShape() {
		return shape;
	}

	public void setShape(Shape shape) {
		this.shape = shape;
	}
	public int compareTo(Card card) {
		return 0;		
	}
}
 abstract class Shape{
	String shapeName;
	
	public Shape() {
		super();
		// TODO 自动生成的构造函数存根
	}

	public Shape(String shapeName) {
		super();
		this.shapeName = shapeName;
	}

	public String getShapeName() {
		return shapeName;
	}

	public void setShapeName(String shapeName) {
		this.shapeName = shapeName;
	}
	public double getArea() {		
		    return 0;
		
	}
	public boolean validate(){
		return true;
		
	}

	@Override
	public String toString() {
		return getShapeName() + ":" + String.format("%.2f ",getArea());
	}
	
}
class Circle extends Shape{
	double radius;
	public Circle(double radius) {
		this.radius = radius;
	}
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	public double getArea() {
		return Math.PI * radius * radius;
	}
	public boolean validate() {
		if(radius > 0)
			return true;
		else return false;
	}
}
class Rectangle extends Shape{
	double width;
	double length;
	
	public Rectangle(double width, double length) {
		super();
		this.width = width;
		this.length = length;
	}

	public double getWidth() {
		return width;
	}

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

	public double getLength() {
		return length;
	}

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

	public double getArea(){
		return width * length;
	}
	public boolean validate() {
		if(width > 0&&length > 0)
			return true;
		else return false;
	}
}
class Triangle extends Shape{
	double side1;
	double side2;
	double side3;
	public Triangle(double side1, double side2, double side3) {
		this.side1 = side1;
		this.side2 = side2;
		this.side3 = side3;
	}
	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() {		
		return  Math.sqrt((side1 + side2 + side3) * (side1 + side2 - side3)
				* (side1 + side3 -side2) * (side2 + side3 - side1)) / 4;
	}
	public boolean validate() {
		if(side1 + side2 > side3&&side1 + side3 > side2&&side2 + side3 > side1
				&&side1 > 0&&side2 > 0&&side3 > 0)
			return true;
		else return false;
	}
}
class Traperoid extends Shape{
	double topSide;
	double bottomSide;
	double height;
	public Traperoid() {
		super();
	}
	public Traperoid(String shapeName) {
		super(shapeName);
	}
	public Traperoid(double topSide, double bottomSide, double height) {
		super();
		this.topSide = topSide;
		this.bottomSide = bottomSide;
		this.height = height;
	}
	public double getArea() {
		return (topSide + bottomSide) * height / 2.0;
	}
	public boolean validate() {
		if(topSide > 0&&bottomSide > 0&&height > 0)
			return true;
		else return false;
	}
}

  接下来看一下亮度生成报表类图和复杂度的对比:

题目集七7-1:

 

 

 

 

 

 

 接下来看一下题目集七7-2:

 

 

 

 

 

 

 

 

两个题目之间存在递进关系,题目集七7-2比7-1更多的功能在于它可以分类,按图形的形状分类,然后再按大小排序,而7-1并没有这些功能,也就是因为这些使得7-1和7-2之间存在递进关系,它们的类其实差不了多少,主要差的是类其中代码,多了一些排序方法,分类的方法。

2.分析题目集8和题目集9两道ATM机仿真题目的设计思路,首先看一下这两道题目的要求以及源码:

首先先看一下题目集八:

 

输入格式:

 

每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:

 

  • 存款、取款功能输入数据格式: 卡号 密码 ATM机编号 金额(由一个或多个空格分隔), 其中,当金额大于0时,代表取款,否则代表存款。
  • 查询余额功能输入数据格式: 卡号

 

输出格式:

 

①输入错误处理

 

  • 如果输入卡号不存在,则输出Sorry,this card does not exist.
  • 如果输入ATM机编号不存在,则输出Sorry,the ATM's id is wrong.
  • 如果输入银行卡密码错误,则输出Sorry,your password is wrong.
  • 如果输入取款金额大于账户余额,则输出Sorry,your account balance is insufficient.
  • 如果检测为跨行存取款,则输出Sorry,cross-bank withdrawal is not supported.

 

②取款业务输出

 

输出共两行,格式分别为:

 

[用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]

 

当前余额为¥[金额]

 

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

 

③存款业务输出

 

输出共两行,格式分别为:

 

[用户姓名]在[银行名称]的[ATM编号]上存款¥[金额]

 

当前余额为¥[金额]

 

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

 

④查询余额业务输出

 

¥[金额]

 

金额保留两位小数。

 

输入样例1:

 

在这里给出一组输入。例如:

 

6222081502001312390 88888888 06 -500.00
#

 

 

 

输出样例1:

 

在这里给出相应的输出。例如:

 

张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00

 

 

 

输入样例2:

 

在这里给出一组输入。例如:

 

6217000010041315709  88888888 02 3500.00
#

 

 

 

输出样例2:

 

在这里给出相应的输出。例如:

 

杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥6500.00

 

 

 

输入样例3:

 

在这里给出一组输入。例如:

 

6217000010041315715
#

 

 

 

输出样例3:

 

在这里给出相应的输出。例如:

 

¥10000.00

 

 

 

输入样例4:

 

在这里给出一组输入。例如:

 

6222081502001312390 88888888 06 -500.00
6222081502051320786 88888888 06 1200.00
6217000010041315715 88888888 02 1500.00
6217000010041315709  88888888 02 3500.00
6217000010041315715
#

 

 

 

输出样例4:

 

在这里给出相应的输出。例如:

 

张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00
韦小宝在中国工商银行的06号ATM机上取款¥1200.00
当前余额为¥8800.00
杨过在中国建设银行的02号ATM机上取款¥1500.00
当前余额为¥8500.00
杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥5000.00
¥5000.00

 

 

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		ArrayList<Card> cardList = new ArrayList<Card>(); 
		ArrayList<Card> cardList1 = new ArrayList<Card>(); 
		ArrayList<Card> cardList2 = new ArrayList<Card>(); 
		ArrayList<Card> cardList3 = new ArrayList<Card>(); 
		ArrayList<Card> cardList4 = new ArrayList<Card>(); 
		ArrayList<Card> cardList5 = new ArrayList<Card>(); 
		ArrayList<Card> cardList6 = new ArrayList<Card>(); 
		ArrayList<Card> cardList7 = new ArrayList<Card>(); 
		ArrayList<Card> cardList8 = new ArrayList<Card>(); 
		ArrayList<Bank> bankList = new ArrayList<Bank>();
		ArrayList<User> userList1 = new ArrayList<User>();
		ArrayList<User> userList2 = new ArrayList<User>();
		ArrayList<Account> accountList1 = new ArrayList<Account>();
		ArrayList<Account> accountList2 = new ArrayList<Account>();
		ArrayList<Account> accountList3 = new ArrayList<Account>();
		ArrayList<Account> accountList4 = new ArrayList<Account>();
		ArrayList<Atm> atmList = new ArrayList<Atm>();
		ArrayList<Atm> atmList1 = new ArrayList<Atm>();
		ArrayList<Atm> atmList2 = new ArrayList<Atm>();
		ArrayList<UnionPay> unionpay = new ArrayList<UnionPay>();
		Atm atm1 = new Atm("01");
		atmList1.add(atm1);
		Atm atm2 = new Atm("02");
		atmList1.add(atm2);
		Atm atm3 = new Atm("03");
		atmList1.add(atm3);
		Atm atm4 = new Atm("04");
		atmList1.add(atm4);
		Atm atm5 = new Atm("05");
		atmList2.add(atm5);
		Atm atm6 = new Atm("06");
		atmList2.add(atm6);
		Card card1 = new Card("6217000010041315709","88888888");
		cardList1.add(card1);
		Card card2 = new Card("6217000010041315715","88888888");
		cardList1.add(card2);
		Card card3 = new Card("6217000010041315718","88888888");
		cardList2.add(card3);
		Card card4 = new Card("6217000010051320007","88888888");
		cardList3.add(card4);
		Card card5 = new Card("6222081502001312389","88888888");
		cardList4.add(card5);
		Card card6 = new Card("6222081502001312390","88888888");
		cardList5.add(card6);
		Card card7 = new Card("6222081502001312399","88888888");
		cardList6.add(card7);
		Card card8 = new Card("6222081502001312400","88888888");
		cardList6.add(card8);
		Card card9 = new Card("6222081502051320785","88888888");
		cardList7.add(card9);
		Card card10 = new Card("6222081502051320786","88888888");
		cardList8.add(card10);
		Account account1 = new Account(10000.00,"3217000010041315709",cardList1);
		accountList1.add(account1);
		Account account2 = new Account(10000.00,"3217000010041315715",cardList2);
		accountList1.add(account2);
		Account account3 = new Account(10000.00,"3217000010051320007",cardList3);
		accountList2.add(account3);
		Account account4 = new Account(10000.00,"3222081502001312389",cardList4);
		accountList3.add(account4);
		Account account5 = new Account(10000.00,"3222081502001312390",cardList5);
		accountList3.add(account5);
		Account account6 = new Account(10000.00,"3222081502001312399",cardList6);
		accountList3.add(account6);
		Account account7 = new Account(10000.00,"3222081502051320785",cardList7);
		accountList4.add(account7);
		Account account8 = new Account(10000.00,"3222081502051320786",cardList8);
		accountList4.add(account8);
		User user1 = new User("杨过",accountList1);
		userList1.add(user1);
		User user2 = new User("郭靖",accountList2);
		userList1.add(user2);
		User user3 = new User("张无忌",accountList3);
		userList2.add(user3);
		User user4 = new User("韦小宝",accountList4);
		userList2.add(user4);
		Bank bank1 = new Bank("中国建设银行",userList1,atmList1);
		bankList.add(bank1);
		Bank bank2 = new Bank("中国工商银行",userList2,atmList2);
		bankList.add(bank2);	
		UnionPay unionpay1 = new UnionPay(bankList);
		unionpay.add(unionpay1);
		Scanner input = new Scanner(System.in);
		Suan suan = new Suan();
		String name = input.next();
		String num1 = input.next();
		String num2 = input.next();
		double num3 = input.nextDouble();
		if(num1 == null&&num2 == null) {
			System.out.print("¥" + account1.getNum());
		}
		String a = null,b = null;	
		double c;
		while(input.next() != "#") {
			if(suan.isExistCard1(unionpay1, name)) {
				if(suan.isExistAtm(unionpay1, num2)) {
					if(suan.isExistCard2(unionpay1, num1)) {
						for(int i = 0;i < bankList.size();i ++) {
						if(suan.isBankAtm(bankList.get(i), num2)) {
							for(int k = 0;k < unionpay1.getBankList().get(i)
									.getUserlist().size();k ++) {
							for(int j = 0;j < bankList.size();j ++) {
							if(suan.isExitAccount(unionpay1.getBankList().get(i)
									.getUserlist().get(k).getAccountList().get(j), num3)) {
								a = unionpay1.getBankList().get(1).getUserlist().get(0).getName();
								b = unionpay1.getBankList().get(1).getName();
								break;
							    }
							  }
							}
							
						  }
						}
					}
				}
			}
			if(!(suan.isExistCard1(unionpay1, name))) {
				System.out.print("Sorry,this card does not exist.");
				System.exit(0);
			}
			if(!(suan.isExistAtm(unionpay1, num2))) {
				System.out.print("Sorry,the ATM's id is wrong.");
				System.exit(0);
			}
			if(!(suan.isExistCard2(unionpay1, num1))) {
				System.out.print("Sorry,your password is wrong.");
				System.exit(0);
			}
			
			if(suan.isBankAtm(bankList.get(0), num2)) {
					System.out.print("Sorry,cross-bank withdrawal is not supported.");
					System.exit(0);
			}
			
			/*for(int i = 0;i < bankList.size();i ++) {
			for(int k = 0;k < unionpay1.getBankList().get(i)
					.getUserlist().size();k ++) {
			for(int j = 0;j < bankList.size();j ++) {
			if(suan.isExitAccount(unionpay1.getBankList().get(i)
					.getUserlist().get(k).getAccountList().get(j), num3)) {
				System.out.print("Sorry,your account balance is insufficient.");
			}
			}
			}
			}*/
			c = account1.getNum() - num3;
			System.out.println(a + "在" + b + "的" + num2 + "号ATM机上存款¥" + Math.abs(num3));
			System.out.printf("当前余额为¥" + String.format("%.2f", c));
		}
	}
}
class Suan{
	public boolean isExistCard1(UnionPay unionpay,String card) {
		Iterator<Bank> bankItr = unionpay.getBankList().iterator();
		while(bankItr.hasNext()) {
			Iterator<User> userItr = bankItr.next().getUserlist().iterator();
			while(userItr.hasNext()) {
				Iterator<Account> accountItr = userItr.next().getAccountList().iterator();
				while(accountItr.hasNext()) {
					Iterator<Card> cardItr = accountItr.next().getCardList().iterator();
					while(cardItr.hasNext()) {
						if(cardItr.next().getNum1().equals(card))
							return true;
						
					}
				}
			}
		}
		return false;
	}
	public boolean isExistCard2(UnionPay unionpay,String card) {
		Iterator<Bank> bankItr = unionpay.getBankList().iterator();
		while(bankItr.hasNext()) {
			Iterator<User> userItr = bankItr.next().getUserlist().iterator();
			while(userItr.hasNext()) {
				Iterator<Account> accountItr = userItr.next().getAccountList().iterator();
				while(accountItr.hasNext()) {
					Iterator<Card> cardItr = accountItr.next().getCardList().iterator();
					while(cardItr.hasNext()) {
						if(cardItr.next().getNum2().equals(card))
							return true;
					}
				}
			}
		}
		return false;
	}
	public boolean isExistAtm(UnionPay unionpay,String atm) {
		Iterator<Bank> bankItr = unionpay.getBankList().iterator();
		while(bankItr.hasNext()) {
			Iterator<Atm> atmItr = bankItr.next().getAtmList().iterator();
			while(atmItr.hasNext()) {
				if(atmItr.next().getId().equals(atm))
					return true;
			}
		}
		return false;
	}
	public boolean isBankAtm(Bank bank,String num){
		String name = null;
		if(num.equals("01")||num.equals("02")||num.equals("03")||num.equals("04")) {
			name = "中国建设银行";
		}
		else if(num.equals("05")||num.equals("06")) {
			name = "中国工商银行";
		}
		if(bank.getName().equals(name)) {
			return true;
		}
		else return false;
	}
	public boolean isExitAccount(Account account,double num) {
		if(account.getNum() - num >= 0) {
			account.setNum(account.getNum() - num); 
			return true;
		}
		else return false;
	}
}

class Card{
	String num1;
	String num2;
	public Card() {
		super();
		// TODO 自动生成的构造函数存根
	}
	public Card(String num1, String num2) {
		super();
		this.num1 = num1;
		this.num2 = num2;
	}
	public String getNum1() {
		return num1;
	}
	public void setNum1(String num1) {
		this.num1 = num1;
	}
	public String getNum2() {
		return num2;
	}
	public void setNum2(String num2) {
		this.num2 = num2;
	}
}
class Atm{
	String id;

	public Atm() {
		super();
		// TODO 自动生成的构造函数存根
	}

	public Atm(String id) {
		super();
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
}
class Account{
	double num;
	String name;
	ArrayList<Card> cardList = new ArrayList<Card>();
	public Account() {
		super();
		// TODO 自动生成的构造函数存根
	}
	
	public Account(double num, String name, ArrayList<Card> cardList) {
		super();
		this.num = num;
		this.name = name;
		this.cardList = cardList;
	}

	public double getNum() {
		return num;
	}

	public void setNum(double num) {
		this.num = num;
	}

	public String getName() {
		return name;
	}

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

	public ArrayList<Card> getCardList() {
		return cardList;
	}

	public void setCardList(ArrayList<Card> cardList) {
		this.cardList = cardList;
	}
	
}
class User{
	String name;
	ArrayList<Account> accountList = new ArrayList<Account>();
	public User() {
		super();
		// TODO 自动生成的构造函数存根
	}

	public User(String name,ArrayList<Account> accountList) {
		super();
		this.name = name;
		this.accountList = accountList;
	}

	public String getName() {
		return name;
	}

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

	public ArrayList<Account> getAccountList() {
		return accountList;
	}

	public void setAccountList(ArrayList<Account> accountList) {
		this.accountList = accountList;
	}
	
}
class Bank{
	String name;
	ArrayList<User> userlist = new ArrayList<User>();
	ArrayList<Atm> atmList = new ArrayList<Atm>();
	public Bank(String name) {
		
	}
	public Bank(String name, ArrayList<User> userlist, ArrayList<Atm> atmList) {
		super();
		this.name = name;
		this.userlist = userlist;
		this.atmList = atmList;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public ArrayList<User> getUserlist() {
		return userlist;
	}
	public void setUserlist(ArrayList<User> userlist) {
		this.userlist = userlist;
	}
	public ArrayList<Atm> getAtmList() {
		return atmList;
	}
	public void setAtmList(ArrayList<Atm> atmList) {
		this.atmList = atmList;
	}
}
class UnionPay{
	ArrayList<Bank> bankList = new ArrayList<Bank>();
	public UnionPay() {
		super();
		// TODO 自动生成的构造函数存根
	}
	public UnionPay(ArrayList<Bank> bankList) {
		super();
		this.bankList = bankList;
	}
	public ArrayList<Bank> getBankList() {
		return bankList;
	}
	public void setBankList(ArrayList<Bank> bankList) {
		this.bankList = bankList;
	} 
}

 

  再看一下题目集九:

输入格式:

每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:

  • 取款功能输入数据格式: 卡号 密码 ATM机编号 金额(由一个或多个空格分隔)
  • 查询余额功能输入数据格式: 卡号

输出格式:

①输入错误处理

  • 如果输入卡号不存在,则输出Sorry,this card does not exist.
  • 如果输入ATM机编号不存在,则输出Sorry,the ATM's id is wrong.
  • 如果输入银行卡密码错误,则输出Sorry,your password is wrong.
  • 如果输入取款金额大于账户余额,则输出Sorry,your account balance is insufficient.

②取款业务输出

输出共两行,格式分别为:

业务:取款 [用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]

当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

③查询余额业务输出

业务:查询余额 ¥[金额]

金额保留两位小数。

输入样例1:

在这里给出一组输入。例如:

6222081502001312390 88888888 06 500.00
#
 

输出样例1:

在这里给出相应的输出。例如:

业务:取款 张无忌在中国工商银行的06号ATM机上取款¥500.00
当前余额为¥9500.00
 

输入样例2:

在这里给出一组输入。例如:

6217000010041315709  88888888 06 3500.00
#
 

输出样例2:

在这里给出相应的输出。例如:

业务:取款 杨过在中国工商银行的06号ATM机上取款¥3500.00
当前余额为¥6395.00
 

输入样例3:

在这里给出一组输入。例如:

6217000010041315715
#
 

输出样例3:

在这里给出相应的输出。例如:

业务:查询余额 ¥10000.00
 

输入样例4:

在这里给出一组输入。例如:

6222081502001312390 88888888 01 500.00
6222081502051320786 88888888 06 1200.00
6217000010041315715 88888888 02 1500.00
6217000010041315709  88888888 02 3500.00
6217000010041315715
#
 

输出样例4:

在这里给出相应的输出。例如:

业务:取款 张无忌在中国建设银行的01号ATM机上取款¥500.00
当前余额为¥9490.00
业务:取款 韦小宝在中国工商银行的06号ATM机上取款¥1200.00
当前余额为¥8800.00
业务:取款 杨过在中国建设银行的02号ATM机上取款¥1500.00
当前余额为¥8500.00
业务:取款 杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥5000.00
业务:查询余额 ¥5000.00
 

输入样例5:

在这里给出一组输入。例如:

6640000010045442002 88888888 09 3000
6640000010045442002 88888888 06 8000
6640000010045442003 88888888 01 10000
6640000010045442002
#
 

输出样例5:

在这里给出相应的输出。例如:

业务:取款 张三丰在中国农业银行的09号ATM机上取款¥3000.00
当前余额为¥6880.00
业务:取款 张三丰在中国工商银行的06号ATM机上取款¥8000.00
当前余额为¥-1416.00
业务:取款 张三丰在中国建设银行的01号ATM机上取款¥10000.00
当前余额为¥-11916.00
业务:查询余额 ¥-11916.00

 

  

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
    	Scanner input = new Scanner(System.in);
    	
    	UnionPay unionPay = new UnionPay();   	
    	
    	Bank ccb = new Bank("1001","中国建设银行");
    	Bank icbc = new Bank("1002","中国工商银行");
    	Bank abc = new Bank("1003","中国农业银行");
    	unionPay.addBank(ccb);
    	unionPay.addBank(icbc);
    	unionPay.addBank(abc);
    	
    	ATM aTM1 = new ATM("01",ccb);
    	ATM aTM2 = new ATM("02",ccb);
    	ATM aTM3 = new ATM("03",ccb);
    	ATM aTM4 = new ATM("04",ccb);
    	ATM aTM5 = new ATM("05",icbc);
    	ATM aTM6 = new ATM("06",icbc);
    	ATM aTM7 = new ATM("07",abc);
    	ATM aTM8 = new ATM("08",abc);
    	ATM aTM9 = new ATM("09",abc);
    	ATM aTM10 = new ATM("10",abc);
    	ATM aTM11 = new ATM("11",abc);
    	
    	ccb.addATM(aTM1);
    	ccb.addATM(aTM2);
    	ccb.addATM(aTM3);
    	ccb.addATM(aTM4);
    	icbc.addATM(aTM5);
    	icbc.addATM(aTM6);
    	abc.addATM(aTM7);
    	abc.addATM(aTM8); 
    	abc.addATM(aTM9);
    	abc.addATM(aTM10);
    	abc.addATM(aTM11);
    	
    	User Yangguo = new User("360101200102122324","杨过","1");
    	User Guojing = new User("360101200012302552","郭靖","1");
    	User Zhangwuji = new User("360502199805163221","张无忌","1");
    	User Weixiaobao = new User("360201200513243326","韦小宝","1");
    	User Zhangsanfeng = new User("3640000010045442002","张三丰","2");
    	User Linghuchong = new User("3640000010045441009","令狐冲","2");
    	User Qiaofeng = new User("3630000010033431001","乔峰","2");
    	User Hongqigong = new User("3630000010033431008","洪七公","2");
    	
    	Account ccbAcc1 = new DebitAccount("3217000010041315709",10000.00,Yangguo,ccb);
    	Account ccbAcc2 = new DebitAccount("3217000010041315715",10000.00,Yangguo,ccb);
    	Account ccbAcc3 = new DebitAccount("3217000010051320007",10000.00,Guojing,ccb);
    	Account icbcAcc1 = new DebitAccount("3222081502001312389",10000.00,Zhangwuji,icbc);
    	Account icbcAcc2 = new DebitAccount("3222081502001312390",10000.00,Zhangwuji,icbc);
    	Account icbcAcc3 = new DebitAccount("3222081502001312399",10000.00,Zhangwuji,icbc);
    	Account icbcAcc4 = new DebitAccount("3222081502051320785",10000.00,Weixiaobao,icbc);
    	Account icbcAcc5 = new DebitAccount("3222081502051320786",10000.00,Weixiaobao,icbc);
    	Account ccbAcc4 = new CreditAccount("3640000010045442002",10000.00,Zhangsanfeng,ccb);
    	Account icbcAcc6 = new CreditAccount("3640000010045441009",10000.00,Linghuchong,icbc);
    	Account abcAcc1 = new CreditAccount("3630000010033431001",10000.00,Qiaofeng,abc);
    	Account abcAcc2 = new CreditAccount("3630000010033431008",10000.00,Hongqigong,abc);
    	
    	ccb.addAccount(ccbAcc1);
    	ccb.addAccount(ccbAcc2);
    	ccb.addAccount(ccbAcc3);
    	ccb.addAccount(ccbAcc4);
    	icbc.addAccount(icbcAcc1);
    	icbc.addAccount(icbcAcc2);
    	icbc.addAccount(icbcAcc3);
    	icbc.addAccount(icbcAcc4);
    	icbc.addAccount(icbcAcc5);
    	icbc.addAccount(icbcAcc6);
    	abc.addAccount(abcAcc1);
    	abc.addAccount(abcAcc2);
    
    	
    	Yangguo.addAccount(ccbAcc1);
    	Yangguo.addAccount(ccbAcc2);
    	Guojing.addAccount(ccbAcc3);
    	Zhangwuji.addAccount(icbcAcc1);
    	Zhangwuji.addAccount(icbcAcc2);
    	Zhangwuji.addAccount(icbcAcc3);
    	Weixiaobao.addAccount(icbcAcc4);
    	Weixiaobao.addAccount(icbcAcc5);
    	Zhangsanfeng.addAccount(ccbAcc4);
    	Linghuchong.addAccount(icbcAcc6);
    	Qiaofeng.addAccount(abcAcc1);
    	Hongqigong.addAccount(abcAcc2);
    	
    	
    	
    	Card ccbCard1 = new DebitCard("6217000010041315709","88888888",ccbAcc1);
    	Card ccbCard2 = new DebitCard("6217000010041315715","88888888",ccbAcc1);
    	Card ccbCard3 = new DebitCard("6217000010041315718","88888888",ccbAcc2);
    	Card ccbCard4 = new DebitCard("6217000010051320007","88888888",ccbAcc3);
    	Card icbcCard5 = new DebitCard("6222081502001312389","88888888",icbcAcc1);
    	Card icbcCard6 = new DebitCard("6222081502001312390","88888888",icbcAcc2);
    	Card icbcCard7 = new DebitCard("6222081502001312399","88888888",icbcAcc3);
    	Card icbcCard8 = new DebitCard("6222081502001312400","88888888",icbcAcc3);
    	Card icbcCard9 = new DebitCard("6222081502051320785","88888888",icbcAcc4);
    	Card icbcCard10 = new DebitCard("6222081502051320786","88888888",icbcAcc5);
    	Card icbcCard11 = new CreditCard("6640000010045442002","88888888",ccbAcc4);
    	Card icbcCard12 = new CreditCard("6640000010045442003","88888888",ccbAcc4);
    	Card icbcCard13 = new CreditCard("6640000010045441009","88888888",icbcAcc6);
    	Card icbcCard14 = new CreditCard("6630000010033431001","88888888",abcAcc1);
    	Card icbcCard15 = new CreditCard("6630000010033431008","88888888",abcAcc2);
    	
    	ccbAcc1.addCard(ccbCard1);
    	ccbAcc1.addCard(ccbCard2);
    	ccbAcc2.addCard(ccbCard3);
    	ccbAcc3.addCard(ccbCard4);
    	icbcAcc1.addCard(icbcCard5);
    	icbcAcc2.addCard(icbcCard6);
    	icbcAcc3.addCard(icbcCard7);
    	icbcAcc3.addCard(icbcCard8);
    	icbcAcc4.addCard(icbcCard9);
    	icbcAcc5.addCard(icbcCard10);
    	ccbAcc4.addCard(icbcCard11);
    	ccbAcc4.addCard(icbcCard12);
    	icbcAcc6.addCard(icbcCard13);
    	abcAcc1.addCard(icbcCard14);
    	abcAcc2.addCard(icbcCard15);
    	
    	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]));
        		wd.withdraw();
        	}
    	}   	
    }
}

class Bank {
    private String bankNO;
    private String bankName;
    private ArrayList<Account> accountList = new ArrayList<Account>();
    private ArrayList<ATM> ATMList = new ArrayList<ATM>();

    public Bank() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Bank(String bankNO, String bankName) {
        super();
        this.bankNO = bankNO;
        this.bankName = bankName;
    }

    public String getBankNO() {
        return bankNO;
    }

    public void setBankNO(String bankNO) {
        this.bankNO = bankNO;
    }

    public String getBankName() {
        return bankName;
    }

    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    public void addAccount(Account account) {
        this.accountList.add(account);
    }

    public void removeAccount(Account account) {
        this.accountList.remove(account);
    }
    
    public void addATM(ATM aTM) {
    	this.ATMList.add(aTM);
    }
    
    public void removeATM(ATM aTM) {
    	this.ATMList.remove(aTM);
    }

	public ArrayList<Account> getAccountList() {
		return accountList;
	}

	public void setAccountList(ArrayList<Account> accountList) {
		this.accountList = accountList;
	}

	public ArrayList<ATM> getATMList() {
		return ATMList;
	}

	public void setATMList(ArrayList<ATM> aTMList) {
		ATMList = aTMList;
	}
}

class User {
    private String ID;
    private String name;
    private String attribute;
    ArrayList<Account> list = new ArrayList<Account>();

    public User() {
        super();
        // TODO Auto-generated constructor stub
    }    

    public User(String iD, String name, String attribute) {
		super();
		ID = iD;
		this.name = name;
		this.attribute = attribute;
	}

	public String getID() {
        return ID;
    }

    public void setID(String iD) {
        ID = iD;
    }

    public String getAttribute() {
        return attribute;
    }

    public void setAttribute(String attribute) {
    	this.attribute = attribute;
    }    

    public String getName() {
		return name;
	}

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

	public void addAccount(Account account) {
        this.list.add(account);
    }

    public void removeAccount(Account account) {
        this.list.remove(account);
    }
}

abstract class Account {
    private String accountNO;
    private double balance = 0;
    private User user = null;
    private Bank bank = null;
    private static ArrayList<Card> list = new ArrayList<Card>();

    public Account() {
        super();
        // TODO Auto-generated constructor stub
    }   

    public Account(String accountNO, double balance, User user, Bank bank) {
		super();
		this.accountNO = accountNO;
		this.balance = balance;
		this.user = user;
		this.bank = bank;
    }

	public void addCard(Card card) {
        list.add(card);
    }

    public void removeCard(Card card) {
        list.remove(card);
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public String getAccountNO() {
        return accountNO;
    }

    public void setAccountNO(String accountNO) {
        this.accountNO = accountNO;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

	public Bank getBank() {
		return bank;
	}

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

	public ArrayList<Card> getList() {
		return list;
	}

	public void setList(ArrayList<Card> list) {
		Account.list = list;
	}
	
	public static Account getAmountbyCardNO(String cardNO) {
		Iterator<Card> cardItr = Account.list.iterator();
		Card card = null;
		
		while(cardItr.hasNext()) {
			card = cardItr.next();
			if(card.getCardNO().equals(cardNO)) {
				return card.getAccount();
			}
		}
		
		return null;
	}
}

class DebitAccount extends Account{
	private String accountNO;
    private double balance = 0;
    private User user = null;
    private Bank bank = null;
    private static ArrayList<Card> list = new ArrayList<Card>();
    public DebitAccount(String accountNO, double balance, User user, Bank bank) {
		this.setAccountNO(accountNO);
		this.setBalance(balance);
		this.setUser(user);
		this.setBank(bank);
    }
}
class CreditAccount extends Account{
    private static ArrayList<Card> list = new ArrayList<Card>();
    public CreditAccount(String accountNO, double balance, User user, Bank bank) {
		this.setAccountNO(accountNO);
		this.setBalance(balance);
		this.setUser(user);
		this.setBank(bank);
    }
}
abstract class Card {
    private String cardNO;
    private String cardPassword;
    private Account account = null;

    public Card() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Card(String cardNO, String cardPassword, Account account) {
        super();
        this.cardNO = cardNO;
        this.cardPassword = cardPassword;
        this.account = account;
    }

    public String getCardNO() {
        return cardNO;
    }

    public void setCardNO(String cardNO) {
        this.cardNO = cardNO;
    }

    public String getCardPassword() {
        return cardPassword;
    }

    public Account getAccount() {
        return account;
    }

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

    public void setCardPassword(String cardPassword) {
        this.cardPassword = cardPassword;
    }

    public boolean checkCard() {
        Pattern p = Pattern.compile("\\d{16}+");
        Matcher m = p.matcher(this.cardNO);
        return m.matches();
    }

    public boolean checkPassword(String password) {
        return this.cardPassword.equals(password);
    }    
}
class DebitCard extends Card{
	    private String cardNO;
	    private String cardPassword;
	    private Account account = null;

	    public DebitCard() {
	        super();
	        // TODO Auto-generated constructor stub
	    }

	    public DebitCard(String cardNO, String cardPassword, Account account) {
	        super();
	        this.setCardNO(cardNO);
	        this.setCardPassword(cardPassword);
	        this.setAccount(account);
	    }

}
class CreditCard extends Card{
	private String cardNO;
    private String cardPassword;
    private Account account = null;

    public CreditCard() {
        super();
        // TODO Auto-generated constructor stub
    }

    public CreditCard(String cardNO, String cardPassword, Account account) {
        super();
        this.setCardNO(cardNO);
        this.setCardPassword(cardPassword);
        this.setAccount(account);
    }

}
class ATM {
    private String ATMID;
    private Bank bank = null;

    public ATM() {
        super();
        // TODO Auto-generated constructor stub
    }   

    public ATM(String aTMID, Bank bank) {
		super();
		ATMID = aTMID;
		this.bank = bank;
	}

	public String getATMID() {
        return ATMID;
    }

    public void setATMID(String aTMID) {
        ATMID = aTMID;
    }

	public Bank getBank() {
		return bank;
	}

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

class Withdraw {
	private UnionPay unionPay;
	private String cardNO;
	private String cardPassword;
	private String ATMID;
	private double amount;

	public Withdraw() {
		super();
		// TODO Auto-generated constructor stub
	}
	

	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;
	}
	
	public Withdraw(String cardNO, String cardPassword, String aTMID, double amount) {
		super();
		this.cardNO = cardNO;
		this.cardPassword = cardPassword;
		ATMID = aTMID;
		this.amount = amount;
	}
	
	public boolean Amount(double amount,double balance1) {
		if(amount - balance1 > 0)
			return true;
		else return false;
	}
	
	public double A(double amount,double balance1) {
		if(Amount(amount,balance1)) {
			return amount - balance1;
		}
		return 0;
	}
	public void withdraw() {
		/**
		 * 校验该卡是否存在
		 */		
		Card card = ValidateData.getCardbyCardNO(unionPay, cardNO);
		if(card == null) {
			System.out.println("Sorry,this card does not exist.");
			System.exit(0);
		}
		
		/**
		 * 校验ATM是否存在
		 */
		ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID);
		if(aTM == null) {
			System.out.println("Sorry,the ATM's id is wrong.");
			System.exit(0);
		}
		
		Account account = Account.getAmountbyCardNO(cardNO);
		double balance = account.getBalance();
		
		/**
		 * 校验卡密码是否正确
		 */
		if(!card.getCardPassword().equals(cardPassword)) {
			System.out.println("Sorry,your password is wrong.");
			System.exit(0);
		}
		
		/**
		 * 校验取款金额是否大于余额
		 */
		if(account.getUser().getAttribute() != "2") {
			if (amount >= balance) {
				System.out.println("Sorry,your account balance is insufficient.");
				System.exit(0);
			}
		}
		else if(account.getUser().getAttribute() != "1") {
			if (balance - amount <= -50000.00||amount >= 50000.00) {
				System.out.println("Sorry,your account balance is insufficient.");
				System.exit(0);
			}
		}
		
		
		/**
		 * 校验是否为跨行取款
		 */
		if (account.getBank().getBankNO() != aTM.getBank().getBankNO()) {
			if(aTM.getBank().getBankNO()=="1001") {
				if(Amount(amount,balance)) 
					account.setBalance(balance - 1.02 * amount - 0.05 * A(amount,balance));
				else{
					account.setBalance(balance - 1.02 * amount);
				}
			}
			if(aTM.getBank().getBankNO()=="1002") {
				if(Amount(amount,balance))
				account.setBalance(balance - 1.03 * amount - 0.05 * A(amount,balance));
				else {
					account.setBalance(balance - 1.03 * amount);
				}
			}
			if(aTM.getBank().getBankNO()=="1003") {
				if(Amount(amount,balance)) 
					account.setBalance(balance - 1.04 * amount - 0.05 * A(amount,balance));
				else{
					account.setBalance(balance - 1.04 * amount);
				}
			}
		}	
		else {
			if(Amount(amount,balance)) {
				if(balance <= 0) {
					account.setBalance(balance - 1.05 * amount);
				}
				else{
					account.setBalance(balance - amount - 0.05 * A(amount,balance));
				}
			}			
			else{
				account.setBalance(balance - amount);//取款更新余额操作
			}
		}
        if(balance <= -50000) {
		System.out.println("Sorry,your account balance is insufficient.");
		System.exit(0);
	}
		if(amount >= 0) {
			showResult(account,1);
		}
		else {
			showResult(account,0);
		}
		
	}
	
	public void showResult(Account account,int flag) {
		ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID);
		String type = "";
		if(flag == 1) {
			type = "取款";			
		}else {
			type = "存款";
			amount *= -1;
		}
		String userName = account.getUser().getName();
		String bankName = aTM.getBank().getBankName();
		System.out.println("业务:" + type + " " + userName + "在" +
				bankName + "的" + ATMID + "号ATM机上" + type + String.format("¥%.2f", amount));
				System.out.println("当前余额为" + String.format("¥%.2f", account.getBalance()));
	}
}

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

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

class GetBalance {
	private UnionPay unionPay;
	
	public GetBalance() {
		super();
		// TODO Auto-generated constructor stub
	}

	public GetBalance(UnionPay unionPay) {
		super();
		this.unionPay = unionPay;
	}

	public double getBalance(String cardNO) {
		return ValidateData.getCardbyCardNO(unionPay, cardNO).getAccount().getBalance();
	}
}

  

 

这两道题都是围绕ATM机来写的,题目集八时只要写借记卡,并不需要写信用卡,所以并没有太难,因为信用卡需要写更多的判断语句还有数据初始化,接下来看一下生成报表类图和复杂度:

 

 

 

 

 

 

 

这两道题都采用了抽象类Account,Card等,

 

 

 其实这两道题主要还是要初始化数据正确,其他的就按题目要求的来写,例如添加类和方法,其他的就例如Accout类,它里面的private String accountNO;private double balance =0;privateUser user = null;private Bank bank = znull;private static ArrayList<Card> list = new ArrayList<Card>();这些数据就需要扫初始化变量,其他的也一样。

 

三:采坑心得

 

1. 在每次提交的时候,都需要去测试一下,因为你可能只是过了其中的一个点,其他的点并没有过,因为我每次用一个数据测试,发现过了,但提交就错了,然后用其他的,数据测试就发现了问题所在,所以每次需要将多个数据测试才能发现问题。

 

2.因为代码都是在eclipse中写的,所以每次报错都能看到,也就能及时的修改,但提交源码是有时候还有些测试点是不会过的,所以要学会根据测试点来优化自己的代码。

 

3.在许多地方你会发现用到了if-else语句,但这些都只是增加复杂度的语法,所以在日期类型题目,能不用if-else语句就不用,多使用swith语句,他的复杂程度会下降很多,所以if-else语句是一种增加复杂程度的语句,毕竟每家公司对复杂度的要求有明确的规定,所以按这些要求来,if-else语句必是不可以用的,所以以后都少用if-else语句。 

四:改进建议

 

1.于题目集八和题目集九我认为可以加强代码的优化,让代码的复杂度降低,同样题目集七也可以加强代码的优化,降低复杂度;少使用if-else语句,除非必须,多将一些语句放进类里面。

 

2.将代码写的符合一些公司的要求,例如阿里巴巴,按照更好的规则要求自己,使代码更整齐完善,也方便其他人观看你的代码,也更有利于你之后发现代码错误然后修改你的代码,所以以更高的要求要求自己,写出更好的代码。

 

五:总结

 

这三次的题目集主要还是题目集八有难度,其他两次还行,题目九是因为老师发了题目集八的源码,只需要添加修改即可,,难度在于题目集八,这道题主要是当时初始化数据好像搞错了,导致后面就都不正确,所以我找到自己的缺陷,这就是进步的第一步—发现问题。最后的题目让我了解到Java的学习之路并不是一帆风顺,也是磕磕绊绊的,但这并不是放弃的理由,所以需要我更加的努力学习,希望在下次作业中继续进步努力学习。教师、课程、作业、实验、课上及课下组织方式都很好。

 

                                 

posted @ 2021-06-20 20:20  浅夏侯青  阅读(84)  评论(0)    收藏  举报