第三次博客作业

一.

知识点:

Java语言中类的声明、创建与使用;类的构造方法;成员变量、成员方法的定义与使用方法;

 java中引用变量与对象实例之间的关系与区别;
 java中方法调用时引用类型参数的传递过程;
 java中图形继承 的使用方法;
 java中对象组合的方式与方法;
Java中数据排序的实现方式。

题量适中 难度还行 、

 

二.

 

7-1 图形卡片排序游戏 (40 分)
 

掌握类的继承、多态性使用方法以及接口的应用。详见作业指导书 2020-OO第07次作业-1指导书V1.0.pdf

输入格式:

  • 首先,在一行上输入一串数字(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. 排序后的各图形类型及面积,格式同排序前的输出;
      1. 所有图形的面积总和,格式为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.*;
        class DealCardList{
        ArrayList<Card> cardList=new ArrayList<Card>();
        public DealCardList()
        {
        }
        public DealCardList(ArrayList<Integer> list)
        { int i;
        for(i=0;i<list.size();i++)
        {
        switch(list.get(i))
        {
        case 1: cardList.add(new Card(new Circle(Main.input.nextDouble())));
        break;
        case 2: cardList.add(new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble())));
        break;
        case 3: cardList.add(new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())));
        break;
        case 4: cardList.add(new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())));
        break;
        }

        }

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

        }
        }
        return true;
        }
        public void cardSort()
        {
        Collections.sort(cardList);
        }
        public void showResult()
        {
        double a=0;
        System.out.print("The original list:\n");
        for(int i =0;i < cardList.size();i++) {

        System.out.print(String.format(cardList.get(i).getShape().getShapeName()+":"+"%.2f",cardList.get(i).getShape().getArea())+" ");
        }
        cardSort();
        System.out.print("\nThe sorted list:\n");
        for(int i =0;i < cardList.size();i++) {

        System.out.print(String.format(cardList.get(i).getShape().getShapeName()+":"+"%.2f",cardList.get(i).getShape().getArea())+" ");
        a=a+cardList.get(i).getShape().getArea();
        }
        System.out.println(String.format("\nSum of area:%.2f",a));
        }
        }
        class Circle extends Shape{
        double radius;
        public Circle(double radius)
        {
        this.radius=radius;
        super.shapeName="Circle";
        }
        public double getRadius() {
        return radius;
        }
        public void setRadius(double radius) {
        this.radius = radius;
        }
        public double getArea()
        {
        return Math.PI*radius*radius;
        }
        public boolean validata()
        {
        if(radius<=0)
        return false;
        else
        return true;

        }

        }
        class Rectangle extends Shape{
        double width;
        double length;
        public Rectangle()
        {

        }
        public Rectangle(double width,double length)
        {
        this.width=width;
        this.length=length;
        super.shapeName="Rectangle";
        }
        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 length*width;
        }
        public boolean validata()
        {
        if(width<=0||length<=0)
        return false;
        else
        return true;
        }
        }
        class Triangle extends Shape{
        double side1;
        double side2;
        double side3;
        public Triangle()
        {

        }
        public Triangle(double side1,double side2,double side3)
        {
        this.side1=side1;
        this.side2=side2;
        this.side3=side3;
        super.shapeName="Triangle";
        }
        public double getArea()
        {
        double p=(side1+side2+side3)/2;
        return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
        }
        public boolean validata()
        {
        if(side1+side2<side3||side2+side3<side1||side1+side3<side2)
        return false;
        else
        return true;
        }

        }
        class Trapezoid extends Shape{
        double topSide;
        double height;
        double bottomSide;
        public Trapezoid()
        {

        }
        public Trapezoid(double topSide,double bottomSide,double height )
        {
        this.topSide=topSide;
        this.bottomSide=bottomSide;
        this.height=height;
        super.shapeName="Trapezoid";
        }
        public double getArea()
        {
        return height*(topSide+bottomSide)/2;
        }
        public boolean validata()
        {
        if(height<=0||topSide<=0||bottomSide<=0)
        return false;
        else
        return true;
        }
        }
        abstract class Shape
        {
        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 double getArea()
        {
        return 0;
        }
        public boolean validata()
        {
        return true;
        }

        }
        class Card implements Comparable<Card>{
        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)
        {
        if(this.getShape().getArea() < card.getShape().getArea())
        return 1;
        else if(this.getShape().getArea() > card.getShape().getArea())
        return -1;
        return 0;
        }
        }

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

         

         

        7-2 图形卡片分组游戏 (60 分)
         

        掌握类的继承、多态性使用方法以及接口的应用。 具体需求参考作业指导书。

        2021-OO第07次作业-2指导书V1.0.pdf

        输入格式:

        • 在一行上输入一串数字(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:面积值

        输入样例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 Separated List:
        [Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]
        The Separated sorted List:
        [Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]
        The max area:98.52
         
        结尾无空行

        输入样例3:

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

        2 1 2 1 1 3 3 4 4 1 1 1 2 1 0
        2.3 3.5 2.5 4.5 2.1 2.6 8.5 3.2 3.1 3.6 8.5 7.5 9.1245 6.5 3.4 10.2 11.2 11.6 15.4 5.8 2.13 6.2011 2.5 6.4 18.65
         
        结尾无空行

        输出样例3:

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

        The original list:
        [Rectangle:8.05 Circle:19.63 Rectangle:9.45 Circle:21.24 Circle:226.98 Triangle:4.65 Triangle:29.80 Trapezoid:50.49 Trapezoid:175.56 Circle:105.68 Circle:14.25 Circle:120.81 Rectangle:16.00 Circle:1092.72 ]
        The Separated List:
        [Circle:19.63 Circle:21.24 Circle:226.98 Circle:105.68 Circle:14.25 Circle:120.81 Circle:1092.72 ][Rectangle:8.05 Rectangle:9.45 Rectangle:16.00 ][Triangle:4.65 Triangle:29.80 ][Trapezoid:50.49 Trapezoid:175.56 ]
        The Separated sorted List:
        [Circle:1092.72 Circle:226.98 Circle:120.81 Circle:105.68 Circle:21.24 Circle:19.63 Circle:14.25 ][Rectangle:16.00 Rectangle:9.45 Rectangle:8.05 ][Triangle:29.80 Triangle:4.65 ][Trapezoid:175.56 Trapezoid:50.49 ]
        The max area:1601.31
         
        结尾无空行

        输入样例4:

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

        1 1 3 0
        6.5 12.54 3.6 5.3 6.4
         
        结尾无空行

        输出样例4:

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

        The original list:
        [Circle:132.73 Circle:494.02 Triangle:9.54 ]
        The Separated List:
        [Circle:132.73 Circle:494.02 ][][Triangle:9.54 ][]
        The Separated sorted List:
        [Circle:494.02 Circle:132.73 ][][Triangle:9.54 ][]
        The max area:626.75
         
        结尾无空行

         

        源代码:

        import java.util.*;

         class DealCardList{

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

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

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

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

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

        public DealCardList()

        }

        public DealCardList(ArrayList<Integer> list)

        {int i;

        for(i=0;i<list.size();i++)

        {

        switch(list.get(i))

        {

        case 1: cardList.add(new Card(new Circle(Main.input.nextDouble())));

        break;

        case 2: cardList.add(new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble())));

        break;

        case 3: cardList.add(new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())));

        break;

        case 4: cardList.add(new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())));

        break;

        }

         

        }

         

        }

        public boolean validate()

        {

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

        {

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

        return false;

         

        }

        }

                if(cardList.size()<=0)

                    return false;

        return true;

        }

        public void cardSort()

        {

        Collections.sort(cardList);

        Collections.sort(a1);

        Collections.sort(a2);

        Collections.sort(a3);

        Collections.sort(a4);

        }

        public void showResult()

        {

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

        {

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

        {

        a1.add(cardList.get(i));

        }

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

        {

        a2.add(cardList.get(i));

        }

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

        {

        a3.add(cardList.get(i));

        }

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

        {

        a4.add(cardList.get(i));

        }

         

        }

        double a=0,b=0,c=0,d=0,e=0;

        System.out.print("The original list:\n[");

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

         

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

        }

        System.out.print("]\n");

        System.out.print("The Separated List:\n");

        System.out.print("[");

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

         

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

        System.out.print("]");

        System.out.print("[");

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

         

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

        System.out.print("]");

        System.out.print("[");

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

         

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

        System.out.print("]");

        System.out.print("[");

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

         

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

        System.out.print("]");

        cardSort();

        System.out.print("\nThe Separated sorted List:\n");

        System.out.print("[");

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

                a=a+a1.get(i).getShape().getArea();

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

        }

        System.out.print("]");

        System.out.print("[");

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

        b=b+a2.get(i).getShape().getArea();

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

        System.out.print("]");

        System.out.print("[");

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

        c=c+a3.get(i).getShape().getArea();

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

        System.out.print("]");

        System.out.print("[");

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

        d=d+a4.get(i).getShape().getArea();

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

        System.out.print("]");

         

         

        double max1=(a>=b)?a:b;

            double max2=(c>=d)?c:d;

             double Max;

         

         

               if(max1>=max2){

               

                   Max=max1;

               }

               else{

               

                   Max=max2;

               }

        System.out.println(String.format("\nThe max area:%.2f",Max));

        }

        }

        class Circle extends Shape{

        double radius;

        public Circle(double radius)

        {

        this.radius=radius;

        super.shapeName="Circle";

        }

        public double getRadius() {

        return radius;

        }

        public void setRadius(double radius) {

        this.radius = radius;

        }

        public double getArea()

        {

        return Math.PI*radius*radius;

        }

        public boolean validata()

        {

        if(radius<=0)

        return false;

        else

        return true;

         

        }

         

         }

        class Rectangle extends Shape{

        double width;

        double length;

        public Rectangle()

        {

         

        }

        public Rectangle(double width,double length)

        {

        this.width=width;

        this.length=length;

        super.shapeName="Rectangle";

        }

        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 length*width;

        }

        public boolean validata()

        {

        if(width<=0||length<=0)

        return false;

        else

        return true;

        }

        }

        class Triangle extends Shape{

        double side1;

        double side2;

        double side3;

        public Triangle()

        {

         

        }

        public Triangle(double side1,double side2,double side3)

        {

        this.side1=side1;

        this.side2=side2;

        this.side3=side3;

        super.shapeName="Triangle";

        }

        public double getArea()

        {

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

        return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));      

        }

        public boolean validata()

        {

        if(side1+side2<side3||side2+side3<side1||side1+side3<side2)

        return false;

        else

        return true;

        }

         

        }

        class Trapezoid extends Shape{

        double topSide;

        double height;

        double bottomSide;

        public Trapezoid()

        {

         

        }

        public Trapezoid(double topSide,double bottomSide,double height )

        {

        this.topSide=topSide;

        this.bottomSide=bottomSide;

        this.height=height;

        super.shapeName="Trapezoid";

        }

        public double getArea()

        {

        return height*(topSide+bottomSide)/2;

        }

        public boolean validata()

        {

        if(height<=0||topSide<=0||bottomSide<=0)

        return false;

        else

        return true;

        }

        }

         abstract class Shape

         {

        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 double getArea()

        {

        return 0;

        }

        public boolean validata()

        {

        return true;

        }

         

         }

         class Card implements Comparable<Card>{

        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)

        {

        if(this.getShape().getArea() < card.getShape().getArea())

        return 1;

        else if(this.getShape().getArea() > card.getShape().getArea())

        return -1;

        return 0;

        }

        }

         

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

        }

        }

         

        7-3 ATM机类结构设计(一) (100 分)
         

        设计ATM仿真系统,具体要求参见作业说明。 OO作业8-1题目说明.pdf

        输入格式:

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

        • 存款、取款功能输入数据格式: 卡号 密码 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.Scanner;

         

        public class Main {

         

        public static void main(String[] args) {

        // TODO 自动生成的方法存根

        Scanner in = new Scanner(System.in);

        // System.out.printf("www");

        UnionPay unionPay = Initialize();

        String line;

        StringBuffer sb = new StringBuffer();

         

        while (true) {

        line = in.nextLine();

        if ("#".equals(line)) {

        break;

        }

        sb.append(line.trim() + " # ");

         

        }

        String[] str = sb.toString().trim().split("\\s+");

        int i = 0;

        while (i <= str.length - 2) {

        if ("#".equals(str[i + 1].trim())) {

        if (!unionPay.translate(str[i])) {

        break;

        }

        i += 2;

        } else {

        if (!unionPay.translate(str[i], str[i + 1], str[i + 2], Double.parseDouble(str[i + 3]))) {

        break;

        }

        i += 5;

        }

        }

        }

         

        static UnionPay Initialize() {

        // 基础数据的初始化----------------------------------------------------

        UnionPay unionPay = new UnionPay("中国银联");

         

        Bank bank1 = new Bank("中国建设银行");

        Bank bank2 = new Bank("中国工商银行");

         

        ArrayList<Bank> Banks = new ArrayList<>();

         

        Banks.add(bank1);

        Banks.add(bank2);

         

        unionPay.setBanks(Banks);

         

        ATM ATM1 = new ATM("01");

        ATM ATM2 = new ATM("02");

        ATM ATM3 = new ATM("03");

        ATM ATM4 = new ATM("04");

        ATM ATM5 = new ATM("05");

        ATM ATM6 = new ATM("06");

         

        ArrayList<ATM> ATMs1 = new ArrayList<>();

        ArrayList<ATM> ATMs2 = new ArrayList<>();

         

        ATMs1.add(ATM1);

        ATMs1.add(ATM2);

        ATMs1.add(ATM3);

        ATMs1.add(ATM4);

        ATMs2.add(ATM5);

        ATMs2.add(ATM6);

         

        bank1.setATMs(ATMs1);

        bank2.setATMs(ATMs2);

         

        User User1 = new User("杨过");

        User User2 = new User("郭靖");

        User User3 = new User("张无忌");

        User User4 = new User("韦小宝");

         

        Account Account1 = new Account("3217000010041315709");

        Account Account2 = new Account("3217000010041315715");

        Account Account3 = new Account("3217000010051320007");

        Account Account4 = new Account("3222081502001312389");

        Account Account5 = new Account("3222081502001312390");

        Account Account6 = new Account("3222081502001312399");

        Account Account7 = new Account("3222081502051320785");

        Account Account8 = new Account("3222081502051320786");

         

        Card card1 = new Card("6217000010041315709");

        Card card2 = new Card("6217000010041315715");

        Card card3 = new Card("6217000010041315718");

        Card card4 = new Card("6217000010051320007");

        Card card5 = new Card("6222081502001312389");

        Card card6 = new Card("6222081502001312390");

        Card card7 = new Card("6222081502001312399");

        Card card8 = new Card("6222081502001312400");

        Card card9 = new Card("6222081502051320785");

        Card card10 = new Card("6222081502051320786");

         

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

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

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

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

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

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

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

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

         

        Cards1.add(card1);

        Cards1.add(card2);

        Cards2.add(card3);

        Cards3.add(card4);

        Cards4.add(card5);

        Cards5.add(card6);

        Cards6.add(card7);

        Cards6.add(card8);

        Cards7.add(card9);

        Cards8.add(card10);

         

        Account1.setCards(Cards1);

        Account2.setCards(Cards2);

        Account3.setCards(Cards3);

        Account4.setCards(Cards4);

        Account5.setCards(Cards5);

        Account6.setCards(Cards6);

        Account7.setCards(Cards7);

        Account8.setCards(Cards8);

         

        ArrayList<Account> Accounts1 = new ArrayList<Account>();

        ArrayList<Account> Accounts2 = new ArrayList<Account>();

        ArrayList<Account> Accounts3 = new ArrayList<Account>();

        ArrayList<Account> Accounts4 = new ArrayList<Account>();

         

        Accounts1.add(Account1);

        Accounts1.add(Account2);

        Accounts2.add(Account3);

        Accounts3.add(Account4);

        Accounts3.add(Account5);

        Accounts3.add(Account6);

        Accounts4.add(Account7);

        Accounts4.add(Account8);

         

        User1.setAccounts(Accounts1);

        User2.setAccounts(Accounts2);

        User3.setAccounts(Accounts3);

        User4.setAccounts(Accounts4);

         

        ArrayList<User> Users1 = new ArrayList<User>();

        ArrayList<User> Users2 = new ArrayList<User>();

         

        Users1.add(User1);

        Users1.add(User2);

        Users2.add(User3);

        Users2.add(User4);

         

        bank1.setUser(Users1);

        bank2.setUser(Users2);

        // 基础数据的初始化----------------------------------------------------

        return unionPay;

        }

        }

         

        class UnionPay {

        private String UnionPayName;

        private ArrayList<Bank> Banks = new ArrayList<>();

         

        public UnionPay() {

        super();

        }

         

        public UnionPay(String unionPayName) {

        super();

        this.UnionPayName = unionPayName;

        }

         

        public boolean translate(String cardID) {

        Bank bank = this.wherehasCard(cardID);

        // 卡号不存在

        if (bank == null) {

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

        return false;

        }

        Account Account = bank.getAccount(cardID);

         

        System.out.printf("¥%.2f\n", Account.getBalance());

        return true;

        }

         

        public boolean translate(String cardID, String password, String ATMID, double money) {

        Bank bank = this.wherehasCard(cardID);

        // 卡号不存在

        if (bank == null) {

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

        return false;

        }

        // ATM编号不存在

        if ("".equals(this.whereisATM(ATMID))) {

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

        return false;

        }

        // ATM和卡不在同一家银行

        if (!bank.hasATM(ATMID)) {

        System.out.println("Sorry,cross-bank withdrawal is not supported.");

        return false;

        }

        User User = bank.getUser(cardID);

        Account Account = bank.getAccount(cardID);

        // 密码错误

        if (!Account.comfirmCardPassword(cardID, password)) {

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

        return false;

        }

        // 取款金额大于余额

        if (money > 0 && money > Account.getBalance()) {

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

        return false;

        }

        // 取款

        if (money > 0) {

        Account.withDraw(money);

        System.out.printf("%s在%s的%s号ATM机上取款¥%.2f\n", User.getUserName(), bank.getBankName(), ATMID, money);

        System.out.printf("当前余额为¥%.2f\n", Account.getBalance());

        } else {

        Account.deposit(-money);

        System.out.printf("%s在%s的%s号ATM机上存款¥%.2f\n", User.getUserName(), bank.getBankName(), ATMID, -money);

        System.out.printf("当前余额为¥%.2f\n", Account.getBalance());

        }

        return true;

        }

         

        private Bank wherehasCard(String cardID) {

        for (Bank Bank : Banks) {

        if (Bank.hasCard(cardID)) {

        return Bank;

        }

        }

        return null;

        }

         

        public String whereisATM(String ATMID) {

        for (Bank Bank : Banks) {

        if (Bank.hasATM(ATMID)) {

        return Bank.getBankName();

        }

        }

        return "";

        }

         

        public String getUnionPayName() {

        return UnionPayName;

        }

         

        public void setUnionPayName(String unionPayName) {

        UnionPayName = unionPayName;

        }

         

        public ArrayList<Bank> getBanks() {

        return Banks;

        }

         

        public void setBanks(ArrayList<Bank> banks) {

        Banks = banks;

        }

         

        }

         

        class Bank {

        private String BankName;

        private String BankID;

        private ArrayList<ATM> ATMs = new ArrayList<>();

        private ArrayList<User> Users = new ArrayList<>();

         

        public Bank() {

        super();

        }

         

        public Bank(String bankName) {

        super();

        BankName = bankName;

        }

         

        public boolean hasCard(String cardID) {

        for (User User : Users) {

        if (User.hasCard(cardID)) {

        return true;

        }

        }

        return false;

        }

         

        public boolean hasATM(String ATMID) {

        for (ATM ATM : ATMs) {

        if (ATMID.equals(ATM.getATMID())) {

        return true;

        }

        }

        return false;

        }

         

        public Account getAccount(String CardID) {

        for (User User : Users) {

        if (User.hasCard(CardID)) {

        return User.getCardAccount(CardID);

        }

        }

        return null;

        }

         

        public User getUser(String CardID) {

        for (User User : Users) {

        if (User.hasCard(CardID)) {

        return User;

        }

        }

        return null;

        }

         

        public String getBankName() {

        return BankName;

        }

         

        public void setBankName(String bankName) {

        BankName = bankName;

        }

         

        public ArrayList<ATM> getATMs() {

        return ATMs;

        }

         

        public void setATMs(ArrayList<ATM> aTMs) {

        ATMs = aTMs;

        }

         

        public ArrayList<User> getUser() {

        return Users;

        }

         

        public void setUser(ArrayList<User> user) {

        Users = user;

        }

         

        }

         

        class User {

        private String userName;

        private ArrayList<Account> Accounts;

         

        public User() {

        super();

        }

         

        public User(String userName) {

        super();

        this.userName = userName;

        }

         

        public User(String userName, ArrayList<Account> accounts) {

        super();

        this.userName = userName;

        Accounts = accounts;

        }

         

        public boolean hasCard(String CardID) {

        for (Account Account : Accounts) {

        if (Account.hasCard(CardID)) {

        return true;

        }

        }

        return false;

        }

         

        public Account getCardAccount(String CardID) {

        for (Account Account : Accounts) {

        if (Account.hasCard(CardID)) {

        return Account;

        }

        }

        return null;

        }

         

        public String getUserName() {

        return userName;

        }

         

        public void setUserName(String userName) {

        this.userName = userName;

        }

         

        public ArrayList<Account> getAccounts() {

        return Accounts;

        }

         

        public void setAccounts(ArrayList<Account> accounts) {

        Accounts = accounts;

        }

        }

         

        class Account {

         

        private String accountID;

        private double balance = 10000;

        private ArrayList<Card> Cards;

         

        Account() {

        balance = 0;

        }

         

        public Account(String accountID) {

        super();

        this.accountID = accountID;

        }

         

        Account(String accountID, double balance) {

        this.accountID = accountID;

        this.balance = balance;

        }

         

        public boolean hasCard(String CardID) {

        for (Card Card : Cards) {

        if (CardID.equals(Card.getCardID())) {

        return true;

        }

        }

        return false;

        }

         

        public boolean comfirmCardPassword(String CardID, String password) {

        for (Card Card : Cards) {

        if (CardID.equals(Card.getCardID()) && password.equals(Card.getPassword())) {

        return true;

        }

        }

        return false;

        }

         

        void withDraw(double money) {

        if (money < 0 || money > balance) {

        System.out.println("WithDraw Amount Wrong");

        } else {

        balance -= money;

        }

         

        }

         

        void deposit(double money) {

        if (money < 0 || money > 20000) {

        System.out.println("Deposit Amount Wrong");

        } else {

        balance += money;

        }

        }

         

        public String getAccountID() {

        return accountID;

        }

         

        public void setAccountID(String accountID) {

        this.accountID = accountID;

        }

         

        public ArrayList<Card> getCards() {

        return Cards;

        }

         

        public void setCards(ArrayList<Card> cards) {

        Cards = cards;

        }

         

        public double getBalance() {

        return balance;

        }

         

        public void setBalance(double balance) {

        this.balance = balance;

        }

        }

         

        class Card {

        private String cardID;

        private String password = "88888888";

         

        public Card() {

        super();

        }

         

        public Card(String cardID) {

        super();

        this.cardID = cardID;

        }

         

        public String getCardID() {

        return cardID;

        }

         

        public void setCardID(String cardID) {

        this.cardID = cardID;

        }

         

        public String getPassword() {

        return password;

        }

         

        public void setPassword(String password) {

        this.password = password;

        }

         

        }

         

        class ATM {

        String ATMID;

         

        public ATM() {

        super();

        }

         

        public ATM(String aTMID) {

        super();

        ATMID = aTMID;

        }

         

        public String getATMID() {

        return ATMID;

        }

         

        public void setATMID(String aTMID) {

        ATMID = aTMID;

        }

        }

         

         

        7-1 ATM机类结构设计(二) (100 分)
         

         

         

        设计ATM仿真系统,具体要求参见作业说明。 OO作业9-1题目说明.pdf

        输入格式:

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

        • 取款功能输入数据格式: 卡号 密码 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.*;

        public class Main {

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

        public static void main(String[] args)

        {

        StringBuilder a = new StringBuilder();

        String str = new String(input.nextLine());

        while(!str.equals("#"))

        {

        a.append(str).append("\n");

        str = input.nextLine();

        }

        operate s = new operate(a);

        s.caozuo();

        }

         

        }

        class Card{ //卡类

        String cardNumber;

        String password;

        Account account;

        Bank bank;

        public Card()

        {

        }

        public Card(String cardNumber,String password,Account account,Bank bank)

        {

        this.cardNumber=cardNumber;

        this.password=password;

        this.account=account;

        this.bank=bank;

        }

         

        public String getPassword() {

        return password;

        }

        public void setPassword(String password) {

        this.password = password;

        }

        public String getCardNumber() {

        return cardNumber;

        }

        public void setCardNumber(String cardNumber) {

        this.cardNumber = cardNumber;

        }

        public Account getAccount() {

        return account;

        }

        public void setAccount(Account account) {

        this.account = account;

        }

        public Bank getBank() {

        return bank;

        }

        public void setBank(Bank bank) {

        this.bank = bank;

        }

        }

        class Account{  //账户类

        String accountName;

        String accountNumber;

        double money;

        public Account() {

         

        }

        public Account(String accountName,String accountNumber,double money)

        {this.money=money;

                this.accountName=accountName;

                this.accountNumber=accountNumber;

        }

        public String getAccountName() {

        return accountName;

        }

        public void setAccountName(String accountName) {

        this.accountName = accountName;

        }

        public String getAccountNumber() {

        return accountNumber;

        }

        public void setAccountNumber(String accountNumber) {

        this.accountNumber = accountNumber;

        }

        public double getMoney() {

        return money;

        }

        public void setMoney(double money) {

        this.money = money;

        }

         

        }

        class Bank{                  //银行类

        String bankName;

        String[] atm;

            double fei;

        public Bank()

        {

         

        }

        public double getFei() {

        return fei;

        }

        public void setFei(double fei) {

        this.fei = fei;

        }

        public Bank(String bankName,String[] atm,double fei)

        {

        this.bankName=bankName; 

        this.atm=atm;

        this.fei = fei;

        }

        public String getBankName() {

        return bankName;

        }

        public void setBankName(String bankName) {

        this.bankName = bankName;

        }

        public String[] getAtm() {

        return atm;

        }

        public void setAtm(String[] atm) {

        this.atm = atm;

        }

        }

        class operate{              //操作类

         

        StringBuilder a;

        ArrayList<Account> accountList;

        String[] atm1 = {"01","02","03","04"};

        double fei1=0.02;

             Bank jsyh = new Bank("中国建设银行", atm1,fei1);

             String[] atm2 = {"05","06"};

             double fei2=0.03;

             Bank gsyh = new Bank("中国工商银行", atm2,fei2);

             String[] atm3 = {"07","08","09","10","11"};

             double fei3=0.04;

             Bank nyyh = new Bank("中国农业银行", atm3,fei3);

             

             

             

             Account yang1=new Account("杨过","3217000010041315709",10000);

             Account yang2=new Account("杨过","3217000010041315715",10000);

             Account guo1=new Account("郭靖","3217000010051320007",10000);

             Account zhang1=new Account("张无忌","3222081502001312389",10000);

             Account zhang2=new Account("张无忌","3222081502001312390",10000);

             Account zhang3=new Account("张无忌","3222081502001312399",10000);

             Account wei1=new Account("韦小宝","322208150205132785",10000);

             Account wei2=new Account("韦小宝","322208150205132786",10000);

             Account zsf1=new Account("张三丰","3640000010045442002",10000);

             Account ling1=new Account("令狐冲","3640000010045441009",10000);

             Account qiao1=new Account("乔峰","3630000010033431001",10000);

             Account hong1=new Account("洪七公","3630000010033431008",10000);

             

             Card card1=new Card("6217000010041315709","88888888",yang1,jsyh);

             Card card2=new Card("6217000010041315715","88888888",yang1,jsyh);

             Card card3=new Card("6217000010041315718","88888888",yang2,jsyh);

             Card card4=new Card("6217000010051320007","88888888",guo1,jsyh);

             Card card5=new Card("6222081502001312389","88888888",zhang1,gsyh);

             Card card6=new Card("6222081502001312390","88888888",zhang1,gsyh);

             Card card7=new Card("6222081502001312399","88888888",zhang2,gsyh);

             Card card8=new Card("6222081502001312400","88888888",zhang2,gsyh);

             Card card9=new Card("6222081502051320785","88888888",wei1,gsyh);

             Card card10=new Card("6222081502051320786","88888888",wei2,gsyh);

             Card card11=new Card("6640000010045442002","88888888",zsf1,jsyh);

             Card card12=new Card("6640000010045442003","88888888",zsf1,jsyh);

             Card card13=new Card("6640000010045441009","88888888",ling1,gsyh);

             Card card14=new Card("6630000010033431001","88888888",qiao1,nyyh);

             Card card15=new Card("6630000010033431008","88888888",hong1,nyyh);

             Card[] cardlist= {card1,card2,card3,card4,card5,card6,card7,card8,card9,card10,card11,card12,card13,card14,card15};

             

             public operate(StringBuilder a) {

            this.a = a;   

            } 

        public void caozuo()

        {

         

        String s = a.toString();

        String[] str2 = s.split("\n");

        for(int i = 0;i < str2.length;i++) 

        {

        int m=-1;

        String[] str1=str2[i].split(" +");

        if(str1.length==1)

        {

        for(int k=0;k<15;k++)

        if(cardlist[k].getCardNumber().equals(str1[0]))

        {

        m=k;

        }

        }

        System.out.print("业务:查询余额 ¥"+ String.format("%.2f",cardlist[m].getAccount().getMoney()));

        continue;

        }

            else {

        for(int k=0;k<15;k++)

        if(cardlist[k].getCardNumber().equals(str1[0]))

        {

        m=k;

        }

        }

        //检查卡号是否错误

        if(m==-1)

        {

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

        return;

        }

         

         

        //检查密码是否错误

        if(!cardlist[m].getPassword().equals(str1[1]))

        {

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

        return;

        }

        //检查atm 是否正7

        double l=0;

        String y;

         

        if(str1[2].equals("01")||str1[2].equals("02")||str1[2].equals("03")||str1[2].equals("04"))

        {

        l=0.02;

        y="中国建设银行";

        }

        else if(str1[2].equals("05")||str1[2].equals("06"))

        {

        l=0.03;

        y="中国工商银行";

        }

        else if(str1[2].equals("07")||str1[2].equals("08")||str1[2].equals("09")||str1[2].equals("10")||str1[2].equals("11"))

        {

        l=0.04;

        y="中国农业银行";

        }

        else {

         

         

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

        return;

        }

         

            //检查是否跨行

        int z=-1;

        for(int j=0;j<cardlist[m].getBank().getAtm().length;j++)

        {

         

        if(cardlist[m].getBank().getAtm()[j].equals(str1[2]))

        {

        z=1;                //z=1没跨行

        }

         

        }

        if(z==-1)         //跨行操作

        {

        if(cardlist[m].getAccount().getMoney()<Double.parseDouble(str1[3])&&m>9&&cardlist[m].getAccount().getMoney()>0) //跨行透支卡操作 且有余额

        {

        if(Double.parseDouble(str1[3])<0)  //跨行透支卡存款 且有余额 

        {

        cardlist[m].getAccount().setMoney(cardlist[m].getAccount().getMoney()-Double.parseDouble(str1[3])+Double.parseDouble(str1[3])*l);

         

        }

         

        else if(Double.parseDouble(str1[3])>0)  //跨行透支卡取款 且有余额 

        {

         

        cardlist[m].getAccount().setMoney(cardlist[m].getAccount().getMoney()-(Double.parseDouble(str1[3])+Double.parseDouble(str1[3])*l-(cardlist[m].getAccount().getMoney()-Double.parseDouble(str1[3]))*0.05));

        }

         

        }

        else if(cardlist[m].getAccount().getMoney()<=0&&m>9) //跨行透支卡操作 且无余额 

        {

        if(Double.parseDouble(str1[3])<0)   //跨行透支卡存款 且无余额 

        {

        cardlist[m].getAccount().setMoney(cardlist[m].getAccount().getMoney()-(Double.parseDouble(str1[3])-Double.parseDouble(str1[3])*l));

        }

         

        else if(Double.parseDouble(str1[3])>0) //跨行透支卡取款 且无余额 

        {

        cardlist[m].getAccount().setMoney(cardlist[m].getAccount().getMoney()-(Double.parseDouble(str1[3])+Double.parseDouble(str1[3])*l+Double.parseDouble(str1[3])*0.05));

        }

         

        }

        else

        {

         

        if(Double.parseDouble(str1[3])<0)  //跨行普通卡存款 

        {

        cardlist[m].getAccount().setMoney(cardlist[m].getAccount().getMoney()-(Double.parseDouble(str1[3])-Double.parseDouble(str1[3])*l));

        }

         

        else if(Double.parseDouble(str1[3])>0)  //跨行普通卡取款 

        {

         

        cardlist[m].getAccount().setMoney(cardlist[m].getAccount().getMoney()-(Double.parseDouble(str1[3])+Double.parseDouble(str1[3])*l));

        }

        }

         

        }

        else if(z==1) //没跨行

        {

        if(cardlist[m].getAccount().getMoney()<Double.parseDouble(str1[3])&&m>9&&cardlist[m].getAccount().getMoney()>0) //没跨行透支卡操作 且有余额  

        {

        if(Double.parseDouble(str1[3])<0)    //没跨行透支卡存款 且有余额

        {

        cardlist[m].getAccount().setMoney(cardlist[m].getAccount().getMoney()-Double.parseDouble(str1[3]));

        }

         

        else if(Double.parseDouble(str1[3])>0)   //没跨行透支卡取款   且有余额

        {

         

        cardlist[m].getAccount().setMoney(cardlist[m].getAccount().getMoney()-(Double.parseDouble(str1[3])-(cardlist[m].getAccount().getMoney()-Double.parseDouble(str1[3]))*0.05));

        }

         

        }

        else if(cardlist[m].getAccount().getMoney()<=0&&m>9)   //没跨行透支卡操作  且负余额

        {

        if(Double.parseDouble(str1[3])<0)    //没跨行透支卡存款 且负余额

        {

        cardlist[m].getAccount().setMoney(cardlist[m].getAccount().getMoney()-Double.parseDouble(str1[3]));

        }

         

        else if(Double.parseDouble(str1[3])>0)    //没跨行透支卡取款 且负余额

        {

         

        cardlist[m].getAccount().setMoney(cardlist[m].getAccount().getMoney()-(Double.parseDouble(str1[3])+Double.parseDouble(str1[3])*0.05));

        }

         

        }

        else 

        {

        if(Double.parseDouble(str1[3])<0)     //没跨行普通卡存款

        {

        cardlist[m].getAccount().setMoney(cardlist[m].getAccount().getMoney()-Double.parseDouble(str1[3]));

         

        }

         

        else if(Double.parseDouble(str1[3])>0)  //没跨行普通卡取款

        {

        cardlist[m].getAccount().setMoney(cardlist[m].getAccount().getMoney()-Double.parseDouble(str1[3]));

         

        }

           }

        }

         

        //检查是否余额不够

        if(m<10)

        {

        if(cardlist[m].getAccount().getMoney()<0)

        {

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

        return ;

        }

        }

        else if(m>=10)

        {

        if(cardlist[m].getAccount().getMoney()<-50000)

        {

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

        return ;

        }

         

        }

         

        if(z==-1)         //跨行操作

        {

        if(cardlist[m].getAccount().getMoney()<Double.parseDouble(str1[3])&&m>9&&cardlist[m].getAccount().getMoney()>0) //跨行透支卡操作 且有余额

        {

        if(Double.parseDouble(str1[3])<0)  //跨行透支卡存款 且有余额 

        {

        System.out.println("业务:存款 "+cardlist[m].getAccount().getAccountName()+"在"+y+"的"+str1[2]+"号ATM机上存款"+"¥"+String.format("%.2f",Math.abs(Double.parseDouble(str1[3]))));

        System.out.println("当前余额为¥" + String.format("%.2f", cardlist[m].getAccount().getMoney()));

        }

         

        else if(Double.parseDouble(str1[3])>0)  //跨行透支卡取款 且有余额 

        {

        System.out.println("业务:取款 "+cardlist[m].getAccount().getAccountName()+"在"+y+"的"+str1[2]+"号ATM机上取款"+"¥"+ String.format("%.2f",Math.abs(Double.parseDouble(str1[3]))));

        System.out.println("当前余额为¥" + String.format("%.2f", cardlist[m].getAccount().getMoney()));

        }

         

        }

        else if(cardlist[m].getAccount().getMoney()<0&&m>9) //跨行透支卡操作 且无余额 

        {

        if(Double.parseDouble(str1[3])<0)   //跨行透支卡存款 且无余额 

        {

         

        System.out.println("业务:存款 "+cardlist[m].getAccount().getAccountName()+"在"+y+"的"+str1[2]+"号ATM机上存款"+"¥"+String.format("%.2f",Math.abs(Double.parseDouble(str1[3]))));

        System.out.println("当前余额为¥" + String.format("%.2f", cardlist[m].getAccount().getMoney()));

        }

         

        else if(Double.parseDouble(str1[3])>0) //跨行透支卡取款 且无余额 

        {

         

         

        System.out.println("业务:取款 "+cardlist[m].getAccount().getAccountName()+"在"+y+"的"+str1[2]+"号ATM机上取款"+"¥"+ String.format("%.2f",Math.abs(Double.parseDouble(str1[3]))));

        System.out.println("当前余额为¥" + String.format("%.2f", cardlist[m].getAccount().getMoney()));

        }

         

        }

        else

        {

         

        if(Double.parseDouble(str1[3])<0)  //跨行普通卡存款 

        {

        System.out.println("业务:存款 "+cardlist[m].getAccount().getAccountName()+"在"+y+"的"+str1[2]+"号ATM机上存款"+"¥"+String.format("%.2f",Math.abs(Double.parseDouble(str1[3]))));

        System.out.println("当前余额为¥" + String.format("%.2f", cardlist[m].getAccount().getMoney()));

        }

         

        else if(Double.parseDouble(str1[3])>0)  //跨行普通卡取款 

        {

         

        System.out.println("业务:取款 "+cardlist[m].getAccount().getAccountName()+"在"+y+"的"+str1[2]+"号ATM机上取款"+"¥"+ String.format("%.2f",Math.abs(Double.parseDouble(str1[3]))));

        System.out.println("当前余额为¥" + String.format("%.2f", cardlist[m].getAccount().getMoney()));

        }

        }

         

        }

        else if(z==1) //没跨行

        {

        if(cardlist[m].getAccount().getMoney()<Double.parseDouble(str1[3])&&m>9&&cardlist[m].getAccount().getMoney()>0) //没跨行透支卡操作 且有余额  

        {

        if(Double.parseDouble(str1[3])<0)    //没跨行透支卡存款 且有余额

        {

         

        System.out.println("业务:存款 "+cardlist[m].getAccount().getAccountName()+"在"+y+"的"+str1[2]+"号ATM机上存款"+"¥"+String.format("%.2f",Math.abs(Double.parseDouble(str1[3]))));

        System.out.println("当前余额为¥" + String.format("%.2f", cardlist[m].getAccount().getMoney()));

        }

         

        else if(Double.parseDouble(str1[3])>0)   //没跨行透支卡取款   且有余额

        {

         

        System.out.println("业务:取款 "+cardlist[m].getAccount().getAccountName()+"在"+y+"的"+str1[2]+"号ATM机上取款"+"¥"+ String.format("%.2f",Math.abs(Double.parseDouble(str1[3]))));

        System.out.println("当前余额为¥" + String.format("%.2f", cardlist[m].getAccount().getMoney()));

        }

         

        }

        else if(cardlist[m].getAccount().getMoney()<0&&m>9)   //没跨行透支卡操作  且负余额

        {

        if(Double.parseDouble(str1[3])<0)    //没跨行透支卡存款 且负余额

        {

         

        System.out.println("业务:存款 "+cardlist[m].getAccount().getAccountName()+"在"+y+"的"+str1[2]+"号ATM机上存款"+"¥"+String.format("%.2f",Math.abs(Double.parseDouble(str1[3]))));

        System.out.println("当前余额为¥" + String.format("%.2f", cardlist[m].getAccount().getMoney()));

        }

         

        else if(Double.parseDouble(str1[3])>0)    //没跨行透支卡取款 且负余额

        {

         

        System.out.println("业务:取款 "+cardlist[m].getAccount().getAccountName()+"在"+y+"的"+str1[2]+"号ATM机上取款"+"¥"+ String.format("%.2f",Math.abs(Double.parseDouble(str1[3]))));

        System.out.println("当前余额为¥" + String.format("%.2f", cardlist[m].getAccount().getMoney()));

        }

         

        }

        else 

        {

        if(Double.parseDouble(str1[3])<0)     //没跨行普通卡存款

        {

        System.out.println("业务:存款 "+cardlist[m].getAccount().getAccountName()+"在"+cardlist[m].getBank().getBankName()+"的"+str1[2]+"号ATM机上存款"+"¥"+String.format("%.2f",Math.abs(Double.parseDouble(str1[3]))));

        System.out.println("当前余额为¥" + String.format("%.2f", cardlist[m].getAccount().getMoney()));

        }

         

        else if(Double.parseDouble(str1[3])>0)  //没跨行普通卡取款

        {

        System.out.println("业务:取款 "+cardlist[m].getAccount().getAccountName()+"在"+cardlist[m].getBank().getBankName()+"的"+str1[2]+"号ATM机上取款"+"¥"+ String.format("%.2f",Math.abs(Double.parseDouble(str1[3]))));

        System.out.println("当前余额为¥" + String.format("%.2f", cardlist[m].getAccount().getMoney()));

        }

           }

        }

          }

        }

        }

         

        public StringBuilder getA() {

        return a;

        }

        public void setA(StringBuilder a) {

        this.a = a;

        }

         

        }

        三.踩坑心得和改进建议

        在写atm题目时 当时很多错误,比如数据没存好,然后存好数据了,却发现方式没对 就对了一个输入错误的点,输入什么数据都是显示错误,经过多次修改才解决了问题

        原因是类里面写错了 导致输入进去的数据都被判断错误;

        在写atm题目二时 想着在atm1作业代码的基础上去增加一些功能 第一次写好的时候最对了一个点没考虑跨行利息 还有透支取款原来有钱透支后欠钱的各种情况,都没考虑清楚

        后面经过一点点修改 改到了八十分样例都是对的 经过好久才发现数据有一个没传进去 以后还是得仔细

         

        总结

           提升的主要方面:类的继承、多态性使用方法以及接口的应用、类的封装性、类间关系设计,完善了类设计的思想,越来越偏向于高复用性的代码设计,也会在其中不断优化自己的设计思路,使得其愈发简洁清晰.

         

         

         

         

         

         
         
         
          
         

         

 

 

import java.util.*; class DealCardList{ ArrayList<Card> cardList=new ArrayList<Card>(); ArrayList<Card> a1=new ArrayList<Card>(); ArrayList<Card> a2=new ArrayList<Card>(); ArrayList<Card> a3=new ArrayList<Card>(); ArrayList<Card> a4=new ArrayList<Card>();public DealCardList(){ }public DealCardList(ArrayList<Integer> list){int i;for(i=0;i<list.size();i++){switch(list.get(i)){case 1: cardList.add(new Card(new Circle(Main.input.nextDouble())));break;case 2: cardList.add(new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble())));break;case 3: cardList.add(new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())));break;case 4: cardList.add(new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())));break;}}}public boolean validate(){for(int i =0;i<cardList.size();i++ ){if(!cardList.get(i).getShape().validata()) {return false;}}        if(cardList.size()<=0)            return false;return true;}public void cardSort(){Collections.sort(cardList);Collections.sort(a1);Collections.sort(a2);Collections.sort(a3);Collections.sort(a4);}public void showResult(){for(int i=0;i<cardList.size();i++){if(cardList.get(i).getShape().getShapeName()=="Circle"){a1.add(cardList.get(i));}if(cardList.get(i).getShape().getShapeName()=="Rectangle"){a2.add(cardList.get(i));}if(cardList.get(i).getShape().getShapeName()=="Triangle"){a3.add(cardList.get(i));}if(cardList.get(i).getShape().getShapeName()=="Trapezoid"){a4.add(cardList.get(i));}}double a=0,b=0,c=0,d=0,e=0;System.out.print("The original list:\n[");for(int i =0;i < cardList.size();i++) {System.out.print(String.format(cardList.get(i).getShape().getShapeName()+":"+"%.2f",cardList.get(i).getShape().getArea())+" ");}System.out.print("]\n");System.out.print("The Separated List:\n");System.out.print("[");for(int i =0;i < a1.size();i++) {System.out.print(String.format(a1.get(i).getShape().getShapeName()+":"+"%.2f",a1.get(i).getShape().getArea())+" ");}System.out.print("]");System.out.print("[");for(int i =0;i < a2.size();i++) {System.out.print(String.format(a2.get(i).getShape().getShapeName()+":"+"%.2f",a2.get(i).getShape().getArea())+" ");}System.out.print("]");System.out.print("[");for(int i =0;i < a3.size();i++) {System.out.print(String.format(a3.get(i).getShape().getShapeName()+":"+"%.2f",a3.get(i).getShape().getArea())+" ");}System.out.print("]");System.out.print("[");for(int i =0;i < a4.size();i++) {System.out.print(String.format(a4.get(i).getShape().getShapeName()+":"+"%.2f",a4.get(i).getShape().getArea())+" ");}System.out.print("]");cardSort();System.out.print("\nThe Separated sorted List:\n");System.out.print("[");for(int i =0;i < a1.size();i++) {        a=a+a1.get(i).getShape().getArea();System.out.print(String.format(a1.get(i).getShape().getShapeName()+":"+"%.2f",a1.get(i).getShape().getArea())+" ");}System.out.print("]");System.out.print("[");for(int i =0;i < a2.size();i++) {b=b+a2.get(i).getShape().getArea();System.out.print(String.format(a2.get(i).getShape().getShapeName()+":"+"%.2f",a2.get(i).getShape().getArea())+" ");}System.out.print("]");System.out.print("[");for(int i =0;i < a3.size();i++) {c=c+a3.get(i).getShape().getArea();System.out.print(String.format(a3.get(i).getShape().getShapeName()+":"+"%.2f",a3.get(i).getShape().getArea())+" ");}System.out.print("]");System.out.print("[");for(int i =0;i < a4.size();i++) {d=d+a4.get(i).getShape().getArea();System.out.print(String.format(a4.get(i).getShape().getShapeName()+":"+"%.2f",a4.get(i).getShape().getArea())+" ");}System.out.print("]");double max1=(a>=b)?a:b;    double max2=(c>=d)?c:d;     double Max;

       if(max1>=max2){                  Max=max1;       }       else{                  Max=max2;       }System.out.println(String.format("\nThe max area:%.2f",Max));}}class Circle extends Shape{ double radius; public Circle(double radius) { this.radius=radius; super.shapeName="Circle"; }public double getRadius() {return radius;}public void setRadius(double radius) {this.radius = radius;}public double getArea(){return Math.PI*radius*radius;}public boolean validata(){if(radius<=0)return false;elsereturn true;} }class Rectangle extends Shape{double width;double length;public Rectangle(){}public Rectangle(double width,double length){this.width=width;this.length=length;super.shapeName="Rectangle";}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 length*width;}public boolean validata(){if(width<=0||length<=0)return false;elsereturn true;}}class Triangle extends Shape{double side1;double side2;double side3;public Triangle(){}public Triangle(double side1,double side2,double side3){this.side1=side1;this.side2=side2;this.side3=side3;super.shapeName="Triangle";}public double getArea(){double p=(side1+side2+side3)/2;return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));      } public boolean validata() { if(side1+side2<side3||side2+side3<side1||side1+side3<side2) return false; else return true; }}class Trapezoid extends Shape{double topSide;double height;double bottomSide;public Trapezoid(){}public Trapezoid(double topSide,double bottomSide,double height ){this.topSide=topSide;this.bottomSide=bottomSide;this.height=height;super.shapeName="Trapezoid";}public double getArea(){return height*(topSide+bottomSide)/2;}public boolean validata(){if(height<=0||topSide<=0||bottomSide<=0)return false;elsereturn true;}} abstract class Shape { 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 double getArea(){return 0;}public boolean validata(){return true;} } class Card implements Comparable<Card>{ Shape shape; public Card() {  } public Card(Shape shape) { this.shape=shape; } public Shape getShape() {return shape;}public void setShape(Shape shape) {this.shape = shape;}@Overridepublic int compareTo(Card card) {if(this.getShape().getArea() < card.getShape().getArea())return 1;else if(this.getShape().getArea() > card.getShape().getArea())return -1;return 0; }}
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();}}

posted @ 2021-12-18 23:17  素鸡已被使用  阅读(41)  评论(0)    收藏  举报