题目集7~9Blog

(1)前言:总结三次题目集的知识点、题量、难度等情况

   后三次作业难度明显提高,对java的能力要求明显更高。题量倒是减少了,但是每题考察的知识点显然更多了。难度中上,不会太难,但还是对java基础有一定的考察。老师上课也会偶尔补充思路,比前几次自己摸索更好一点了。

(2)设计与分析:重点对题目的提交源码进行分析,可参考SourceMonitor的生成报表内容以及PowerDesigner的相应类图,要有相应的解释和心得(做到有图有真相),本次Blog必须分析的内容如下:

    ①题目集7(7-1)、(7-2)两道题目的递进式设计分析总结

1.题目集7-1

输入格式:

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

输出格式:

  • 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边不能组成三角形),则输出Wrong Format
  • 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
  1. 排序前的各图形类型及面积,格式为图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格;
  2. 排序后的各图形类型及面积,格式同排序前的输出;
  3. 所有图形的面积总和,格式为Sum of area:总面积值
  1. 实验代码:

  2. import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Scanner;

    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();
    }
    }
    class Card{
    Shape shape;

    Card() {
    }

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

    public Shape getShape() {
    return shape;
    }

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

    class DealCardList {
    DecimalFormat df = new DecimalFormat("0.00");
    ArrayList<Card> cardList = new ArrayList<Card>();
    ArrayList<Integer> list = new ArrayList<Integer>();
    public DealCardList(){
    }
    public DealCardList(ArrayList<Integer> list){
    this.list = list;
    for(int i=0;i<list.size();i++)
    {
    if(list.get(i)==1)
    {
    double r=Main.input.nextDouble();
    Circle circle=new Circle(r);
    Card card=new Card(circle);
    card.getShape().setShapeName("Circle");
    cardList.add(card);
    }
    if(list.get(i)==2) {
    double a=Main.input.nextDouble();
    double b=Main.input.nextDouble();
    Rectangle rectangle=new Rectangle(a,b);
    Card card=new Card(rectangle);
    card.getShape().setShapeName("Rectangle");
    cardList.add(card);
    }
    if(list.get(i)==3) {
    double a=Main.input.nextDouble();
    double b=Main.input.nextDouble();
    double c=Main.input.nextDouble();
    Triangle triangle=new Triangle(a,b,c);
    Card card=new Card(triangle);
    card.getShape().setShapeName("Triangle");
    cardList.add(card);
    }
    if(list.get(i)==4) {
    double a=Main.input.nextDouble();
    double b=Main.input.nextDouble();
    double c=Main.input.nextDouble();
    Trapezoid trapezoid=new Trapezoid(a,b,c);
    Card card=new Card(trapezoid);
    card.getShape().setShapeName("Trapezoid");
    cardList.add(card);
    }
    }
    }
    public boolean validate(){
    int k = 1;
    for(int i = 0 ;i< cardList.size(); i++){
    if(!cardList.get(i).getShape().check()){
    k = 0;
    }
    }
    if(k==0){
    return false;
    }
    else{
    return true;
    }
    }
    public void showResult(){
    if(!validate()){
    System.out.println("Wrong Format");
    }
    else{
    System.out.println("The original list:");
    for(int i = 0 ;i<list.size(); i++){
    if(list.get(i)==1){
    System.out.print("Circle:"+df.format(cardList.get(i).getShape().getArea())+" ");
    }
    if(list.get(i)==2){
    System.out.print("Rectangle:"+df.format(cardList.get(i).getShape().getArea())+" ");
    }
    if(list.get(i)==3){
    System.out.print("Triangle:"+df.format(cardList.get(i).getShape().getArea())+" ");
    }
    if(list.get(i)==4){
    System.out.print("Trapezoid:"+df.format(cardList.get(i).getShape().getArea())+" ");
    }
    }
    System.out.println();
    System.out.println("The sorted list:");

    double array[] = new double[cardList.size()];
    double sum = 0;
    for(int i=0 ;i< cardList.size() ;i++){
    array[i] = cardList.get(i).getShape().getArea();
    sum += cardList.get(i).getShape().getArea();
    }
    for(int k=0;k<cardList.size();k++)
    for(int i=k+1;i<cardList.size();i++)
    {
    if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea())
    Collections.swap(cardList, k, i);
    }

    for(int i=0;i<cardList.size();i++)
    System.out.print(cardList.get(i).getShape().getShapeName()+":"+df.format(cardList.get(i).getShape().getArea())+" ");
    System.out.println();

    System.out.println("Sum of area:"+df.format(sum));
    }
    }
    }
    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 double getArea(){
    return 0.0;
    }
    public boolean check(){
    return true;
    }
    }
    class Circle extends Shape{
    double r;
    public Circle(){
    }
    public Circle(double r){
    this.r = r;
    }
    public boolean check(){
    if(r>0){
    return true;
    }
    else
    return false;
    }
    public double getArea(){
    return Math.PI*r*r;
    }
    }
    class Rectangle extends Shape{
    double w,l;
    public Rectangle(){
    }
    public Rectangle(double w,double l){
    this.w = w;
    this.l = l;
    }
    public boolean check(){
    if(w>0&&l>0){
    return true;
    }
    else
    return false;
    }
    public double getArea(){
    return w*l;
    }
    }
    class Triangle extends Shape{
    double a,b,c;
    public Triangle(){

    }
    public Triangle(double a,double b,double c){
    this.a = a;
    this.b = b;
    this.c = c;
    }
    public boolean check(){
    if(a+b>c&&a+c>b&&b+c>a){
    return true;
    }
    else{
    return false;
    }
    }
    public double getArea(){
    double p = (a+b+c)/2;
    return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }
    }
    class Trapezoid extends Shape{
    double a, b, h;

    public Trapezoid(double a, double b, double h) {
    this.a = a;
    this.b = b;
    this.h = h;
    }

    public boolean check() {
    if (a > 0 && b > 0 && h > 0) {
    return true;
    } else
    return false;
    }

    public double getArea() {
    return (a + b) * h / 2;
    }
    }

  3. 心得体会:7-1相对来说并不难,它考察的更多是多个数据的读入储存,最后再简单的通过arraylist对面积和排序。
  4. 遇到的坑:一开始做这题的时候并没有想到先创建一个类cardlist把数据储存起来,总想着怎么把图形名称和输入相对应,后来也想到了一个办法,但是当时因为太长就pass,现在隔了这么久也忘记当时怎么想的了。就酱吧。
  5. 输入格式:

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

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

    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Scanner;
    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();
    }
    }
    class Card{
    Shape shape;
    Card() {
    }
    Card(Shape shape) {
    this.shape = shape;
    }
    public Shape getShape() {
    return shape;
    }
    public void setShape(Shape Shape) {
    this.shape = shape;
    }
    }
    class DealCardList {
    DecimalFormat df = new DecimalFormat("0.00");
    ArrayList<Card> cardList = new ArrayList<Card>();
    ArrayList<Integer> list = new ArrayList<Integer>();
    public DealCardList(){
    }
    public DealCardList(ArrayList<Integer> list){
    this.list = list;
    for(int i=0;i<list.size();i++)
    {
    if(list.get(i)==1) {double r=Main.input.nextDouble();
    Circle circle=new Circle(r);
    Card card=new Card(circle);
    card.getShape().setShapeName("Circle");
    cardList.add(card);}
    if(list.get(i)==2) {
    double a=Main.input.nextDouble();
    double b=Main.input.nextDouble();
    Rectangle rectangle=new Rectangle(a,b);
    Card card=new Card(rectangle);
    card.getShape().setShapeName("Rectangle");
    cardList.add(card);}
    if(list.get(i)==3) {
    double a=Main.input.nextDouble();
    double b=Main.input.nextDouble();
    double c=Main.input.nextDouble();
    Triangle triangle=new Triangle(a,b,c);
    Card card=new Card(triangle);
    card.getShape().setShapeName("Triangle");
    cardList.add(card);}
    if(list.get(i)==4) {
    double a=Main.input.nextDouble();
    double b=Main.input.nextDouble();
    double c=Main.input.nextDouble();
    Trapezoid trapezoid=new Trapezoid(a,b,c);
    Card card=new Card(trapezoid);
    card.getShape().setShapeName("Trapezoid");
    cardList.add(card); }
    } }
    public boolean validate(){
    int k = 1;
    for(int i = 0 ;i< cardList.size(); i++){
    if(!cardList.get(i).getShape().check()){
    k = 0; } }
    if(k==0){
    return false; } else return true;
    }
    public void showResult(){
    if(!validate()){
    System.out.println("Wrong Format");
    }
    else{
    System.out.println("The original list:");
    System.out.print("[");
    for(int i = 0 ;i<list.size(); i++){
    if(list.get(i)==1){
    System.out.print("Circle:"+df.format(cardList.get(i).getShape().getArea())+" "); }
    if(list.get(i)==2){
    System.out.print("Rectangle:"+df.format(cardList.get(i).getShape().getArea())+" "); }
    if(list.get(i)==3){
    System.out.print("Triangle:"+df.format(cardList.get(i).getShape().getArea())+" "); }
    if(list.get(i)==4){
    System.out.print("Trapezoid:"+df.format(cardList.get(i).getShape().getArea())+" "); }
    }
    System.out.println("]");
    ArrayList<Double> area1 = new ArrayList<Double>();
    ArrayList<Double> area2 = new ArrayList<Double>();
    ArrayList<Double> area3 = new ArrayList<Double>();
    ArrayList<Double> area4 = new ArrayList<Double>();
    double allarea1=0,allarea2=0,allarea3=0,allarea4=0;
    double max = 0;
    for(int i = 0 ;i<cardList.size(); i++){
    if(cardList.get(i).getShape().getShapeName()=="Circle"){
    area1.add(cardList.get(i).getShape().getArea());
    allarea1 += cardList.get(i).getShape().getArea(); }
    if(cardList.get(i).getShape().getShapeName()=="Rectangle"){
    area2.add(cardList.get(i).getShape().getArea());
    allarea2 += cardList.get(i).getShape().getArea(); }
    if(cardList.get(i).getShape().getShapeName()=="Triangle"){
    area3.add(cardList.get(i).getShape().getArea());
    allarea3 += cardList.get(i).getShape().getArea(); }
    if(cardList.get(i).getShape().getShapeName()=="Trapezoid"){
    area4.add(cardList.get(i).getShape().getArea());
    allarea4 += cardList.get(i).getShape().getArea(); }
    }
    System.out.println("The Separated List:");
    if(allarea1!=0){
    System.out.print("[");
    for(int i=0 ;i<area1.size(); i++)
    System.out.print("Circle:"+df.format(area1.get(i))+" ");
    System.out.print("]");
    } else System.out.print("[]");

    if(allarea2!=0){
    System.out.print("[");
    for(int i=0 ;i<area2.size(); i++)
    System.out.print("Rectangle:"+df.format(area2.get(i))+" ");
    System.out.print("]");
    } else System.out.print("[]");

    if(allarea3!=0){
    System.out.print("[");
    for(int i=0 ;i<area3.size(); i++)
    System.out.print("Triangle:"+df.format(area3.get(i))+" ");
    System.out.print("]");
    } else System.out.print("[]");

    if(allarea4!=0){
    System.out.print("[");
    for(int i=0 ;i<area4.size(); i++)
    System.out.print("Trapezoid:"+df.format(area4.get(i))+" ");
    System.out.print("]");
    } else System.out.print("[]");
    System.out.println();
    System.out.println("The Separated sorted List:");
    Collections.sort(area1);
    Collections.sort(area2);
    Collections.sort(area3);
    Collections.sort(area4);
    if(allarea1!=0){
    System.out.print("[");
    for(int i=0 ;i<area1.size(); i++)
    System.out.print("Circle:"+df.format(area1.get(area1.size()-1-i))+" ");
    System.out.print("]");
    } else System.out.print("[]");
    if(allarea2!=0){
    System.out.print("[");
    for(int i=0 ;i<area2.size(); i++)
    System.out.print("Rectangle:"+df.format(area2.get(area2.size()-1-i))+" ");
    System.out.print("]");
    } else System.out.print("[]");
    if(allarea3!=0){
    System.out.print("[");
    for(int i=0 ;i<area3.size(); i++)
    System.out.print("Triangle:"+df.format(area3.get(area3.size()-1-i))+" ");
    System.out.print("]");
    } else System.out.print("[]");
    if(allarea4!=0){
    System.out.print("[");
    for(int i=0 ;i<area4.size(); i++)
    System.out.print("Trapezoid:"+df.format(area4.get(area4.size()-1-i))+" ");
    System.out.print("]");
    } else System.out.print("[]");
    double a[] = new double[4];
    a[0] = allarea1;
    a[1] = allarea2;
    a[2] = allarea3;
    a[3] = allarea4;
    Arrays.sort(a);
    System.out.println();
    System.out.println("The max area:"+df.format(a[3]));
    }
    }
    }
    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 double getArea(){
    return 0.0;
    }
    public boolean check(){
    return true;
    }
    }
    class Circle extends Shape{
    double r;
    public Circle(){
    }
    public Circle(double r){
    this.r = r;
    }
    public boolean check(){
    if(r>0){
    return true;
    } else return false;
    }
    public double getArea(){
    return Math.PI*r*r;
    }
    }
    class Rectangle extends Shape{
    double w,l;
    public Rectangle(){
    }
    public Rectangle(double w,double l){
    this.w = w;
    this.l = l;
    }
    public boolean check(){
    if(w>0&&l>0){
    return true;
    } else return false;
    }
    public double getArea(){
    return w*l; }
    }
    class Triangle extends Shape{
    double a,b,c;
    public Triangle(){
    }
    public Triangle(double a,double b,double c){
    this.a = a;
    this.b = b;
    this.c = c;
    }
    public boolean check(){
    if(a+b>c&&a+c>b&&b+c>a){
    return true;
    } else return false;
    }
    public double getArea(){
    double p = (a+b+c)/2;
    return Math.sqrt(p*(p-a)*(p-b)*(p-c)); }
    }
    class Trapezoid extends Shape{
    double a, b, h;
    public Trapezoid(double a, double b, double h) {
    this.a = a;
    this.b = b;
    this.h = h;
    }
    public boolean check() {
    if (a > 0 && b > 0 && h > 0) {
    return true;
    } else return false;
    }
    public double getArea() {
    return (a + b) * h / 2; }
    }

  8. 心得体会:7-2难就难在,它并不像7-1只是单纯的对面积和有要求,它对每个输入的图形面积都有要求,相对7-1它需要更多的储存数组来储存面积信息。更多的就是要注意格式要求了。

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

题目集八实验代码:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
StringBuilder s = new StringBuilder();
String str = in.nextLine();
String ending = "#";
boolean flag = str.matches(ending);
while(flag == false){
s.append(str);
s.append("\n");
str = in.nextLine();
flag = str.matches(ending);
}
DealData dealdata = new DealData(s);
dealdata.getDealDataResult(); }}
class DealData{
StringBuilder s = new StringBuilder();
public DealData(){}
public DealData(StringBuilder s){
this.s = s; }
public void getDealDataResult(){
String zhengfu;
String[] everydata = this.s.toString().split("\n");
for(int i= 0 ;i< everydata.length; i++){
zhengfu = "取款";
String regex = "(.*)\\s(.*)\\s(.*)\\s(.*)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(everydata[i]);
if(matcher.find()){
CheckData c = new CheckData(matcher.group(1),matcher.group(2), matcher.group(3), matcher.group(4));
boolean flag = c.validataDate();
if(flag==true){
Double money = new Double(matcher.group(4));
if(money<0){
zhengfu = "存款"; }
if(matcher.group(1).equals("6217000010041315709")||matcher.group(1).equals("6217000010041315715")){
Account1 account1 = new Account1();
if(money>= account1.getBalance()){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account1.setBalance(money);
System.out.println(account1.name+"在"+"中国建设银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account1.getBalance())); } }
if(matcher.group(1).equals("6217000010041315718")){
Account2 account2 = new Account2();
if(money>= account2.getBalance()){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account2.setBalance(money);
System.out.println(account2.name+"在"+"中国建设银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account2.getBalance()));
}
}
if(matcher.group(1).equals("6217000010051320007")){
Account3 account3 = new Account3();
if(money>= account3.getBalance()){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account3.setBalance(money);
System.out.println(account3.name+"在"+"中国建设银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account3.getBalance())); } }
if(matcher.group(1).equals("6222081502001312389")){
Account4 account4 = new Account4();
if(money>= account4.getBalance()){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account4.setBalance(money);
System.out.println(account4.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account4.getBalance())); } }
if(matcher.group(1).equals("6222081502001312390")){
Account5 account5 = new Account5();
if(money>= account5.getBalance()){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account5.setBalance(money);
System.out.println(account5.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account5.getBalance())); } }
if(matcher.group(1).equals("6222081502001312399")||matcher.group(1).equals("6222081502001312400")){
Account6 account6 = new Account6();
if(money>= account6.getBalance()){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account6.setBalance(money);
System.out.println(account6.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account6.getBalance())); } }
if(matcher.group(1).equals("6222081502051320785")){
Account7 account7 = new Account7();
if(money>= account7.getBalance()){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account7.setBalance(money);
System.out.println(account7.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account7.getBalance())); } }
if(matcher.group(1).equals("6222081502051320786")){
Account8 account8 = new Account8();
if(money>= account8.getBalance()){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account8.setBalance(money);
System.out.println(account8.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account8.getBalance())); } }
}else{
System.exit(0); }
}else if(everydata[i]!=null){
if(everydata[i].equals("6217000010041315709")||everydata[i].equals("6217000010041315715")){
Account1 account1 = new Account1();
System.out.println("¥"+String.format("%.2f",account1.getBalance()));
}
if(everydata[i].equals("6217000010041315718")){
Account2 account2 = new Account2();
System.out.println("¥"+String.format("%.2f",account2.getBalance()));
}
if(everydata[i].equals("6217000010051320007")){
Account3 account3 = new Account3();
System.out.println("¥"+String.format("%.2f",account3.getBalance()));
}
if(everydata[i].equals("6222081502001312389")){
Account4 account4 = new Account4();
System.out.println("¥"+String.format("%.2f",account4.getBalance()));
}
if(everydata[i].equals("6222081502001312390")){
Account5 account5 = new Account5();
System.out.println("¥"+String.format("%.2f",account5.getBalance()));
}
if(everydata[i].equals("6222081502001312399")||everydata[i].equals("6222081502001312400")){
Account6 account6 = new Account6();
System.out.println("¥"+String.format("%.2f",account6.getBalance()));
}
if(everydata[i].equals("6222081502051320785")){
Account7 account7 = new Account7();
System.out.println("¥"+String.format("%.2f",account7.getBalance()));
}
if(everydata[i].equals("6222081502051320786")){
Account8 account8 = new Account8();
System.out.println("¥"+String.format("%.2f",account8.getBalance())); }
}
}
}
}
class CheckData {
String id,password,atmid,money;
public CheckData() {
}
public CheckData(String id,String password,String atmid,String money) {
this.id = id;
this.password = password;
this.atmid = atmid;
this.money = money; }
public boolean validataDate() {
int flag1 = 0, flag2 = 0;
int key = 0;
String[] id = {"","6217000010041315709", "6217000010041315715", "6217000010041315718", "6217000010051320007", "6222081502001312389", "6222081502001312390", "6222081502001312399", "6222081502001312400", "6222081502051320785", "6222081502051320786"};
String[] atmid = {"01", "02", "03", "04", "05", "06"};
for(int i = 0 ;i<id.length; i++){
if(this.id.equals(id[i])){
flag1 = 1;
key = i;
break; } }
if(flag1==1){
if(this.password.equals("88888888")){
for (int i = 0; i < atmid.length; i++) {
if (this.atmid.equals(atmid[i])) {
flag2 = 1;
break; }
}
if(flag2==1){
if (key == 0 || key == 1 || key == 2 || key == 3) {
if (this.atmid.equals("01")|| this.atmid.equals("02")|| this.atmid.equals("03")|| this.atmid.equals("04")) {
return true;
} else {
System.out.println("Sorry,cross-bank withdrawal is not supported.");
return false;
}
}
else{
if(this.atmid.equals("05")|| this.atmid.equals("06")){
return true;
}else{
System.out.println("Sorry,cross-bank withdrawal is not supported.");
return false; }
}
}else{
System.out.println("Sorry,the ATM's id is wrong.");
return false; }
}else{
System.out.println("Sorry,your password is wrong.");
return false;
}
}else{
System.out.println("Sorry,this card does not exist.");
return false; } }}
class Account1{
String name = "杨过";
private static double balance = 10000.00;
public Account1(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money){
balance = balance-money;
}
}
class Account2{
String name = "杨过";
private static double balance = 10000.00;
public Account2(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money){
balance = balance-money;
}
}
class Account3{
String name = "郭靖";
private static double balance = 10000.00;
public Account3(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money){
balance = balance-money;
}
}
class Account4{
String name = "张无忌";
private static double balance = 10000.00;
public Account4(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money){
balance = balance-money;
}
}
class Account5{
String name = "张无忌";
private static double balance = 10000.00;
public Account5(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money){
balance = balance-money;
}
}
class Account6{
String name = "张无忌";
private static double balance = 10000.00;
public Account6(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money){
balance = balance-money;
}
}
class Account7{
String name = "韦小宝";
private static double balance = 10000.00;
public Account7(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money){
balance = balance-money;
}
}
class Account8{
String name = "韦小宝";
private static double balance = 10000.00;
public Account8(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money){
balance = balance-money;
}
}

题目集九实验代码:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
StringBuilder s = new StringBuilder();
String str = in.nextLine();
String ending = "#";
boolean flag = str.matches(ending);
while(flag == false){
s.append(str);
s.append("\n");
str = in.nextLine();
flag = str.matches(ending);
}
DealData dealdata = new DealData(s);
dealdata.getDealDataResult(); }}
class DealData{
StringBuilder s = new StringBuilder();
public DealData(){}
public DealData(StringBuilder s){
this.s = s; }
public void getDealDataResult(){
String zhengfu;
double balance0,loan;
double k = 0.05;
String[] everydata = this.s.toString().split("\n");
for(int i= 0 ;i< everydata.length; i++){
zhengfu = "取款";
String regex = "(.*)\\s(.*)\\s(.*)\\s(.*)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(everydata[i]);
if(matcher.find()){
CheckData c = new CheckData(matcher.group(1),matcher.group(2),matcher.group(3), matcher.group(4));
boolean flag = c.validataDate();
double ratio = c.getRatio();
if(flag==true){
Double money = new Double(matcher.group(4));
if(money<0){
zhengfu = "存款"; }
if(matcher.group(1).equals("6217000010041315709")||matcher.group(1).equals("6217000010041315715")){
Account1 account1 = new Account1();
balance0 = account1.getBalance()-money-ratio*money;
if(balance0<0){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account1.setBalance(money,ratio);
System.out.println("业务:"+zhengfu+" "+account1.name+"在"+"中国建设银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account1.getBalance())); } }
if(matcher.group(1).equals("6217000010041315718")){
Account2 account2 = new Account2();
balance0 = account2.getBalance()-money-ratio*money;
if(balance0<0){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account2.setBalance(money,ratio);
System.out.println("业务:"+zhengfu+" "+account2.name+"在"+"中国建设银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account2.getBalance()));
}
}
if(matcher.group(1).equals("6217000010051320007")){
Account3 account3 = new Account3();
balance0 = account3.getBalance()-money-ratio*money;
if(balance0<0){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account3.setBalance(money,ratio);
System.out.println("业务:"+zhengfu+" "+account3.name+"在"+"中国建设银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account3.getBalance())); } }
if(matcher.group(1).equals("6222081502001312389")){
Account4 account4 = new Account4();
balance0 = account4.getBalance()-money-ratio*money;
if(balance0<0){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account4.setBalance(money,ratio);
System.out.println("业务:"+zhengfu+" "+account4.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account4.getBalance())); } }
if(matcher.group(1).equals("6222081502001312390")){
Account5 account5 = new Account5();
balance0 = account5.getBalance()-money-ratio*money;
if(balance0<0){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account5.setBalance(money,ratio);
System.out.println("业务:"+zhengfu+" "+account5.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account5.getBalance())); } }
if(matcher.group(1).equals("6222081502001312399")||matcher.group(1).equals("6222081502001312400")){
Account6 account6 = new Account6();
balance0 = account6.getBalance()-money-ratio*money;
if(balance0<0){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account6.setBalance(money,ratio);
System.out.println("业务:"+zhengfu+" "+account6.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account6.getBalance())); } }
if(matcher.group(1).equals("6222081502051320785")){
Account7 account7 = new Account7();
balance0 = account7.getBalance()-money-ratio*money;
if(balance0<0){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account7.setBalance(money,ratio);
System.out.println("业务:"+zhengfu+" "+account7.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account7.getBalance())); } }
if(matcher.group(1).equals("6222081502051320786")){
Account8 account8 = new Account8();
balance0 = account8.getBalance()-money-ratio*money;
if(balance0<0){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
account8.setBalance(money,ratio);
System.out.println("业务:"+zhengfu+" "+account8.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account8.getBalance())); } }
if(matcher.group(1).equals("6640000010045442002")||matcher.group(1).equals("6640000010045442003")){
Account9 account9 = new Account9();
loan = account9.getBalance()-money-ratio*money-k*money;
if(loan<(-50000.00)){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
if(loan<0){
account9.setBalance(money,ratio,k);
}else{
account9.setBalance(money,ratio); }
System.out.println("业务:"+zhengfu+" "+account9.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account9.getBalance())); }
}
if(matcher.group(1).equals("6640000010045441009")){
Account10 account10 = new Account10();
loan = account10.getBalance()-money-ratio*money-k*money;
if(loan<(-50000.00)){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
if(loan<0){
account10.setBalance(money,ratio,k);
}else{
account10.setBalance(money,ratio); }
System.out.println("业务:"+zhengfu+" "+account10.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account10.getBalance())); } }
if(matcher.group(1).equals("6630000010033431001")){
Account11 account11 = new Account11();
loan = account11.getBalance()-money-ratio*money-k*money;
if(loan<(-50000.00)){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
if(loan<0){
account11.setBalance(money,ratio,k);
}else{
account11.setBalance(money,ratio); }
System.out.println("业务:"+zhengfu+" "+account11.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account11.getBalance())); } }
if(matcher.group(1).equals("6630000010033431008")){
Account12 account12 = new Account12();
loan = account12.getBalance()-money-ratio*money-k*money;
if(loan<(-50000.00)){
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}else{
if(loan<0){
account12.setBalance(money,ratio,k);
}else{
account12.setBalance(money,ratio); }
System.out.println("业务:"+zhengfu+" "+account12.name+"在"+"中国工商银行的"+matcher.group(3)+"号ATM机上"+zhengfu+"¥"+String.format("%.2f",Math.abs(money)));
System.out.println("当前余额为¥"+String.format("%.2f",account12.getBalance())); }}
}else{
System.exit(0); }
}else if(everydata[i]!=null){
if(everydata[i].equals("6217000010041315709")||everydata[i].equals("6217000010041315715")){
Account1 account1 = new Account1();
System.out.println("业务:查询余额 ¥"+String.format("%.2f",account1.getBalance())); }
if(everydata[i].equals("6217000010041315718")){
Account2 account2 = new Account2();
System.out.println("业务:查询余额 ¥"+String.format("%.2f",account2.getBalance())); }
if(everydata[i].equals("6217000010051320007")){
Account3 account3 = new Account3();
System.out.println("业务:查询余额 ¥"+String.format("%.2f",account3.getBalance())); }
if(everydata[i].equals("6222081502001312389")){
Account4 account4 = new Account4();
System.out.println("业务:查询余额 ¥"+String.format("%.2f",account4.getBalance())); }
if(everydata[i].equals("6222081502001312390")){
Account5 account5 = new Account5();
System.out.println("业务:查询余额 ¥"+String.format("%.2f",account5.getBalance())); }
if(everydata[i].equals("6222081502001312399")||everydata[i].equals("6222081502001312400")){
Account6 account6 = new Account6();
System.out.println("业务:查询余额 ¥"+String.format("%.2f",account6.getBalance())); }
if(everydata[i].equals("6222081502051320785")){
Account7 account7 = new Account7();
System.out.println("业务:查询余额 ¥"+String.format("%.2f",account7.getBalance())); }
if(everydata[i].equals("6222081502051320786")){
Account8 account8 = new Account8();
System.out.println("业务:查询余额 ¥"+String.format("%.2f",account8.getBalance())); }
if(everydata[i].equals("6640000010045442002")||everydata[i].equals("6640000010045442003")){
Account9 account9 = new Account9();
System.out.println("业务:查询余额 ¥"+String.format("%.2f",account9.getBalance())); }
if(everydata[i].equals("6640000010045441009")){
Account10 account10 = new Account10();
System.out.println("业务:查询余额 ¥"+String.format("%.2f",account10.getBalance())); }
if(everydata[i].equals("6630000010033431001")){
Account11 account11 = new Account11();
System.out.println("业务:查询余额 ¥"+String.format("%.2f",account11.getBalance())); }
if(everydata[i].equals("6630000010033431008")){
Account12 account12 = new Account12();
System.out.println("业务:查询余额 ¥"+String.format("%.2f",account12.getBalance())); }
}
}
}
}
class CheckData {
String id,password,atmid,money;
private double ratio;
public CheckData() {
}
public CheckData(String id,String password,String atmid,String money) {
this.id = id;
this.password = password;
this.atmid = atmid;
this.money = money;
}
public void setRatio(double ratio) {
this.ratio = ratio;
}
public double getRatio(){
return ratio;
}
public boolean validataDate() {
int flag1 = 0, flag2 = 0;
int key = -1;
String[] id = {"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007","6640000010045442002","6640000010045442003","6222081502001312389","6222081502001312390", "6222081502001312399", "6222081502001312400", "6222081502051320785", "6222081502051320786","6640000010045441009","6630000010033431001","6630000010033431008"};
String[] atmid = {"01", "02", "03", "04", "05", "06","07","08","09","10","11"};
for(int i = 0 ;i<id.length; i++){
if(this.id.equals(id[i])){
flag1 = 1;
key = i;
break; } }
if(flag1==1){
if(this.password.equals("88888888")){
for (int i = 0; i < atmid.length; i++) {
if (this.atmid.equals(atmid[i])) {
flag2 = 1;
break; }
}
if(flag2==1){
if (key == 0 || key == 1 || key == 2 || key == 3||key == 4||key == 5) {
if(this.atmid.equals("07")||this.atmid.equals("08")||this.atmid.equals("09")||this.atmid.equals("10")||this.atmid.equals("11")) {
this.setRatio(0.04);
return true;
}else if(this.atmid.equals("05")||this.atmid.equals("06")){
this.setRatio(0.03);
return true;
}else{
this.setRatio(0.00);
return true;
}
}
else if(key == 6||key == 7||key == 8||key == 9||key == 10||key == 11||key == 12){
if(this.atmid.equals("07")||this.atmid.equals("08")||this.atmid.equals("09")||this.atmid.equals("10")||this.atmid.equals("11")){
this.setRatio(0.04);
return true;
}else if(this.atmid.equals("01")|| this.atmid.equals("02")|| this.atmid.equals("03")|| this.atmid.equals("04")){
this.setRatio(0.02);
return true; }
else{
this.setRatio(0.00);
return true;
}
}
else{
if (this.atmid.equals("05")|| this.atmid.equals("06")) {
this.setRatio(0.03);
return true;
} else if(this.atmid.equals("01")|| this.atmid.equals("02")|| this.atmid.equals("03")|| this.atmid.equals("04")){
this.setRatio(0.02);
return true;
}
else{
this.setRatio(0.00);
return true;
}
}
}else{
System.out.println("Sorry,the ATM's id is wrong.");
return false; }
}else{
System.out.println("Sorry,your password is wrong.");
return false;
}
}else{
System.out.println("Sorry,this card does not exist.");
return false; }
}
}
class Account1{
String name = "杨过";
private static double balance = 10000.00;
public Account1(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money,double ratio){
balance = balance-money-ratio*money;
}
}
class Account2{
String name = "杨过";
private static double balance = 10000.00;
public Account2(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money,double ratio){
balance = balance-money-ratio*money;
}
}
class Account3{
String name = "郭靖";
private static double balance = 10000.00;
public Account3(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money,double ratio){
balance = balance-money-ratio*money;
}
}
class Account4{
String name = "张无忌";
private static double balance = 10000.00;
public Account4(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money,double ratio){
balance = balance-money-ratio*money;
}
}
class Account5{
String name = "张无忌";
private static double balance = 10000.00;
public Account5(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money,double ratio){
balance = balance-money-ratio*money;
}
}
class Account6{
String name = "张无忌";
private static double balance = 10000.00;
public Account6(){
}
public double getBalance(){
return balance;
}
public void setBalance(double money,double ratio){
balance = balance-money-ratio*money;
}
}
class Account7{
String name = "韦小宝";
private static double balance = 10000.00;
public Account7(){}
public double getBalance(){
return balance;}
public void setBalance(double money,double ratio){
balance = balance-money-ratio*money;}
}
class Account8{
String name = "韦小宝";
private static double balance = 10000.00;
public Account8(){}
public double getBalance(){
return balance;}
public void setBalance(double money,double ratio){
balance = balance-money-ratio*money;}
}
class Account9{
String name = "张三丰";
private static double balance = 10000.00;
public Account9(){
}
public double getBalance(){
return balance;}
public void setBalance(double money,double ratio){
balance = balance-money-ratio*money;}
public void setBalance(double money,double ratio,double k){
if(getBalance()>=0){ balance = 0.00-ratio*money-(money-balance)*(k+1); }else{
balance = balance-money-ratio*money-k*money;
}}
}
class Account10{
String name = "令狐冲";
private static double balance = 10000.00;
public Account10(){
}
public double getBalance(){
return balance;}
public void setBalance(double money,double ratio){
balance = balance-money-ratio*money;}
public void setBalance(double money,double ratio,double k){
if(getBalance()>=0){ balance = 0.00-ratio*money-(money-balance)*(k+1); }else{
balance = balance-money-ratio*money-k*money;
}}
}
class Account11{
String name = "乔峰";
private static double balance = 10000.00;
public Account11(){
}
public double getBalance(){
return balance;}
public void setBalance(double money,double ratio){
balance = balance-money-ratio*money;}
public void setBalance(double money,double ratio,double k){
if(getBalance()>=0){ balance = 0.00-ratio*money-(money-balance)*(k+1); }else{
balance = balance-money-ratio*money-k*money;
}}
}
class Account12{
String name = "洪七公";
private static double balance = 10000.00;
public Account12(){}
public double getBalance(){
return balance;}
public void setBalance(double money,double ratio){
balance = balance-money-ratio*money;}
public void setBalance(double money,double ratio,double k){
if(getBalance()>=0){ balance = 0.00-ratio*money-(money-balance)*(k+1); }else{
balance = balance-money-ratio*money-k*money;}}
}

(3)采坑心得:对源码的提交过程中出现的问题及心得进行总结,务必做到详实,拿数据、源码及测试结果说话,切忌假大空

直说吧,两道题我都没过,只是碰着测试点拿分的。第一题我简直懵逼,明明对于id数组我没弄错,但是第一个6217000010041315709就是出不来,也有人说是我多个账号衔接不上。我就emmm了。第二题也是同样的错误,测试点要求除了6217000010041315709我是全过了。在同一个坑摔两次,还不知道原因就......。

然后呢,就这样吧,毕竟没拿全分,没啥话语权。

(4)改进建议:对相应题目的编码改进给出自己的见解,做到可持续

多看视频,多写代码。

瞧瞧自己的代码,彷佛看到一团shit。

代码复用?在我这里,每次都字数越界。

然后,我也没有更好的意见了,多练吧。

(5)总结:对本阶段三次题目集的综合性总结,学到了什么,哪些地方需要进一步学习及研究,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见。

这几次的综合性练习,我学到的东西比前几次加起来都多。对很多地方比如arraylist、hashmap、iterator之类都不太熟悉,要进一步加强。

老师课堂上会照顾到学生作业完成了,比一个人瞎摸索肯定是有进步的,课程也逐渐由浅入深、逐层深进。

实验呢,也跟着课堂学的东西走,算是与时俱进吧。

课下学习还是太少了,需要改进!

posted @ 2021-06-19 20:54  be1109  阅读(61)  评论(0)    收藏  举报