第三阶段Blog作业

(1)前言:

题目集7:7-1图形卡片排序游戏以及7-2图形卡片分组游戏考察类的继承、多态性使用方法以及Comparable接口(Compareto()方法)的应用,

题目集8:7-1 ATM机类结构设计(一)尝试使用面向对象技术来设计。

题目集9:7-1ATM机类结构设计(二)是在上一次作业上进行的一次改进,同样考察面向对象技术,但对类的设计更为复杂。

这次题目量不大,但是对我来说,花了较长时间,能拿一定分数,但是要满分觉得很难。

 

(2)设计与分析

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

掌握类的继承、多态性使用方法以及接口的应用。详见作业指导书 2020-OO第07次作业-1指导书V1.0.pdf

输入格式:

  • 首先,在一行上输入一串数字(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(7-1)是图片卡牌排序游戏,输入卡片图形类型,再依次输入各图形的相关参数,Card类通过关键字implements声明自己使用Comparable接口,从而使用compareTo()方法进行排序。

 

import java.util.*;

class Shape {
String shapename;

public Shape() {

}
public Shape(String shapename) {
this.shapename=shapename;
}

public String getshapename() {
return shapename;
}

public void setshapename(String shapename) {
this.shapename=shapename;
}

double getarea() {
return 0.0;
}

boolean validate() {
return false;
}

public String toString(){
return getshapename()+":"+String.format("%.2f ",getarea());
}




}

class Circle extends Shape{
double r;

Circle() {


}
Circle(double r) {
this.r=r;
}

public double getr() {
return r;
}

public void setr(double r) {
this.r=r;
}

public double getarea() {
return Math.PI*r*r;
}

public boolean validate() {
if(r>0)
return true;
return false;
}



}

class Triangle extends Shape{

double a,b,c;

Triangle(){

}

Triangle(double a,double b,double c){
this.a=a;
this.b=b;
this.c=c;
}

public double getarea(){
double p=(a+b+c)/2;
double s=Math.sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}

public boolean validate() {
if(a<0||b<0||c<0)
return false;
else if(a+b<=c||a+c<=b||b+c<=a)
{
return false;
}

return true;
}
}

class Rectangle extends Shape{

double width;
double length;
Rectangle(){
}
Rectangle(double width,double length)
{
this.width=width;
this.length=length;
}

public double getwidth(){
return width;
}
public void setwidth(double width) {
this.width=width;
}
public double getlenth(){
return length;
}
public void setlenth(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{
double sd;
double xd;
double h;
Trapezoid(){

}
Trapezoid(double sd,double xd,double h){
this.sd=sd;
this.xd=xd;
this.h=h;

}
public double getarea() {
return (sd+xd)*h*0.5;
}

public boolean validate() {
if(sd>0&&xd>0&&h>0)
return true;
else
return false;
}
}

class Card implements Comparable<Card>{
Shape shape;
Card(){

}
Card (Shape shape){
this.shape=shape;
}
public Shape getshape() {
return shape;
}

public void setshape(Shape shape) {
this.shape=shape;
}
public int compareTo(Card o) {
// TODO Auto-generated method stub
return -(int)(shape.getarea()-o.getshape().getarea());
}

}
class DealCardList{
ArrayList<Card> cardList=new ArrayList<>();
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() {

for(Card cardname : cardList) {
if(cardname.getshape().validate())
return true;
}
return false;
}

@SuppressWarnings("unchecked")
public void cardSort() {
Collections.sort(this.cardList);

}

public double getall() {
double ss=0;
for(Card c : cardList) {
ss=ss+c.getshape().getarea();
}
return ss;
}

public void show() {

System.out.println("The original list:");
for (Card card : cardList) {
System.out.print(card.getshape().getshapename());
System.out.print(":"+String.format("%.2f ",card.getshape().getarea()) );

}
System.out.println();
System.out.println("The sorted list:");
cardSort();
for (Card cardt : cardList) {
System.out.print(cardt.getshape().getshapename());
System.out.print(":"+String.format("%.2f ",cardt.getshape().getarea()) );
}
System.out.println();
System.out.printf("Sum of area:%.2f\n",getall());
}
}

 

 

public class Main {
//在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
//使用Main.input.next…即可(避免采坑)
public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
int num = input.nextInt();
while(num != 0){
if(num < 0 || num > 4){
System.out.println("Wrong Format");
System.exit(0);
}
list.add(num);
num = input.nextInt();
}
DealCardList dealCardList = new DealCardList(list);
if(!dealCardList.validate()){
System.out.println("Wrong Format");
System.exit(0);

}
dealCardList.show();
input.close();
}
}

 

 

 

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

掌握类的继承、多态性使用方法以及接口的应用。 具体需求参考作业指导书。

2021-OO第07次作业-2指导书V1.0.pdf

输入格式:

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

 

 


import java.util.*;

class Shape {
String shapename;

public Shape() {

}
public Shape(String shapename) {
this.shapename=shapename;
}

public String getshapename() {
return shapename;
}

public void setshapename(String shapename) {
this.shapename=shapename;
}

double getarea() {
return 0.0;
}

boolean validate() {
return false;
}

public String toString(){
return getshapename()+":"+String.format("%.2f ",getarea());
}




}

class Circle extends Shape{
double r;

Circle() {


}
Circle(double r) {
this.r=r;
}

public double getr() {
return r;
}

public void setr(double r) {
this.r=r;
}

public double getarea() {
return Math.PI*r*r;
}

public boolean validate() {
if(r>0)
return true;
return false;
}



}

class Triangle extends Shape{

double a,b,c;

Triangle(){

}

Triangle(double a,double b,double c){
this.a=a;
this.b=b;
this.c=c;
}

public double getarea(){
double p=(a+b+c)/2;
double s=Math.sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}

public boolean validate() {
if(a<0||b<0||c<0)
return false;
else if(a+b<=c||a+c<=b||b+c<=a)
{
return false;
}

return true;
}
}

class Rectangle extends Shape{

double width;
double length;
Rectangle(){
}
Rectangle(double width,double length)
{
this.width=width;
this.length=length;
}

public double getwidth(){
return width;
}
public void setwidth(double width) {
this.width=width;
}
public double getlenth(){
return length;
}
public void setlenth(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{
double sd;
double xd;
double h;
Trapezoid(){

}
Trapezoid(double sd,double xd,double h){
this.sd=sd;
this.xd=xd;
this.h=h;

}
public double getarea() {
return (sd+xd)*h*0.5;
}

public boolean validate() {
if(sd>0&&xd>0&&h>0)
return true;
else
return false;
}
}

class Card implements Comparable<Card>{
Shape shape;
Card(){

}
Card (Shape shape){
this.shape=shape;
}
public Shape getshape() {
return shape;
}

public void setshape(Shape shape) {
this.shape=shape;
}
public int compareTo(Card o) {
// TODO Auto-generated method stub
return -(int)(shape.getarea()-o.getshape().getarea());
}

}
class DealCardList{
ArrayList<Card> cardList=new ArrayList<>();
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() {

for(Card cardname : cardList) {
if(cardname.getshape().validate())
return true;
}
return false;
}

@SuppressWarnings("unchecked")
public void cardSort() {
Collections.sort(this.cardList);

}

public double getall() {
double ss=0;
for(Card c : cardList) {
ss=ss+c.getshape().getarea();
}
return ss;
}
public double getmax() {


double max1=0,max2=0,max3=0,max4=0;

for(Card a : cardList){

if(a.getshape().getshapename()=="Circle")
{
max1=max1+a.getshape().getarea();
}
if(a.getshape().getshapename()=="Rectangle")
{
max2=max2+a.getshape().getarea();
}
if(a.getshape().getshapename()=="Triangle")
{
max3=max3+a.getshape().getarea();
}
if(a.getshape().getshapename()=="Trapezoid")
{
max4=max4+a.getshape().getarea();
}

}
double a[] = {max1,max2,max3,max4};
double max5=0;


for(int i=0;i<a.length-1;i++){
if(max5<a[i]){
max5=a[i];
}
}

return max5;
}


public void show() {

System.out.println("The original List:");
System.out.print("[");
for (Card card : cardList) {
System.out.print(card.getshape().getshapename());
System.out.print(":"+String.format("%.2f ",card.getshape().getarea()) );

}
System.out.print("]");
System.out.println();

System.out.println("The Separated list:");

System.out.print("[");
for (Card cardt : cardList) {
if(cardt.getshape().getshapename()=="Circle")
{
System.out.print(cardt.getshape().getshapename());
System.out.print(":"+String.format("%.2f ",cardt.getshape().getarea()) );
}
}
System.out.print("]");

System.out.print("[");
for (Card cardt : cardList) {

if(cardt.getshape().getshapename()=="Rectangle")
{

System.out.print(cardt.getshape().getshapename());
System.out.print(":"+String.format("%.2f ",cardt.getshape().getarea()) );
}
}
System.out.print("]");

System.out.print("[");
for (Card cardt : cardList) {

if(cardt.getshape().getshapename()=="Triangle")
{

System.out.print(cardt.getshape().getshapename());
System.out.print(":"+String.format("%.2f ",cardt.getshape().getarea()) );

}

}System.out.print("]");

System.out.print("[");
for (Card cardt : cardList) {

if(cardt.getshape().getshapename()=="Trapezoid")
{

System.out.print(cardt.getshape().getshapename());
System.out.print(":"+String.format("%.2f ",cardt.getshape().getarea()) );


}

}
System.out.print("]");
//////////////////////////////////////////////////////////////////
cardSort();
System.out.println();

System.out.println("The Separated sorted List:");

System.out.print("[");
for (Card cardt : cardList) {
if(cardt.getshape().getshapename()=="Circle")
{
System.out.print(cardt.getshape().getshapename());
System.out.print(":"+String.format("%.2f ",cardt.getshape().getarea()) );
}
}
System.out.print("]");

System.out.print("[");
for (Card cardt : cardList) {

if(cardt.getshape().getshapename()=="Rectangle")
{

System.out.print(cardt.getshape().getshapename());
System.out.print(":"+String.format("%.2f ",cardt.getshape().getarea()) );
}
}
System.out.print("]");

System.out.print("[");
for (Card cardt : cardList) {

if(cardt.getshape().getshapename()=="Triangle")
{

System.out.print(cardt.getshape().getshapename());
System.out.print(":"+String.format("%.2f ",cardt.getshape().getarea()) );

}

}System.out.print("]");

System.out.print("[");
for (Card cardt : cardList) {

if(cardt.getshape().getshapename()=="Trapezoid")
{

System.out.print(cardt.getshape().getshapename());
System.out.print(":"+String.format("%.2f ",cardt.getshape().getarea()) );


}

}
System.out.print("]");



System.out.println();
System.out.printf("The max area:%.2f\n",getmax());
}
}

 

 

public class Main {
//在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
//使用Main.input.next…即可(避免采坑)
public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
int num = input.nextInt();
while(num != 0){
if(num < 0 || num > 4){
System.out.println("Wrong Format");
System.exit(0);
}
list.add(num);
num = input.nextInt();
}
DealCardList dealCardList = new DealCardList(list);
if(!dealCardList.validate()){
System.out.println("Wrong Format");
System.exit(0);

}
dealCardList.show();
input.close();
}
}

 

 

设计ATM仿真系统,具体要求参见作业说明。 OO作业8-1题目说明.pdf

输入格式:

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

  • 存款、取款功能输入数据格式: 卡号 密码 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编号]上存款¥[金额]

当前余额为¥[金额]

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

④查询余额业务输出

¥[金额]

 


import java.util.Scanner;
import java.util.ArrayList;

//Bank类
class Bank {
String bankname;
ArrayList<String> ATMList;
//创建无参构造方法
public Bank(){
}
//创建带参构造方法
public Bank(String bankname,ArrayList<String> ATMList)
{
this.bankname=bankname;
this.ATMList=ATMList;
}
//getter
public String getBankname() {
return bankname;
}
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{
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;

}
//检查是否跨行
if(flag==4){
for(i=0;i<accountList.get(k).bank.ATMList.size();i++){
if(number.equals(accountList.get(k).bank.ATMList.get(i))){
flag=5;
break;
}
}
}
if(flag!=5){
System.out.println("Sorry,cross-bank withdrawal is not supported.");//跨行存取款
return false;
}
else
return true;
}

}


//主类
public class Main {
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);
ArrayList<String> ATMList2 = new ArrayList<>();
ATMList2.add("05");
ATMList2.add("06");
Bank gsyh = new Bank("中国工商银行", ATMList2);
//银行数组
ArrayList<Bank> bankList = new ArrayList<>();
bankList.add(jsyh);
bankList.add(gsyh);
//用户数组
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<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);

Scanner x=new Scanner(System.in);
String kp;
Check check;

kp=x.nextLine();
while(!kp.equals("#")){
int i,j;
String[] shuju=kp.split("\\s+");//输入的每行数据用空格隔开,存为数组
/*
for(i=0;i<shuju.length;i++) {
System.out.println(shuju[i]);
}
*/
int m=4;
if(shuju.length!=1){
check = new Check(accountList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3]));
if (check.check()){
if(m==5)
m=3;
}
}

kp=x.nextLine();
}
}
}

 

 

 

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

设计ATM仿真系统,具体要求参见作业说明。 OO作业9-1题目说明.pdf

输入格式:

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

  • 取款功能输入数据格式: 卡号 密码 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编号]上取款¥[金额]

当前余额为¥[金额]

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

③查询余额业务输出

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

 

package fff;

import java.util.*;

 

class Bank{
String bankname;
ArrayList<String> atm;

Bank(String bankname,ArrayList <String> atm)
{
this.bankname=bankname;
this.atm=atm;
}

public String getbankname(){
return bankname;
}

public void setbankname (String bankname){
this.bankname=bankname;
}
public ArrayList<String> getatm(){
return atm;
}

public void setatm(ArrayList<String> atm){
this.atm=atm;
}

public boolean atmtest(String catm) {
for(String a : atm ) {
if(catm.equals(a))
return true;
}
return false;
}


}

//----------------------------------

class Account{

String name;
Bank bank;

String account;
String password;
double balance;
String cardnum;
ArrayList <Bank> banklist;

Account(String name,Bank bank,String account,
String password, double balance, String card
)
{
this.name=name;
this.bank=bank;

this.account=account;
this.password=password;
this.balance = balance;
this.cardnum=card;
}

public String getname(){
return name;
}

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

public Bank getbank(){
return bank;
}

public void setbank(Bank bank){
this.bank= bank;
}

public String getaccount(){
return account;
}

public void setaccount(String account){
this.account= account;
}

public String getpassword(){
return password;
}

public void setpassword(String password){
this.password= password;
}

public double getbalance(){
return balance;
}

public void setbalance(double balance){
this.balance= balance;
}

public String getcard(){
return cardnum;
}

public void setcard(String cardnum){
this.cardnum= cardnum;
}

public boolean passwordtest(String cpassword) {

if(password.equals(cpassword))
{
return true;
}


return false;
}

}


class cwMatch{


String atm;
ArrayList <Account> acclist;
ArrayList<String> allatmlist;
String password;
double balance;
String card;


cwMatch(String card,String password,String atm,

double balance,ArrayList <Account> acclist,ArrayList<String> allatmlist){

this.atm=atm;
this.password=password;
this.balance = balance;
this.card=card;
this.acclist=acclist;
this.allatmlist=allatmlist;

}

public int show() {
int flag=0;
int i=0;int j=0;


for(i=0;i<acclist.size();i++) {

if(card.equals(this.acclist.get(i).cardnum)) {
flag=1;break;
}
}

if(flag==0) {
System.out.println("Sorry,this card does not exist.");

}


if(flag==1) {
for( String a : allatmlist) {
if(atm.equals(a)) {
flag=2;break;
}
}
}


if(flag==1) {
System.out.println("Sorry,the ATM's id is wrong.");
}



if(flag==2) {
for(j=0;j<acclist.size();j++) {
if(acclist.get(i).passwordtest(password)) {
flag=3;
break;
}
}
}


if(flag==2) {
System.out.println("Sorry,your password is wrong.");
}


if(flag==3) {
if(balance<=this.acclist.get(i).balance) {
flag=4;
}
}

if(flag==3)
System.out.println("Sorry,your account balance is insufficient.");



if(flag==4) {

if( acclist.get(i).getbank().atmtest(atm)) {
flag=5;
}
}

if(flag==4)
System.out.println("Sorry,cross-bank withdrawal is not supported.");

if(flag==5)
System.out.println("....");

return i;
}


}

class chunqu{
String atm;
ArrayList <Account> acclist;
double balance;
String card;
int i;

public chunqu(String atm,String card,double balance,ArrayList <Account> acclist,int key){
this.atm=atm;
this.balance = balance;
this.card=card;
this.acclist=acclist;
this.i=key;
}

public void showm() {

acclist.get(i).balance=acclist.get(i).balance-balance;

if(balance>0) {
System.out.println(acclist.get(i).getname()+"在"
+acclist.get(i).getbank().getbankname()
+"的"+atm+"号 ATM 机上取款"+balance);
}

if(balance<0) {
System.out.println(acclist.get(i).getname()+"在"
+acclist.get(i).getbank().getbankname()
+"的"+atm+"号 ATM 机上存款"+balance);
}

System.out.println("当前余额为¥"+String.format("%.2f",acclist.get(i).getbalance()));
}
}

public class Main{
public static void main(String[] args) {

ArrayList<String> jsatmlist = new ArrayList<String>();
jsatmlist.add("01");
jsatmlist.add("02");
jsatmlist.add("03");
jsatmlist.add("04");

ArrayList<String> gsatmlist = new ArrayList<String>();
gsatmlist.add("05");
gsatmlist.add("06");

ArrayList<String> allatmlist = new ArrayList<String>();
allatmlist.add("01");
allatmlist.add("02");
allatmlist.add("03");
allatmlist.add("04");
allatmlist.add("05");
allatmlist.add("06");

Bank js = new Bank("中国建设银行", jsatmlist);//

Bank gs = new Bank("中国工商银行", gsatmlist);//创建工商银行对象



Account a1 = new Account("杨过",js,"3217000010041315709"
,"88888888",10000.00,"6217000010041315709");
Account a2 = new Account("杨过",js,"3217000010041315709"
,"88888888",10000.00,"6217000010041315715");
Account a3 = new Account("杨过",js,"3217000010041315715"
,"88888888",10000.00,"6217000010041315718");

Account a4 = new Account("郭靖",js,"3217000010051320007"
,"88888888",10000.00,"6217000010051320007");

Account a5 = new Account("张无忌",gs,"3222081502001312389"
,"88888888",10000.00,"6222081502001312389");

Account a6 = new Account("张无忌",gs,"3222081502001312390"
,"88888888",10000.00,"6222081502001312390");

Account a7 = new Account("张无忌",gs,"3222081502001312399"
,"88888888",10000.00,"6222081502001312399");

Account a8 = new Account("张无忌",gs,"3222081502001312399"
,"88888888",10000.00,"6222081502001312400");

Account a9 = new Account("韦小宝",gs,"3222081502051320785"
,"88888888",10000.00,"6222081502051320785");

Account a10 = new Account("韦小宝",gs,"3222081502051320786"
,"88888888",10000.00,"6222081502051320786");

ArrayList<Account> acclist = new ArrayList<Account>();
acclist.add(a1);acclist.add(a2);acclist.add(a3);
acclist.add(a4);acclist.add(a5);acclist.add(a6);
acclist.add(a7);acclist.add(a8);acclist.add(a9);
acclist.add(a10);


Scanner x=new Scanner(System.in);
String kp;
cwMatch cwmatch;

kp=x.nextLine();
while(!kp.equals("#")){
//int i,j;
String[] shuju=kp.split("\\s+");//输入的每行数据用空格隔开,存为数组
/*
for(i=0;i<shuju.length;i++) {
System.out.println(shuju[i]);
}
*/

if(shuju.length!=1) {
cwmatch = new cwMatch(shuju[0],shuju[1],shuju[2], Double.parseDouble(shuju[3])
,acclist,allatmlist);//创建match对象

int key=cwmatch.show();
chunqu chunqu1 = new chunqu(shuju[0],shuju[1],Double.parseDouble(shuju[3])
,acclist,key);

chunqu1.showm();
}




kp=x.nextLine();
}
}
}

 

 

(3)采坑心得

如何对数据进行降序处理是这道题的一个关键点,list集合有对元素的排序方法,但是默认的是升序,只有重新排序方法才能使元素升序。

(4)改进建议

使用正则表达式匹配解决字符串匹配问题,将存取款设计成实体类,更加简单清晰。

(5)总结

在本次学习中;对类的构造更加熟悉,但是还应该多做题目多练习。

对ArrayList的使用更加深刻。

面向对象编程不同于面向过程编程,在创立一个对象后便有明确的功能性,通过继承,多态,接口可以实现不同对象实现不同的功能。

其中在类设计上,通过学习类属性的访问权限,初步了解了四种访问权限:public,private,protected,default;

以及不同类的类型:实体类,业务类,接口类;对于类设计上,学习的多。

 

 

 

 

 

 

 

 

 

posted on 2021-12-18 22:03  192072  阅读(39)  评论(0编辑  收藏  举报