第三次博客作业
前言
随着题目集的进行,本学期的java学习进入了最后阶段,题目集的量越来越多,难度也越来越大。题目集7(7-1)、(7-2)两道题目呈递进式设计,侧重于考察继承与多态。题目集8和题目集9为两道ATM机仿真题,其中题目集8银行卡包只含借记卡,且不允许跨行办理相关业务;题目集9银行卡包含借记卡和信用卡两类,且允许跨行办理相关业银行卡包含借记卡和信用卡两类(例如在中国建设银行的 ATM 机上使用中国工商银行的银行卡进行业务操作)。题目集9的难度明显高于题目集8。
一.题目集7-9分析
1.7(7-1)与(7-2)


代码如下:
import java.util.ArrayList;
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();
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();
}
}
interface Comparable {
}
class Card implements Comparable {
private Shape shape;
public Card() {
super();
}
public Card(Shape shape) {
super();
this.shape = shape;
}
public Shape getShape() {
return shape;
}
public void setShape(Shape shape) {
this.shape = shape;
}
}
class Circle extends Shape {
private double radius;
public Circle() {
super();
}
public Circle(double radius) {
super();
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI*radius*radius;
}
public boolean validate() {
if(radius>0) {
return true;
}else return false;
}
}
class DealCardList {
ArrayList<Card> cardList=new ArrayList<Card>();
public DealCardList() {
super();
}
DealCardList(ArrayList<Integer> 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 traperzoid=new Trapezoid(a,b,c);
Card card=new Card(traperzoid);
card.getShape().setShapename("Trapezoid");
cardList.add(card);
}
}
}
public boolean validate() {
for(int i=0;i<cardList.size();i++)
{if(!cardList.get(i).getShape().validate())
return false;}
return true;
}
public void cardSort() {
for(int j=0;j<cardList.size();j++)
for(int i=j+1;i<cardList.size();i++)
{
if(cardList.get(j).getShape().getArea()<cardList.get(i).getShape().getArea())
Collections.swap(cardList, j, i);
}
}
public double getAllArea() {
double area=0;
for(int i=0;i<cardList.size();i++)
area=area+cardList.get(i).getShape().getArea();
return area;
}
public void showResult() {
System.out.println("The original list:");
for(int i=0;i<cardList.size();i++)
System.out.print(cardList.get(i).getShape().getShapename()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
System.out.println();
System.out.println("The sorted list:");
cardSort();
for(int i=0;i<cardList.size();i++)
System.out.print(cardList.get(i).getShape().getShapename()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
System.out.println();
System.out.println("Sum of area:"+String.format("%.2f",getAllArea()));
}
}
class Shape {
private String Shapename;
public Shape() {
super();
}
public Shape(String shapename) {
super();
Shapename = shapename;
}
public String getShapename() {
return Shapename;
}
public void setShapename(String shapename) {
Shapename = shapename;
}
public double getArea() {
return 1;
}
public boolean validate() {
return true;
}
public String toString() {
return null;
}
}
class Rectangle extends Shape {
private double width;
private double length;
public Rectangle() {
super();
}
public Rectangle(double width, double length) {
super();
this.width = width;
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getArea() {
return width*length;
}
public boolean validate() {
if(width>0&&length>0) {
return true;
}else return false;
}
}
class Trapezoid extends Shape {
private double topSide;
private double bottomSide;
private double height;
public Trapezoid() {
super();
}
public Trapezoid(double topSide, double bottomSide, double height) {
super();
this.topSide = topSide;
this.bottomSide = bottomSide;
this.height = height;
}
public double getArea() {
return (topSide+bottomSide)*height/2;
}
public boolean validate() {
if(topSide>0&&bottomSide>0&&height>0) {
return true;
}else return false;
}
}
class Triangle extends Shape{
private double side1;
private double side2;
private double side3;
public Triangle() {
super();
}
public Triangle(double side1, double side2, double side3) {
super();
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getArea() {
double s = (side1 + side2 + side3) / 2;
return Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
}
public boolean validate() {
if (side1 + side2 <= side3 || side2 + side3 <= side1 || side3 + side1 <= side2) {
return false;
} else {
return true;
}
}
}
其中题目集7(7-1)中多张卡片测试未通过
2.题目集8

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
UnionPay unionPay = new UnionPay();
Bank ccb = new Bank("1001","中国建设银行");
Bank icbc = new Bank("1002","中国工商银行");
unionPay.addBank(ccb);
unionPay.addBank(icbc);
ATM aTM1 = new ATM("01",ccb);
ATM aTM2 = new ATM("02",ccb);
ATM aTM3 = new ATM("03",ccb);
ATM aTM4 = new ATM("04",ccb);
ATM aTM5 = new ATM("05",icbc);
ATM aTM6 = new ATM("06",icbc);
ccb.addATM(aTM1);
ccb.addATM(aTM2);
ccb.addATM(aTM3);
ccb.addATM(aTM4);
icbc.addATM(aTM5);
icbc.addATM(aTM6);
User Zhangwuji = new User("360502199805163221","张无忌","13952110011");
User Weixiaobao = new User("360201200513243326","韦小宝","13025996587");
User Yangguo = new User("360101200102122324","杨过","13856223254");
User Guojing = new User("360101200012302552","郭靖","13800021124");
Account ccbAcc1 = new Account("3217000010041315709",10000.00,Yangguo,ccb);
Account ccbAcc2 = new Account("3217000010041315715",10000.00,Yangguo,ccb);
Account ccbAcc3 = new Account("3217000010051320007",10000.00,Guojing,ccb);
Account icbcAcc1 = new Account("3222081502001312389",10000.00,Zhangwuji,icbc);
Account icbcAcc2 = new Account("3222081502001312390",10000.00,Zhangwuji,icbc);
Account icbcAcc3 = new Account("3222081502001312399",10000.00,Zhangwuji,icbc);
Account icbcAcc4 = new Account("3222081502051320785",10000.00,Weixiaobao,icbc);
Account icbcAcc5 = new Account("3222081502051320786",10000.00,Weixiaobao,icbc);
ccb.addAccount(ccbAcc1);
ccb.addAccount(ccbAcc2);
ccb.addAccount(ccbAcc3);
icbc.addAccount(icbcAcc1);
icbc.addAccount(icbcAcc2);
icbc.addAccount(icbcAcc3);
icbc.addAccount(icbcAcc4);
icbc.addAccount(icbcAcc5);
Yangguo.addAccount(ccbAcc1);
Yangguo.addAccount(ccbAcc2);
Guojing.addAccount(ccbAcc3);
Zhangwuji.addAccount(icbcAcc1);
Zhangwuji.addAccount(icbcAcc2);
Zhangwuji.addAccount(icbcAcc3);
Weixiaobao.addAccount(icbcAcc4);
Weixiaobao.addAccount(icbcAcc5);
Card ccbCard1 = new Card("6217000010041315709","88888888",ccbAcc1);
Card ccbCard2 = new Card("6217000010041315715","88888888",ccbAcc1);
Card ccbCard3 = new Card("6217000010041315718","88888888",ccbAcc2);
Card ccbCard4 = new Card("6217000010051320007","88888888",ccbAcc3);
Card icbcCard5 = new Card("6222081502001312389","88888888",icbcAcc1);
Card icbcCard6 = new Card("6222081502001312390","88888888",icbcAcc2);
Card icbcCard7 = new Card("6222081502001312399","88888888",icbcAcc3);
Card icbcCard8 = new Card("6222081502001312400","88888888",icbcAcc3);
Card icbcCard9 = new Card("6222081502051320785","88888888",icbcAcc4);
Card icbcCard10 = new Card("6222081502051320786","88888888",icbcAcc5);
ccbAcc1.addCard(ccbCard1);
ccbAcc1.addCard(ccbCard2);
ccbAcc2.addCard(ccbCard3);
ccbAcc3.addCard(ccbCard4);
icbcAcc1.addCard(icbcCard5);
icbcAcc2.addCard(icbcCard6);
icbcAcc3.addCard(icbcCard7);
icbcAcc3.addCard(icbcCard8);
icbcAcc4.addCard(icbcCard9);
icbcAcc5.addCard(icbcCard10);
StringBuilder sb = new StringBuilder();
String data;
while(!((data = input.nextLine()).equals("#"))) {
sb.append(data + "\n");
}
String[] dt = sb.toString().split("\n");
for(int i = 0; i < dt.length; i++) {
String[] dataLine = dt[i].toString().split("\\s+");
if(dataLine.length == 1) {
GetBalance gb = new GetBalance(unionPay);
System.out.println(String.format("¥%.2f", gb.getBalance(dataLine[0])));
}else {
Withdraw wd = new Withdraw(unionPay,dataLine[0],dataLine[1],dataLine[2],Double.parseDouble(dataLine[3]));
wd.withdraw();
}
}
}
}
class Bank {
private String bankNO;
private String bankName;
private ArrayList<Account> accountList = new ArrayList<Account>();
private ArrayList<ATM> ATMList = new ArrayList<ATM>();
public Bank() {
super();
}
public Bank(String bankNO, String bankName) {
super();
this.bankNO = bankNO;
this.bankName = bankName;
}
public String getBankNO() {
return bankNO;
}
public void setBankNO(String bankNO) {
this.bankNO = bankNO;
}
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public void addAccount(Account account) {
this.accountList.add(account);
}
public void removeAccount(Account account) {
this.accountList.remove(account);
}
public void addATM(ATM aTM) {
this.ATMList.add(aTM);
}
public void removeATM(ATM aTM) {
this.ATMList.remove(aTM);
}
public ArrayList<Account> getAccountList() {
return accountList;
}
public void setAccountList(ArrayList<Account> accountList) {
this.accountList = accountList;
}
public ArrayList<ATM> getATMList() {
return ATMList;
}
public void setATMList(ArrayList<ATM> aTMList) {
ATMList = aTMList;
}
}
class User {
private String ID;
private String name;
private String Phone;
ArrayList<Account> list = new ArrayList<Account>();
public User() {
super();
}
public User(String iD, String name, String phone) {
super();
ID = iD;
this.name = name;
Phone = phone;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addAccount(Account account) {
this.list.add(account);
}
public void removeAccount(Account account) {
this.list.remove(account);
}
}
class Account {
private String accountNO;
private double balance = 0;
private User user = null;
private Bank bank = null;
private static ArrayList<Card> list = new ArrayList<Card>();
public Account() {
super();
}
public Account(String accountNO, double balance, User user, Bank bank) {
super();
this.accountNO = accountNO;
this.balance = balance;
this.user = user;
this.bank = bank;
}
public void addCard(Card card) {
list.add(card);
}
public void removeCard(Card card) {
list.remove(card);
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getAccountNO() {
return accountNO;
}
public void setAccountNO(String accountNO) {
this.accountNO = accountNO;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Bank getBank() {
return bank;
}
public void setBank(Bank bank) {
this.bank = bank;
}
public ArrayList<Card> getList() {
return list;
}
public void setList(ArrayList<Card> list) {
Account.list = list;
}
public static Account getAmountbyCardNO(String cardNO) {
Iterator<Card> cardItr = Account.list.iterator();
Card card = null;
while(cardItr.hasNext()) {
card = cardItr.next();
if(card.getCardNO().equals(cardNO)) {
return card.getAccount();
}
}
return null;
}
}
class Card {
private String cardNO;
private String cardPassword;
private Account account = null;
public Card() {
super();
}
public Card(String cardNO, String cardPassword, Account account) {
super();
this.cardNO = cardNO;
this.cardPassword = cardPassword;
this.account = account;
}
public String getCardNO() {
return cardNO;
}
public void setCardNO(String cardNO) {
this.cardNO = cardNO;
}
public String getCardPassword() {
return cardPassword;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public void setCardPassword(String cardPassword) {
this.cardPassword = cardPassword;
}
public boolean checkCard() {
Pattern p = Pattern.compile("\\d{16}+");
Matcher m = p.matcher(this.cardNO);
return m.matches();
}
public boolean checkPassword(String password) {
return this.cardPassword.equals(password);
}
}
class ATM {
private String ATMID;
private Bank bank = null;
public ATM() {
super();
}
public ATM(String aTMID, Bank bank) {
super();
ATMID = aTMID;
this.bank = bank;
}
public String getATMID() {
return ATMID;
}
public void setATMID(String aTMID) {
ATMID = aTMID;
}
public Bank getBank() {
return bank;
}
public void setBank(Bank bank) {
this.bank = bank;
}
}
class Withdraw {
private UnionPay unionPay;
private String cardNO;
private String cardPassword;
private String ATMID;
private double amount;
public Withdraw() {
super();
}
public Withdraw(UnionPay unionPay, String cardNO, String cardPassword, String aTMID, double amount) {
super();
this.unionPay = unionPay;
this.cardNO = cardNO;
this.cardPassword = cardPassword;
ATMID = aTMID;
this.amount = amount;
}
public Withdraw(String cardNO, String cardPassword, String aTMID, double amount) {
super();
this.cardNO = cardNO;
this.cardPassword = cardPassword;
ATMID = aTMID;
this.amount = amount;
}
public void withdraw() {
Card card = ValidateData.getCardbyCardNO(unionPay, cardNO);
if(card == null) {
System.out.println("Sorry,this card does not exist.");
System.exit(0);
}
ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID);
if(aTM == null) {
System.out.println("Sorry,the ATM's id is wrong.");
System.exit(0);
}
Account account = Account.getAmountbyCardNO(cardNO);
double balance = account.getBalance();
if(!card.getCardPassword().equals(cardPassword)) {
System.out.println("Sorry,your password is wrong.");
System.exit(0);
}
if (amount > balance) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
if (account.getBank().getBankNO() != aTM.getBank().getBankNO()) {
System.out.println("Sorry,cross-bank withdrawal is not supported.");
System.exit(0);
}
account.setBalance(balance - amount);
if(amount >= 0) {
showResult(account,1);
}else {
showResult(account,0);
}
}
public void showResult(Account account,int flag) {
String type = "";
if(flag == 1) {
type = "取款";
}else {
type = "存款";
amount *= -1;
}
String userName = account.getUser().getName();
String bankName = account.getBank().getBankName();
System.out.println("业务:"+type+" "+userName + "在" +
bankName + "的" + ATMID + "号ATM机上" + type + String.format("¥%.2f", amount));
System.out.print("当前余额为" + String.format("¥%.2f", account.getBalance()));
}
}
class ValidateData {
public static Card getCardbyCardNO(UnionPay unionPay,String cardNO) {
Card card = null;
Iterator<Bank> bankItr = unionPay.getBankList().iterator();
while(bankItr.hasNext()) {
ArrayList<Account> accountList = bankItr.next().getAccountList();
Iterator<Account> accountItr = accountList.iterator();
while(accountItr.hasNext()) {
ArrayList<Card> cardList = accountItr.next().getList();
Iterator<Card> cardItr = cardList.iterator();
while(cardItr.hasNext()) {
card = cardItr.next();
if(card.getCardNO().equals(cardNO)) {
return card;
}
}
}
}
return null;
}
public static ATM getATMbyATMID(UnionPay unionPay,String ATMID) {
Iterator<Bank> bankItr = unionPay.getBankList().iterator();
Bank bank = null;
ATM aTM = null;
while(bankItr.hasNext()) {
bank = bankItr.next();
Iterator<ATM> aTMItr = bank.getATMList().iterator();
while(aTMItr.hasNext()) {
aTM = aTMItr.next();
if(aTM.getATMID().equals(ATMID)) {
return aTM;
}
}
}
return null;
}
}
class UnionPay {
private ArrayList<Bank> bankList = new ArrayList<Bank>();
public UnionPay(ArrayList<Bank> bankList) {
super();
this.bankList = bankList;
}
public ArrayList<Bank> getBankList() {
return bankList;
}
public UnionPay() {
super();
}
public void setBankList(ArrayList<Bank> bankList) {
this.bankList = bankList;
}
public void addBank(Bank bank) {
this.bankList.add(bank);
}
public void removeBank(Bank bank) {
this.bankList.remove(bank);
}
}
class GetBalance {
private UnionPay unionPay;
public GetBalance() {
super();
}
public GetBalance(UnionPay unionPay) {
super();
this.unionPay = unionPay;
}
public double getBalance(String cardNO) {
return ValidateData.getCardbyCardNO(unionPay, cardNO).getAccount().getBalance();
}
}
二.作业总结
这三次的题目集让我意识到我还有很多的不足,具体表现为知识上的欠缺,很多东西想用却不会,又需要立刻去补上来。
三.解决方案
提前去学习可能会运用到的知识,不仅要能写得出代码还要以及自己写代码的规范性,不能只求能运行出来就行,不安规范写代码往往会导致更容易错误,而且在后续的改进和查错中会更麻烦
四.总结
一学期的Java学习到尾声了,这也是本学期的最后一次blog了,这个学期还是太懒了,下学期得更努力。

浙公网安备 33010602011771号