第三次博客作业
一、前言
虽然这三次PTA的题目量较少,但是每道题需要花非常多的时间去思考,每次作业都要花许多的心思与时间去完成。对于我这种基础不好的都是一点点摸索出来的,因为不太确定自己写的程序能不能正常运行,所以都是边问边参考同学的代码一点点写的,这样就会导致整体的结构很乱。总的来说还得提升下自己的水平,做到得心应手才行。
1、第7次PTA作业涉及知识点及难度分析
2、第8、9次PTA作业涉及知识点及难度分析
二、设计与分析
题目集7-1
题目要求如下:
本题类图如下:
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.TreeSet; public class Main { public static Scanner input = new Scanner(System.in); public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<Integer>(); int num = input.nextInt(); while(num != 0){ if(num < 0 || num > 4){ System.out.println("Wrong Format"); System.exit(0); } list.add(num); num = input.nextInt(); } DealCardList dealCardList = new DealCardList(list); if(!dealCardList.validate()){ System.out.println("Wrong Format"); System.exit(0); } dealCardList.showResult(); input.close(); } } 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 { 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.PI*radius*radius; } public boolean validate() { boolean validate; validate=(radius>0); return validate; } } class Rectangle extends Shape { private double width; private double length; public Rectangle() { } public Rectangle(double width,double length) { this.width=width; this.length=length; } public double getWidth() { return width; } public void setWidth(double width) { this.width=width; } public double getLength() { return length; } public void setLength(double length) { this.length=length; } public double getArea() { return width*length; } public boolean validate() { boolean validate; validate=(width>0&&length>0); return validate; } } class Triangle extends Shape { private double side1; private double side2; private double side3; public Triangle() { } public Triangle(double side1,double side2,double side3) { this.side1=side1; this.side2=side2; this.side3=side3; } public double getArea() { return 0.25*Math.sqrt((side1+side2+side3)*(side1+side2-side3)*(side1+side3-side2)*(side2+side3-side1)); } public boolean validate() { boolean validate; double []a=new double[3]; a[0]=side1; a[1]=side2; a[2]=side3; Arrays.sort(a); validate=(side1>0&&side2>0&&side3>0&&(side1+side2>side3)); return validate; } } class Trapezoid extends Shape { private double topSide; private double bottomSide; private double 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() { boolean validate; validate=(topSide>0&&bottomSide>0&&height>0); return validate; } } class Card implements Comparable<Card> { private Shape shape; public Card() { } public Card(Shape shape) { this.shape=shape; } public Shape getShape() { return shape; } public void setShape(Shape shape) { this.shape=shape; } public int compareTo(Card card) { if(shape.getArea()>card.getShape().getArea()) { return -1; } else if(shape.getArea()<card.getShape().getArea()){ return 1; } return 0; } } class DealCardList { ArrayList<Card> list = new ArrayList<>(); public DealCardList() { } public DealCardList(ArrayList<Integer>cardlist) { for (Integer a : cardlist) { switch (a) { case 1: Card card1=new Card(new Circle(Main.input.nextDouble())); card1.getShape().setShapeName("Circle"); list.add(card1); break; case 2: Card card2=new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble())); card2.getShape().setShapeName("Rectangle"); list.add(card2); break; case 3: Card card3=new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); card3.getShape().setShapeName("Triangle"); list.add(card3); break; case 4: Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); card4.getShape().setShapeName("Trapezoid"); list.add(card4); break; } } } public boolean validate() { boolean validate = true; for (Card card : list) { if (!card.getShape().validate()){ validate=false; break; } } return validate; } public void cardSort() { TreeSet<Card> cards = new TreeSet<>(list); for (Card card : cards) { System.out.print(card.getShape()); } } public double getAllArea() { double sum=0; for(Card card:list) { sum=sum+card.getShape().getArea(); } return sum; } public void showResult() { System.out.println("The original list:"); for(Card card:list) { System.out.print(card.getShape()); } System.out.println(); System.out.println("The sorted list:"); cardSort(); System.out.println(); System.out.printf(String.format("Sum of area:%.2f",getAllArea())); } }
解题思路:
1.首先题目要求应用类的继承、多态使用方法和接口
2.结合之前的题目,不难得出应该对圆形、矩形、三角形和梯形设计一个父类,即shape类
3.设计的父类需要包括两个父类,接着就是对接口CompareTo的重写,在本次题目中,需要比较的是各个类返回的面积的大小,所以在处理卡片顺序上需要重写CompareTo这个方法,对传进来的类的面积值进行大小比较并返回相应的参数
易错点:
1.判断图形输入的数据是否非法的时候要想一想三角形任意两边之和要大于第三边,而不是三边都大于0
2.compareTo方法的重写
3.输出的时候用到了增强for循环
题目集7-2
本题要求如下:
本题类图如下:
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.TreeSet; public class Main { public static Scanner input = new Scanner(System.in); public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<Integer>(); int num = input.nextInt(); if(num==0) { System.out.println("Wrong Format"); System.exit(0); } while(num!=0) { if(num <= 0 || num > 4){ System.out.println("Wrong Format"); System.exit(0); } list.add(num); num = input.nextInt(); } DealCardList dealCardList = new DealCardList(list); if(!dealCardList.validate()){ System.out.println("Wrong Format"); System.exit(0); } dealCardList.showResult(); input.close(); } } abstract class Shape { 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 { 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.PI*radius*radius; } public boolean validate() { boolean validate; validate=(radius>0); return validate; } } class Rectangle extends Shape { private double width; private double length; public Rectangle() { } public Rectangle(double width,double length) { this.width=width; this.length=length; } public double getWidth() { return width; } public void setWidth(double width) { this.width=width; } public double getLength() { return length; } public void setLength(double length) { this.length=length; } public double getArea() { return width*length; } public boolean validate() { boolean validate; validate=(width>0&&length>0); return validate; } } class Triangle extends Shape { private double side1; private double side2; private double side3; public Triangle() { } public Triangle(double side1,double side2,double side3) { this.side1=side1; this.side2=side2; this.side3=side3; } public double getArea() { return 0.25*Math.sqrt((side1+side2+side3)*(side1+side2-side3)*(side1+side3-side2)*(side2+side3-side1)); } public boolean validate() { boolean validate; double []a=new double[3]; a[0]=side1; a[1]=side2; a[2]=side3; Arrays.sort(a); validate=(side1>0&&side2>0&&side3>0&&(side1+side2>side3)); return validate; } } class Trapezoid extends Shape { private double topSide; private double bottomSide; private double 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() { boolean validate; validate=(topSide>0&&bottomSide>0&&height>0); return validate; } } class Card implements Comparable<Card> { private Shape shape; public Card() { } public Card(Shape shape) { this.shape=shape; } public Shape getShape() { return shape; } public void setShape(Shape shape) { this.shape=shape; } public int compareTo(Card card) { if(shape.getArea()>card.getShape().getArea()) { return -1; } else if(shape.getArea()<card.getShape().getArea()){ return 1; } return 0; } } class DealCardList { ArrayList<Card> list = new ArrayList<>(); double []amount = new double [4]; public DealCardList() { } public DealCardList(ArrayList<Integer>cardlist) { for (Integer a : cardlist) { switch (a) { case 1: Card card1=new Card(new Circle(Main.input.nextDouble())); card1.getShape().setShapeName("Circle"); list.add(card1); break; case 2: Card card2=new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble())); card2.getShape().setShapeName("Rectangle"); list.add(card2); break; case 3: Card card3=new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); card3.getShape().setShapeName("Triangle"); list.add(card3); break; case 4: Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); card4.getShape().setShapeName("Trapezoid"); list.add(card4); break; } } } public boolean validate() { boolean validate = true; for (Card card : list) { if (!card.getShape().validate()){ validate=false; break; } } return validate; } public void circle() { amount[0]=0; System.out.print("["); for(Card card : list) { if(card.getShape().getShapeName().equals("Circle")) { System.out.print(card.getShape()); amount[0]=amount[0]+card.getShape().getArea(); } } System.out.print("]"); } public void rectangle() { amount[1]=0; System.out.print("["); for(Card card : list) { if(card.getShape().getShapeName().equals("Rectangle")) { System.out.print(card.getShape()); amount[1]=amount[1]+card.getShape().getArea(); } } System.out.print("]"); } public void triangle() { amount[2]=0; System.out.print("["); for(Card card : list) { if(card.getShape().getShapeName().equals("Triangle")) { System.out.print(card.getShape()); amount[2]=amount[2]+card.getShape().getArea(); } } System.out.print("]"); } public void trapezoid() { amount[3]=0; System.out.print("["); for(Card card : list) { if(card.getShape().getShapeName().equals("Trapezoid")) { System.out.print(card.getShape()); amount[3]=amount[3]+card.getShape().getArea(); } } System.out.print("]"); } public void circlesort() { TreeSet<Card> cards = new TreeSet<>(list); System.out.print("["); for(Card card : cards) { if(card.getShape().getShapeName().equals("Circle")) { System.out.print(card.getShape()); } } System.out.print("]"); } public void rectanglesort() { TreeSet<Card> cards = new TreeSet<>(list); System.out.print("["); for(Card card : cards) { if(card.getShape().getShapeName().equals("Rectangle")) { System.out.print(card.getShape()); } } System.out.print("]"); } public void trianglesort() { TreeSet<Card> cards = new TreeSet<>(list); System.out.print("["); for(Card card : cards) { if(card.getShape().getShapeName().equals("Triangle")) { System.out.print(card.getShape()); } } System.out.print("]"); } public void trapezoidsort() { TreeSet<Card> cards = new TreeSet<>(list); System.out.print("["); for(Card card : cards) { if(card.getShape().getShapeName().equals("Trapezoid")) { System.out.print(card.getShape()); } } System.out.print("]"); } public double getMax() { double max=0; for(int i=0;i<4;i++) { if(max<amount[i]) { max=amount[i]; } } return max; } public void showResult() { System.out.println("The original list:"); System.out.print("["); for(Card card:list) { System.out.print(card.getShape()); } System.out.println("]"); System.out.println("The Separated List:"); circle(); rectangle(); triangle(); trapezoid(); System.out.println(); System.out.println("The Separated sorted List:"); circlesort(); rectanglesort(); trianglesort(); trapezoidsort(); System.out.println(); System.out.printf(String.format("The max area:%.2f",getMax())); } }
解题思路:
1.本题在7-1的基础上将相同的图形分为一组并输出
2.为每一个图形类的输入定义一个ArrlyList用于保存,在输出时再去取其中的数据并作相应的修改即可
易错点:
1.相同的图形要放在同一个数组里
2.输出图形的面积为最大的那个
题目集8-1
题目要求如下:
本题类图:
import java.util.Scanner; import java.util.ArrayList; //Bank类 class Bank { String bankname; ArrayList<String> ATMList; //创建带参构造方法 public Bank(String bankname,ArrayList<String> ATMList) { this.bankname=bankname; this.ATMList=ATMList; } } //Account类 class Account { private final String name; private final String password; private double balance; ArrayList<Bank> banklist; Bank bank; private final ArrayList<String> cardList; //创建带参构造方法 public Account(String name, String password, double balance, ArrayList<Bank> banklist, Bank bank, ArrayList<String> cardList){ this.name=name; this.password=password; this.balance=balance; this.bank=bank; this.banklist=banklist; this.cardList=cardList; } //getter public String getName() { return name; } public String getPassword() { return password; } public double getBalance() { return balance; } public ArrayList<String> getCardList() { return cardList; } public void setBalance(double balance) { this.balance = balance; } } //存取款的检查类 class Check { ArrayList<Account> accountList; String card; String password; String number; double money; public Check(ArrayList<Account> accountList,String card,String password,String number,double money){ this.accountList=accountList; this.card=card; this.password=password; this.number=number; this.money=money; } public boolean check(){ int flag=0; int i,j; int k=0; //检查账号是否正确 i=0; while (i<accountList.size()) { j=0; while (j<accountList.get(i).getCardList().size()) { if (!card.equals(accountList.get(i).getCardList().get(j))) { i++; j++; continue; } flag=1; k=i; break; } if (flag != 1) { i++; continue; } break; } //检查密码是否正确 if (flag != 1) { System.out.println("Sorry,this card does not exist.");//卡号不存在 return false; } else { if (!password.equals(accountList.get(k).getPassword())) { System.out.println("Sorry,your password is wrong.");//银行卡密码错误 return false; } else { flag=2; } } //检查ATM机编号是否正确 for(i=0;i<accountList.get(k).banklist.size();i++) for (j = 0; j < accountList.get(k).banklist.get(i).ATMList.size(); j++) { if (!number.equals(accountList.get(k).banklist.get(i).ATMList.get(j))) { continue; } flag = 3; break; } //检查金额是否正确 if (flag == 3) { if (!(money <= accountList.get(k).getBalance())) { System.out.println("Sorry,your account balance is insufficient.");//取款金额大于账户余额 return false; } else { flag = 4; } } else { System.out.println("Sorry,the ATM's id is wrong.");//ATM机编号不存在 return false; } //检查是否跨行 i=0; while (i<accountList.get(k).bank.ATMList.size()) { if (!number.equals(accountList.get(k).bank.ATMList.get(i))) { i++; continue; } flag = 5; break; } if (flag != 5) { System.out.println("Sorry,cross-bank withdrawal is not supported.");//跨行存取款 return false; } else { return true; } } } //余额查询的检查类 class Check1 { ArrayList<Account> accountList; String card; public Check1(ArrayList<Account> accountList, String card){ this.accountList = accountList; this.card = card; } public boolean check(){ int i,j; int flag=0; //卡号校验 i=0; while (i<accountList.size()) { j=0; while (j<accountList.get(i).getCardList().size()) { if (!card.equals(accountList.get(i).getCardList().get(j))) { i++; j++; continue; } flag=1; break; } if (flag != 1) { i++; continue; } break; } if (flag != 1) { System.out.println("Sorry,this card does not exist.");//卡号不存在 return false; } else { return true; } } } //存取款类 class Access{ ArrayList<Account> accountList; String card; String password; String number; double money; public Access(ArrayList<Account> accountList,String card,String password,String number,double money){ this.password=password; this.number=number; this.card=card; this.accountList=accountList; this.money=money; } public void access(){ int i,j; int t=0; //卡号校验 for(i=0;i<accountList.size();i++) { j = 0; while (j < accountList.get(i).getCardList().size()) { if (!card.equals(accountList.get(i).getCardList().get(j))) { j++; continue; } t = i; break; } } accountList.get(t).setBalance(accountList.get(t).getBalance()-money); } } //存取款的展示类 class Show{ ArrayList<Account> accountList; String card; String password; String number; double money; public Show(ArrayList<Account> accountList,String card,String password,String number,double money){ this.password=password; this.number=number; this.card=card; this.accountList=accountList; this.money=money; } public void show(){ int i,j; int t=0; //卡号校验 for(i=0;i<accountList.size();i++) { j = 0; while (j < accountList.get(i).getCardList().size()) { if (!card.equals(accountList.get(i).getCardList().get(j))) { j++; continue; } t = i; break; } } //取款 if (!(money >= 0)) { money=-money; System.out.printf(accountList.get(t).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上存款¥%.2f\n",money); } else System.out.printf(accountList.get(t).getName() + "在" + accountList.get(t).bank.bankname + "的" + number + "号ATM机上取款¥%.2f\n", money); System.out.printf("当前余额为¥%.2f\n",accountList.get(t).getBalance()); } } //余额查询的展示类 class Show1{ ArrayList<Account> accountList; String card; public Show1(ArrayList<Account> accountList,String card){ this.accountList=accountList; this.card=card; } public void show1(){ int i,j; int t=0; //卡号校验 i=0; while (i<accountList.size()) { j=0; while (j<accountList.get(i).getCardList().size()) { if (!card.equals(accountList.get(i).getCardList().get(j))) { i++; j++; continue; } t=i; break; } i++; } System.out.printf("¥%.2f\n",accountList.get(t).getBalance()); } } //主类 public class Main { public static void main(String[] args) { //ATM机编号数组 ArrayList<String> ATMList1 = new ArrayList<>(); ATMList1.add("01"); ATMList1.add("02"); ATMList1.add("03"); ATMList1.add("04"); Bank A; A = new Bank("中国建设银行", ATMList1); ArrayList<String> ATMList2 = new ArrayList<>(); ATMList2.add("05"); ATMList2.add("06"); Bank B; B = new Bank("中国工商银行", ATMList2); //银行数组 ArrayList<Bank> bankList = new ArrayList<>(); bankList.add(A); bankList.add(B); //用户数组 ArrayList<String> cardList1 = new ArrayList<>(); cardList1.add("6217000010041315709"); cardList1.add("6217000010041315715"); Account account1 = new Account("杨过", "88888888", 10000.00, bankList, A,cardList1); ArrayList<String> cardList2 = new ArrayList<>(); cardList2.add("6217000010041315718"); Account account2 = new Account("杨过", "88888888", 10000.00, bankList,A,cardList2); ArrayList<String> cardList3 = new ArrayList<>(); cardList3.add("6217000010051320007"); Account account3 = new Account("郭靖", "88888888", 10000.00, bankList,A,cardList3); ArrayList<String> cardList4 = new ArrayList<>(); cardList4.add("6222081502001312389"); Account account4 = new Account("张无忌", "88888888", 10000.00, bankList,B,cardList4); ArrayList<String> cardList5 = new ArrayList<>(); cardList5.add("6222081502001312390"); Account account5 = new Account("张无忌", "88888888", 10000.00, bankList,B,cardList5); ArrayList<String> cardList6 = new ArrayList<>(); cardList6.add("6222081502001312399"); cardList6.add("6222081502001312400"); Account account6 = new Account("张无忌", "88888888", 10000.00, bankList,B,cardList6); ArrayList<String> cardList7 = new ArrayList<>(); cardList7.add("6222081502051320785"); Account account7 = new Account("韦小宝", "88888888", 10000.00, bankList,B,cardList7); ArrayList<String> cardList8 = new ArrayList<>(); cardList8.add("6222081502051320786"); Account account8 = new Account("韦小宝", "88888888", 10000.00, bankList,B,cardList8); //用户总数组 ArrayList<Account> accountList = new ArrayList<>(); accountList.add(account1); accountList.add(account2); accountList.add(account3); accountList.add(account4); accountList.add(account5); accountList.add(account6); accountList.add(account7); accountList.add(account8); Scanner x=new Scanner(System.in); String kp; Check check; Check1 check1; kp=x.nextLine(); if (!kp.equals("#")) { do { String[] DATE = kp.split("\\s+");//输入的每行数据用空格隔开,存为数组 if (DATE.length == 1) { check1 = new Check1(accountList, DATE[0]); if (check1.check()) { Show1 show1 = new Show1(accountList, DATE[0]); show1.show1(); } } else { check = new Check(accountList, DATE[0], DATE[1], DATE[2], Double.parseDouble(DATE[3])); if (check.check()) { Access access = new Access(accountList, DATE[0], DATE[1], DATE[2], Double.parseDouble(DATE[3])); access.access(); Show show = new Show(accountList, DATE[0], DATE[1], DATE[2], Double.parseDouble(DATE[3])); show.show(); } } kp = x.nextLine(); } while (!kp.equals("#")); } } }
解题思路:
主要难点就在于对输入数据的处理,以及合法性的判断
易错点:
1.合法性判断包括卡号不存在、输入ATM机编号错误、密码错误、取款金额大于银行卡余额和跨行存取错误
2.通过字符串的方法将输入的数据分为四个字符串,并对每一个字符串进行相应的合法性判断
题目集9-1
题目要求如下:
本题类图如下:
import java.util.Objects; import java.util.Scanner; import java.util.ArrayList; class Account{ double money; String user,bank,bankaccount,type; ArrayList<String> card; public Account(String user,String type,String bank,String bankaccount,double money,ArrayList<String> card) { this.user=user; this.type=type; this.bank=bank; this.bankaccount=bankaccount; this.money=money; this.card=card; } } class Bank{ String name; ArrayList<String> ATM; public Bank(String name,ArrayList<String> ATM) { this.name=name; this.ATM=ATM; } } public class Main { static double a; static String []ab=new String[100]; public static void main(String[] args){ Scanner in=new Scanner(System.in); int m; ArrayList<String> ATM1=new ArrayList<>(); ATM1.add("01"); ATM1.add("02"); ATM1.add("03"); ATM1.add("04"); Bank bank1=new Bank("中国建设银行",ATM1); ArrayList<String> ATM2=new ArrayList<>(); ATM2.add("05"); ATM2.add("06"); Bank bank2=new Bank("中国工商银行",ATM2); ArrayList<String> ATM3=new ArrayList<>(); ATM3.add("07"); ATM3.add("08"); ATM3.add("09"); ATM3.add("10"); ATM3.add("11"); Bank bank3=new Bank("中国农业银行",ATM3); ArrayList<Bank> bank=new ArrayList<>(); bank.add(bank1); bank.add(bank2); bank.add(bank3); ArrayList<String> card1=new ArrayList<>(); card1.add("6217000010041315709"); card1.add("6217000010041315715"); Account account1 = new Account("杨过","借记","中国建设银行","3217000010041315709",10000.00,card1); ArrayList<String> card2=new ArrayList<>(); card2.add("6217000010041315718"); Account account2 = new Account("杨过","借记","中国建设银行","3217000010041315709",10000.00,card2); ArrayList<String> card3=new ArrayList<>(); card3.add("6217000010051320007"); Account account3 = new Account("郭靖","借记","中国建设银行","3217000010051320007",10000.00,card3); ArrayList<String> card4=new ArrayList<>(); card4.add("6222081502001312389"); Account account4 = new Account("张无忌","借记","中国工商银行","3222081502001312389",10000.00,card4); ArrayList<String> card5=new ArrayList<>(); card5.add("6222081502001312390"); Account account5 = new Account("张无忌","借记","中国工商银行","3222081502001312390",10000.00,card5); ArrayList<String> card6=new ArrayList<>(); card6.add("6222081502001312399"); card6.add("6222081502001312400"); Account account6 = new Account("张无忌","借记","中国工商银行","3222081502001312399",10000.00,card6); ArrayList<String> card7=new ArrayList<>(); card7.add("6222081502051320785"); Account account7 = new Account("韦小宝","借记","中国工商银行","3222081502051320785",10000.00,card7); ArrayList<String> card8=new ArrayList<>(); card8.add("6222081502051320786"); Account account8 = new Account("韦小宝","借记","中国工商银行","3222081502051320786",10000.00,card8); ArrayList<String> card9=new ArrayList<>(); card9.add("6640000010045442002"); card9.add("6640000010045442003"); Account account9 = new Account("张三丰","贷记","中国建设银行","3640000010045442002",10000.00,card9); ArrayList<String> card10=new ArrayList<>(); card10.add("6640000010045441009"); Account account10 = new Account("令狐冲","贷记","中国工商银行","3640000010045441009",10000.00,card10); ArrayList<String> card11=new ArrayList<>(); card11.add("6630000010033431001"); Account account11 = new Account("乔峰","贷记","中国农业银行","3630000010033431001",10000.00,card11); ArrayList<String> card12=new ArrayList<>(); card12.add("6630000010033431008"); Account account12 = new Account("洪七公","贷记","中国农业银行","3630000010033431008",10000.00,card12); ArrayList<Account> account=new ArrayList<>(); account.add(account1); account.add(account2); account.add(account3); account.add(account4); account.add(account5); account.add(account6); account.add(account7); account.add(account8); account.add(account9); account.add(account10); account.add(account11); account.add(account12); int i; while(true) { m=0; for (i=0;i<4;i++) { ab[i]=in.next(); if(ab[i].equals("#")) { break; } else m++; if (i != 3) { continue; } a=Double.parseDouble(ab[3]); } int flag5=0; for (Account value : account) { for (int n = 0; n < value.card.size(); n++) { if (!ab[0].equals(value.card.get(n))) { continue; } flag5 = 1; break; } } if (flag5 == 0) { System.out.print("Sorry,this card does not exist."); System.exit(0); } if (m != 1) { } else { show1(account); System.exit(0); } int flag6=0; if(ab[2].equals("01")||ab[2].equals("02")||ab[2].equals("03")||ab[2].equals("04")||ab[2].equals("05")||ab[2].equals("06")||ab[2].equals("07")||ab[2].equals("08")||ab[2].equals("09")||ab[2].equals("10")||ab[2].equals("11")) { flag6 = 1; } if(flag6==0) { System.out.print("Sorry,the ATM's id is wrong."); System.exit(0); } int t1=0; int p; for(;i<account.size();i++) for (p = 0; p < account.get(i).card.size(); p++) { if (!ab[0].equals(account.get(i).card.get(p))) { continue; } t1 = i; break; } if(!ab[1].equals("88888888")) { System.out.print("Sorry,your password is wrong."); System.exit(0); } else { if(Double.parseDouble(ab[3])>account.get(t1).money&&t1>=0&&t1<=7) { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } else if(Double.parseDouble(ab[3])>account.get(t1).money&&t1>=8&&t1<=11&&account.get(t1).money-Double.parseDouble(ab[3])>=50000) { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } else { int flag3=0; int t3=0; for(i=0;i<account.size();i++) for (int j = 0; j < account.get(i).card.size(); j++) { if (!ab[0].equals(account.get(i).card.get(j))) { continue; } t3 = i; break; } for(i=0;i<ATM1.size();i++) { if (!ab[2].equals(ATM1.get(i))) { continue; } if(!Objects.equals(account.get(t3).bank, "中国建设银行")) { flag3=1; } if(Objects.equals(account.get(t3).bank, "中国建设银行")) flag3=2; break; } for(i=0;i<ATM2.size();i++) { if (!ab[2].equals(ATM2.get(i))) { continue; } if (account.get(t3).bank != "中国工商银行") { flag3 = 1; } if(account.get(t3).bank=="中国工商银行") flag3=2; break; } for(i=0;i<ATM3.size();i++) { if (!ab[2].equals(ATM3.get(i))) { continue; } if (account.get(t3).bank == "中国农业银行") { } else { flag3=1; } if(account.get(t3).bank=="中国农业银行") flag3=2; break; } if(flag3==1) { show2(account,ATM1,ATM2,ATM3,bank); } else if(flag3==2) { show(account,ATM1,ATM2,ATM3); } } } } } public static void show2(ArrayList<Account> account,ArrayList<String> ATM1,ArrayList<String> ATM2,ArrayList<String> ATM3,ArrayList<Bank> bank) { int i,j; int t1=0,t2=0,t3=0,t4=0; int flag1=0,flag2=0,flag3=0; for(i=0;i<account.size();i++){ for(j=0;j<account.get(i).card.size();j++){ if (!ab[0].equals(account.get(i).card.get(j))) { continue; } t1=i; break; } } for(i=0;i<ATM1.size();i++) { if (!ab[2].equals(ATM1.get(i))) { continue; } t2=i; flag1=1; break; } for(i=0;i<ATM2.size();i++) { if (!ab[2].equals(ATM2.get(i))) { continue; } t3=i; flag2=1; break; } for(i=0;i<ATM3.size();i++) { if (!ab[2].equals(ATM3.get(i))) { continue; } t4=i; flag3=1; break; } if(flag1==1) { if (!account.get(t1).type.equals("借记")) { if (account.get(t1).type.equals("贷记")) if (account.get(t1).money - a < 0) { if (!(account.get(t1).money > 0)) { account.get(t1).money = account.get(t1).money - a - a * 0.02 - a * 0.05; } else { account.get(t1).money = account.get(t1).money - a - a * 0.02 - (a - account.get(t1).money) * 0.05; } if (account.get(t1).money >= -50000) { System.out.printf("业务:取款 " + account.get(t1).user + "在" + bank.get(0).name + "的" + ATM1.get(t2) + "号ATM机上取款¥%.2f\n", Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n", account.get(t1).money); } else { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } } else { account.get(t1).money = account.get(t1).money - a - a * 0.02; if (!(account.get(t1).money >= 0)) { } else { System.out.printf("业务:取款 " + account.get(t1).user + "在" + bank.get(0).name + "的" + ATM1.get(t2) + "号ATM机上取款¥%.2f\n", Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n", account.get(t1).money); } } } else { account.get(t1).money = account.get(t1).money - a - a * 0.02; if (!(account.get(t1).money >= 0)) { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } else { System.out.printf("业务:取款 " + account.get(t1).user + "在" + bank.get(0).name + "的" + ATM1.get(t2) + "号ATM机上取款¥%.2f\n", Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n", account.get(t1).money); } } } if(flag2==1) { if(account.get(t1).type.equals("借记")) { account.get(t1).money=account.get(t1).money-a-a*0.03; if(account.get(t1).money>=0) { System.out.printf("业务:取款 "+account.get(t1).user+"在"+bank.get(1).name+"的"+ATM2.get(t3)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money);} else { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } } else if(account.get(t1).type.equals("贷记")) { if(account.get(t1).money-a<0) { if(account.get(t1).money>0) account.get(t1).money=account.get(t1).money-a-a*0.03-(a-account.get(t1).money)*0.05; else account.get(t1).money=account.get(t1).money-a-a*0.03-a*0.05; if(account.get(t1).money>=-50000) { System.out.printf("业务:取款 "+account.get(t1).user+"在"+bank.get(1).name+"的"+ATM2.get(t3)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money);} else { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } } else { account.get(t1).money=account.get(t1).money-a-a*0.03; System.out.printf("业务:取款 "+account.get(t1).user+"在"+bank.get(1).name+"的"+ATM2.get(t3)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money); } } } if(flag3==1) { if(account.get(t1).type.equals("借记")) { account.get(t1).money=account.get(t1).money-a-a*0.04; if(account.get(t1).money>=0) { System.out.printf("业务:取款 "+account.get(t1).user+"在"+bank.get(2).name+"的"+ATM3.get(t4)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money); } else { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } } else if(account.get(t1).type.equals("贷记")) { if(account.get(t1).money-a<0) { if(account.get(t1).money>0) account.get(t1).money=account.get(t1).money-a-a*0.04-(a-account.get(t1).money)*0.05; else account.get(t1).money=account.get(t1).money-a-a*0.04-a*0.05; if(account.get(t1).money>=-50000) { System.out.printf("业务:取款 "+account.get(t1).user+"在"+bank.get(2).name+"的"+ATM3.get(t4)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money);} else { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } } else { account.get(t1).money=account.get(t1).money-a-a*0.04; System.out.printf("业务:取款 "+account.get(t1).user+"在"+bank.get(2).name+"的"+ATM3.get(t4)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money); } } } } public static void show(ArrayList<Account> account,ArrayList<String> ATM1,ArrayList<String> ATM2,ArrayList<String> ATM3) { int i,j; int t1=0,t2=0,t3=0,t4=0; int flag1=0,flag2=0,flag3=0; for(i=0;i<account.size();i++){ for(j=0;j<account.get(i).card.size();j++){ if(ab[0].equals(account.get(i).card.get(j))){ t1=i; break; } } } i=0; while (i<ATM1.size()) { if (!ab[2].equals(ATM1.get(i))) { i++; continue; } t2=i; flag1=1; break; } i=0; while (i<ATM2.size()) { if (!ab[2].equals(ATM2.get(i))) { i++; continue; } t3=i; flag2=1; break; } i=0; while (i<ATM3.size()) { if (!ab[2].equals(ATM3.get(i))) { i++; continue; } t4=i; flag3=1; break; } if(flag1==1) { if(account.get(t1).money-a<0) { if(account.get(t1).money>0) { account.get(t1).money=account.get(t1).money-a-(a-account.get(t1).money)*0.05; System.out.printf("业务:取款 "+account.get(t1).user+"在"+account.get(t1).bank+"的"+ATM1.get(t2)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money); } else { account.get(t1).money=account.get(t1).money-a-a*0.05; if(account.get(t1).money>=-50000) { System.out.printf("业务:取款 "+account.get(t1).user+"在"+account.get(t1).bank+"的"+ATM1.get(t2)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money);} else { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } } } else { account.get(t1).money=account.get(t1).money-a; if(account.get(t1).money>=0) { System.out.printf("业务:取款 "+account.get(t1).user+"在"+account.get(t1).bank+"的"+ATM1.get(t2)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money);} else { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } } } if(flag2==1) { if(account.get(t1).money-a<0) { if(account.get(t1).money>0) { account.get(t1).money=account.get(t1).money-a-(a-account.get(t1).money)*0.05; } else { account.get(t1).money=account.get(t1).money-a-a*0.05; } System.out.printf("业务:取款 "+account.get(t1).user+"在"+account.get(t1).bank+"的"+ATM2.get(t3)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money); } else { account.get(t1).money=account.get(t1).money-a; if(account.get(t1).money>=0) {System.out.printf("业务:取款 "+account.get(t1).user+"在"+account.get(t1).bank+"的"+ATM2.get(t3)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money);} else { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } } } if(flag3==1) { if(account.get(t1).money-a<0) { if(account.get(t1).money>0) { account.get(t1).money=account.get(t1).money-a-(a-account.get(t1).money)*0.05; } else { account.get(t1).money=account.get(t1).money-a-a*0.05; } System.out.printf("业务:取款 "+account.get(t1).user+"在"+account.get(t1).bank+"的"+ATM3.get(t4)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money); } else { account.get(t1).money=account.get(t1).money-a; if(account.get(t1).money>=0) { System.out.printf("业务:取款 "+account.get(t1).user+"在"+account.get(t1).bank+"的"+ATM3.get(t4)+"号ATM机上取款¥%.2f\n",Double.parseDouble(ab[3])); System.out.printf("当前余额为¥%.2f\n",account.get(t1).money);} else { System.out.print("Sorry,your account balance is insufficient."); System.exit(0); } } } } public static void show1(ArrayList<Account> account) { int flag5=0; for (Account value : account) { for (int n = 0; n < value.card.size(); n++) { if (ab[0].equals(value.card.get(n))) { flag5 = 1; break; } } } if(flag5==0) { System.out.print("Sorry,this card does not exist."); System.exit(0); } int i,j; int t1=0; for(i=0;i<account.size();i++){ for(j=0;j<account.get(i).card.size();j++){ if(ab[0].equals(account.get(i).card.get(j))){ t1=i; break; } } } System.out.printf("业务:查询余额 "+"¥%.2f\n",account.get(t1).money); } }
解题思路:
1.在8-1的基础上本题添加了跨行取款的机制——每次跨行取款需要扣取相应手续费
2.在8-1的基础上增加了信用卡机制——可以透支,并且每次取款需要扣取相应手续费
易错点:
1.如果透支取款时也是跨行取款,是否需要收取两份手续费
2.如果透支恰好等于规定的透支金额,此时还没有计算手续费,那么是否可以进行透支
三、总结
通过这一学期的学习,学习到了很多与以往不同的东西。比如面向对象的编程方法,java类的运用等等非常实用的知识;这些知识不仅能用在编程中,对于我们思维的开拓也有着非常大的帮助。在学习的过程中也是痛并快乐着的,比如有些问题迟迟不能解决就很难受,但通过课上课后的学习、资料的查阅以及同学之间的相互帮助解决这些困难后又是件快乐的事情。最后,我认为学习是一件终生的事,所谓的学无止境便是这样。虽然java的学习暂时告一段落了,但是我们的学习的步伐是不会停下的!