BLOG-3

一、前言
学习java已经一学期,这次题目的难度对我来说不大,题目偏向实际应用,就要求我们更加全面、细节的考虑,代码的行数也突破到了几百行,对我们的知识储备和编码能力来说都是极大的挑战,虽然每次作业都只有一道题或两道题目,但我几乎都需要花费几天的时间设计和写出代码,再利用剩下的时间不断调试,更改细节问题。大量的时间和精力没有白费,我得到了自己较满意的成果,接下来是我对于这几次题目的具体分析和完成情况以及遇到的问题和我是如何解决的。
题目集7-9像相比于前几次题目集难度如果就正则表达式啥的来比较的话确实减少了,但相比另外的题目难度个人觉得都差不多。题目集7的两道题其实是差不多的,但也有不同,7-1的相对简单一些,7-2的除了7-1的要求,每种图形也有几张,还要排序,之后又要总的排一下序。题目集8和题目集9就都差不多了......
题目集7-9的知识点主要涉及了ArrayList的应用、继承、抽象类、if...else等,其实也没有涉及很多知识点。至于题量相比前面几次要少一些,但如果没写出来就亏大了。

二、设计与分析

题目集7-1:

掌握类的继承、多态性使用方法以及接口的应用。详见作业指导书 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图形名称2:面积值2 …图形名称n:面积值n ,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格;
排序后的各图形类型及面积,格式同排序前的输出;
所有图形的面积总和,格式为Sum of area:总面积值。
类图为:

代码为:

import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
public static Scanner x = new Scanner(System.in);
public static void main(String[] args){
ArrayList list = new ArrayList();
int num = x.nextInt();
while(num!= 0){
if(num<0||num>4){
System.out.println("Wrong Format");
System.exit(1);
}
list.add(num);
num = x.nextInt();
}
DealCardList dealCardList = new DealCardList(list);
if(!dealCardList.validate()){
System.out.println("Wrong Format");
System.exit(1);
}
dealCardList.showResult();
x.close();
}
}
class DealCardList{
ArrayList cardList=new ArrayList<>();

public DealCardList() {
}

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

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

public Card() {
}

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

public Shape getShape() {
return shape;
}

public void setShape(Shape shape) {
this.shape = shape;
}
public int compareTo(Card card) {
return -(int)(shape.getArea()-card.getShape().getArea());
}
}
abstract class Shape{
private String shapeName;
public Shape() {
}

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

public String getShapeName() {
return shapeName;
}

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

public String toString() {
return getShapeName()+":"+String.format("%.2f ",getArea());
}
}

class Circle extends Shape{
private double radius;

public Circle() {
}

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

public double getRadius() {
return radius;
}

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

public double getArea() {
return Math.PIradiusradius;
}

public boolean validate() {
return this.radius>0;
}
}
class Triangle extends Shape{
private double side1,side2,side3;

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

public boolean validate() {
boolean ret=true;
if (!(side1>0&&side3>0&&side2>0))ret=false;
else{
if (!(side1+side2>side3&&side1+side3>side2&&side2+side3>side1))ret=false;
}
return ret;
}
}
class Rectangle extends Shape{
private double width,height;

public Rectangle() {
}

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

public double getWidth() {
return width;
}

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

public double getHeight() {
return height;
}

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

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

public boolean validate() {
return width>0&&height>0;
}
}

class Trapezoid extends Shape{
private double topSide,bottomSide,height;

public Trapezoid() {
}

public Trapezoid(double topSide, double bottomSide, double height) {
this.topSide = topSide;
this.bottomSide = bottomSide;
this.height = height;
}
public double getArea() {
return (topSide+bottomSide)*height/2;
}

public boolean validate() {
return topSide>0&&height>0&&bottomSide>0;
}
}

题目集7-2:

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

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图形名称2:面积值2 …图形名称n:面积值n ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前;
 输出分组后的图形类型及面积,格式为[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出;
  各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
  各组中面积之和的最大值输出,格式为The max area:面积值。

类图:

代码为:

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
public class Main {
public static Scanner input= new Scanner(System.in);
public static void main(String[] args){
ArrayList list = new ArrayList();
int num = input.nextInt();
if(num0) {
System.out.println("Wrong Format");
System.exit(1);}
while(num != 0){
if(num < 0 || num >4){
System.out.println("Wrong Format");
System.exit(1);}
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();}}
class DealCardList{
ArrayList cardList=new ArrayList<>();
double sum1=0.0;
double sum4=0.0;
double sum2=0.0;
double sum3=0.0;
double max=0.0;
ArrayList l1=new ArrayList<>();
ArrayList l2=new ArrayList<>();
ArrayList l4=new ArrayList<>();
ArrayList l3=new ArrayList<>();
public DealCardList(ArrayList list) {
for(int i = 0; i<list.size(); i++){
if(list.get(i)
1){
Shape d=new Circle(Main.input.nextDouble());
Card circle=new Card(d);
l1.add(d);
cardList.add(circle);
circle.getShape().setShapeName("Circle");}
else if(list.get(i)2){
Shape e=new Rectangle(Main.input.nextDouble(),Main.input.nextDouble());
Card rectangle=new Card(e);
cardList.add(rectangle);
l2.add(e);
rectangle.getShape().setShapeName("Rectangle");}
else if(list.get(i)
3){
Shape f=new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
Card triangle=new Card(f);
l3.add(f);
cardList.add(triangle);
triangle.getShape().setShapeName("Triangle");}
else if(list.get(i)==4){
Shape h=new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
Card trapezoid=new Card(h);
l4.add(h);
cardList.add(trapezoid);
trapezoid .getShape().setShapeName("Trapezoid");}}}
public boolean validate(){
boolean k=true;
for(int i=0;i<cardList.size();i++){
if(!cardList.get(i).getShape().validate())
k=true;break;}
return k;}
public void cardSort(){
Collections.sort(l1);
Collections.sort(l2);
Collections.sort(l4);
Collections.sort(l3);}
public double getAllArea(){
double sum=0;
for (Card card : cardList) {
sum+=card.getShape().getbArea();}
return sum;}
public void showResult(){
System.out.println("The original list:");
System.out.print("[");
for(int i=0;i<cardList.size();i++) {
System.out.print(cardList.get(i).getShape());
}
System.out.print("]");
System.out.println();
System.out.println("The Separated List:");
System.out.print("[");
for(int i=0;i<l1.size();i++) {
System.out.print(l1.get(i).toString());}
System.out.print("]");
System.out.print("[");
for(int i=0;i<l2.size();i++) {
System.out.print(l2.get(i).toString());}
System.out.print("]");
System.out.print("[");
for(int i=0;i<l3.size();i++) {
System.out.print(l3.get(i).toString());}
System.out.print("]");
System.out.print("[");
for(int i=0;i<l4.size();i++) {
System.out.print(l4.get(i).toString());}
System.out.println("]");
System.out.println("The Separated sorted List:");
cardSort();
System.out.print("[");
for(int i=0;i<l1.size();i++) {
System.out.print(l1.get(i).toString());}
System.out.print("]");
System.out.print("[");
for(int i=0;i<l2.size();i++) {
System.out.print(l2.get(i).toString());}
System.out.print("]");
System.out.print("[");
for(int i=0;i<l3.size();i++) {
System.out.print(l3.get(i).toString());}
System.out.print("]");
System.out.print("[");
for(int i=0;i<l4.size();i++) {
System.out.print(l4.get(i).toString());}
System.out.println("]");
for(int i=0;i<l1.size();i++) {
sum1=sum1+l1.get(i).getbArea();}
for(int i=0;i<l2.size();i++) {
sum2=sum2+l2.get(i).getbArea();}
for(int i=0;i<l3.size();i++) {
sum3=sum3+l3.get(i).getbArea();}
for(int i=0;i<l4.size();i++) {
sum4=sum4+l4.get(i).getbArea();}
max=sum1;
if(max<sum2){
max=sum2;}
if(max<sum3){
max=sum3;}
if(max<sum4){
max=sum4;}
System.out.printf("The max area:%.2f",max);}}
class Card implements Comparable{
private Shape shape;
public Card(Shape shape) {
this.shape = shape;}
public double getbArea() {
return 1;
}
public boolean validate() {
return true;
}
public int compareTo(Card t) {
return -(int)(shape.getbArea()-t.getShape().getbArea());}
public Shape getShape() {
return shape;
}
}
abstract class Shape implements Comparable{
private String shapeName;
public Shape(){}
public String getShapeName() {
return shapeName;}
public void setShapeName(String shapeName) {
this.shapeName = shapeName;
}
public double getbArea() {
return 1;
}
public boolean validate() {
return true;
}
public String toString() {
return getShapeName()+":"+String.format("%.2f ",getbArea());
}
public int compareTo(Shape t) {
if(t.getbArea()>getbArea()) {
return 1;
}
else if(t.getbArea()<getbArea()) {
return -1;
}
else
return 0;}}
class Circle extends Shape{
private double radius;
public Circle(double radius) {
this.radius=radius;
}
public double getRadius() {
return radius;}
public void setRadius(double radius) {
this.radius = radius;}
public double getbArea() {
return Math.PIradiusradius;}
public boolean validate() {
return radius>=0;}}
class Rectangle extends Shape{
private double width,height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;}
public double getbArea() {
return heightwidth;}
public boolean validate() {
return width>=0&&height>=0;}}
class Triangle extends Shape{
private double side1,side2,side3;
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;}
public double getbArea() {
double p=(side1+side2+side3)/2.0;
return Math.sqrt(p
(p-side1)(p-side2)(p-side3));}
public boolean validate() {
return (side1+side2>side3&&side2+side3>side1&&side1+side3>side2);}}
class Trapezoid extends Shape{
private double topSide,bottomSide,height;
public Trapezoid(double topSide, double bottomSide, double height) {
this.topSide = topSide;
this.bottomSide = bottomSide;
this.height = height;}
public double getbArea() {
return (topSide+bottomSide)*height/2;}
public boolean validate() {
return topSide>0&&height>0&&bottomSide>0;}}

   题目集7-1:首先它是有四张卡牌,我就先用ArrayList把数字标识出来,再创建及几个类(DealCardList、Card、Shape、Circle、Triangle、Rectangle、Trapezoid),在这些类中分别判断四种图形是否合法,设置不同的属性,最后在主类分别调用。
   题目集7-2其实是在题目集7-1的情况下改进的,

不过他也有他的不同之处。首先把输出改成题目所给的,再在每个图形设置ArrayList,然后分别把四个图形的几个排序,最后再放到一起判断排序......

(2)题目集8和题目集9两道ATM机仿真题目的设计思路分析总结:

题目集8:

设计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编号]上存款¥[金额]

当前余额为¥[金额]

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

④查询余额业务输出

¥[金额]

金额保留两位小数。

代码为:

import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
StringBuilder sb = new StringBuilder();
String b=input.nextLine();
double e1=10000.0;
double e2=10000.0;
double e3=10000.0;
double e4=10000.0;
double e5=10000.0;
double e6=10000.0;
while(!b.equals("#")) {
sb.append(b).append("\n");
b=input.nextLine();}
String ss= String.valueOf(sb);
String rows[]=ss.split("\n");
for(int i=0;i<rows.length;i++){
String d[]=rows[i].split(" ");
if(rows[i].length()>20){
if (d[0].equals("6217000010041315709")){
if (d[2].equals("88888888")){
if(d[3].equals("01")||d[3].equals("02")||d[3].equals("03")||d[3].equals("04")){
if(Double.parseDouble(d[4])>0.0) {
if(Double.parseDouble(d[4])>e1){
System.out.println("Sorry,your account balance "+"is insufficient.");
}
else{
e1=e1-Double.parseDouble(d[4]);
System.out.println("杨过在中国建设银行的"+d[3]+"号ATM机上取款¥"+Double.parseDouble(d[4])+"0");
System.out.println("当前余额为¥"+e1+"0");
}}
else{
e1=e1-Double.parseDouble(d[4]);
System.out.println("杨过在中国建设银行的"+d[3]+"号ATM机上存款¥"+-Double.parseDouble(d[4])+"0");
System.out.println("当前余额为¥"+e1+"0");
}}
else if(d[3].equals("05")||d[3].equals("06")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}}
else if(d[0].equals("6217000010041315715")) {
if (d[1].equals("88888888")){
if(d[2].equals("01")||d[2].equals("02")||d[2].equals("03")||d[2].equals("04")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e1){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e1=e1-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e1+"0");
}}
else{
e1=e1-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e1+"0");
}}
else if(d[2].equals("05")||d[2].equals("06")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}}
if(d[0].equals("6217000010041315718")){
if (d[1].equals("88888888")){
if(d[2].equals("01")||d[2].equals("02")||d[2].equals("03")||d[2].equals("04")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e2){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e2=e2-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e2+"0");
}}
else{
e2=e2-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e2+"0");
}}
else if(d[2].equals("05")||d[2].equals("06")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}
}
else if(d[0].equals("6217000010051320007")){
if (d[1].equals("88888888")){
if(d[2].equals("01")||d[2].equals("02")||d[2].equals("03")||d[2].equals("04")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e3){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e3=e3-Double.parseDouble(d[3]);
System.out.println("郭靖在中国建设银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e3+"0");
}}
else{
e3=e3-Double.parseDouble(d[3]);
System.out.println("郭靖在中国建设银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e3+"0");
}}
else if(d[2].equals("05")||d[2].equals("06")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}}
else if(d[0].equals("6222081502001312389")){
if (d[1].equals("88888888")){
if(d[2].equals("05")||d[2].equals("06")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e4){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e4=e4-Double.parseDouble(d[3]);
System.out.println("张无忌在中国工商银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e4+"0");
}}
else{
e4=e4-Double.parseDouble(d[3]);
System.out.println("张无忌在中国工商银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e4+"0");
}}
else if(d[2].equals("01")||d[2].equals("02")|d[2].equals("03")|d[2].equals("04")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}}
else if(d[0].equals("6222081502001312390")||d[0].equals("6222081502001312399")||d[0].equals("6222081502001312400")){
if (d[1].equals("88888888")){
if(d[2].equals("05")||d[2].equals("06")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e5){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e5=e5-Double.parseDouble(d[3]);
System.out.println("张无忌在中国工商银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e5+"0");
}}
else{
e5=e5-Double.parseDouble(d[3]);
System.out.println("张无忌在中国工商银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e5+"0");
}}
else if(d[2].equals("01")||d[2].equals("02")|d[2].equals("03")|d[2].equals("04")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}
}
else if(d[0].equals("6222081502051320785")||d[0].equals("6222081502051320786")){
if (d[1].equals("88888888")){
if(d[2].equals("05")||d[2].equals("06")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>10000.0){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e6=e6-Double.parseDouble(d[3]);
System.out.println("韦小宝在中国工商银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e6+"0");
}}
else{
e6=e6-Double.parseDouble(d[3]);
System.out.println("韦小宝在中国工商银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e6+"0");
}}
else if(d[2].equals("01")||d[2].equals("02")|d[2].equals("03")|d[2].equals("04")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}}
else{
System.out.println("Sorry,your password is wrong.");
}}
else if(!d[0].equals("6217000010041315709")&&(!d[0].equals("6217000010041315715"))&&(!d[0].equals("6217000010041315718"))&&(!d[0].equals("6217000010051320007"))&&(!d[0].equals("6222081502001312389"))&&(!d[0].equals("6222081502001312390"))&&(!d[0].equals("6222081502001312399"))&&(!d[0].equals("6222081502001312400"))&&(!d[0].equals("6222081502051320785"))&&(!d[0].equals("6222081502051320786"))){
System.out.println("Sorry,this card does not exist");
}}
else{
if(d[0].equals("6217000010041315709")){
System.out.println("¥"+e1+"0");
}
else if(d[0].equals("6217000010041315715")){
System.out.println("¥"+e1+"0");
}
else if(d[0].equals("6217000010041315718")){
System.out.println("¥"+e2+"0");
}
else if(d[0].equals("6217000010051320007")){
System.out.println("¥"+e3+"0");
}
else if(d[0].equals("6222081502001312389")){
System.out.println("¥"+e4+"0");
}
else if(d[0].equals("6222081502001312390")){
System.out.println("¥"+e5+"0");
}
else if(d[0].equals("6222081502001312399")){
System.out.println("¥"+e5+"0");
}
else if(d[0].equals("6222081502001312400")){
System.out.println("¥"+e5+"0");
}
else if(d[0].equals("6222081502051320785")){
System.out.println("¥"+e6+"0");
}
else if(d[0].equals("6222081502051320786")){
System.out.println("¥"+e6+"0");
}
else{ System.out.println("Sorry,this card does not exist.");
}
}}}}

题目集9:

设计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编号]上取款¥[金额]

当前余额为¥[金额]

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

③查询余额业务输出

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

金额保留两位小数。

代码为:

import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String tmp;
int i;
String name1="杨过";String name2="郭靖";String name3="张无忌";String name4="韦小宝";String name7="乔峰";String name8="洪七公";
double card1 = 10000.0;double card2 = 10000.0;double card3 = 10000.0;double card4 = 10000.0;double card5 = 10000.0;double card6 = 10000.0;double card7 = 10000.0;double card8 = 10000.0;double card9 = 10000.0;double card10 = 10000.0;double card11 = 10000.0;double card12 = 10000.0;
StringBuilder sb = new StringBuilder();
while (true) {tmp = input.nextLine();
if (tmp.equals("#"))
break;sb.append(tmp).append("\n"); }
String regex = sb.toString();
String a[] = regex.split("\n");
for (i = 0; i < a.length; i++) {
a[i] = a[i].replaceAll("[\s]+", " ");
String b[] = a[i].split(" ");
if (a[i].length() > 20) { switch (b[0]) {
case "6217000010041315709":
case "6217000010041315715": card1 = getCard1(card1, b, name1);
continue; case "6217000010041315718":
card2 = getCard1(card2, b, name1);
continue; case "6217000010051320007":
card3 = getCard1(card3, b, name2);
continue; case "6222081502001312389":
card4 = getCard4(card4, b, name3);
continue; case "6222081502001312390":
card5 = getCard4(card5, b, name3);
continue; case "6222081502001312399":
case "6222081502001312400": card6 = getCard4(card6, b, name3);
continue; case "6222081502051320785":
card7 = getCard4(card7, b, name4);
continue; case "6222081502051320786":
card8 = getCard4(card8, b, name4);
continue; case "6640000010045442002":
case "6640000010045442003": card9 = getCard9(card9, b);
continue; case "6640000010045441009":
card10 = getCard10(card10, b);
continue; case "6630000010033431001":
card11 = getCard11(card11, b, name7);
continue; case "6630000010033431008":
card12 = getCard11(card12, b, name8);
continue; default: System.out.println("Sorry,this card does not exist."); } } else {
switch (b[0]) { case "6217000010041315709":
case "6217000010041315715":
System.out.println("业务:查询余额 " + "¥" + card1 + "0");
continue; case "6217000010041315718":
System.out.println("业务:查询余额 " + "¥" + card2 + "0");
continue; case "6217000010051320007":
System.out.println("业务:查询余额 " + "¥" + card3 + "0");
continue; case "6222081502001312389":
System.out.println("业务:查询余额 " + "¥" + card4 + "0");
continue; case "6222081502001312390":
System.out.println("业务:查询余额 " + "¥" + card5 + "0");
continue; case "6222081502001312399":
case "6222081502001312400": System.out.println("业务:查询余额 " + "¥" + card6 + "0");
continue; case "6222081502051320785":
System.out.println("业务:查询余额 " + "¥" + card7 + "0");
continue; case "6222081502051320786":
System.out.println("业务:查询余额 " + "¥" + card8 + "0");
continue; case "6640000010045442002": case "6640000010045442003":
System.out.println("业务:查询余额 " + "¥" + card9 + "0");
continue; case "6640000010045441009":
System.out.println("业务:查询余额 " + "¥" + card10 + "0");
continue; case "6630000010033431001":
System.out.println("业务:查询余额 " + "¥" + card11 + "0");
continue; case "6630000010033431008":
System.out.println("业务:查询余额 " + "¥" + card12 + "0");
continue;
default: System.out.println("Sorry,this card does not exist.");} } } }
public static double getCard4(double card4, String[] b,String name) {
if(b[1].equals("88888888")){ double c=Double.parseDouble(b[3]);
switch (b[2]) { case "05":
case "06": if (c > card4) { System.out.println("Sorry,your account balance is insufficient."); } else { System.out.println("业务:取款 " + name + "在中国工商银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card4 = card4 - c;System.out.println("当前余额为¥" + card4 + "0"); }break;
case "01":
case "02":
case "03":
case "04": if (card4 - c - 0.02 * c >= 0) { System.out.println("业务:取款 " + name + "在中国建设银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card4 = card4 - c - 0.02 * c;
System.out.println("当前余额为¥" + card4 + "0"); } else { System.out.println("Sorry,your account balance is insufficient."); }break;
case "07":
case "08":
case "09":
case "10":
case "11": if (card4 - c - 0.04 * c >= 0) { System.out.println("业务:取款 " + name + "在中国农业银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card4 = card4 - c - 0.04 * c;
System.out.println("当前余额为¥" + card4 + "0");} else { System.out.println("Sorry,your account balance is insufficient."); }break;
default: System.out.println("Sorry,the ATM's id is wrong."); } }
else {System.out.println("Sorry,your password is wrong."); }return card4; }
private static double getCard1(double card1, String[] b,String name) {
if(b[1].equals("88888888")){
double c=Double.parseDouble(b[3]);
switch (b[2]) { case "01":
case "02":
case "03":
case "04": if (c > card1) { System.out.println("Sorry,your account balance is insufficient.");
}else { System.out.println("业务:取款 " + name + "在中国建设银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card1 = card1 - c;
System.out.println("当前余额为¥" + card1 + "0"); }break;
case "05":
case "06": if (card1 - c - 0.03 * c >= 0) { System.out.println("业务:取款 " + name + "在中国工商银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card1 = card1 - c - 0.03 * c;
System.out.println("当前余额为¥" + card1 + "0");
} else { System.out.println("Sorry,your account balance is insufficient.");}break;
case "07":
case "08":
case "09":
case "10":
case "11":
if (card1 - c - 0.04 * c >= 0) { System.out.println("业务:取款 " + name + "在中国农业银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card1 = card1 - c - 0.04 * c;
System.out.println("当前余额为¥" + card1 + "0");
} else { System.out.println("Sorry,your account balance is insufficient."); }break;
default: System.out.println("Sorry,the ATM's id is wrong."); } }
else {System.out.println("Sorry,your password is wrong."); }return card1; }
private static double getCard9(double card9, String[] b) {
if(b[1].equals("88888888")){ double c=Double.parseDouble(b[3]);
switch (b[2]) { case "01":
case "02":
case "03":
case "04": if (c >= card9) { if (card9 >= 0)
card9 = card9 - c - (c - card9) * 0.05;
else
card9 = card9 - c - c * 0.05;
if (card9 >= -50000) { System.out.println("业务:取款 张三丰在中国建设银行的" + b[2] + "号ATM机上取款¥" + c + "0");
System.out.println("当前余额为¥" + card9 + "0");
} else { System.out.println("Sorry,your account balance is insufficient."); }
} else { System.out.println("业务:取款 张三丰在中国建设银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card9 = card9 - c;
System.out.println("当前余额为¥" + card9 + "0"); }break;
case "05":
case "06": if (c >= card9) {
if (card9 >= 0)
card9 = card9 - c - c * 0.03 - (c - card9) * 0.05;
else
card9 = card9 - c - c * 0.05 - c * 0.03;
if (card9 >= -50000) { System.out.println("业务:取款 张三丰在中国工商银行的" + b[2] + "号ATM机上取款¥" + c + "0");
System.out.println("当前余额为¥" + card9 + "0");
} else { System.out.println("Sorry,your account balance is insufficient."); }
} else { System.out.println("业务:取款 张三丰在中国工商银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card9 = card9 - c - 0.03 * c;
System.out.println("当前余额为¥" + card9 + "0"); }break;
case "07":
case "08":
case "09":
case "10":
case "11": if (c >= card9) {
if (card9 >= 0)
card9 = card9 - c - c * 0.04 - (c - card9) * 0.05;
else
card9 = card9 - c - c * 0.05 - c * 0.04;
if (card9 >= -50000) { System.out.println("业务:取款 张三丰在中国农业银行的" + b[2] + "号ATM机上取款¥" + c + "0");
System.out.println("当前余额为¥" + card9 + "0");
} else { System.out.println("Sorry,your account balance is insufficient.");}
} else { System.out.println("业务:取款 张三丰在中国农业银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card9 = card9 - c - 0.04 * c;
System.out.println("当前余额为¥" + card9 + "0"); }break;
default: System.out.println("Sorry,the ATM's id is wrong."); } }
else {System.out.println("Sorry,your password is wrong."); }return card9; }
private static double getCard10(double card10, String[] b) {
if(b[1].equals("88888888")){
double c=Double.parseDouble(b[3]);
switch (b[2]) { case "05":
case "06": if (c >= card10) { if (card10>= 0)
card10 = card10 - c - (c - card10) * 0.05;
else
card10 = card10 - c - c * 0.05;
if (card10 >= -50000) { System.out.println("业务:取款 令狐冲在中国工商银行的" + b[2] + "号ATM机上取款¥" + c + "0");
System.out.println("当前余额为¥" + card10 + "0");
} else { System.out.println("Sorry,your account balance is insufficient."); }
} else { System.out.println("业务:取款 令狐冲在中国工商银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card10 = card10 - c;
System.out.println("当前余额为¥" + card10 + "0"); }break;
case "01":
case "02":
case "03":
case "04": if (c >= card10) { if (card10 >= 0)
card10 = card10 - c - c * 0.02 - (c - card10) * 0.05;
else
card10 = card10 - c - c * 0.05 - c * 0.02;
if (card10 >= -50000) { System.out.println("业务:取款 令狐冲在中国建设银行的" + b[2] + "号ATM机上取款¥" + c + "0");
System.out.println("当前余额为¥" + card10 + "0");
} else { System.out.println("Sorry,your account balance is insufficient."); } } else { System.out.println("业务:取款 令狐冲在中国建设银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card10=card10-c-0.02*c;System.out.println("当前余额为¥" + card10 + "0"); }break;
case "07":
case "08":
case "09":
case "10":
case "11": if (c>= card10) { if (card10 >= 0)
card10 = card10 - c - c * 0.04 - (c - card10) * 0.05;
else
card10 = card10 - c - c * 0.05 - c * 0.04;
if (card10 >= -50000) { System.out.println("业务:取款 令狐冲在中国农业银行的" + b[2] + "号ATM机上取款¥" + c + "0");
System.out.println("当前余额为¥" + card10 + "0"); } else { System.out.println("Sorry,your account balance is insufficient.");
} } else { System.out.println("业务:取款 令狐冲在中国农业银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card10 = card10 - c - 0.04 * c;
System.out.println("当前余额为¥" + card10 + "0"); }break;
default: System.out.println("Sorry,the ATM's id is wrong."); } }
else {System.out.println("Sorry,your password is wrong."); }return card10; }
private static double getCard11(double card10, String[] b,String name) {
if(b[1].equals("88888888")){ double c=Double.parseDouble(b[3]);
switch (b[2]) { case "07":
case "08":
case "09":
case "10":
case "11":if (c >= card10) { if (card10 >= 0)
card10 = card10 - c - (c - card10) * 0.05;
else
card10 = card10 - c - c * 0.05;
if (card10>=-50000) { System.out.println("业务:取款 "+name+"在中国农业银行的" + b[2] + "号ATM机上取款¥" + c + "0");
System.out.println("当前余额为¥" + card10 + "0"); }
else { System.out.println("Sorry,your account balance is insufficient."); } }
else { System.out.println("业务:取款 "+name+"在中国农业银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card10 = card10 - c;
System.out.println("当前余额为¥" + card10 + "0"); }break;
case "01":
case "02":
case "03":
case "04": if (c >= card10) { if (card10 >= 0)
card10 = card10 - c - c * 0.02 - (c - card10) * 0.05;
else
card10 = card10 - c - c * 0.05 - c * 0.02;
if (card10 >= -50000) { System.out.println("业务:取款 "+name+"在中国建设银行的" + b[2] + "号ATM机上取款¥" + c + "0");
System.out.println("当前余额为¥" + card10 + "0"); }
else { System.out.println("Sorry,your account balance is insufficient."); } }
else { System.out.println("业务:取款 "+name+"在中国建设银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card10 = card10 - c - 0.02 * c;
System.out.println("当前余额为¥" + card10 + "0"); }break;
case "05":
case "06": if (c >= card10) { if (card10 >= 0)
card10 = card10 - c - c * 0.03 - (c - card10) * 0.05;
else
card10 = card10 - c - c * 0.05 - c * 0.03;
if (card10 >= -50000) { System.out.println("业务:取款 " + name + "在中国工商银行的" + b[2] + "号ATM机上取款¥" + c + "0");
System.out.println("当前余额为¥" + card10 + "0"); } else {
System.out.println("Sorry,your account balance is insufficient."); } } else {
System.out.println("业务:取款 "+name+"在中国工商银行的" + b[2] + "号ATM机上取款¥" + c + "0");
card10 = card10 - c - 0.03 * c;System.out.println("当前余额为¥" + card10 + "0"); }break;
default: System.out.println("Sorry,the ATM's id is wrong."); } }
else {System.out.println("Sorry,your password is wrong."); }return card10; }}

   题目集8首先是要先了解几种组成成分(银行、银行用户、银行账户、银行卡、ATM、密码等),然后看四个人分别对应的隶属银行、隶属卡号、ATM编号,再看是取款还是存款,然后是输出格式、输入格式、各种错误的输出,最后是敲代码(首先用字符串分割,把每行的数据分别切割出来,再在每行用数组分别表示出来,然后用if...else的形式分别把每行的情况表示再输出,因为我刚开始设置的初始金额是以为小数,而输出有两位小数,所以在输出后面+“0”就行了。

   题目集9其实和题目集8差不多,但也有不同,题目集9把银行卡分为了两种(借记卡和信用卡),两种卡又是可以跨行的,跨行又要有相应的手续费,借记账号不能超过初始金额,而贷记帐号可以超过初始金额,但累计取款不能超过50000元。用户多了四个(张三丰、令狐冲、乔峰、洪七公),银行也多了一个(中国农业银行)。本来开始我用的还是题目集8一样的方法,在之前的代码改动一下,但奈何代码长度有限制,只能换种写法。这次用的是switch...case方法,代码长度会稍微少一些,切割还是用原来那个 ,最后还是在同学的帮助下缝缝补补写完了。

三、踩坑心得

   就拿题目集8来说吧,刚开始的时候输入有4个(卡号、密码、ATM编号、取款或存款金额),我是把这四个东西放到一起判断并输出,但到最后怎么也不行,后面是同学提醒我要一行一行判断、输出,这个时候发现小数点没有符合,所以就在后面加了一个0,然后又没注意到当卡号是6217000010041315709时,卡号和密码有两个空格,到后面才弄好的,其实我这也没有得到满分的

四、改进建议
还是说一下那个小数点的,其实代码都差不多,但还是不应该忽视,还是要认真审题
改进前:
import java.util.Scanner;
public class gt{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
StringBuilder sb = new StringBuilder();
String b=input.nextLine();
double e1=10000.0;
double e2=10000.0;
double e3=10000.0;
double e4=10000.0;
double e5=10000.0;
double e6=10000.0;
while(!b.equals("#")) {
sb.append(b).append("\n");
b=input.nextLine();}
String ss= String.valueOf(sb);
String rows[]=ss.split("\n");
for(int i=0;i<rows.length;i++){
String d[]=rows[i].split(" ");
if(rows[i].length()>20){
if (d[0].equals("6217000010041315709")){
if (d[1].equals("88888888")){
if(d[2].equals("01")||d[2].equals("02")||d[2].equals("03")||d[2].equals("04")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e1){
System.out.println("Sorry,your account balance "+"is insufficient.");
}
else{
e1=e1-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e1+"0");
}}
else{
e1=e1-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e1+"0");
}}
else if(d[3].equals("05")||d[3].equals("06")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}}
else if(d[0].equals("6217000010041315715")) {
if (d[1].equals("88888888")){
if(d[2].equals("01")||d[2].equals("02")||d[2].equals("03")||d[2].equals("04")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e1){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e1=e1-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e1+"0");
}}
else{
e1=e1-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e1+"0");
}}
else if(d[2].equals("05")||d[2].equals("06")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}}
if(d[0].equals("6217000010041315718")){
if (d[1].equals("88888888")){
if(d[2].equals("01")||d[2].equals("02")||d[2].equals("03")||d[2].equals("04")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e2){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e2=e2-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e2+"0");
}}
else{
e2=e2-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e2+"0");
}}
else if(d[2].equals("05")||d[2].equals("06")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}
}
else if(d[0].equals("6217000010051320007")){
if (d[1].equals("88888888")){
if(d[2].equals("01")||d[2].equals("02")||d[2].equals("03")||d[2].equals("04")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e3){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e3=e3-Double.parseDouble(d[3]);
System.out.println("郭靖在中国建设银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e3+"0");
}}
else{
e3=e3-Double.parseDouble(d[3]);
System.out.println("郭靖在中国建设银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e3+"0");
}}
else if(d[2].equals("05")||d[2].equals("06")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}}
else if(d[0].equals("6222081502001312389")){
if (d[1].equals("88888888")){
if(d[2].equals("05")||d[2].equals("06")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e4){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e4=e4-Double.parseDouble(d[3]);
System.out.println("张无忌在中国工商银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e4+"0");
}}
else{
e4=e4-Double.parseDouble(d[3]);
System.out.println("张无忌在中国工商银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e4+"0");
}}
else if(d[2].equals("01")||d[2].equals("02")|d[2].equals("03")|d[2].equals("04")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}}
else if(d[0].equals("6222081502001312390")||d[0].equals("6222081502001312399")||d[0].equals("6222081502001312400")){
if (d[1].equals("88888888")){
if(d[2].equals("05")||d[2].equals("06")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e5){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e5=e5-Double.parseDouble(d[3]);
System.out.println("张无忌在中国工商银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e5+"0");
}}
else{
e5=e5-Double.parseDouble(d[3]);
System.out.println("张无忌在中国工商银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e5+"0");
}}
else if(d[2].equals("01")||d[2].equals("02")|d[2].equals("03")|d[2].equals("04")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}
}
else if(d[0].equals("6222081502051320785")||d[0].equals("6222081502051320786")){
if (d[1].equals("88888888")){
if(d[2].equals("05")||d[2].equals("06")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>10000.0){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e6=e6-Double.parseDouble(d[3]);
System.out.println("韦小宝在中国工商银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e6+"0");
}}
else{
e6=e6-Double.parseDouble(d[3]);
System.out.println("韦小宝在中国工商银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e6+"0");
}}
else if(d[2].equals("01")||d[2].equals("02")|d[2].equals("03")|d[2].equals("04")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}}
else{
System.out.println("Sorry,your password is wrong.");
}}
else if(!d[0].equals("6217000010041315709")&&(!d[0].equals("6217000010041315715"))&&(!d[0].equals("6217000010041315718"))&&(!d[0].equals("6217000010051320007"))&&(!d[0].equals("6222081502001312389"))&&(!d[0].equals("6222081502001312390"))&&(!d[0].equals("6222081502001312399"))&&(!d[0].equals("6222081502001312400"))&&(!d[0].equals("6222081502051320785"))&&(!d[0].equals("6222081502051320786"))){
System.out.println("Sorry,this card does not exist");
}}
else{
if(d[0].equals("6217000010041315709")){
System.out.println("¥"+e1+"0");
}
else if(d[0].equals("6217000010041315715")){
System.out.println("¥"+e1+"0");
}
else if(d[0].equals("6217000010041315718")){
System.out.println("¥"+e2+"0");
}
else if(d[0].equals("6217000010051320007")){
System.out.println("¥"+e3+"0");
}
else if(d[0].equals("6222081502001312389")){
System.out.println("¥"+e4+"0");
}
else if(d[0].equals("6222081502001312390")){
System.out.println("¥"+e5+"0");
}
else if(d[0].equals("6222081502001312399")){
System.out.println("¥"+e5+"0");
}
else if(d[0].equals("6222081502001312400")){
System.out.println("¥"+e5+"0");
}
else if(d[0].equals("6222081502051320785")){
System.out.println("¥"+e6+"0");
}
else if(d[0].equals("6222081502051320786")){
System.out.println("¥"+e6+"0");
}
else{ System.out.println("Sorry,this card does not exist.");
}
}}}}
改进后:
import java.util.Scanner;
public class gt{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
StringBuilder sb = new StringBuilder();
String b=input.nextLine();
double e1=10000.0;
double e2=10000.0;
double e3=10000.0;
double e4=10000.0;
double e5=10000.0;
double e6=10000.0;
while(!b.equals("#")) {
sb.append(b).append("\n");
b=input.nextLine();}
String ss= String.valueOf(sb);
String rows[]=ss.split("\n");
for(int i=0;i<rows.length;i++){
String d[]=rows[i].split(" ");
if(rows[i].length()>20){
if (d[0].equals("6217000010041315709")){
if (d[2].equals("88888888")){
if(d[3].equals("01")||d[3].equals("02")||d[3].equals("03")||d[3].equals("04")){
if(Double.parseDouble(d[4])>0.0) {
if(Double.parseDouble(d[4])>e1){
System.out.println("Sorry,your account balance "+"is insufficient.");
}
else{
e1=e1-Double.parseDouble(d[4]);
System.out.println("杨过在中国建设银行的"+d[3]+"号ATM机上取款¥"+Double.parseDouble(d[4])+"0");
System.out.println("当前余额为¥"+e1+"0");
}}
else{
e1=e1-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[3]+"号ATM机上存款¥"+-Double.parseDouble(d[4])+"0");
System.out.println("当前余额为¥"+e1+"0");
}}
else if(d[3].equals("05")||d[3].equals("06")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}}
else if(d[0].equals("6217000010041315715")) {
if (d[1].equals("88888888")){
if(d[2].equals("01")||d[2].equals("02")||d[2].equals("03")||d[2].equals("04")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e1){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e1=e1-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e1+"0");
}}
else{
e1=e1-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e1+"0");
}}
else if(d[2].equals("05")||d[2].equals("06")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}}
if(d[0].equals("6217000010041315718")){
if (d[1].equals("88888888")){
if(d[2].equals("01")||d[2].equals("02")||d[2].equals("03")||d[2].equals("04")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e2){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e2=e2-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e2+"0");
}}
else{
e2=e2-Double.parseDouble(d[3]);
System.out.println("杨过在中国建设银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e2+"0");
}}
else if(d[2].equals("05")||d[2].equals("06")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}
}
else if(d[0].equals("6217000010051320007")){
if (d[1].equals("88888888")){
if(d[2].equals("01")||d[2].equals("02")||d[2].equals("03")||d[2].equals("04")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e3){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e3=e3-Double.parseDouble(d[3]);
System.out.println("郭靖在中国建设银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e3+"0");
}}
else{
e3=e3-Double.parseDouble(d[3]);
System.out.println("郭靖在中国建设银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e3+"0");
}}
else if(d[2].equals("05")||d[2].equals("06")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}}
else if(d[0].equals("6222081502001312389")){
if (d[1].equals("88888888")){
if(d[2].equals("05")||d[2].equals("06")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e4){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e4=e4-Double.parseDouble(d[3]);
System.out.println("张无忌在中国工商银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e4+"0");
}}
else{
e4=e4-Double.parseDouble(d[3]);
System.out.println("张无忌在中国工商银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e4+"0");
}}
else if(d[2].equals("01")||d[2].equals("02")|d[2].equals("03")|d[2].equals("04")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}}
else if(d[0].equals("6222081502001312390")||d[0].equals("6222081502001312399")||d[0].equals("6222081502001312400")){
if (d[1].equals("88888888")){
if(d[2].equals("05")||d[2].equals("06")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>e5){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e5=e5-Double.parseDouble(d[3]);
System.out.println("张无忌在中国工商银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e5+"0");
}}
else{
e5=e5-Double.parseDouble(d[3]);
System.out.println("张无忌在中国工商银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e5+"0");
}}
else if(d[2].equals("01")||d[2].equals("02")|d[2].equals("03")|d[2].equals("04")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}
}
else{
System.out.println("Sorry,your password is wrong.");
}
}
else if(d[0].equals("6222081502051320785")||d[0].equals("6222081502051320786")){
if (d[1].equals("88888888")){
if(d[2].equals("05")||d[2].equals("06")){
if(Double.parseDouble(d[3])>0.0) {
if(Double.parseDouble(d[3])>10000.0){
System.out.println("Sorry,your account balance "+"is insufficient.");}
else{
e6=e6-Double.parseDouble(d[3]);
System.out.println("韦小宝在中国工商银行的"+d[2]+"号ATM机上取款¥"+Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e6+"0");
}}
else{
e6=e6-Double.parseDouble(d[3]);
System.out.println("韦小宝在中国工商银行的"+d[2]+"号ATM机上存款¥"+-Double.parseDouble(d[3])+"0");
System.out.println("当前余额为¥"+e6+"0");
}}
else if(d[2].equals("01")||d[2].equals("02")|d[2].equals("03")|d[2].equals("04")){
System.out.println("Sorry,cross-bank withdrawal is not supported.");
}
else{
System.out.println("Sorry,the ATM's id is wrong.");
}}
else{
System.out.println("Sorry,your password is wrong.");
}}
else if(!d[0].equals("6217000010041315709")&&(!d[0].equals("6217000010041315715"))&&(!d[0].equals("6217000010041315718"))&&(!d[0].equals("6217000010051320007"))&&(!d[0].equals("6222081502001312389"))&&(!d[0].equals("6222081502001312390"))&&(!d[0].equals("6222081502001312399"))&&(!d[0].equals("6222081502001312400"))&&(!d[0].equals("6222081502051320785"))&&(!d[0].equals("6222081502051320786"))){
System.out.println("Sorry,this card does not exist");
}}
else{
if(d[0].equals("6217000010041315709")){
System.out.println("¥"+e1+"0");
}
else if(d[0].equals("6217000010041315715")){
System.out.println("¥"+e1+"0");
}
else if(d[0].equals("6217000010041315718")){
System.out.println("¥"+e2+"0");
}
else if(d[0].equals("6217000010051320007")){
System.out.println("¥"+e3+"0");
}
else if(d[0].equals("6222081502001312389")){
System.out.println("¥"+e4+"0");
}
else if(d[0].equals("6222081502001312390")){
System.out.println("¥"+e5+"0");
}
else if(d[0].equals("6222081502001312399")){
System.out.println("¥"+e5+"0");
}
else if(d[0].equals("6222081502001312400")){
System.out.println("¥"+e5+"0");
}
else if(d[0].equals("6222081502051320785")){
System.out.println("¥"+e6+"0");
}
else if(d[0].equals("6222081502051320786")){
System.out.println("¥"+e6+"0");
}
else{ System.out.println("Sorry,this card does not exist.");
}
}}}}
五、总结
希望可以更加调动我们的学习气氛。

posted @ 2021-12-18 21:03  触碰雨  阅读(34)  评论(0编辑  收藏  举报