第三次博客作业(7~9题目集)
Posted 爱玩的星智
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三次博客作业(7~9题目集)相关的知识,希望对你有一定的参考价值。
一、前言
1. 7~9题目集主要是对图形的面积和ATM机类的结构设计,主要利用了ArrayList这个容器进行对数据的储存与处理,对于我们同学的要求不低,要求我们能对存出数据的ArrayList进行处理,对于对java数据类型和处理手段不清楚的同学来说,该题目集会造成一定困难,而且这几次的练习总的来说难度较大,很难几个小时就写出来,对于我门来说应该算是压轴题,对我们不太友好,但是通过一定的查阅资料和思考能对其有一个解决方法,图形的面积题来说还比较容易,但是对于ATM机类结构设计就相对较难,有很多类要进行一定的功能补充,对于面向过程语言熟练的同学来说,这种面向对象的题目可能会比较困难,对于ATM机类结构设计来说,它主要面向对象编程,对于ArrayList数据容器不熟练的来说这几次题目集来说也会是一次很困难的作业,这几次题目的分值也比较高,对于我们来说也非常重要,所以,这次题目集总体来说还是比较困难的;
2. 这三次题目集来说的话,题量少但是题目难度大,很难写出来,但是不知道会不会很大程度上影响总体编程能力,所以这次题目集还是比较具有挑战性,对于我们编程能力的提升有着很大帮助,第一题的图形卡片是对图形的类别进行分类,再对其进行求面积的处理,而ATM机类结构设计是对输入的卡号和取钱数进行对用户账户余额的查询,属于经典的面向对象编程,该程序主要是要我们对ATM机的工作流程有一个清晰的认识,对于我们今后的编程有一个清醒的认识,对于我们对面向对象编程也有一个清醒的认识,对于我们的编程习惯很能力将会有一个很大的提升,对于我们今后的学习是一个巨大的体验和挑战,但是我并不觉得这几次题目会影响到我们今后的学习,这门课是今后学习的基础,对于我们今后的发展至关重要,所以这次考试的成绩应该不会影响到下个学期的学习,希望今后能够更加努力地学习其他编程语言课程,使自己变得更加充实。
3. 这三次题目集的压轴题是ATM类结构设计,对于我来说是一次很大的考验,写了非常久才写出来,对于我们的思维考验很大,它对我们的要求非常高,要求我们能对各个类的对象进行判断和选择,对于我们的要求很高
二、设计与分析
1. 对于题目集七来说,它要求我们对ArrayL容器进行数据处理,对各个图形的面积公式要熟悉,对于我们对各个图形的类的设计有一个很清晰的方向。
代码如下:
package 作业;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class 图像继承与多态{
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
int a=input.nextInt();
int b=input.nextInt();
int c=input.nextInt();
if(a<0||b<0||c<0){
System.out.printf("Wrong Formate");
System.exit(0);
}
ArrayList<Double> list1=new ArrayList<Double>();
ArrayList<Double> list2=new ArrayList<Double>();
ArrayList<Double> list3=new ArrayList<Double>();
ArrayList<Double> list4=new ArrayList<Double>();
ArrayList<Double> list5=new ArrayList<Double>();
ArrayList<Double> list6=new ArrayList<Double>();
ArrayList<Double> list7=new ArrayList<Double>();
for(int i=0;i<a;i++) {
list1.add(input.nextDouble());
}
for(int i=0;i<b;i++) {
list2.add(input.nextDouble());
list3.add(input.nextDouble());
}
for(int i=0;i<c;i++) {
list4.add(input.nextDouble());
list5.add(input.nextDouble());
list6.add(input.nextDouble());
}
for(int i=0;i<a;i++){
if(list1.get(i)<0) {
System.out.printf("Wrong Format");
System.exit(0);
}
}
for(int i=0;i<b;i++) {
if(list2.get(i)<0||list3.get(i)<0){
System.out.printf("Wrong Format");
System.exit(0);
}
}
for(int i=0;i<c;i++) {
if(list4.get(i)<0||list5.get(i)<0||list6.get(i)<0||list4.get(i)+list5.get(i)<=list6.get(i)||list4.get(i)+list6.get(i)<=list5.get(i)||list5.get(i)+list6.get(i)<=list4.get(i)) {
System.out.printf("Wrong Format");
System.exit(0);
}
}
circle circle=new circle();
rectangle rectangle=new rectangle();
Triangle triangle=new Triangle();
double s=0;
System.out.println("Original area:");
for(int i=0;i<a;i++) {
System.out.printf("%.2f ",circle.getArea(list1.get(i)));
list7.add(circle.getArea(list1.get(i)));
s=s+circle.getArea(list1.get(i));
}
for(int i=0;i<b;i++) {
System.out.printf("%.2f ",rectangle.getArea(list2.get(i),list3.get(i)));
list7.add(rectangle.getArea(list2.get(i),list3.get(i)));
s=s+rectangle.getArea(list2.get(i),list3.get(i));
}
for(int i=0;i<c;i++) {
System.out.printf("%.2f ",triangle.getArea(list4.get(i),list5.get(i),list6.get(i)));
list7.add(triangle.getArea(list4.get(i),list5.get(i),list6.get(i)));
s=s+triangle.getArea(list4.get(i),list5.get(i),list6.get(i));
}
System.out.println();
System.out.printf("Sum of Area:%.2f\\n",s);
Collections.sort(list7);
System.out.println("Sorted area:");
for(int i=0;i<list7.size();i++) {
System.out.printf("%.2f ",list7.get(i));
}
System.out.println();
System.out.printf("Sum of Area:%.2f",s);
}
}
class circle{
double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea(double radius) {
double s=Math.PI*radius*radius;
return s;
}
}
class rectangle{
double width;
double 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(double width,double length) {
double s=width*length;
return s;
}
}
class Triangle{
double side1;
double side2;
double side3;
public double getSide1() {
return side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
public double getArea(double side1,double side2,double side3) {
double s=(side1+side2+side3)/2;
double p=Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return p;
}
}
2.对于题目集八来说,要我们设计一个ATM机来计算用户账户的余额,进行显示输出。
代码如下:
package test;
import java.util.Scanner;
import java.util.ArrayList;
//Bank类
class Bank{
String bankname;
ArrayList<String> ATMList;
//创建无参构造方法
double rate;
public Bank(){
}
//创建带参构造方法
public Bank(String bankname,ArrayList<String> ATMList,double rate)
{
this.bankname=bankname;
this.ATMList=ATMList;
this.rate=rate;
}
//getter
public String getBankname() {
return bankname;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public ArrayList<String> getATMList() {
return ATMList;
}
//setter
public void setBankname(String bankname){
this.bankname=bankname;
}
public void setATMList(ArrayList<String> tATMList){
this.ATMList=ATMList;
}
}
//Account类
class Account {
private String name;
private String account;
private String password;
private double balance;
ArrayList<Bank> banklist;
Bank bank;
private ArrayList<String> cardList;
//创建无参构造方法
public Account(){
}
//创建带参构造方法
public Account(String name,String account,String password,double balance,ArrayList<Bank> banklist,Bank bank,ArrayList<String> cardList){
this.name=name;
this.account=account;
this.password=password;
this.balance=balance;
this.bank=bank;
this.banklist=banklist;
this.cardList=cardList;
}
//getter
public String getName() {
return name;
}
public String getAccount() {
return account;
}
public String getPassword() {
return password;
}
public double getBalance() {
return balance;
}
public ArrayList<String> getCardList() {
return cardList;
}
//setter
public void setName(String name) {
this.name = name;
}
public void setAccount(String account) {
this.account = account;
}
public void setPassword(String password) {
this.password = password;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setCardList(ArrayList<String> cardList) {
this.cardList = cardList;
}
}
//存取款的检查类
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;
//检查账号是否正确
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if (card.equals(accountList.get(i).getCardList().get(j))){
flag=1;
k=i;
break;
}
}
if(flag==1){
break;
}
}
//检查密码是否正确
if(flag==1){
if(password.equals(accountList.get(k).getPassword())){
flag=2;
}
else{
System.out.println("Sorry,your password is wrong.");//银行卡密码错误
return false;
}
}
else{
System.out.println("Sorry,this card does not exist.");//卡号不存在
return false;
}
//检查ATM机编号是否正确
if(flag==2){
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))){
flag=3;
break;
}
}
}
}
//检查金额是否正确
if(flag==3){
if(money<=accountList.get(k).getBalance()){
flag=4;
}
else{
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if(card.equals(accountList.get(i).getCardList().get(j))){
k=i;
break;
}
}
}
if(((accountList.get(k).getName().equals("杨过")||accountList.get(k).getName().equals("张无忌")||accountList.get(k).getName().equals("郭靖")||accountList.get(k).getName().equals("韦小宝"))) ||((accountList.get(k).getName().equals("张三丰")||accountList.get(k).getName().equals("令狐冲")||accountList.get(k).getName().equals("乔峰")||accountList.get(k).getName().equals("洪七公"))&&(accountList.get(k).getBalance()<money))) {
return true;
}
else{
System.out.println("Sorry,your account balance is insufficient.");//取款金额大于账户余额
return false;
}
}
}
else
{System.out.println("Sorry,the ATM\'s id is wrong.");//ATM机编号不存在
return false;}
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;
//卡号校验
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if (card.equals(accountList.get(i).getCardList().get(j))){
flag=1;
break;
}
}
if(flag==1){
break;
}
}
if(flag==1)
return true;
else{
System.out.println("Sorry,this card does not exist.");//卡号不存在
return false;
}
}
}
class check2{
ArrayList<Account> accountList;
String card;
String password;
String number;
double money;
public check2(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;
//检查账号是否正确
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if (card.equals(accountList.get(i).getCardList().get(j))){
flag=1;
k=i;
break;
}
}
if(flag==1){
break;
}
}
//检查密码是否正确
if(flag==1){
if(password.equals(accountList.get(k).getPassword())){
flag=2;
}
else{
System.out.println("Sorry,your password is wrong.");//银行卡密码错误
return false;
}
}
else{
System.out.println("Sorry,this card does not exist.");//卡号不存在
return false;
}
//检查ATM机编号是否正确
if(flag==2){
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))){
flag=3;
break;
}
}
}
}
if(flag!=3) {
System.out.println("Sorry,the ATM\'s id is wrong.");//ATM机编号不存在
return false;
}
return true;
}
}
//存取款类
class Access{
Bank bank;
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++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if(number.equals(accountList.get(i).bank.getATMList().get(j))){
t=j;
break;
}
}
}
int k=0;
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if(card.equals(accountList.get(i).getCardList().get(j))){
k=i;
break;
}
}
}
if(!(number.equals(accountList.get(k).bank.getATMList().get(t)))){
if(accountList.get(k).getBalance()<money&&((number.equals("01")||number.equals("02")||number.equals("03")||number.equals("04")))) {
accountList.get(k).setBalance((accountList.get(k).getBalance()-money)*0.05+accountList.get(k).getBalance()-money-money*0.02);
}
else if(number.equals("01")||number.equals("02")||number.equals("03")||number.equals("04")) {
accountList.get(k).setBalance(accountList.get(k).getBalance()-money-money*0.02);
}
if((number.equals("05")||number.equals("06"))&&accountList.get(k).getBalance()<money) {
accountList.get(k).setBalance((accountList.get(k).getBalance()-money)*0.05+accountList.get(k).getBalance()-money-money*0.03);
}
else if(number.equals("05")||number.equals("06")) {
accountList.get(k).setBalance(accountList.get(k).getBalance()-money-money*0.03);
}
if(number.equals("07")||number.equals("08")||number.equals("09")||number.equals("10")||number.equals("11")) {
accountList.get(k).setBalance(accountList.get(k).getBalance()-money-money*0.04);
}
else if((number.equals("07")||number.equals("08")||number.equals("09")||number.equals("10")||number.equals("11"))&&accountList.get(k).getBalance()<money) {
accountList.get(k).setBalance((accountList.get(k).getBalance()-money)*0.05+accountList.get(k).getBalance()-money-money*0.04);
}
}
else{
if(accountList.get(k).getBalance()<money&&((number.equals("01")||number.equals("02")||number.equals("03")||number.equals("04")))) {
accountList.get(k).setBalance(-money*0.05+accountList.get(k).getBalance());
}
if((number.equals("05")||number.equals("06"))&&accountList.get(k).getBalance()<money) {
accountList.get(k).setBalance(-money*0.05+accountList.get(k).getBalance());
}
if(number.equals("07")||number.equals("08")||number.equals("09")||number.equals("10")||number.equals("11")) {
accountList.get(k).setBalance(-money*0.05+accountList.get(k).getBalance());
}
accountList.get(k).setBalance(accountList.get(k).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;
int k=0;
//卡号校验
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).bank.getATMList().size();j++){
if(number.equals(accountList.get(i).bank.getATMList().get(j))){
t=i;
break;
}
}
}
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if(card.equals(accountList.get(i).getCardList().get(j))){
k=i;
break;
}
}
}
//取款
System.out.printf("业务:取款 "+accountList.get(k).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上取款¥%.2f\\n",money);
System.out.printf("当前余额为¥%.2f\\n",accountList.get(k).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;
//卡号校验
int k=0;
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if(card.equals(accountList.get(i).getCardList().get(j))){
k=i;
break;
}
}
}
System.out.printf("业务:查询余额 ¥%.2f\\n",accountList.get(k).getBalance());
}
}
//主类
public class Main1{
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 jsyh = new Bank("中国建设银行", ATMList1,0.02);
ArrayList<String> ATMList2 = new ArrayList<>();
ATMList2.add("05");
ATMList2.add("06");
Bank gsyh = new Bank("中国工商银行", ATMList2,0.03);
//银行数组
ArrayList<Bank> bankList = new ArrayList<>();
ArrayList<String> ATMList3 = new ArrayList<>();
Bank nyyh =new Bank("中国农业银行",ATMList3,0.04);
bankList.add(jsyh);
bankList.add(gsyh);
bankList.add(nyyh);
ATMList3.add("07");
ATMList3.add("08");
ATMList3.add("09");
ATMList3.add("10");
ATMList3.add("11");
//用户数组
ArrayList<String> cardList1 = new ArrayList<>();
cardList1.add("6217000010041315709");
cardList1.add("6217000010041315715");
Account account1 = new Account("杨过", "3217000010041315709", "88888888", 10000.00, bankList, jsyh,cardList1);
ArrayList<String> cardList2 = new ArrayList<>();
cardList2.add("6217000010041315718");
Account account2 = new Account("杨过", "3217000010041315715", "88888888", 10000.00, bankList,jsyh,cardList2);
ArrayList<String> cardList3 = new ArrayList<>();
cardList3.add("6217000010051320007");
Account account3 = new Account("郭靖", "3217000010051320007", "88888888", 10000.00, bankList,jsyh,cardList3);
ArrayList<String> cardList4 = new ArrayList<>();
cardList4.add("6222081502001312389");
Account account4 = new Account("张无忌", "3222081502001312389", "88888888", 10000.00, bankList,gsyh,cardList4);
ArrayList<String> cardList5 = new ArrayList<>();
cardList5.add("6222081502001312390");
Account account5 = new Account("张无忌", "3222081502001312390", "88888888", 10000.00, bankList,gsyh,cardList5);
ArrayList<String> cardList6 = new ArrayList<>();
cardList6.add("6222081502001312399");
cardList6.add("6222081502001312400");
Account account6 = new Account("张无忌", "3222081502001312399", "88888888", 10000.00, bankList,gsyh,cardList6);
ArrayList<String> cardList7 = new ArrayList<>();
cardList7.add("6222081502051320785");
Account account7 = new Account("韦小宝", "3222081502051320785", "88888888", 10000.00, bankList,gsyh,cardList7);
ArrayList<String> cardList8 = new ArrayList<>();
cardList8.add("6222081502051320786");
Account account8 = new Account("韦小宝", "3222081502051320786", "88888888", 10000.00, bankList,gsyh,cardList8);
ArrayList<String> cardList9 = new ArrayList<>();
cardList9.add("6640000010045442002");
cardList9.add("6640000010045442003");
Account account9=new Account("张三丰","3640000010045442002","88888888",10000.00,bankList,jsyh,cardList9);
ArrayList<String> cardList11= new ArrayList<>();
cardList11.add("6640000010045441009");
Account account11=new Account("令狐冲","3640000010045441009","88888888",10000.00,bankList,gsyh,cardList11);
ArrayList<String> cardList12= new ArrayList<>();
cardList12.add("6630000010033431001");
Account account12=new Account("乔峰","3630000010033431001","88888888",10000.00,bankList,nyyh,cardList12);
ArrayList<String> cardList13= new ArrayList<>();
cardList13.add("6630000010033431008");
Account account13=new Account("洪七公","3630000010033431008","88888888",10000.00,bankList,nyyh,cardList13);
//用户总数组
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);
accountList.add(account9);
accountList.add(account11);
accountList.add(account12);
accountList.add(account13);
Scanner x=new Scanner(System.in);
String kp;
Check check;
Check1 check1;
check2 check2;
kp=x.nextLine();
while(!kp.equals("#")){
int i,j;
String[] shuju=kp.split("\\\\s+");//输入的每行数据用空格隔开,存为数组
if(shuju.length!=1){
check = new Check(accountList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3]));
check2 =new check2(accountList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3]));
if(check.check()){
Access access = new Access(accountList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3]));
access.access();
Show show = new Show(accountList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3]));
show.show();
}
}
else {
check1=new Check1(accountList,shuju[0]);
if(check1.check()){
Show1 show1= new Show1(accountList,shuju[0]);
show1.show1();
}
}
kp=x.nextLine();
}
}
}
3.对于题目集9,要求对ATM机进行贷款功能的增加,包括对账户的跨行取款,透支取款等等进行输出对账户金额的输出。
代码如下:
package test;
import java.util.Scanner;
import java.util.ArrayList;
//Bank类
class Bank{
String bankname;
ArrayList<String> ATMList;
//创建无参构造方法
double rate;
public Bank(){
}
//创建带参构造方法
public Bank(String bankname,ArrayList<String> ATMList,double rate)
{
this.bankname=bankname;
this.ATMList=ATMList;
this.rate=rate;
}
//getter
public String getBankname() {
return bankname;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public ArrayList<String> getATMList() {
return ATMList;
}
//setter
public void setBankname(String bankname){
this.bankname=bankname;
}
public void setATMList(ArrayList<String> tATMList){
this.ATMList=ATMList;
}
}
//Account类
class Account {
private String name;
private String account;
private String password;
private double balance;
ArrayList<Bank> banklist;
Bank bank;
private ArrayList<String> cardList;
//创建无参构造方法
public Account(){
}
//创建带参构造方法
public Account(String name,String account,String password,double balance,ArrayList<Bank> banklist,Bank bank,ArrayList<String> cardList){
this.name=name;
this.account=account;
this.password=password;
this.balance=balance;
this.bank=bank;
this.banklist=banklist;
this.cardList=cardList;
}
//getter
public String getName() {
return name;
}
public String getAccount() {
return account;
}
public String getPassword() {
return password;
}
public double getBalance() {
return balance;
}
public ArrayList<String> getCardList() {
return cardList;
}
//setter
public void setName(String name) {
this.name = name;
}
public void setAccount(String account) {
this.account = account;
}
public void setPassword(String password) {
this.password = password;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setCardList(ArrayList<String> cardList) {
this.cardList = cardList;
}
}
//存取款的检查类
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;
//检查账号是否正确
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if (card.equals(accountList.get(i).getCardList().get(j))){
flag=1;
k=i;
break;
}
}
if(flag==1){
break;
}
}
//检查密码是否正确
if(flag==1){
if(password.equals(accountList.get(k).getPassword())){
flag=2;
}
else{
System.out.println("Sorry,your password is wrong.");//银行卡密码错误
return false;
}
}
else{
System.out.println("Sorry,this card does not exist.");//卡号不存在
return false;
}
//检查ATM机编号是否正确
if(flag==2){
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))){
flag=3;
break;
}
}
}
}
//检查金额是否正确
if(flag==3){
if(money<=accountList.get(k).getBalance()){
flag=4;
}
else{
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if(card.equals(accountList.get(i).getCardList().get(j))){
k=i;
break;
}
}
}
if(((accountList.get(k).getName().equals("杨过")||accountList.get(k).getName().equals("张无忌")||accountList.get(k).getName().equals("郭靖")||accountList.get(k).getName().equals("韦小宝"))) ||((accountList.get(k).getName().equals("张三丰")||accountList.get(k).getName().equals("令狐冲")||accountList.get(k).getName().equals("乔峰")||accountList.get(k).getName().equals("洪七公"))&&(accountList.get(k).getBalance()<money))) {
return true;
}
else{
System.out.println("Sorry,your account balance is insufficient.");//取款金额大于账户余额
return false;
}
}
}
else
{System.out.println("Sorry,the ATM\'s id is wrong.");//ATM机编号不存在
return false;}
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;
//卡号校验
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if (card.equals(accountList.get(i).getCardList().get(j))){
flag=1;
break;
}
}
if(flag==1){
break;
}
}
if(flag==1)
return true;
else{
System.out.println("Sorry,this card does not exist.");//卡号不存在
return false;
}
}
}
class check2{
ArrayList<Account> accountList;
String card;
String password;
String number;
double money;
public check2(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;
//检查账号是否正确
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if (card.equals(accountList.get(i).getCardList().get(j))){
flag=1;
k=i;
break;
}
}
if(flag==1){
break;
}
}
//检查密码是否正确
if(flag==1){
if(password.equals(accountList.get(k).getPassword())){
flag=2;
}
else{
System.out.println("Sorry,your password is wrong.");//银行卡密码错误
return false;
}
}
else{
System.out.println("Sorry,this card does not exist.");//卡号不存在
return false;
}
//检查ATM机编号是否正确
if(flag==2){
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))){
flag=3;
break;
}
}
}
}
if(flag!=3) {
System.out.println("Sorry,the ATM\'s id is wrong.");//ATM机编号不存在
return false;
}
return true;
}
}
//存取款类
class Access{
Bank bank;
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++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if(number.equals(accountList.get(i).bank.getATMList().get(j))){
t=j;
break;
}
}
}
int k=0;
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if(card.equals(accountList.get(i).getCardList().get(j))){
k=i;
break;
}
}
}
if(!(number.equals(accountList.get(k).bank.getATMList().get(t)))){
if(accountList.get(k).getBalance()<money&&((number.equals("01")||number.equals("02")||number.equals("03")||number.equals("04")))) {
accountList.get(k).setBalance((accountList.get(k).getBalance()-money)*0.05+accountList.get(k).getBalance()-money-money*0.02);
}
else if(number.equals("01")||number.equals("02")||number.equals("03")||number.equals("04")) {
accountList.get(k).setBalance(accountList.get(k).getBalance()-money-money*0.02);
}
if((number.equals("05")||number.equals("06"))&&accountList.get(k).getBalance()<money) {
accountList.get(k).setBalance((accountList.get(k).getBalance()-money)*0.05+accountList.get(k).getBalance()-money-money*0.03);
}
else if(number.equals("05")||number.equals("06")) {
accountList.get(k).setBalance(accountList.get(k).getBalance()-money-money*0.03);
}
if(number.equals("07")||number.equals("08")||number.equals("09")||number.equals("10")||number.equals("11")) {
accountList.get(k).setBalance(accountList.get(k).getBalance()-money-money*0.04);
}
else if((number.equals("07")||number.equals("08")||number.equals("09")||number.equals("10")||number.equals("11"))&&accountList.get(k).getBalance()<money) {
accountList.get(k).setBalance((accountList.get(k).getBalance()-money)*0.05+accountList.get(k).getBalance()-money-money*0.04);
}
}
else{
if(accountList.get(k).getBalance()<money&&((number.equals("01")||number.equals("02")||number.equals("03")||number.equals("04")))) {
accountList.get(k).setBalance(-money*0.05+accountList.get(k).getBalance());
}
if((number.equals("05")||number.equals("06"))&&accountList.get(k).getBalance()<money) {
accountList.get(k).setBalance(-money*0.05+accountList.get(k).getBalance());
}
if(number.equals("07")||number.equals("08")||number.equals("09")||number.equals("10")||number.equals("11")) {
accountList.get(k).setBalance(-money*0.05+accountList.get(k).getBalance());
}
accountList.get(k).setBalance(accountList.get(k).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;
int k=0;
//卡号校验
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).bank.getATMList().size();j++){
if(number.equals(accountList.get(i).bank.getATMList().get(j))){
t=i;
break;
}
}
}
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if(card.equals(accountList.get(i).getCardList().get(j))){
k=i;
break;
}
}
}
//取款
System.out.printf("业务:取款 "+accountList.get(k).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上取款¥%.2f\\n",money);
System.out.printf("当前余额为¥%.2f\\n",accountList.get(k).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;
//卡号校验
int k=0;
for(i=0;i<accountList.size();i++){
for(j=0;j<accountList.get(i).getCardList().size();j++){
if(card.equals(accountList.get(i).getCardList().get(j))){
k=i;
break;
}
}
}
System.out.printf("业务:查询余额 ¥%.2f\\n",accountList.get(k).getBalance());
}
}
//主类
public class Main1{
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 jsyh = new Bank("中国建设银行", ATMList1,0.02);
ArrayList<String> ATMList2 = new ArrayList<>();
ATMList2.add("05");
ATMList2.add("06");
Bank gsyh = new Bank("中国工商银行", ATMList2,0.03);
//银行数组
ArrayList<Bank> bankList = new ArrayList<>();
ArrayList<String> ATMList3 = new ArrayList<>();
Bank nyyh =new Bank("中国农业银行",ATMList3,0.04);
bankList.add(jsyh);
bankList.add(gsyh);
bankList.add(nyyh);
ATMList3.add("07");
ATMList3.add("08");
ATMList3.add("09");
ATMList3.add("10");
ATMList3.add("11");
//用户数组
ArrayList<String> cardList1 = new ArrayList<>();
cardList1.add("6217000010041315709");
cardList1.add("6217000010041315715");
Account account1 = new Account("杨过", "3217000010041315709", "88888888", 10000.00, bankList, jsyh,cardList1);
ArrayList<String> cardList2 = new ArrayList<>();
cardList2.add("6217000010041315718");
Account account2 = new Account("杨过", "3217000010041315715", "88888888", 10000.00, bankList,jsyh,cardList2);
ArrayList<String> cardList3 = new ArrayList<>();
cardList3.add("6217000010051320007");
Account account3 = new Account("郭靖", "3217000010051320007", "88888888", 10000.00, bankList,jsyh,cardList3);
ArrayList<String> cardList4 = new ArrayList<>();
cardList4.add("6222081502001312389");
Account account4 = new Account("张无忌", "3222081502001312389", "88888888", 10000.00, bankList,gsyh,cardList4);
ArrayList<String> cardList5 = new ArrayList<>();
cardList5.add("6222081502001312390");
Account account5 = new Account("张无忌", "3222081502001312390", "88888888", 10000.00, bankList,gsyh,cardList5);
ArrayList<String> cardList6 = new ArrayList<>();
cardList6.add("6222081502001312399");
cardList6.add("6222081502001312400");
Account account6 = new Account("张无忌", "3222081502001312399", "88888888", 10000.00, bankList,gsyh,cardList6);
ArrayList<String> cardList7 = new ArrayList<>();
cardList7.add("6222081502051320785");
Account account7 = new Account("韦小宝", "3222081502051320785", "88888888", 10000.00, bankList,gsyh,cardList7);
ArrayList<String> cardList8 = new ArrayList<>();
cardList8.add("6222081502051320786");
Account account8 = new Account("韦小宝", "3222081502051320786", "88888888", 10000.00, bankList,gsyh,cardList8);
ArrayList<String> cardList9 = new ArrayList<>();
cardList9.add("6640000010045442002");
cardList9.add("6640000010045442003");
Account account9=new Account("张三丰","3640000010045442002","88888888",10000.00,bankList,jsyh,cardList9);
ArrayList<String> cardList11= new ArrayList<>();
cardList11.add("6640000010045441009");
Account account11=new Account("令狐冲","3640000010045441009","88888888",10000.00,bankList,gsyh,cardList11);
ArrayList<String> cardList12= new ArrayList<>();
cardList12.add("6630000010033431001");
Account account12=new Account("乔峰","3630000010033431001","88888888",10000.00,bankList,nyyh,cardList12);
ArrayList<String> cardList13= new ArrayList<>();
cardList13.add("6630000010033431008");
Account account13=new Account("洪七公","3630000010033431008","88888888",10000.00,bankList,nyyh,cardList13);
//用户总数组
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);
accountList.add(account9);
accountList.add(account11);
accountList.add(account12);
accountList.add(account13);
Scanner x=new Scanner(System.in);
String kp;
Check check;
Check1 check1;
check2 check2;
kp=x.nextLine();
while(!kp.equals("#")){
int i,j;
String[] shuju=kp.split("\\\\s+");//输入的每行数据用空格隔开,存为数组
if(shuju.length!=1){
check = new Check(accountList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3]));
check2 =new check2(accountList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3]));
if(check.check()){
Access access = new Access(accountList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3]));
access.access();
Show show = new Show(accountList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3]));
show.show();
}
}
else {
check1=new Check1(accountList,shuju[0]);
if(check1.check()){
Show1 show1= new Show1(accountList,shuju[0]);
show1.show1();
}
}
kp=x.nextLine();
}
}
}
三、踩坑心得
1.对于这几次题目集来说,第一个题目为对图形的各个面积进行输出的题目,该题要求对ArrayList这个容器进行熟练运用,对各个图形的面积进行判断和输出,刚开始还想用数组1进行书写,后面发现根本行不通,数据太多数组无法进行完整储存和应用,所以只能用ArrayList进行书写,但是该题的ArrayList又对数据不能完全保存,必须定义多个ArrayList进行数据的保存和处理,对我们的要求比较高,需要我们对这个容器进行熟练应用,所以需要我们在课程上多用心,在讲ArrayList这个容器时认真听,对于这个容器来说,最重要的是理解它的内存模型,对于我们的利用会有很大帮助,但是平常如果不认真听讲的同学来说则会有较大困难,特别对于这个题目来说,刚开始我也一脸懵,不知道一下子应该如何来处理这么多数据,后来通过学习ArrayList相关知识才知道ArrayList的运用,总之,对于ArrayList这个容器来说需要我们不断进行学习和写题目进行输出。
2.ATM机类的结构设计对于我们理解面向对象的理解又更深了一步,上学期学的c语言主要面向过程,而java大多数是面向对象进行编程,需要我们对对象进行深刻地理解,我在面向对象的学习上很吃力,无法理解相关知识,通过网课和询问同学才知道相关的知识,但是对于我来说,面向对象编程还是比较困难,在写定义账户类的时候,无法对类进行正确的定义,常常报错,在查询相关资料后了解类的相关知识后能进行正确的定义。
3、在数据类型的定义的时候,常常搞错数据的类型,无法定义数据的类型,需要我们在读题目的时候认真读题,将数据正确认知并定义,java编程中许多编程方法和c语言并不相同,所以学起来仍然很困难。
四、改进建议
五、总结
1、在上java课的时候发现常常许多人没有听课,都在自顾自的学,这对我们的学习很不利,我建议老师多与同学互动,而不是干讲知识点,而且建议老师发布题目时要根据学生的具体完成情况来调整时间,而不仅仅是只设置一个截止时间,在许多同学都没完成作业的情况下,要了解情况后进行调整时间。
2、写完了这些题目的时候,我学到了正则表达式里面的各种表达方式,以及正则表达式里的各种函数,了解了它们各自的作用,java中大部分情况是对字符串的操作,正则表达式有着非常重要的作用,我充分理解了正则表达式的重要作用,并且明白了这对我们今后的写程序来说是很有用的,我认为在今后的学习中要更加变得耐心,对于从未见过的知识要敢于接受,内化于心,而不是害怕接受新知识,在正则表达式的知识方面还有许多知识要学习,正则表达式里面还有很多函数需要去了解,需要我不断去探索。
3、这几次题目集我觉得学的很困难,需要再进行不断地学习,对于今后的编程之路有很大帮助,对于我们今后的学习有很大的指导意义,需要我们对这几次的代码进行不断理解和去深入学习,代码之路还有很长,需要我们不断前进,对于前几次作业的知识点来说,知识点覆盖的较为全面,刚学java时将其与c语言进行对比起来学,刚开始进行对过程的程序过渡到面向对象的程序,给了我们熟悉的感觉,对我们的入门也比较友好,在写面向过程的程序来说,主要是为了解决某一个存在的问题,而面向对象的程序主要是为了解决某个对象要完成的功能,对我们的思考也有一定挑战,同时,在写程序的同时,也要注意数据的各种形式,方法的类型,类的调用等等,对我们的细心也有一定的考察。
4、单从Java框架来说是没问题,但是对于主题算法就比较的困难,正则表达式针对字符串有一些特殊的操作,因此在之后的Java学习中要不断地进行提前学习,而不能跟在老师的后面,那样的话就无法真正学到知识。
5、在写程序时无法建立正确的算法,常常无法通过测试点,我知道自己的问题后马上就会去eclipse上进行进行调试,看看是不是算法问题,将问题进行不断思考,若实在思考不出来就去问班上编程能力强的同学,再将其自我量化,将知识进行吸收。
6、写代码时需要多思考,多利用闲暇时间来思考程序如何来设计。
以上是关于第三次博客作业(7~9题目集)的主要内容,如果未能解决你的问题,请参考以下文章