目录

前言

设计与分析

踩坑心得

改进建议

总结

前言

这三次作业的题目只有2道题,但是在题量减小的同时,这三次作业集的难度也相应的上去了,题目的质量很好,运用很广泛,也考验了很多的知识点。这三次的作业的知识面比前两次覆盖的更加广泛。

第7次作业集的第一题和第二题都应用了继承,考察了类和构造方法的使用,父类子类的知识。

第8次作业集的1个题目,是对字符串相关知识的考查则可以使用接口,也可以用字符串的知识解决,也复习了基础和类还有构造方法。

第9次作业的题目,是对上次题目集的改编题目比较难,但是后面做起来会简单一点,知识过硬的话也很容易过。

题目集7

题量:2道

难度:较难

7-2:

  • 首先,在一行上输入一串数字(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:总面积值

7-3:

输入格式:

  • 在一行上输入一串数字(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. 输出分组后的图形类型及面积,格式为[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出;
  3. 各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
  4. 各组中面积之和的最大值输出,格式为The max area:面积值

题目集8

题量:1道

难度:较难

7-4:

每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:

  • 存款、取款功能输入数据格式: 卡号 密码 ATM机编号 金额(由一个或多个空格分隔), 其中,当金额大于0时,代表取款,否则代表存款。
  • 查询余额功能输入数据格式: 卡号

输出格式:

①输入错误处理

  • 如果输入卡号不存在,则输出Sorry,this card does not exist.
  • 如果输入ATM机编号不存在,则输出Sorry,the ATM's id is wrong.
  • 如果输入银行卡密码错误,则输出Sorry,your password is wrong.
  • 如果输入取款金额大于账户余额,则输出Sorry,your account balance is insufficient.
  • 如果检测为跨行存取款,则输出Sorry,cross-bank withdrawal is not supported.

②取款业务输出

输出共两行,格式分别为:

[用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]

当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

③存款业务输出

输出共两行,格式分别为:

[用户姓名]在[银行名称]的[ATM编号]上存款¥[金额]

当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

④查询余额业务输出

¥[金额]

金额保留两位小数。

题目集9

题量:1道

难度:较难

7-5:

  1. 每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:

    • 取款功能输入数据格式: 卡号 密码 ATM机编号 金额(由一个或多个空格分隔)
    • 查询余额功能输入数据格式: 卡号

    输出格式:

    ①输入错误处理

    • 如果输入卡号不存在,则输出Sorry,this card does not exist.
    • 如果输入ATM机编号不存在,则输出Sorry,the ATM's id is wrong.
    • 如果输入银行卡密码错误,则输出Sorry,your password is wrong.
    • 如果输入取款金额大于账户余额,则输出Sorry,your account balance is insufficient.

    ②取款业务输出

    输出共两行,格式分别为:

    业务:取款 [用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]

    当前余额为¥[金额]

    其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

    ③查询余额业务输出

    业务:查询余额 ¥[金额]

    金额保留两位小数。

设计与分析

import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet; public class Main { public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
int x = input.nextInt();
while(x != 0){
if(x < 0 || x > 4){
System.out.println("Wrong Format");
System.exit(0);
}
list.add(x);
x = input.nextInt();
}
DealCardList dealCardList = new DealCardList(list);
if(dealCardList.validate()==false){
System.out.println("Wrong Format");
System.exit(0);
}
dealCardList.showResult();
input.close();
}
} class DealCardList{
ArrayList<Card> cardList=new ArrayList<>(); public DealCardList() {
} public DealCardList(ArrayList<Integer> card) {
for (Integer integer : card) {
if (integer==0)break;
switch (integer){
case 1:
Card card1=new Card(new Circle(Main.input.nextDouble()));
card1.getShape().setShapeName("Circle");
cardList.add(card1);
break;
case 2:
Card card2=new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble()));
card2.getShape().setShapeName("Rectangle");
cardList.add(card2);
break;
case 3:
Card card3=new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
card3.getShape().setShapeName("Triangle");
cardList.add(card3);
break;
case 4:
Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
card4.getShape().setShapeName("Trapezoid");
cardList.add(card4);
break;
} }
}
public boolean validate(){
boolean ret=true;
for (Card card : cardList) {
if (card.getShape().validate()==false){
ret=false;
break;
}
}
return ret;
}
public void cardSort(){
TreeSet<Card> cards = new TreeSet<>(cardList);
for (Card card : cards) {
System.out.print(card.getShape());
}
}
public double getAllArea(){
double sum=0;
for (Card card : cardList) {
sum+=card.getShape().getArea();
}
return sum;
}
public void showResult(){
System.out.println("The original list:");
for (Card card : cardList) {
System.out.print(card.getShape());
}
System.out.println();
System.out.println("The sorted list:");
cardSort();
System.out.println();
System.out.printf("Sum of area:%.2f\n",getAllArea());
}
}
class Card implements Comparable<Card>{
private Shape shape; public Card() {
} public Card(Shape shape) {
this.shape = shape;
} public Shape getShape() {
return shape;
} public void setShape(Shape shape) {
this.shape = shape;
} public int compareTo(Card card) {
return (int)(shape.getArea()-card.getShape().getArea());
}
} abstract class Shape{
private String shapeName; public Shape() {
} public Shape(String shapeName) {
this.shapeName = shapeName;
} public String getShapeName() {
return shapeName;
} public void setShapeName(String shapeName) {
this.shapeName = shapeName;
}
public abstract double getArea();
public abstract boolean validate(); public String toString() {
return getShapeName()+":"+String.format("%.2f ",getArea());
}
} class Circle extends Shape{
private double radius; public Circle() {
} public Circle(double radius) {
this.radius = radius;
} public double getRadius() {
return radius;
} public void setRadius(double radius) {
this.radius = radius;
} public double getArea() {
return Math.PI*radius*radius;
} public boolean validate() {
return this.radius>0;
}
} class Trapezoid extends Shape{
private double top;
private double bottom;
private double height; public Trapezoid() {
} public Trapezoid(double top, double bottom, double height) {
this.top = top;
this.bottom = bottom;
this.height = height;
} public double getArea() {
return (top+bottom)*height/2;
} public boolean validate() {
return top>0&&height>0&&bottom>0;
}
}
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet; public class Main { public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
int x = input.nextInt();
while(x != 0){
if(x < 0 || x > 4){
System.out.println("Wrong Format");
System.exit(0);
}
list.add(x);
x = input.nextInt();
}
DealCardList dealCardList = new DealCardList(list);
if(dealCardList.validate()==false){
System.out.println("Wrong Format");
System.exit(0);
}
dealCardList.showResult();
input.close();
}
} class DealCardList{
ArrayList<Card> cardList=new ArrayList<>(); public DealCardList() {
} public DealCardList(ArrayList<Integer> card) {
for (Integer integer : card) {
if (integer==0)break;
switch (integer){
case 1:
Card card1=new Card(new Circle(Main.input.nextDouble()));
card1.getShape().setShapeName("Circle");
cardList.add(card1);
break;
case 2:
Card card2=new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble()));
card2.getShape().setShapeName("Rectangle");
cardList.add(card2);
break;
case 3:
Card card3=new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
card3.getShape().setShapeName("Triangle");
cardList.add(card3);
break;
case 4:
Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
card4.getShape().setShapeName("Trapezoid");
cardList.add(card4);
break;
} }
}
public boolean validate(){
boolean ret=true;
for (Card card : cardList) {
if (card.getShape().validate()==false){
ret=false;
break;
}
}
return ret;
}
public void cardSort(){
TreeSet<Card> cards = new TreeSet<>(cardList);
for (Card card : cards) {
System.out.print(card.getShape());
}
}
public double getAllArea(){
double sum=0;
for (Card card : cardList) {
sum+=card.getShape().getArea();
}
return sum;
}
public void showResult(){
System.out.println("The original list:");
for (Card card : cardList) {
System.out.print(card.getShape());
}
System.out.println();
System.out.println("The sorted list:");
cardSort();
System.out.println();
System.out.printf("Sum of area:%.2f\n",getAllArea());
}
}
class Card implements Comparable<Card>{
private Shape shape; public Card() {
} public Card(Shape shape) {
this.shape = shape;
} public Shape getShape() {
return shape;
} public void setShape(Shape shape) {
this.shape = shape;
} public int compareTo(Card card) {
return (int)(shape.getArea()-card.getShape().getArea());
}
} abstract class Shape{
private String shapeName; public Shape() {
} public Shape(String shapeName) {
this.shapeName = shapeName;
} public String getShapeName() {
return shapeName;
} public void setShapeName(String shapeName) {
this.shapeName = shapeName;
}
public abstract double getArea();
public abstract boolean validate(); public String toString() {
return getShapeName()+":"+String.format("%.2f ",getArea());
}
} class Circle extends Shape{
private double radius; public Circle() {
} public Circle(double radius) {
this.radius = radius;
} public double getRadius() {
return radius;
} public void setRadius(double radius) {
this.radius = radius;
} public double getArea() {
return Math.PI*radius*radius;
} public boolean validate() {
return this.radius>0;
}
} class Trapezoid extends Shape{
private double topSide;
private double bottomSide;
private double height; public Trapezoid() {
} public Trapezoid(double topSide, double bottomSide, double height) {
this.topSide = topSide;
this.bottomSide = bottomSide;
this.height = height;
} public double getArea() {
return (topSide+bottomSide)*height/2;
} public boolean validate() {
return topSide>0&&height>0&&bottomSide>0;
}
}
package sa;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class c {
public static void main(String[] args) {
List<Atm> atmList1 = new ArrayList<>();
atmList1.add(new Atm("01","中国建设银行")); atmList1.add(new Atm("02","中国建设银行"));
atmList1.add(new Atm("03","中国建设银行")); atmList1.add(new Atm("04","中国建设银行"));
List<Account> accountList1 = new ArrayList<>();
List<Card> cardList1 = new ArrayList<>();
List<String > subCardNumList1_1 = new ArrayList<>(); List<String > subCardNumList1_2 = new ArrayList<>();
subCardNumList1_1.add("6217000010041315709");subCardNumList1_1.add("6217000010041315715");
subCardNumList1_2.add("6217000010041315718");
cardList1.add(new Card("中国建设银行",1,"3217000010041315709","88888888",10000,subCardNumList1_1));
cardList1.add(new Card("中国建设银行",1,"3217000010041315715 ","88888888",10000,subCardNumList1_2));
Account a1 = new Account("杨过",cardList1);
List<Card> cardList2 = new ArrayList<>();
List<String > subCardNumList2_1 = new ArrayList<>();
subCardNumList2_1.add("6217000010051320007");
cardList1.add(new Card("中国建设银行",1,"3217000010051320007", "88888888",10000,subCardNumList2_1));
Account a2 = new Account("郭靖",cardList2);
accountList1.add(a1); accountList1.add(a2);
Bank bank1 = new ChinaConstructionBank("中国建设银行",1,atmList1,accountList1);
List<Atm> atmList2 = new ArrayList<>();
atmList2.add(new Atm("05","中国工商银行")); atmList2.add(new Atm("06","中国工商银行"));
List<Account> accountList2 = new ArrayList<>();
List<Card> cardList3 = new ArrayList<>();
List<Card> cardList4 = new ArrayList<>();
List<String > subCardNumList3_1 = new ArrayList<>(); List<String > subCardNumList3_2 = new ArrayList<>(); List<String > subCardNumList3_3 = new ArrayList<>();
List<String > subCardNumList4_1 = new ArrayList<>(); List<String > subCardNumList4_2 = new ArrayList<>();
subCardNumList3_1.add("6222081502001312389"); subCardNumList3_2.add("6222081502001312390");
subCardNumList3_3.add("6222081502001312399"); subCardNumList3_3.add("6222081502001312400");
cardList3.add(new Card("中国工商银行",2,"3222081502001312389","88888888",10000,subCardNumList3_1));
cardList3.add(new Card("中国工商银行",2,"3222081502001312390","88888888",10000,subCardNumList3_2));
cardList3.add(new Card("中国工商银行",2,"3222081502001312399","88888888",10000,subCardNumList3_3));
subCardNumList4_1.add("6222081502051320785"); subCardNumList4_2.add("6222081502051320786");
cardList4.add(new Card("中国工商银行",2,"3222081502051320785","88888888",10000,subCardNumList4_1));
cardList4.add(new Card("中国工商银行",2,"3222081502051320786","88888888",10000,subCardNumList4_2));
Account a3 = new Account("张无忌",cardList3);
Account a4 = new Account("韦小宝",cardList4);
accountList2.add(a3); accountList2.add(a4);
Bank bank2 = new IcBc("中国工商银行",2,atmList2,accountList2);
Card card1 = new Card("6217000010041315709");
Scanner scanner = new Scanner(System.in);
List<String> stringsList = new ArrayList<>();
String s = scanner.nextLine();
while(!s.equals("#")){
stringsList.add(s);
s = scanner.nextLine();
}
Bank bank = null, anotherBank = null;
for (String s1 : stringsList) {
String[] str = s1.split("\\s+");
if (str[0].substring(0,4).equals("6222")) {
bank = bank2; anotherBank = bank1;
} else if (str[0].substring(0,4).equals("6217")) {
bank = bank1; anotherBank = bank2;
}
if(str.length == 1){
bank.findMoney(str[0]);
} else {
Card card = new Card(str[0],str[1]);
bank.reviseMoney(anotherBank,card,str[2],Double.parseDouble(str[3]));
}
}
}
}
class Account{
private String userName;
List<Card> cardList; public Account() { }
public Account(String userName, List<Card> cardList) {
this.userName = userName;
this.cardList = cardList;
} public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public List<Card> getCardList() { return cardList; }
public void setCardList(List<Card> cardList) { this.cardList = cardList; }
}
class Atm extends Bank{
public String num;
public String belongBankName;
public Atm() { }
public Atm(String num,String belongBankName) {
this.num = num;
this.belongBankName = belongBankName;
}
public String getNum() { return num; }
}
class Bank {
public String bankName;
public int bankNum;
List<Atm> atmList;
private List<Account> accountList;
Card findCard;
Account findAccount;
public Bank() { }
public Bank(String bankName, int bankNum, List<Atm> atmList, List<Account> accountList) {
this.bankName = bankName;
this.bankNum = bankNum;
this.atmList = atmList;
this.accountList = accountList;
}
public List<Atm> getAtmList() { return atmList; }
public void setAtmList(List<Atm> atmList) { this.atmList = atmList; }
public List<Account> getAccountList() { return accountList; }
public void setAccountList(List<Account> accountList) { this.accountList = accountList; }
public boolean reviseMoney(Bank anotherBank, Card card,String atmNum, double reviseMoney){
if(checkCardIsHave(card) && checkAtmIsHave(atmNum,anotherBank) && checkDrawMoneyIsCorrect(reviseMoney)){
this.findCard.modifyMoney(reviseMoney);
if (reviseMoney < 0) {
System.out.printf("%s在%s的%s号ATM机上存款¥%.2f\n",this.findAccount.getUserName()
,this.bankName,atmNum,(-reviseMoney));
} else {
System.out.printf("%s在%s的%s号ATM机上取款¥%.2f\n",this.findAccount.getUserName()
,this.bankName,atmNum,reviseMoney);
}
System.out.printf("当前余额为¥%.2f\n",this.findCard.getBalance());
return true;
}
return false;
}
public double findMoney(String cardNum){
for(Account a : this.accountList){
for(Card c : a.cardList){
for (String sudNum : c.getSubCardNumList()) {
if ( cardNum.equals(sudNum) ) {
System.out.printf("¥%.2f\n",c.getBalance());
return c.getBalance();
}
}
}
}
return -1;
}
public boolean checkCardIsHave(Card card){
boolean numError = true;
boolean pwdError = true;
boolean haveAtm = false;
for(Account a : this.accountList){
for(Card c : a.cardList){
for (String sudNum : c.getSubCardNumList()) {
if ( card.getBankCardNum().equals(sudNum) ) {
numError = false;
}
if ( card.getCardPassword().equals(c.getCardPassword()) ) {
pwdError = false;
}
}
if( !numError && !pwdError ){
this.findCard = c;
this.findAccount = a;
return true;
}
}
}
if (numError) {
System.out.println("Sorry,this card does not exist.");
}
if (pwdError) {
System.out.println("Sorry,your password is wrong.");
}
return false;
}
public boolean checkAtmIsHave(String atmNum,Bank antherBank){
for (Atm atm : this.atmList){
if ( atmNum.equals(atm.getNum()) ) {
return true;
}
}
for (Atm atm : antherBank.atmList) {
if ( atmNum.equals(atm.getNum()) ) {
System.out.println("Sorry,cross-bank withdrawal is not supported.");
return false;
}
}
System.out.println("Sorry,the ATM's id is wrong.");
return false;
}
private boolean checkDrawMoneyIsCorrect(double drawMoney) {
boolean success = false;
if (this.findCard.getBalance() < drawMoney) {
System.out.println("Sorry,your account balance is insufficient.");
return false;
}
if (findCard.subBankNum != this.bankNum) {
System.out.println("Sorry,cross-bank withdrawal is not supported.");
} else {
success = true;
}
return success;
} }
class ChinaConstructionBank extends Bank{
public ChinaConstructionBank() { }
public ChinaConstructionBank(String bankName, int bankNum, List<Atm> atmList, List<Account> accountList) {
super(bankName, bankNum, atmList, accountList);
}
} class IcBc extends Bank{
public IcBc() { }
public IcBc(String bankName, int bankNum, List<Atm> atmList, List<Account> accountList) {
super(bankName, bankNum, atmList, accountList);
}
}
class Card extends Bank{
public String subBankName;
public int subBankNum;
private String bankCardNum;
private String cardPassword;
private double balance;
List<String> subCardNumList; public Card(String subBankName, int subBankNum, String bankCardNum, String cardPassword, double balance, List<String> subCardNumList) {
this.subBankName = subBankName;
this.subBankNum = subBankNum;
this.bankCardNum = bankCardNum;
this.cardPassword = cardPassword;
this.balance = balance;
this.subCardNumList = subCardNumList;
} public Card(String bankCardNum) {
this.bankCardNum = bankCardNum;
this.cardPassword = "88888888";
}
public Card(String bankCardNum,String cardPassword) {
this.bankCardNum = bankCardNum;
this.cardPassword = cardPassword;
}
public void modifyMoney(double money) {
this.balance = this.balance - money;
}
public String getSubBankName() { return subBankName; }
public void setSubBankName(String subBankName) { this.subBankName = subBankName; }
public int getSubBankNum() { return subBankNum; }
public void setSubBankNum(int subBankNum) { this.subBankNum = subBankNum; }
public List<String> getSubCardNumList() { return subCardNumList; }
public void setSubCardNumList(List<String> subCardNumList) { this.subCardNumList = subCardNumList; }
public String getBankCardNum() { return bankCardNum; }
public void setBankCardNum(String bankCardNum) { this.bankCardNum = bankCardNum; }
public String getCardPassword() { return cardPassword; }
public void setCardPassword(String cardPassword) { this.cardPassword = cardPassword; }
public double getBalance() { return balance; }
public void setBalance(double balance) { this.balance = balance; } }
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","中国工商银行");
Bank ook = new Bank("1003","中国农业银行");
unionPay.addBank(ccb);
unionPay.addBank(icbc);
unionPay.addBank(ook); 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);
ATM aTM7 = new ATM("07",ook);
ATM aTM8 = new ATM("08",ook);
ATM aTM9 = new ATM("09",ook);
ATM aTM10 = new ATM("10",ook);
ATM aTM11 = new ATM("11",ook); ccb.addATM(aTM1);
ccb.addATM(aTM2);
ccb.addATM(aTM3);
ccb.addATM(aTM4);
icbc.addATM(aTM5);
icbc.addATM(aTM6);
ook.addATM(aTM7);
ook.addATM(aTM8);
ook.addATM(aTM9);
ook.addATM(aTM10);
ook.addATM(aTM11); User Yangguo = new User("360101200102122324","杨过","13856223254");
User Guojing = new User("360101200012302552","郭靖","13800021124");
User Zhangwuji = new User("360502199805163221","张无忌","13952110011");
User Weixiaobao = new User("360201200513243326","韦小宝","13025996587");
User Zhangsanfeng = new User("3640000010045442002","张三丰","1300002323002");
User Linghuchong = new User("3640000010045441009","令狐冲","13824456772");
User Qiaofeng = new User("3630000010033431001","乔峰","15924441812");
User Hongqigong = new User("3630000010033431008","洪七公","15924555153"); 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);
Account ookAcc1 = new Account("3640000010045442002",10000.00,Zhangsanfeng,ook);
Account ookAcc2 = new Account("3640000010045441009",10000.00,Linghuchong,ook);
Account ookAcc3 = new Account("3630000010033431001",10000.00,Qiaofeng,ook);
Account ookAcc4 = new Account("3630000010033431008",10000.00,Hongqigong,ook); ccb.addAccount(ccbAcc1);
ccb.addAccount(ccbAcc2);
ccb.addAccount(ccbAcc3);
icbc.addAccount(icbcAcc1);
icbc.addAccount(icbcAcc2);
icbc.addAccount(icbcAcc3);
icbc.addAccount(icbcAcc4);
icbc.addAccount(icbcAcc5);
ook.addAccount(ookAcc1);
ook.addAccount(ookAcc2);
ook.addAccount(ookAcc3);
ook.addAccount(ookAcc4); Yangguo.addAccount(ccbAcc1);
Yangguo.addAccount(ccbAcc2);
Guojing.addAccount(ccbAcc3);
Zhangwuji.addAccount(icbcAcc1);
Zhangwuji.addAccount(icbcAcc2);
Zhangwuji.addAccount(icbcAcc3);
Weixiaobao.addAccount(icbcAcc4);
Weixiaobao.addAccount(icbcAcc5);
Zhangsanfeng.addAccount(ookAcc1);
Linghuchong.addAccount(ookAcc2);
Qiaofeng.addAccount(ookAcc3);
Hongqigong.addAccount(ookAcc4); 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);
Card ookCard11 = new Card("6640000010045442002","88888888",ookAcc1);
Card ookCard12 = new Card("6640000010045442003","88888888",ookAcc2);
Card ookCard13 = new Card("6640000010045441009","88888888",ookAcc3);
Card ookCard14 = new Card("6630000010033431001","88888888",ookAcc4);
Card ookCard15 = new Card("6630000010033431008","88888888",ookAcc4); 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);
ookAcc1.addCard(ookCard11);
ookAcc2.addCard(ookCard12);
ookAcc3.addCard(ookCard13);
ookAcc4.addCard(ookCard14);
ookAcc4.addCard(ookCard15); 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();
// TODO Auto-generated constructor stub
} 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();
// TODO Auto-generated constructor stub
} 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();
// TODO Auto-generated constructor stub
} 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();
// TODO Auto-generated constructor stub
} 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();
// TODO Auto-generated constructor stub
} 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();
// TODO Auto-generated constructor stub
} 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 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(userName + "在" +
bankName + "的" + ATMID + "号ATM机上" + type + String.format("¥%.2f", amount));
System.out.println("当前余额为" + String.format("¥%.2f", account.getBalance()));
}
} class ValidateData {
/**
* 校验卡号是否存在
* @param unionPay
* @param cardNO
* @return
*/
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;
} /**
* 校验ATM ID是否存在
* @param unionPay
* @param ATMID
* @return
*/
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() {
super();
// TODO Auto-generated constructor stub
} public UnionPay(ArrayList<Bank> bankList) {
super();
this.bankList = bankList;
} public ArrayList<Bank> getBankList() {
return bankList;
} 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();
// TODO Auto-generated constructor stub
} public GetBalance(UnionPay unionPay) {
super();
this.unionPay = unionPay;
} public double getBalance(String cardNO) {
return ValidateData.getCardbyCardNO(unionPay, cardNO).getAccount().getBalance();
}
}

踩坑心得

作业集7

1:最开始没有看准题目,就直接嘎嘎乱写一通,把类图里面的东西全都创建了一遍,最后发现好多东西并没有用到,就全都删了,真的浪费了很多时间。

然后这个悲惨的故事告诉我们,看到题别冲动,要冷静思考后再下手。以免浪费时间,白白耗费自己的精力。

这个是我做的第一个这么难继承的题目,其实上课讲了好多那些函数还有用法,我在上课时就只是听听,并没有把他们记下来,后面只能通过上网自己查资料来写来看,好在最后写对了。

作业集8

1:不得不说这个银行的这个体系真的很难,很考验我们的知识,但是最后输出的顺序不是按照题目要求的那顺序输出的,还有用户和账户,一个人可以有两到三个账户,合起来就很麻烦。

所以这个输出就没有问题。

作业集9

7-5:这个在我感觉应该是最好的一个对类的考查的题目,不仅考查了一点点的继承,还有很多小的知识点,这个题我花的时间不少,最后一想,应该创建一个结构体,这样会更简单

改进建议

pta的作业的每一题希望可以多弄些样例,就是输出的例子,可以让做题者更好的理解题的意思,能更快的,更精准的做题这三次作业的知识跨度比较大,如果能在题目上说明本题所用的知识点会更好
另外可以支持多种方法解决问题,不单单局限于一种方法
我自己的改进方法如下:

对齐下括号.
良好的编码格式和命名。
从语法和算法上精简代码,永远永远不要把同一个变量用于多个不同的目的。
使用自描述的变量名和方法名

方法名应该小写字母开头,其后用字母大写的单词连接(veryLongVariableName)

类名应该都使用首字母大写的单词连接而成常量名应该全部大写,

用下划线连接左大括号应该跟 if 语句在同一行

分清楚类名和构造方法名,不会混

总结

第7单元作业花费时间:花费时间较长
第8单元作业花费时间:花费时间适中
最后9部分作业:花费时间较长。
通过这三次作业,我还是收获了很多知识的。
第7次作业:每个题目的点都不一样,题是类和构造方法和继承。
第8次作业:学会了银行问题的使用方法和基本操作
第9次作业:学习了解了银行问题,和深度的继承知识,对象数组,类的创建和声明。
每次作业花费的时间较长,需要查阅大量的资料才能解决问题,作业的成功率不高,得到的分数不高。解决方法:遇到不懂的知识点上网查阅资料,请教他人,多看书记住一些基本的知识点。
作为Java的思维来说,
当你拿到一个问题时,你分析这个问题不再是第一步先做什么,
第二步再做什么,这是面向过程的思维,你应该分析这个问题里面有哪些类和对象,这是第一点,然后再分析这些类和对象应该具有哪些属性和方法。这是第二点。最后分析类和类之间具体有什么关系,
这是第三点。这些都是面向过程思想所不具备的,在解决一些问题的时候,运用面向对象的思想来编写程序解决问题才是最好的解决方式
作业量可以适当增多,多多给我们练习的机会,适当放慢一些课堂进度,给我们足够的时间去看书,去思考,实验课可以改成三周俩节,适当要宽恕留给我们写报告的时间。
对教师的意见:多多培养我们的动手能力,在课堂上,给我们多一些时间去写自己的代码,老师能多现场展示一些功能,我觉得效果比较好。

Blog作业03的更多相关文章

  1. http://www.ruanyifeng.com/blog/2007/03/metadata.html

    http://www.ruanyifeng.com/blog/2007/03/metadata.html

  2. JAVA自学作业03

    JAVA自学作业03 1.请用最有效率的方式计算2*8的结果 使用位运算是最有效率的. System.out.println(2<<3); 2.请交换两整数的值 int x = 10; i ...

  3. C语言I—2019秋作业03

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 C语言I-2019秋作业03 我在这个课程的目标是 掌握if-else语句,运算关系 这个作业在那个具体方面帮助我实现目标 row 2 c ...

  4. | C语言I作业03

    | C语言I作业03 标签: 18软件 李煦亮 问题 答案 这个作业属于那个课程 C语言程序设计I 这个作业要求在哪里 https://edu.cnblogs.com 我在这个课程的目标是 学会和掌握 ...

  5. 假期作业03:使用IDE开发你的Java程序

    假期作业03:使用IDE开发你的Java程序 一.使用Eclipse创建一个Java项目HelloWorldPrj,编写一个Java程序并运行. 首先要下载eclipse. (注意这里要选一个中国的, ...

  6. https://blog.helong.info/blog/2015/03/13/jump_consistent_hash/

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179     一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT) ...

  7. DS博客作业03——栈和队列

    1.本周学习总结 谈谈你对栈和队列结构的认识及学习体会. 栈和队列的本质就是线性表.所以,相应的栈跟队列都有两种存储结构:顺序存储结构.链式存储结构. 栈的特点是后进先出,根据栈时进时出的规则,出栈的 ...

  8. JAVA课程课后作业03之作业一

    作业:使用类的静态字段和构造函数,可以跟踪某个类所创建对象的个数.请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?”. 代码: package TestJava; import java ...

  9. DS博客作业03—栈和队列

    1.本周学习总结 本周学习了栈和队列两种数据结构,分别对应后进先出,先进先出两种数据操作 学会栈的特殊类型-共享栈,队列的特殊类型-循环队列的一系列操作 学会熟练使用栈和队列的STL容器,使代码简洁 ...

  10. C博客作业03—函数

    1.本章学习总结 1.1思维导图 1.2本章学习体会及代码量学习体会 1.2.1 学习体会 知道了程序的模块化设计可使程序结构清晰,简化复杂问题,解决代码重复问题 学会使用自定义函数简化主函数,使代码 ...

随机推荐

  1. FME视频教程

    FME视频教程 分为三种 10分钟 2011 2012

  2. iOS证书签名

    苹果官方有一对密钥,即私钥和公钥,私钥在苹果后台,公钥在iOS系统中(如iPhone手机在出厂后,其中就保存有苹果官方的公钥):在Mac系统打包app时也会生成一对密钥(私钥.公钥),并保存在钥匙串中 ...

  3. RayLink远程控制软件:叮~你收到一份年度关键词报告

    叮~~~ 今天是12月31日,2022年的最后一天.今天过后,明天就是2023年啦!R君提前恭祝大家新年快乐,温情满满的跨年之际,RayLink感恩2022遇见大家,2023还请大家多多关照~ 202 ...

  4. css代码中的2个类名连一起写是啥意思?

    <div class="menu-btn closed"> <div class="menu-line"></div> &l ...

  5. python中的platform模块

    platform模块给我们提供了很多方法去获取操作系统的信息: import platform print(platform.platform())# 获取操作系统名称和版本号:macOS-10.14 ...

  6. C++ 11 std::mem_fn

    mem_fn 想到member function 成员函数 这还真就是用来调用成员函数的 普通的函数我们通过函数指针可以调用,但对于成员函数的调用稍微复杂一点,需要对象,也即this指针 因为成员函数 ...

  7. unity tex2Dlod in vert

    https://forum.unity.com/threads/how-to-sample-a-texture-in-vertex-shader.513816/   GreatWall said: ↑ ...

  8. 安装centos7 报错no controller found踩坑

    安装centos7.9时出现如上报错,特记录解决方法: 1.查看硬盘 出现如上报错时,耐心等待,等错误提醒不断滚动时,一会出现命令行输入界面,输入命令: [root@localhost ~]# ls ...

  9. fabric2.2学习笔记1

    fabric2.2学习笔记1 20201303张奕博 2023年1月9日 hyperledger fabric 结构分析 每个Server作用: AdminServer:控制该节点的命运,可以删除该节 ...

  10. javaProject

    import java.io.*; public class Print{ public static void main(String[] args) throws IOException { // ...