极光Danisa

导航

第三阶段Blog作业--题目集7~9的总结性Blog

好的,这是我第二次在博客这种大型网站上发布我对于java的认知和感悟,新手上路,请多关照,若有错误之处,望君谅解并指出。废话不多说,现在就总结一下题目集7~9的部分做过的题及遇到的困难及总结 。

 

 

1. 题目集7(7-1)

1.这道题考察的是根据用户输入的数字和图形数据来创建不同图形对象并把不同图形的面积大小计算并进行比较得出结果并显示,因为涉及到图形对象的面积比较和用户输入需要创建的图形数未知的原因,故需创建 ArrayList<shape> arrayList 进行存储未知数量的图形对象,并利用比较器对其中创建图形对象的面积进行排序,然后对数据的输出格式进行调试,这道题唯一的难点就是图形比较器的创建,其余抽象类的继承和创建都很基础(三角形的面积公式之前有写)。

 

 

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  下面为源码:

 

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Iterator;
import java.util.*;

 

public class Main {

public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Scanner input =new Scanner(System.in);

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();
}
GArrayList g=new GArrayList();
for (Integer integer : list) {
switch (integer){
case 1:
Circle ch1 = new Circle(input.nextDouble());
if(!ch1.valid())
{
System.out.println("Wrong Format");
return ;
}
else
g.m(ch1);
break;
case 2:
Rectangle k= new Rectangle(input.nextDouble(),input.nextDouble());
if(!k.valid())
{
System.out.println("Wrong Format");
return;
}
else
g.m(k);

break;
case 3:
Triangle j= new Triangle(input.nextDouble(),input.nextDouble(),input.nextDouble());
if(!j.valid())
{
System.out.println("Wrong Format");
return;
}
else
g.m(j);
break;
case 4:
Trapezoid l=new Trapezoid(input.nextDouble(),input.nextDouble(),input.nextDouble());
if(!l.valid())
{
System.out.println("Wrong Format");
return;
}
g.m(l);
break;
}
}
// 到了这里就可以进行输出的处理结果数据

//ArrayList<Integer> l1 = new ArrayList<>(); Iterator it = l1.iterator();
System.out.println("The original list:");
g.outGshape();
System.out.println();
System.out.println("The sorted list:");
g.inGshape();
System.out.println();
g.sumarea();
}
}
class GArrayList {
ArrayList<shape> arrayList = new ArrayList<>();
DecimalFormat df = new DecimalFormat("0.00");
public void m(shape e){
arrayList.add(e);

}
public void outGshape(){
for(shape e: this.arrayList){
System.out.print(e.toString());
}
}
public void inGshape() {

Collections.sort(arrayList,new Comparator<shape>(){
@Override
public int compare(shape o1, shape o2) {
return o1.getarea()<o2.getarea()?1:-1;
}
});
for(shape s : this.arrayList) {
System.out.print(s.toString());
}
}
public void sumarea() {
double sum=0;
for(shape k: this.arrayList) {
sum=sum+k.getarea();
}
System.out.print("Sum of area:");
System.out.printf("%.2f",sum);
}
}
abstract class shape {
public abstract double getarea();
public abstract boolean valid();
DecimalFormat df = new DecimalFormat("0.00");
String name = "null";

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return this.getName()+ ":"+df.format(this.getarea())+" ";
}

}
class Circle extends shape {
String name= "Circle";
double r;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Circle(double r) {
// TODO 自动生成的构造函数存根
this.r=r;
}
public double getarea() {

return Math.PI*this.r*this.r ;
}
public boolean valid() {
if(this.r<0)
return false;
else
return true;
}
}
class Rectangle extends shape {
double chang;
double kuan;
String name = "Rectangle";
public Rectangle(double chang,double kuan) {
// TODO 自动生成的构造函数存根
this.chang=chang;
this.kuan=kuan;
}
public double getarea() {
return chang*kuan;
}
public boolean valid() {
if(this.chang<0||this.kuan<0)
{
return false;
}
else
return true;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}
class Trapezoid extends shape {
double top;
double bottle;
double tall;
String name = "Trapezoid";
public Trapezoid(double bian1,double bian2, double bian3) {
// TODO 自动生成的构造函数存根
this.top=bian1;
this.bottle=bian2;
this.tall=bian3;
}

public double getarea() {

return (top+bottle)*tall/2;
}

@Override
public boolean valid() {
if(top>=0&&bottle>=0&&tall>=0)
return true;
else
return false;
}

public String getName() {
return name;
}

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

}
class Triangle extends shape {
double bian1;
double bian2;
double bian3;
String name = "Triangle";
public Triangle(double bian1,double bian2, double bian3) {
// TODO 自动生成的构造函数存根
this.bian1=bian1;
this.bian2=bian2;
this.bian3=bian3;
}
public boolean valid() {

if(bian1>0&&bian2>0&&bian3>0&&(bian1+bian2>bian3)&&(bian1+bian3>bian2)&&(bian2+bian3>bian1))
{
return true;
}
else
return false;
}
public double getarea() {
double q=(bian1+bian2+bian3)/2;
return Math.sqrt(q*(q-bian1)*(q-bian2)*(q-bian3));
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

 

 

 

2.题目集7(7-2)

1.这道题是7-1的升级版,它在7-1的基础上增加了对各个图形类的统一输出,且在之后对各个图形类进行面积比较排序后输出,输出格式也比7-1的复杂一些,但这道题目的核心和7-1是一样的,我的做法是在7-1的基础上增加判断条件将各个图形类存储到逐一的ArrayList<shape> arrayList中,在进行排序后输出即可,在写这道题时输出格式中的[]的要求看错了花了一些时间。 

                           

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

 

 

 

 

 

 

 

下面为代码:

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

 


public class Main {

public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Scanner input =new Scanner(System.in);

int num = input.nextInt();
if(num==0){
System.out.print("Wrong Format");
return;
}
while(num != 0){
if(num < 0 || num > 4){
System.out.println("Wrong Format");
System.exit(0);
}
list.add(num);
num = input.nextInt();
}
GArrayList g=new GArrayList();
for (Integer integer : list) {
switch (integer){
case 1:
Circle ch1 = new Circle(input.nextDouble());
if(!ch1.valid())
{
System.out.println("Wrong Format");
return ;
}
else
g.m(ch1);
break;
case 2:
Rectangle k= new Rectangle(input.nextDouble(),input.nextDouble());
if(!k.valid())
{
System.out.println("Wrong Format");
return;
}
else
g.m(k);

break;
case 3:
Triangle j= new Triangle(input.nextDouble(),input.nextDouble(),input.nextDouble());
if(!j.valid())
{
System.out.println("Wrong Format");
return;
}
else
g.m(j);
break;
case 4:
Trapezoid l=new Trapezoid(input.nextDouble(),input.nextDouble(),input.nextDouble());
if(!l.valid())
{
System.out.println("Wrong Format");
return;
}
g.m(l);
break;
}
}
// 到了这里就可以进行输出的处理结果数据

//ArrayList<Integer> l1 = new ArrayList<>(); Iterator it = l1.iterator();
System.out.println("The original list:");
System.out.print("[");
g.outGshape();
System.out.print("]");
System.out.println();
System.out.println("The Separated List:");
g.classsshape();
System.out.println();
System.out.println("The Separated sorted List:");
g.inGshape();
System.out.println();
// g.sumarea();
}
}
class GArrayList {

ArrayList<shape> arrayList = new ArrayList<>();
DecimalFormat df = new DecimalFormat("0.00");
public void m(shape e){
arrayList.add(e);

}

public void classsshape() {
int c=0;
System.out.print("[");
for(int ss=0;ss<arrayList.size();ss++) {
if(arrayList.get(ss).getName().equals("Circle"))
{
System.out.print(arrayList.get(ss).toString());
c++;

}
}

System.out.print("]");

System.out.print("[");
for( int gs=0;gs<arrayList.size();gs++) {
if(arrayList.get(gs).getName().equals("Rectangle"))
{

System.out.print(arrayList.get(gs).toString());
c++;

}
}

System.out.print("]");
System.out.print("[");
for(int gs=0;gs<arrayList.size();gs++) {
if(arrayList.get(gs).getName().equals("Triangle"))
{


System.out.print(arrayList.get(gs).toString());
c++;

}
}

System.out.print("]");
System.out.print("[");
for(int gs=0;gs<arrayList.size();gs++) {
if(arrayList.get(gs).getName().equals("Trapezoid"))
{

System.out.print(arrayList.get(gs).toString());
c++;

}
}

System.out.print("]");

}

public void outGshape(){
for(shape e: this.arrayList){
System.out.print(e.toString());
}
}
public void inGshape() {
ArrayList<shape> a = new ArrayList<>();
ArrayList<shape> b = new ArrayList<>();
ArrayList<shape> c = new ArrayList<>();
ArrayList<shape> d = new ArrayList<>();
for(shape s : this.arrayList) {
if(s.getName().equals("Circle"))
{
a.add(s);
}
else if(s.getName().equals("Rectangle"))
{
b.add(s);
}
else if(s.getName().equals("Triangle"))
{
c.add(s);
}
else if(s.getName().equals("Trapezoid"))
{
d.add(s);
}

}
Collections.sort(a,new Comparator<shape>(){
@Override
public int compare(shape o1, shape o2) {

return o1.getarea()<o2.getarea()?1:-1;
}
});

double aa=0;
double bb=0;
double cc=0;
double dd=0;
System.out.print("[");
for(int ss=0;ss<a.size();ss++) {

aa=aa+a.get(ss).getarea();

System.out.print(a.get(ss).toString());
}

System.out.print("]");
Collections.sort(b,new Comparator<shape>(){
@Override
public int compare(shape o1, shape o2) {

return o1.getarea()<o2.getarea()?1:-1;
}
});
System.out.print("[");
for(int ss=0;ss<b.size();ss++) {

bb=bb+b.get(ss).getarea();

System.out.print(b.get(ss).toString());
}

System.out.print("]");


Collections.sort(c,new Comparator<shape>(){
@Override
public int compare(shape o1, shape o2) {

return o1.getarea()<o2.getarea()?1:-1;
}
});
System.out.print("[");
for(int ss=0;ss<c.size();ss++) {

cc=cc+c.get(ss).getarea();

System.out.print(c.get(ss).toString());
}

System.out.print("]");


Collections.sort(d,new Comparator<shape>(){
@Override
public int compare(shape o1, shape o2) {

return o1.getarea()<o2.getarea()?1:-1;
}
});
System.out.print("[");
for(int ss=0;ss<d.size();ss++) {
dd=dd+d.get(ss).getarea();

System.out.print(d.get(ss).toString());
}

System.out.print("]");

double sum=0;

if(sum<aa)
sum=aa;
if(sum<bb)
sum=bb;
if(sum<cc)
sum=cc;
if(sum<dd)
sum=dd;
System.out.println();
System.out.print("The max area:");
System.out.printf("%.2f",sum);
}

}
abstract class shape {
public abstract double getarea();
public abstract boolean valid();
DecimalFormat df = new DecimalFormat("0.00");
String name = "null";

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return this.getName()+ ":"+df.format(this.getarea())+" ";
}


}
class Circle extends shape {
String name= "Circle";
double r;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Circle(double r) {
// TODO 自动生成的构造函数存根
this.r=r;
}
public double getarea() {

return Math.PI*this.r*this.r ;
}
public boolean valid() {
if(this.r<0)
return false;
else
return true;
}
}
class Rectangle extends shape {
double chang;
double kuan;
String name = "Rectangle";
public Rectangle(double chang,double kuan) {
// TODO 自动生成的构造函数存根
this.chang=chang;
this.kuan=kuan;
}
public double getarea() {
return chang*kuan;
}
public boolean valid() {
if(this.chang<0||this.kuan<0)
{
return false;
}
else
return true;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}
class Trapezoid extends shape {
double top;
double bottle;
double tall;
String name = "Trapezoid";
public Trapezoid(double bian1,double bian2, double bian3) {
// TODO 自动生成的构造函数存根
this.top=bian1;
this.bottle=bian2;
this.tall=bian3;
}

public double getarea() {

return (top+bottle)*tall/2;
}

@Override
public boolean valid() {
if(top>=0&&bottle>=0&&tall>=0)
return true;
else
return false;
}

public String getName() {
return name;
}

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

}
class Triangle extends shape {
double bian1;
double bian2;
double bian3;
String name = "Triangle";
public Triangle(double bian1,double bian2, double bian3) {
// TODO 自动生成的构造函数存根
this.bian1=bian1;
this.bian2=bian2;
this.bian3=bian3;
}
public boolean valid() {

if(bian1>0&&bian2>0&&bian3>0&&(bian1+bian2>bian3)&&(bian1+bian3>bian2)&&(bian2+bian3>bian1))
{
return true;
}
else
return false;
}
public double getarea() {
double q=(bian1+bian2+bian3)/2;
return Math.sqrt(q*(q-bian1)*(q-bian2)*(q-bian3));
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

 

 

3.题目集8(7-1)

1.这道题说到底就是搞清银行,账户,用户,ATM机,卡号之间的包含关系,经过一番思索,我设置了建行、工行两个银行类,它们包含各自的账户arraylist 和ATM arraylist,在账户arraylist中不但包含用户的姓名和其他基本信息,也包含了卡号arraylist,这些arraylist中卡号共用一个账户中的金额,基本的取款和存款方法放在银行类中,跨行的判断是先判断输入的卡号前5位的数字所属于哪类银行,再遍历该银行帐户列表中的ATM机列表,如果没有输入ATM机数字的匹配对象,再进行另一银行的ATM机列表匹配,若有则进行先相应的跨行服务,若无,输出错误。

 

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

 

 

 

 

下面为代码:

import java.util.ArrayList;
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner input =new Scanner(System.in);
ArrayList<String> listinput = new ArrayList<>();
String num = input.nextLine();
while(!num.equals("#")){

listinput.add(num);
num = input.nextLine();
}

ArrayList<String> listcard1 = new ArrayList<>();
listcard1.add("6217000010041315709");
listcard1.add("6217000010041315715");
Card a1=new Card();
a1.setsum(listcard1,10000);
ArrayList<String> listcard2 = new ArrayList<>();
listcard2.add("6217000010041315718");
Card a2=new Card();
a2.setsum(listcard2,10000);
ArrayList<Card> account1 = new ArrayList<>();
account1.add(a2);
account1.add(a1);
Account accountsum1=new Account();
accountsum1.setsum(account1,"杨过");

ArrayList<String> listcard11 = new ArrayList<>();
listcard11.add("6217000010051320007");
Card a11=new Card();
a11.setsum(listcard1,10000);
ArrayList<Card> account11 = new ArrayList<>();
account11.add(a11);
Account accountsum11=new Account();
accountsum11.setsum(account11,"郭靖");

ArrayList<Account> listaccount1=new ArrayList<>();
listaccount1.add(accountsum1);
listaccount1.add(accountsum11);

ArrayList<Atm> listatm1=new ArrayList<>();
Atm atm1=new Atm();
atm1.setsum("01", "中国建设银行");
listatm1.add(atm1);

Atm atm2=new Atm();
atm2.setsum("02", "中国建设银行");
listatm1.add(atm2);

Atm atm3=new Atm();
atm3.setsum("03", "中国建设银行");
listatm1.add(atm3);

Atm atm4=new Atm();
atm4.setsum("04", "中国建设银行");
listatm1.add(atm4);

Bank bank1=new Bank();
bank1.setsum("中国建设银行",listaccount1,listatm1);

ArrayList<String> listcardq1 = new ArrayList<>();
listcardq1.add("6222081502001312389");
Card q1=new Card();
q1.setsum(listcardq1,10000);
ArrayList<String> listcardq2 = new ArrayList<>();
listcardq2.add("6222081502001312390");
Card q2=new Card();
q2.setsum(listcardq2,10000);
ArrayList<String> listcardq3 = new ArrayList<>();
listcardq3.add("6222081502001312399");
listcardq3.add("6222081502001312400");
Card q3=new Card();
q3.setsum(listcardq3,10000);
ArrayList<Card> accountq1 = new ArrayList<>();
accountq1.add(q1);
accountq1.add(q2);
accountq1.add(q3);
Account accountsumq1=new Account();
accountsumq1.setsum(accountq1,"张无忌");

ArrayList<String> listcards1 = new ArrayList<>();
listcards1.add("6222081502051320785");
Card s1=new Card();
s1.setsum(listcards1,10000);
ArrayList<String> listcards2 = new ArrayList<>();
listcards2.add("6222081502051320786");
Card s2=new Card();
s2.setsum(listcards2,10000);
ArrayList<Card> accounts1 = new ArrayList<>();
accounts1.add(s1);
accounts1.add(s2);
Account accountsums1=new Account();
accountsums1.setsum(accounts1,"韦小宝");

ArrayList<Account> listaccountq1=new ArrayList<>();
listaccountq1.add(accountsumq1);
listaccountq1.add(accountsums1);

ArrayList<Atm> listatmq1=new ArrayList<>();
Atm atmq1=new Atm();
atmq1.setsum("05", "中国工商银行");
listatmq1.add(atmq1);

Atm atmq2=new Atm();
atmq2.setsum("06", "中国工商银行");
listatmq1.add(atmq2);


Bank bankq1=new Bank();
bankq1.setsum("中国工商银行",listaccountq1,listatmq1);
for(String kk:listinput) {
String kk1[]=kk.split("\\s+");
//for(int a=0;a<kk1.length;a++)
//System.out.println(kk1[a]);
if(kk1[0].substring(0,4).equals("6217"))
{
if(kk1.length==1)
bank1.chabalance(kk1[0]);
else
bank1.cunqumony(kk1[0],kk1[1],kk1[2],kk1[3]);
}
else
{
if(kk1.length==1)
bankq1.chabalance(kk1[0]);
else
{

bankq1.cunqumony(kk1[0],kk1[1],kk1[2],kk1[3]);

}
}

}

}

}

class Account {
private ArrayList<Card> listcard;
private String peoplename;
public void setsum(ArrayList<Card> listcard,String peoplename){
this.listcard=listcard;
this.peoplename=peoplename;
}

public ArrayList<Card> getListcard() {
return listcard;
}
public void setListcard(ArrayList<Card> listcard) {
this.listcard = listcard;
}
public String getPeoplename() {
return peoplename;
}
public void setPeoplename(String peoplename) {
this.peoplename = peoplename;
}

}

class Atm {
private String num;
private String bankname;
public void setsum(String num,String bankname) {
this.bankname=bankname;
this.num=num;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getBankname() {
return bankname;
}
public void setBankname(String bankname) {
this.bankname = bankname;
}
}

class Bank {
private String bankname;
private ArrayList<Account> listaccount ;
private ArrayList<Atm> listatm;
Account findaccount;
Card findcard;

public void setsum(String bankname,ArrayList<Account> listaccount, ArrayList<Atm> listatm) {

this.bankname=bankname;
this.listaccount=listaccount;
this.listatm=listatm;
}
public String getBankname() {
return bankname;
}
public void setBankname(String bankname) {
this.bankname = bankname;
}
public ArrayList<Account> getListaccount() {
return listaccount;
}
public void setListaccount(ArrayList<Account> listaccount) {
this.listaccount = listaccount;
}
public ArrayList<Atm> getListatm() {
return listatm;
}
public void setListatm(ArrayList<Atm> listatm) {
this.listatm = listatm;
}
/*
* 查找卡是否存在
*/

public boolean cheackcardmoney(String cardnum) {
for(Account a:this.listaccount)
{
for(Card b:a.getListcard())
{
for(String c:b.getList())
{
if(c.equals(cardnum))
{
findcard=b;
findaccount=a;
return true;
}

}
}
}
//System.out.println("Sorry,this card does not exist.");
return false;
}
/*
* 查找所属atm是否存在
*/
public boolean cheackatmnum(String atmnum) {
for(Atm s: listatm)
{
if(s.getNum().equals(atmnum))
{
return true;
}
}
return false;
}
/*
* 查找密码是否匹配
*/

public boolean cheackmima(String mima) {
if(findcard.getMima().equals(mima))
{
return true;
}
else
return false;
}

/*
* 查询余额(输入只有卡号)
*/
public void chabalance(String cardnum) {
if(cheackcardmoney(cardnum))
{
System.out.printf("¥%.2f",findcard.getBalance());
System.out.println();
}

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

}

/*
* 取钱或存钱功能
* 检验输入卡号,atm编号,密码有效性,判断卡余额与输入金额,存取钱
*/
public void cunqumony(String cardnum,String mima,String atmnum,String money)
{
Double i = Double.parseDouble(money);
if(!cheackcardmoney(cardnum))
{
System.out.println("Sorry,this card does not exist.");
return;
}
else if(cheackcardmoney(cardnum)&&!cheackmima(mima))
{
//System.out.println(mima);
//System.out.println(findcard.getMima());
System.out.println("Sorry,your password is wrong.");
return;
}
else if(cheackcardmoney(cardnum)&&cheackmima(mima)&&!cheackatmnum(atmnum))
{
System.out.println("Sorry,the ATM's id is wrong.");
return;
}
else if(cheackcardmoney(cardnum)&&cheackmima(mima)&&cheackatmnum(atmnum))
{
if(i<=0)
{
findcard.changemoney(i);
System.out.print(findaccount.getPeoplename()+"在"+this.bankname+"的"+atmnum+"号ATM机上存款¥");
System.out.printf("%.2f", -i);
System.out.println();
System.out.printf("当前余额为¥");
System.out.printf("%.2f",findcard.getBalance());
System.out.println();
return;
}
else if(i>0)
{
if(i>findcard.getBalance())
{
System.out.println("Sorry,your account balance is insufficient.");
return;
}
else
{
findcard.changemoney(i);
System.out.print(findaccount.getPeoplename()+"在"+this.bankname+"的"+atmnum+"号ATM机上取款¥");
System.out.printf("%.2f",i);
System.out.println();
System.out.print("当前余额为¥");
System.out.printf("%.2f",findcard.getBalance());
System.out.println();
return;
}
}
}

}

}
class Card {
private ArrayList<String> list;
private double balance;
private String mima="88888888";

public ArrayList<String> getList() {
return list;
}
public void setsum(ArrayList<String> list,int balance) {
this.list=list;
this.balance=balance;
}

public void setList(ArrayList<String> list) {
this.list = list;
}
public double getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}

public String getMima() {
return mima;
}
public void setMima(String mima) {
this.mima = mima;
}
public void changemoney(Double i) {
// TODO 自动生成的方法存根
this.balance=this.balance-i;
}

}

 

4.题目集9(7-1)

1.这道题是题目集8的升级版,增加了农行,新的用户、跨行业务需要计算额外的百分比费用,另外还添加了透支业务,计算倒是没什么问题,但这个收取百分比的费用我有些不懂,我按照题目所说的百分比计算得出的结果和pta上的测试结果不一样,我很无奈,另外我写的程序也有一个问题,是取款后查询该账户余额它还是一万,我以为它没保存,但在多次取款中却没有这个问题,我不知道出现了什么问题,这道题没怎么写好。

                                        

 

 

 

 

下面为代码:

import java.util.ArrayList;
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner input =new Scanner(System.in);
ArrayList<String> listinput = new ArrayList<>();
String num = input.nextLine();
while(!num.equals("#")){

listinput.add(num);
num = input.nextLine();
}

ArrayList<String> listcard1 = new ArrayList<>();
listcard1.add("6217000010041315709");
listcard1.add("6217000010041315715");
Card a1=new Card();
a1.setsum(listcard1,10000);
ArrayList<String> listcard2 = new ArrayList<>();
listcard2.add("6217000010041315718");
Card a2=new Card();
a2.setsum(listcard2,10000);
ArrayList<Card> account1 = new ArrayList<>();
account1.add(a2);
account1.add(a1);
Account accountsum1=new Account();
accountsum1.setsum(account1,"杨过","借记卡");

ArrayList<String> listcard11 = new ArrayList<>();
listcard11.add("6217000010051320007");
Card a11=new Card();
a11.setsum(listcard1,10000);
ArrayList<Card> account11 = new ArrayList<>();
account11.add(a11);
Account accountsum11=new Account();
accountsum11.setsum(account11,"郭靖","借记卡");

ArrayList<String> listcard3 =new ArrayList<>();
listcard3.add("6640000010045442002");
listcard3.add("6640000010045442003");
Card a3=new Card();
a3.setsum(listcard3, 10000);
ArrayList<Card> account3 = new ArrayList<>();
account3.add(a3);
Account accountsum3=new Account();
accountsum3.setsum(account3,"张三丰","贷记卡");// 加入张三丰


ArrayList<Account> listaccount1=new ArrayList<>();
listaccount1.add(accountsum1);
listaccount1.add(accountsum11);
listaccount1.add(accountsum3);

ArrayList<Atm> listatm1=new ArrayList<>();
Atm atm1=new Atm();
atm1.setsum("01", "中国建设银行");
listatm1.add(atm1);

Atm atm2=new Atm();
atm2.setsum("02", "中国建设银行");
listatm1.add(atm2);

Atm atm3=new Atm();
atm3.setsum("03", "中国建设银行");
listatm1.add(atm3);

Atm atm4=new Atm();
atm4.setsum("04", "中国建设银行");
listatm1.add(atm4);

Bank bank1=new Bank();
bank1.setsum("中国建设银行",listaccount1,listatm1);// 建设银行

ArrayList<String> listcardq1 = new ArrayList<>();
listcardq1.add("6222081502001312389");
Card q1=new Card();
q1.setsum(listcardq1,10000);
ArrayList<String> listcardq2 = new ArrayList<>();
listcardq2.add("6222081502001312390");
Card q2=new Card();
q2.setsum(listcardq2,10000);
ArrayList<String> listcardq3 = new ArrayList<>();
listcardq3.add("6222081502001312399");
listcardq3.add("6222081502001312400");
Card q3=new Card();
q3.setsum(listcardq3,10000);
ArrayList<Card> accountq1 = new ArrayList<>();
accountq1.add(q1);
accountq1.add(q2);
accountq1.add(q3);
Account accountsumq1=new Account();
accountsumq1.setsum(accountq1,"张无忌","借记卡");

ArrayList<String> listcards1 = new ArrayList<>();
listcards1.add("6222081502051320785");
Card s1=new Card();
s1.setsum(listcards1,10000);
ArrayList<String> listcards2 = new ArrayList<>();
listcards2.add("6222081502051320786");
Card s2=new Card();
s2.setsum(listcards2,10000);
ArrayList<Card> accounts1 = new ArrayList<>();
accounts1.add(s1);
accounts1.add(s2);
Account accountsums1=new Account();
accountsums1.setsum(accounts1,"韦小宝","借记卡");

ArrayList<String> listcardss1 = new ArrayList<>();
listcardss1.add("6640000010045441009");
Card c1=new Card();
c1.setsum(listcardss1,10000);
ArrayList<Card> accountc1 = new ArrayList<>();
accountc1.add(c1);
Account accountsumsc1=new Account();
accountsumsc1.setsum(accountc1,"令狐冲","贷记卡");

ArrayList<Account> listaccountq1=new ArrayList<>();
listaccountq1.add(accountsumq1);
listaccountq1.add(accountsums1);
listaccountq1.add(accountsumsc1);// 加入令狐冲


ArrayList<Atm> listatmq1=new ArrayList<>();
Atm atmq1=new Atm();
atmq1.setsum("05", "中国工商银行");
listatmq1.add(atmq1);

Atm atmq2=new Atm();
atmq2.setsum("06", "中国工商银行");
listatmq1.add(atmq2);

boolean kong=false;
Bank bankq1=new Bank();
bankq1.setsum("中国工商银行",listaccountq1,listatmq1); //工商银行

ArrayList<String> listcarddd1 = new ArrayList<>();
listcarddd1.add("6630000010033431001");
Card d1=new Card();
d1.setsum(listcarddd1,10000);
ArrayList<Card> accountd1 = new ArrayList<>();
accountd1.add(d1);
Account accountsumsd1=new Account();
accountsumsd1.setsum(accountd1,"乔峰","贷记卡"); //加入乔峰

ArrayList<String> listcarddd2 = new ArrayList<>();
listcarddd2.add("6630000010033431008");
Card d2=new Card();
d2.setsum(listcarddd2,10000);
ArrayList<Card> accountd2 = new ArrayList<>();
accountd2.add(d2);
Account accountsumsd2=new Account();
accountsumsd2.setsum(accountd2,"洪七公","贷记卡"); //加入洪七公

ArrayList<Account> listaccountd1=new ArrayList<>();
listaccountd1.add(accountsumsd1);
listaccountd1.add(accountsumsd2);

ArrayList<Atm> listatmd1=new ArrayList<>();
Atm atmd1=new Atm();
atmd1.setsum("07", "中国农业银行");
listatmd1.add(atmd1);

Atm atmd2=new Atm();
atmd2.setsum("08", "中国农业银行");
listatmd1.add(atmd2);

Atm atmd3=new Atm();
atmd3.setsum("09", "中国农业银行");
listatmd1.add(atmd3);

Atm atmd4=new Atm();
atmd4.setsum("10", "中国农业银行");
listatmd1.add(atmd4);

Atm atmd5=new Atm();
atmd5.setsum("11", "中国农业银行");
listatmd1.add(atmd5);

Bank bankd1=new Bank();
bankd1.setsum("中国农业银行",listaccountd1,listatmd1); //农业银行



for(String kk:listinput) {
kong=false;
String kk1[]=kk.split("\\s+");
//for(int a=0;a<kk1.length;a++)
//System.out.println(kk1[a]);
if(kk1[0].substring(0,4).equals("6217")||kk1[0].substring(0,16).equals("6640000010045442"))
{
// System.out.println("kkkkkkkkkkkkk");
if(kk1.length==1)
{
bank1.chabalance(kk1[0]);
kong=true;
}
else if(bank1.cheackatmnum(kk1[2]))
{
// System.out.println("kkkkkkkkkkkkk");
bank1.cunqumony(kk1[0],kk1[1],kk1[2],kk1[3]);
kong=true;
}
else
{
for(Atm fg:bankq1.getListatm())
{//System.out.println("kkkkkkkkkkkkk");
if(fg.getNum().equals(kk1[2]))
{//System.out.println("kkkkkkkkkkkkk");
bank1.kuqumoney(kk1[0],kk1[1],0.03,kk1[3],kk1[2],"中国工商银行");
kong=true;
//System.out.println("kkkkkkkkkkkkk");
// return;
}
}
for(Atm fg:bankd1.getListatm())
{
if(fg.getNum().equals(kk1[2]))
{
bank1.kuqumoney(kk1[0],kk1[1],0.04,kk1[3],kk1[2],"中国农业银行");
kong=true;
//return;
}
}
if(kong==false)
System.out.println("Sorry,the ATM's id is wrong.");
// return ;
}
}
else if(kk1[0].substring(0,4).equals("6222")||kk1[0].substring(0,16).equals("6640000010045441"))
{
if(kk1.length==1)
bankq1.chabalance(kk1[0]);
else if(bankq1.cheackatmnum(kk1[2]))
{
bankq1.cunqumony(kk1[0],kk1[1],kk1[2],kk1[3]);
kong=true;
}
else
{
for(Atm fg:bank1.getListatm())//ban1
{
if(fg.getNum().equals(kk1[2]))
{
bankq1.kuqumoney(kk1[0],kk1[1] ,0.02 ,kk1[3] ,kk1[2],"中国建设银行");
kong=true;
//return;
}
}
for(Atm fg:bankd1.getListatm())//d1
{
if(fg.getNum().equals(kk1[2]))
{
bankq1.kuqumoney(kk1[0],kk1[1],0.04,kk1[3],kk1[2],"中国农业银行");
kong=true;
//return;
}
}
if(kong==false)
System.out.println("Sorry,the ATM's id is wrong.");
//return ;
}
}
else if(kk1[0].substring(0,4).equals("6630"))
{
if(kk1.length==1)
{
bankd1.chabalance(kk1[0]);
kong=true;
}
else if(bankd1.cheackatmnum(kk1[2]))
{
bankd1.cunqumony(kk1[0],kk1[1],kk1[2],kk1[3]);
kong=true;
}
else
{
for(Atm fg:bank1.getListatm())//ban1
{
if(fg.getNum().equals(kk1[2]))
{
bankd1.kuqumoney(kk1[0],kk1[1] ,0.02 ,kk1[3] ,kk1[2],"中国建设银行");
kong=true;
//return;
}
}
for(Atm fg:bankq1.getListatm())//d1
{
if(fg.getNum().equals(kk1[2]))
{
bankd1.kuqumoney(kk1[0],kk1[1],0.03,kk1[3],kk1[2],"中国工商银行");
kong=true;
//return;
}
}
if(kong==false)
System.out.println("Sorry,the ATM's id is wrong.");
//return ;
}
}

}

}

}
class Bank {
private String bankname;
private ArrayList<Account> listaccount ;
private ArrayList<Atm> listatm;
private double rate;
//private double rate;
Account findaccount;
Card findcard;

public void setsum(String bankname,ArrayList<Account> listaccount, ArrayList<Atm> listatm) {

this.bankname=bankname;
this.listaccount=listaccount;
this.listatm=listatm;
//this.rate=rate;
}
public String getBankname() {
return bankname;
}
public void setBankname(String bankname) {
this.bankname = bankname;
}
public ArrayList<Account> getListaccount() {
return listaccount;
}
public void setListaccount(ArrayList<Account> listaccount) {
this.listaccount = listaccount;
}
public ArrayList<Atm> getListatm() {
return listatm;
}
public void setListatm(ArrayList<Atm> listatm) {
this.listatm = listatm;
}
public double getrate() {
return this.rate;
}

/*
* 查找卡是否存在
*/

public boolean cheackcardmoney(String cardnum) {
for(Account a:this.listaccount)
{
for(Card b:a.getListcard())
{
for(String c:b.getList())
{
if(c.equals(cardnum))
{
findcard=b;
findaccount=a;
return true;
}

}
}
}
//System.out.println("Sorry,this card does not exist.");
return false;
}
/*
* 查找所属atm是否存在
*/
public boolean cheackatmnum(String atmnum) {
for(Atm s: listatm)
{
if(s.getNum().equals(atmnum))
{
return true;
}
}
return false;
}
/*
* 查找密码是否匹配
*/

public boolean cheackmima(String mima) {
if(findcard.getMima().equals(mima))
{
return true;
}
else
return false;
}

/*
* 查询余额(输入只有卡号)
*/
public void chabalance(String cardnum) {
if(cheackcardmoney(cardnum))
{
System.out.printf("业务:查询余额 "+"¥%.2f",findcard.getBalance());
}

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

}

/*
* 取钱或存钱功能
* 检验输入卡号,atm编号,密码有效性,判断卡余额与输入金额,存取钱
*/
public void cunqumony(String cardnum,String mima,String atmnum,String money)
{
Double i = Double.parseDouble(money);
if(!cheackcardmoney(cardnum))
{
System.out.println("Sorry,this card does not exist.");
return;
}
else if(cheackcardmoney(cardnum)&&!cheackmima(mima))
{
//System.out.println(mima);
//System.out.println(findcard.getMima());
System.out.println("Sorry,your password is wrong.");
return;
}
/*else if(cheackcardmoney(cardnum)&&cheackmima(mima)&&!cheackatmnum(atmnum))
{
System.out.println("Sorry,the ATM's id is wrong.");
return;
}*/
else if(cheackcardmoney(cardnum)&&cheackmima(mima)&&cheackatmnum(atmnum))//非跨行
{
if(i<=0)
{
findcard.changemoney(i);
System.out.print("业务:存款 "+findaccount.getPeoplename()+"在"+this.bankname+"的"+atmnum+"号ATM机上存款¥");
System.out.printf("%.2f", -i);
System.out.println();
System.out.printf("当前余额为¥");
System.out.printf("%.2f",findcard.getBalance());
System.out.println();
return;
}
else if(i>0)// 取钱
{
if(i>findcard.getBalance()) //取钱数大于账上余额,需透支
{
if(findaccount.getcardtype().equals("借记卡"))//为借记卡不能透支,非跨行
{
System.out.println("Sorry,your account balance is insufficient.");
return;
}
else if(i*(1+0.05)-findcard.getBalance()<=50000)// 否则为贷记卡且透支额度小于最大值
{
double a=0;
a= i*(1+0.05);
findcard.changemoney(a);
System.out.print("业务:取款 "+findaccount.getPeoplename()+"在"+this.bankname+"的"+atmnum+"号ATM机上取款¥");
System.out.printf("%.2f",i);
System.out.println();
System.out.print("当前余额为¥");
System.out.printf("%.2f",findcard.getBalance());
System.out.println();
return;
}
else
{
System.out.println("Sorry,your account balance is insufficient.");
return;
}
}
else //小于或等于账上余额 直接取
{
findcard.changemoney(i);
System.out.print("业务:取款 "+findaccount.getPeoplename()+"在"+this.bankname+"的"+atmnum+"号ATM机上取款¥");
System.out.printf("%.2f",i);
System.out.println();
System.out.print("当前余额为¥");
System.out.printf("%.2f",findcard.getBalance());
System.out.println();
return;
}
}
}

}
public void kuqumoney(String cardnum,String mima,double ratek,String money,String atmnum,String bankname)//跨行取款
{
//System.out.println("Sorry,this card does not exist.");
Double i = Double.parseDouble(money);
if(!cheackcardmoney(cardnum))
{
System.out.println("Sorry,this card does not exist.");
return;
}
else if(cheackcardmoney(cardnum)&&!cheackmima(mima))
{
//System.out.println(mima);
//System.out.println(findcard.getMima());
System.out.println("Sorry,your password is wrong.");
return;
}
else if(cheackcardmoney(cardnum)&&cheackmima(mima))
{
//System.out.println("Sorry,your password is wrong.");
if(findcard.getBalance()-i*(1+ratek)<0&&i*(1+0.05+ratek)-findcard.getBalance()<=50000)//需借但没达到最大
{

//System.out.println("Sorry,your password is wrong.");
if(findaccount.getcardtype().equals("贷记卡"))
{
double a=0;
a= i*(1+0.05+ratek);
findcard.changemoney(a);
System.out.print("业务:取款 "+findaccount.getPeoplename()+"在"+bankname+"的"+atmnum+"号ATM机上取款¥");
System.out.printf("%.2f",i);
System.out.println();
System.out.print("当前余额为¥");
System.out.printf("%.2f",findcard.getBalance());
System.out.println();
return;
}
else
{
System.out.println("Sorry,your account balance is insufficient.");
return ;
}
}
else if(findcard.getBalance()-i*(1+ratek)>=0)//不需要借
{
//System.out.println("sssssss");
double a=0;
a= i*(1+ratek);
findcard.changemoney(a);
System.out.print("业务:取款 "+findaccount.getPeoplename()+"在"+bankname+"的"+atmnum+"号ATM机上取款¥");
System.out.printf("%.2f",i);
System.out.println();
System.out.print("当前余额为¥");
System.out.printf("%.2f",findcard.getBalance());
System.out.println();
return;
}
else if(findcard.getBalance()-i*(1+ratek)<0&&i*(1+0.05+ratek)-findcard.getBalance()>50000)//需要借但达到最大
{
System.out.println("Sorry,your account balance is insufficient.");return;
}
}
}
}




class Card {
private ArrayList<String> list;
private double balance;
private String mima="88888888";

public ArrayList<String> getList() {
return list;
}
public void setsum(ArrayList<String> list,int balance) {
this.list=list;
this.balance=balance;
}

public void setList(ArrayList<String> list) {
this.list = list;
}
public double getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}

public String getMima() {
return mima;
}
public void setMima(String mima) {
this.mima = mima;
}
public void changemoney(double i) {
// TODO 自动生成的方法存根
this.balance=this.balance-i;
}

}

class Atm {
private String num;
private String bankname;
public void setsum(String num,String bankname) {
this.bankname=bankname;
this.num=num;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getBankname() {
return bankname;
}
public void setBankname(String bankname) {
this.bankname = bankname;
}

}
class Account {
private ArrayList<Card> listcard;
private String peoplename;
private String cardtype;
public void setsum(ArrayList<Card> listcard,String peoplename,String cardtype){
this.listcard=listcard;
this.peoplename=peoplename;
this.cardtype=cardtype;
}

public ArrayList<Card> getListcard() {
return listcard;
}
public void setListcard(ArrayList<Card> listcard) {
this.listcard = listcard;
}
public String getPeoplename() {
return peoplename;
}
public void setPeoplename(String peoplename) {
this.peoplename = peoplename;
}
public String getcardtype() {
return this.cardtype;
}

}

 

(3)踩坑心得;就这三次题目集来说由于输入数据的数量较多,调用函数的次数较多,一旦出现错误很难发现

关于类的继承则要注意父类的编写,要注意其中父类方法与数据的类型,以防止子类在继承中的顺利使用。

(4)改进建议:我觉得这三次的题目的代码都具有比较大的可扩展性,但是我学的不多,还需要进一步扩展。

(5)总结:就这三次的题目集写下来,虽然有些题目的难度较大,但毕竟这是一个循序渐进的过程,到了后期题目难度明显的下降了,但我觉得我学到的东西也变多了,慢慢的了解了JAVA的运作方式,用对象来完成工作,因此重要之处就在于如何编写类,类的结构因该要如何。总的来说我要学的东西还有许多。

 

posted on 2021-06-20 23:45  极光Danisa  阅读(45)  评论(0编辑  收藏  举报