[Java]题目集7-9总结
[Java]题目集7-9总结
一.前言:
总结三次题目集的知识点、题量、难度等情况:
题目集7考察的题目为“图形卡片排序游戏”,里面考察的知识点有
- 继承、多态的应用;
- ArrayList泛型的应用方法
- Comparable接口及泛型的应用
- 单一职责原则的应用
- “开-闭”原则的应用
其中,题目集7包含两个题目,两个题目的区别是对类聚合程度的不同与其设计思路的不同,从而在类图上体现出来的类图的继承属性的不一样,其中第二小题为了防止用第一题的代码骗取测试点,其答案也不一样,必须在作业7-1的基础上进行进一步改进和完善。
考虑面向对象设计的“单一职责原则”,思考该程序是否能够符合“开-闭”原则。
还需要进一步掌握接口在面向对象程序设计中的作用。
需要正确理解:类结构如何完善才能使得系统具有较好的可复用性。通过此例,进一步深入理解面向对象设计原则中的“单一职责”原则和“开-闭”原则。
而题目集8-9,则是考察仿真ATM机的设计,其中第八题的ATM机取存款仅包含借记卡,没有信用卡存取款这一功能,即无贷款利息等其他繁琐的步骤,总体而言较为简单,只需要根据类图的指示,按步骤写,并加入适当的算法即可,而题目集9则是题目集8的升级版,其中升级的内容有:存在信用卡这一栏,可以贷款但需要算一定比例的利息,通过还增加了跨行取款的功能,不过也是需要支付一定百分比的费用的。
由此以来,题目集9的难度也大大增加了,其中需要在原有的基础上加入判断跨行取款的方法,跨行取款以及信用卡贷款利息的选择与计算,同时某些类也需要发生改变,如在原有卡类的基础上还需要重新开辟出一个信用卡类,以此来进行该题目功能要求的实现。
主要考察的知识点有:务必注意本题目中的各实体类之间的关系,尤其是一对多的组合关系;对实体类的设计要做到单一职责原则,且不能缺少规定的实体类;编程时考虑面向对象中的封装性本题目中的应用以及是否有能够扩展的衍生需求;在“合成复用原则”及“单一职责原则”基础上,尽量对上次作业的程序进行重构,使之符合“开-闭”原则。
设计与分析:
题目集7——-1:
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图形名称2:面积值2 …图形名称n:面积值n,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格; - 排序后的各图形类型及面积,格式同排序前的输出;
- 所有图形的面积总和,格式为
Sum of area:总面积值。
输入样例1:
在这里给出一组输入。例如:
1 5 3 2 0
输出样例1:
在这里给出相应的输出。例如:
Wrong Format
输入样例2:
在这里给出一组输入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5
输出样例2:
在这里给出相应的输出。例如:
The original list:
Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02
The sorted list:
Circle:98.52 Triangle:4.02 Rectangle:3.22 Trapezoid:1.14
Sum of area:106.91
输入样例3:
在这里给出一组输入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 8.4
输出样例3:
在这里给出相应的输出。例如:
Wrong Format
源代码:
package ptaTest.demo07;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
class Shape{
String shapeName;
public Shape(String
shapeName) {
this.shapeName = shapeName;
}
public Shape() {
}
public String
getShapeName() {
return shapeName;
}
public void setShapeName(String
shapeName) {
this.shapeName = shapeName;
}
public double getArea(){
double area=0;
return area;
}
public boolean vaildate(){
return true;
}
@Override
public String
toString() {
return super.toString();
}
}
class Circle extends
Shape{
double radius;
public Circle(double radius) {
this.radius = radius;
}
public Circle() {
}
@Override
public double getArea( ){
double area
=Math.PI*radius*radius;
return area;
}
@Override
public boolean vaildate() {
boolean result
=true;
if(radius<=0){
result =false;
return result;
}else
return result;
}
}
class Rectangle extends
Shape{
double width;
double length;
public Rectangle(double width,
double length) {
this.width = width;
this.length
= length;
}
public Rectangle() {
}
@Override
public double getArea() {
double area
=width*length;
return area;
}
@Override
public boolean vaildate() {
if(width<0||length<0)
return false;
else
return true;
}
}
class Triangle extends
Shape{
double side1;
double side2;
double side3;
public Triangle(double side1,
double side2, double side3) {
this.side1 = side1;
this.side2
= side2;
this.side3
= side3;
}
public Triangle() {
}
@Override
public boolean vaildate() {
if(side1<0||side2<0||side3<0){
int a;//
return false;
}
else if(side1+side2>side3&&side1+side3>side2&&side2+side3>side3){
return true;}
else
return false;
}
@Override
public double getArea() {
double area=0;
double s =(side1+side2+side3)/2f;
double S = (float) Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return S;
}
}
class Trapeeziod extends
Shape{
double topSide;
double bottomSide;
double height;
public Trapeeziod(double topSide,
double bottomSide, double height) {
this.topSide = topSide;
this.bottomSide
= bottomSide;
this.height
= height;
}
public Trapeeziod() {
}
@Override
public double getArea() {
double area
=(topSide+bottomSide)*height/2.0;
return area;
}
@Override
public boolean vaildate() {
if(topSide<0||bottomSide<0||height<0)
return false;
else
return true;
}
}
class Card extends
Shape{
Shape shape;
public Card(Shape
shape) {
this.shape = shape;
}
public Card() {
}
public Shape
getShape() {
return shape;
}
public void setShape(Shape shape) {
this.shape = shape;
}
}
class DealCardList extends Card{
ArrayList<Card> cardList =new ArrayList<Card>();
ArrayList<Card> cardListCircle =new ArrayList<Card>();
ArrayList<Card> cardListRectangle =new ArrayList<Card>();
ArrayList<Card> cardListTriangle =new ArrayList<Card>();
ArrayList<Card> cardListTrapeziod =new ArrayList<Card>();
DealCardList() {
}
DealCardList(ArrayList<Integer>list){
for (int i = 0; i <list.size() ; i++) {
if (list.get(i)==1){
Circle circle =new Circle();
circle.radius=p1.input.nextDouble();
Card card =new Card(circle);
card.getShape().setShapeName("Circle");
cardList.add(card);
cardListCircle.add(card);
}
else if(list.get(i)==2){
Rectangle rectangle =new Rectangle();
rectangle.length=p1.input.nextDouble();
rectangle.width=p1.input.nextDouble();
Card card =new Card(rectangle);
card.getShape().setShapeName("Rectangle");
cardList.add(card);
cardListRectangle.add(card);
}
else if(list.get(i)==3){
Triangle triangle =new Triangle();
triangle.side1=p1.input.nextDouble();
triangle.side2=p1.input.nextDouble();
triangle.side3=p1.input.nextDouble();
Card card =new Card(triangle);
card.getShape().setShapeName("Triangle");
cardList.add(card);
cardListTriangle.add(card);
}
else if(list.get(i)==4){
Trapeeziod trapeeziod =new Trapeeziod();
trapeeziod.topSide=p1.input.nextDouble();
trapeeziod.bottomSide=p1.input.nextDouble();
trapeeziod.height=p1.input.nextDouble();
Card card =new Card(trapeeziod);
card.getShape().setShapeName("Trapezoid");
cardList.add(card);
cardListTrapeziod.add(card);
}
}
}
public void cardSort(
ArrayList<Card> cardList)
{
Collections.sort(cardList,new SortByArea());
/*for(int j=0;j<cardList.size();j++)
System.out.print(cardList.get(j).getShape().getShapeName()+":"+String.format("%.2f",
cardList.get(j).getShape().getArea())+" ");//
System.out.println();*/
}
public double getAllArea(ArrayList<Card>
cardList){
double allArea=
0;
for (int i = 0; i <cardList.size() ; i++) {
allArea=allArea+cardList.get(i).getShape().getArea();
}
return allArea;
}
public double getAllArea2(){
double[]
areaAll =new double[4];
areaAll[0]=getAllArea(cardListCircle);
areaAll[1]=getAllArea(cardListRectangle);
areaAll[2]=getAllArea(cardListTriangle);
areaAll[3]=getAllArea(cardListTrapeziod);
/*double areaCircle=0;
for (int i = 0; i
<cardListCircle.size() ; i++) {
areaCircle=areaCircle+cardListCircle.get(i).getShape().getArea();
}
areaAll[0]=areaCircle;
double areaRectangle=0;
for (int i = 0; i
<cardListRectangle.size() ; i++) {
areaRectangle=areaRectangle+cardListRectangle.get(i).getShape().getArea();
}
areaAll[1]=areaRectangle;
double areaTriangle=0;
for (int i = 0; i
<cardListTriangle.size() ; i++) {
areaRectangle=areaRectangle+cardListTriangle.get(i).getShape().getArea();
}
areaAll[2]=areaTriangle;
double areaTrapezoid=0;
for (int i = 0; i
<cardListTrapeziod.size() ; i++) {
areaRectangle=areaRectangle+cardListTrapeziod.get(i).getShape().getArea();
}
areaAll[3]=areaTrapezoid;*/
double maxArea
=areaAll[0];
for (int i = 1; i <4 ; i++) {
if(maxArea<areaAll[i]){
maxArea=areaAll[i];
}
}
return maxArea;
}
public void showResult() {
System.out.println("The
original list:");
for (int i = 0; i <cardList.size()
; i++) {
System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
}
System.out.println();
System.out.println("The sorted list:");
cardSort(cardList);
System.out.println("Sum of area:"+String.format("%.2f",getAllArea2()));
}
public void showResult3(
ArrayList<Card> cardList) {
for (int i = 0; i <cardList.size() ; i++) {
System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
}
}
public void showResult23(){
System.out.println("The
original list:");
System.out.print("[");
showResult3(cardList);
System.out.print("]");
System.out.println();
System.out.println("The Separated List:");
System.out.print("[");
showResult3(cardListCircle);
System.out.print("]");
System.out.print("[");
showResult3(cardListRectangle);
System.out.print("]");
System.out.print("[");
showResult3(cardListTriangle);
System.out.print("]");
System.out.print("[");
showResult3(cardListTrapeziod);
System.out.print("]");
System.out.println();
cardSort(cardListCircle);
cardSort(cardListRectangle);
cardSort(cardListTrapeziod);
cardSort(cardListTriangle);
//cardSort();//排序后
System.out.println("The Separated sorted List:");
System.out.print("[");
showResult3(cardListCircle);
System.out.print("]");
System.out.print("[");
showResult3(cardListRectangle);
System.out.print("]");
System.out.print("[");
showResult3(cardListTriangle);
System.out.print("]");
System.out.print("[");
showResult3(cardListTrapeziod);
System.out.print("]");
System.out.println();
System.out.print("The max area:");
System.out.println(String.format("%.2f",getAllArea2()));
}
public void showResult2(){
System.out.println("The
original list:");
System.out.print("[");
for (int i = 0; i <cardList.size()
; i++) {
//if(i==0) System.out.print("[");
System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
if(i==-1){}//
//if(i==cardList.size()-1)
System.out.print("]");
}
System.out.print("]");
System.out.println();
System.out.println("The Separated List:");
System.out.print("[");
for (int i = 0; i <cardListCircle.size(); i++) {
//if(i==0) System.out.print("[");
if(i==-99){}
System.out.print(cardListCircle.get(i).getShape().shapeName+":"+String.format("%.2f",cardListCircle.get(i).getShape().getArea())+" ");
// if(i==cardListCircle.size()-1) System.out.print("]");
}
System.out.print("]");
//if(cardListCircle.size()==0)
System.out.print("[]");
System.out.print("[");
for (int i = 0; i <cardListRectangle.size() ; i++) {
// if(i==0) System.out.print("[");
if(i==-98){}//
System.out.print(cardListRectangle.get(i).getShape().shapeName+":"+String.format("%.2f",cardListRectangle.get(i).getShape().getArea())+" ");
// if(i==cardListRectangle.size()-1) System.out.print("]");
}
System.out.print("]");
//if(cardListRectangle.size()==0)
System.out.print("[]");
System.out.print("[");
for (int i = 0; i <cardListTriangle.size() ; i++) {
//if(i==0) System.out.print("[");
if(i==-97){}//
System.out.print(cardListTriangle.get(i).getShape().shapeName+":"+String.format("%.2f",cardListTriangle.get(i).getShape().getArea())+" ");
//if(i==cardListTriangle.size()-1) System.out.print("]");
}
System.out.print("]");
//if(cardListTriangle.size()==0)
System.out.print("[]");
System.out.print("[");
for (int i = 0; i <cardListTrapeziod.size() ; i++) {
//if(i==0)
System.out.print("[");
if(i==-96){}//
System.out.print(cardListTrapeziod.get(i).getShape().shapeName+":"+String.format("%.2f",cardListTrapeziod.get(i).getShape().getArea())+" ");
//if(i==cardListTrapeziod.size()-1) System.out.print("]");
}
System.out.print("]");
//if(cardListTrapeziod.size()==0)
System.out.print("[]");
System.out.println();
cardSort(cardListCircle);
cardSort(cardListRectangle);
cardSort(cardListTrapeziod);
cardSort(cardListTriangle);
//cardSort();//排序后
System.out.println("The Separated sorted List:");
for (int i = 0; i <cardListCircle.size(); i++) {
if(i==0) System.out.print("[");
System.out.print(cardListCircle.get(i).getShape().shapeName+":"+String.format("%.2f",cardListCircle.get(i).getShape().getArea())+" ");
if(i==cardListCircle.size()-1)
System.out.print("]");
}
if(cardListCircle.size()==0) System.out.print("[]");
for (int i = 0; i <cardListRectangle.size() ; i++) {
if(i==0) System.out.print("[");
if(i==-1){}//
System.out.print(cardListRectangle.get(i).getShape().shapeName+":"+String.format("%.2f",cardListRectangle.get(i).getShape().getArea())+" ");
if(i==cardListRectangle.size()-1) System.out.print("]");
}
if(cardListRectangle.size()==0) System.out.print("[]");
for (int i = 0; i <cardListTriangle.size() ; i++) {
if(i==0) System.out.print("[");
if(i==-2){}//
System.out.print(cardListTriangle.get(i).getShape().shapeName+":"+String.format("%.2f",cardListTriangle.get(i).getShape().getArea())+" ");
if(i==cardListTriangle.size()-1) System.out.print("]");
}
if(cardListTriangle.size()==0) System.out.print("[]");
for (int i = 0; i <cardListTrapeziod.size() ; i++) {
if(i==0) System.out.print("[");
if(i==-3){}//
System.out.print(cardListTrapeziod.get(i).getShape().shapeName+":"+String.format("%.2f",cardListTrapeziod.get(i).getShape().getArea())+" ");
if(i==cardListTrapeziod.size()-1) System.out.print("]");
}
if(cardListTrapeziod.size()==0) System.out.print("[]");
System.out.println();
System.out.print("The max area:");
System.out.println(String.format("%.2f",getAllArea2()));
}
public boolean validate() {
int n=0;
for (int i = 0; i <cardList.size()
; i++) {
if(cardList.get(i).getShape().vaildate()==false)
n++;
}
if(n==0)
return true;
else
return false;
}
}
class SortByArea implements
Comparator {
public int compare(Object a,Object
b)
{
Card A=(Card) a;
Card B=(Card) b;
if(A.getShape().getArea()>B.getShape().getArea())
return -1;
else
return 1;
}
}
public class p1 {
public static Scanner input= new Scanner(System.in);
public static void main(String[] args) {
ArrayList<Integer> list= new ArrayList<Integer>();
int num =input.nextInt();
if(num==0){System.out.println("Wrong Format");System.exit(0);}
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.showResult23();
input.close();
}
}
类图:
以下为SourceMonitor 的生成报表:
这题的难度虽然不算大,仅在第二次输出时需要用到链表排序的方法,其他都还中规中矩。
题目集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图形名称2:面积值2 …图形名称n:面积值n ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前; - 输出分组后的图形类型及面积,格式为
[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出; - 各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
- 各组中面积之和的最大值输出,格式为
The max area:面积值。
输入样例1:
在这里给出一组输入。例如:
1 5 3 2 0
输出样例1:
在这里给出相应的输出。例如:
Wrong Format
输入样例2:
在这里给出一组输入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5
输出样例2:
在这里给出相应的输出。例如:
The original list:
[Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02 ]
The Separated List:
[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]
The Separated sorted List:
[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]
The max area:98.52
输入样例3:
在这里给出一组输入。例如:
2 1 2 1 1 3 3 4 4 1 1 1 2 1 0
2.3 3.5 2.5 4.5 2.1 2.6 8.5 3.2 3.1 3.6 8.5 7.5 9.1245 6.5 3.4 10.2 11.2 11.6 15.4 5.8 2.13 6.2011 2.5 6.4 18.65
输出样例3:
在这里给出相应的输出。例如:
The original list:
[Rectangle:8.05 Circle:19.63 Rectangle:9.45 Circle:21.24 Circle:226.98 Triangle:4.65 Triangle:29.80 Trapezoid:50.49 Trapezoid:175.56 Circle:105.68 Circle:14.25 Circle:120.81 Rectangle:16.00 Circle:1092.72 ]
The Separated List:
[Circle:19.63 Circle:21.24 Circle:226.98 Circle:105.68 Circle:14.25 Circle:120.81 Circle:1092.72 ][Rectangle:8.05 Rectangle:9.45 Rectangle:16.00 ][Triangle:4.65 Triangle:29.80 ][Trapezoid:50.49 Trapezoid:175.56 ]
The Separated sorted List:
[Circle:1092.72 Circle:226.98 Circle:120.81 Circle:105.68 Circle:21.24 Circle:19.63 Circle:14.25 ][Rectangle:16.00 Rectangle:9.45 Rectangle:8.05 ][Triangle:29.80 Triangle:4.65 ][Trapezoid:175.56 Trapezoid:50.49 ]
The max area:1601.31
输入样例4:
在这里给出一组输入。例如:
1 1 3 0
6.5 12.54 3.6 5.3 6.4
输出样例4:
在这里给出相应的输出。例如:
The original list:
[Circle:132.73 Circle:494.02 Triangle:9.54 ]
The Separated List:
[Circle:132.73 Circle:494.02 ][][Triangle:9.54 ][]
The Separated sorted List:
[Circle:494.02 Circle:132.73 ][][Triangle:9.54 ][]
The max area:626.75
源代码:
package ptaTest.demo07;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
class Shape{
String shapeName;
public Shape(String
shapeName) {
this.shapeName = shapeName;
}
public Shape() {
}
public String
getShapeName() {
return shapeName;
}
public void setShapeName(String
shapeName) {
this.shapeName = shapeName;
}
public double getArea(){
double area=0;
return area;
}
public boolean vaildate(){
return true;
}
@Override
public String
toString() {
return super.toString();
}
}
class Circle extends
Shape{
double radius;
public Circle(double radius) {
this.radius = radius;
}
public Circle() {
}
@Override
public double getArea( ){
double area
=Math.PI*radius*radius;
return area;
}
@Override
public boolean vaildate() {
boolean result
=true;
if(radius<=0){
result =false;
return result;
}else
return result;
}
}
class Rectangle extends
Shape{
double width;
double length;
public Rectangle(double width,
double length) {
this.width = width;
this.length
= length;
}
public Rectangle() {
}
@Override
public double getArea() {
double area
=width*length;
return area;
}
@Override
public boolean vaildate() {
if(width<0||length<0)
return false;
else
return true;
}
}
class Triangle extends
Shape{
double side1;
double side2;
double side3;
public Triangle(double side1,
double side2, double side3) {
this.side1 = side1;
this.side2
= side2;
this.side3
= side3;
}
public Triangle() {
}
@Override
public boolean vaildate() {
if(side1<0||side2<0||side3<0){
int a;//
return false;
}
else if(side1+side2>side3&&side1+side3>side2&&side2+side3>side3){
return true;}
else
return false;
}
@Override
public double getArea() {
double area=0;
double s =(side1+side2+side3)/2f;
double S = (float) Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return S;
}
}
class Trapeeziod extends
Shape{
double topSide;
double bottomSide;
double height;
public Trapeeziod(double topSide,
double bottomSide, double height) {
this.topSide = topSide;
this.bottomSide
= bottomSide;
this.height
= height;
}
public Trapeeziod() {
}
@Override
public double getArea() {
double area
=(topSide+bottomSide)*height/2.0;
return area;
}
@Override
public boolean vaildate() {
if(topSide<0||bottomSide<0||height<0)
return false;
else
return true;
}
}
class Card extends
Shape{
Shape shape;
public Card(Shape
shape) {
this.shape = shape;
}
public Card() {
}
public Shape
getShape() {
return shape;
}
public void setShape(Shape shape) {
this.shape = shape;
}
}
class DealCardList extends Card{
ArrayList<Card> cardList =new ArrayList<Card>();
ArrayList<Card> cardListCircle =new ArrayList<Card>();
ArrayList<Card> cardListRectangle =new ArrayList<Card>();
ArrayList<Card> cardListTriangle =new ArrayList<Card>();
ArrayList<Card> cardListTrapeziod =new ArrayList<Card>();
DealCardList() {
}
DealCardList(ArrayList<Integer>list){
for (int i = 0; i <list.size() ; i++) {
if (list.get(i)==1){
Circle circle =new Circle();
circle.radius=p1.input.nextDouble();
Card card =new Card(circle);
card.getShape().setShapeName("Circle");
cardList.add(card);
cardListCircle.add(card);
}
else if(list.get(i)==2){
Rectangle rectangle =new Rectangle();
rectangle.length=p1.input.nextDouble();
rectangle.width=p1.input.nextDouble();
Card card =new Card(rectangle);
card.getShape().setShapeName("Rectangle");
cardList.add(card);
cardListRectangle.add(card);
}
else if(list.get(i)==3){
Triangle triangle =new Triangle();
triangle.side1=p1.input.nextDouble();
triangle.side2=p1.input.nextDouble();
triangle.side3=p1.input.nextDouble();
Card card =new Card(triangle);
card.getShape().setShapeName("Triangle");
cardList.add(card);
cardListTriangle.add(card);
}
else if(list.get(i)==4){
Trapeeziod trapeeziod =new Trapeeziod();
trapeeziod.topSide=p1.input.nextDouble();
trapeeziod.bottomSide=p1.input.nextDouble();
trapeeziod.height=p1.input.nextDouble();
Card card =new Card(trapeeziod);
card.getShape().setShapeName("Trapezoid");
cardList.add(card);
cardListTrapeziod.add(card);
}
}
}
public void cardSort(
ArrayList<Card> cardList)
{
Collections.sort(cardList,new SortByArea());
/*for(int j=0;j<cardList.size();j++)
System.out.print(cardList.get(j).getShape().getShapeName()+":"+String.format("%.2f",
cardList.get(j).getShape().getArea())+" ");//
System.out.println();*/
}
public double getAllArea(ArrayList<Card>
cardList){
double allArea=
0;
for (int i = 0; i <cardList.size() ; i++) {
allArea=allArea+cardList.get(i).getShape().getArea();
}
return allArea;
}
public double getAllArea2(){
double[]
areaAll =new double[4];
areaAll[0]=getAllArea(cardListCircle);
areaAll[1]=getAllArea(cardListRectangle);
areaAll[2]=getAllArea(cardListTriangle);
areaAll[3]=getAllArea(cardListTrapeziod);
/*double areaCircle=0;
for (int i = 0; i
<cardListCircle.size() ; i++) {
areaCircle=areaCircle+cardListCircle.get(i).getShape().getArea();
}
areaAll[0]=areaCircle;
double areaRectangle=0;
for (int i = 0; i
<cardListRectangle.size() ; i++) {
areaRectangle=areaRectangle+cardListRectangle.get(i).getShape().getArea();
}
areaAll[1]=areaRectangle;
double areaTriangle=0;
for (int i = 0; i
<cardListTriangle.size() ; i++) {
areaRectangle=areaRectangle+cardListTriangle.get(i).getShape().getArea();
}
areaAll[2]=areaTriangle;
double areaTrapezoid=0;
for (int i = 0; i
<cardListTrapeziod.size() ; i++) {
areaRectangle=areaRectangle+cardListTrapeziod.get(i).getShape().getArea();
}
areaAll[3]=areaTrapezoid;*/
double maxArea
=areaAll[0];
for (int i = 1; i <4 ; i++) {
if(maxArea<areaAll[i]){
maxArea=areaAll[i];
}
}
return maxArea;
}
public void showResult() {
System.out.println("The
original list:");
for (int i = 0; i <cardList.size()
; i++) {
System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
}
System.out.println();
System.out.println("The sorted list:");
cardSort(cardList);
System.out.println("Sum of area:"+String.format("%.2f",getAllArea2()));
}
public void showResult3(
ArrayList<Card> cardList) {
for (int i = 0; i <cardList.size() ; i++) {
System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
}
}
public void showResult23(){
System.out.println("The
original list:");
System.out.print("[");
showResult3(cardList);
System.out.print("]");
System.out.println();
System.out.println("The Separated List:");
System.out.print("[");
showResult3(cardListCircle);
System.out.print("]");
System.out.print("[");
showResult3(cardListRectangle);
System.out.print("]");
System.out.print("[");
showResult3(cardListTriangle);
System.out.print("]");
System.out.print("[");
showResult3(cardListTrapeziod);
System.out.print("]");
System.out.println();
cardSort(cardListCircle);
cardSort(cardListRectangle);
cardSort(cardListTrapeziod);
cardSort(cardListTriangle);
//cardSort();//排序后
System.out.println("The Separated sorted List:");
System.out.print("[");
showResult3(cardListCircle);
System.out.print("]");
System.out.print("[");
showResult3(cardListRectangle);
System.out.print("]");
System.out.print("[");
showResult3(cardListTriangle);
System.out.print("]");
System.out.print("[");
showResult3(cardListTrapeziod);
System.out.print("]");
System.out.println();
System.out.print("The max area:");
System.out.println(String.format("%.2f",getAllArea2()));
}
public void showResult2(){
System.out.println("The
original list:");
System.out.print("[");
for (int i = 0; i <cardList.size()
; i++) {
//if(i==0) System.out.print("[");
System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
if(i==-1){}//
//if(i==cardList.size()-1)
System.out.print("]");
}
System.out.print("]");
System.out.println();
System.out.println("The Separated List:");
System.out.print("[");
for (int i = 0; i <cardListCircle.size(); i++) {
//if(i==0) System.out.print("[");
if(i==-99){}
System.out.print(cardListCircle.get(i).getShape().shapeName+":"+String.format("%.2f",cardListCircle.get(i).getShape().getArea())+" ");
// if(i==cardListCircle.size()-1) System.out.print("]");
}
System.out.print("]");
//if(cardListCircle.size()==0)
System.out.print("[]");
System.out.print("[");
for (int i = 0; i <cardListRectangle.size() ; i++) {
// if(i==0) System.out.print("[");
if(i==-98){}//
System.out.print(cardListRectangle.get(i).getShape().shapeName+":"+String.format("%.2f",cardListRectangle.get(i).getShape().getArea())+" ");
// if(i==cardListRectangle.size()-1) System.out.print("]");
}
System.out.print("]");
//if(cardListRectangle.size()==0)
System.out.print("[]");
System.out.print("[");
for (int i = 0; i <cardListTriangle.size() ; i++) {
//if(i==0) System.out.print("[");
if(i==-97){}//
System.out.print(cardListTriangle.get(i).getShape().shapeName+":"+String.format("%.2f",cardListTriangle.get(i).getShape().getArea())+" ");
//if(i==cardListTriangle.size()-1) System.out.print("]");
}
System.out.print("]");
//if(cardListTriangle.size()==0)
System.out.print("[]");
System.out.print("[");
for (int i = 0; i <cardListTrapeziod.size() ; i++) {
//if(i==0)
System.out.print("[");
if(i==-96){}//
System.out.print(cardListTrapeziod.get(i).getShape().shapeName+":"+String.format("%.2f",cardListTrapeziod.get(i).getShape().getArea())+" ");
//if(i==cardListTrapeziod.size()-1) System.out.print("]");
}
System.out.print("]");
//if(cardListTrapeziod.size()==0)
System.out.print("[]");
System.out.println();
cardSort(cardListCircle);
cardSort(cardListRectangle);
cardSort(cardListTrapeziod);
cardSort(cardListTriangle);
//cardSort();//排序后
System.out.println("The Separated sorted List:");
for (int i = 0; i <cardListCircle.size(); i++) {
if(i==0) System.out.print("[");
System.out.print(cardListCircle.get(i).getShape().shapeName+":"+String.format("%.2f",cardListCircle.get(i).getShape().getArea())+" ");
if(i==cardListCircle.size()-1)
System.out.print("]");
}
if(cardListCircle.size()==0) System.out.print("[]");
for (int i = 0; i <cardListRectangle.size() ; i++) {
if(i==0) System.out.print("[");
if(i==-1){}//
System.out.print(cardListRectangle.get(i).getShape().shapeName+":"+String.format("%.2f",cardListRectangle.get(i).getShape().getArea())+" ");
if(i==cardListRectangle.size()-1) System.out.print("]");
}
if(cardListRectangle.size()==0) System.out.print("[]");
for (int i = 0; i <cardListTriangle.size() ; i++) {
if(i==0) System.out.print("[");
if(i==-2){}//
System.out.print(cardListTriangle.get(i).getShape().shapeName+":"+String.format("%.2f",cardListTriangle.get(i).getShape().getArea())+" ");
if(i==cardListTriangle.size()-1) System.out.print("]");
}
if(cardListTriangle.size()==0) System.out.print("[]");
for (int i = 0; i <cardListTrapeziod.size() ; i++) {
if(i==0) System.out.print("[");
if(i==-3){}//
System.out.print(cardListTrapeziod.get(i).getShape().shapeName+":"+String.format("%.2f",cardListTrapeziod.get(i).getShape().getArea())+" ");
if(i==cardListTrapeziod.size()-1) System.out.print("]");
}
if(cardListTrapeziod.size()==0) System.out.print("[]");
System.out.println();
System.out.print("The max area:");
System.out.println(String.format("%.2f",getAllArea2()));
}
public boolean validate() {
int n=0;
for (int i = 0; i <cardList.size()
; i++) {
if(cardList.get(i).getShape().vaildate()==false)
n++;
}
if(n==0)
return true;
else
return false;
}
}
class SortByArea implements
Comparator {
public int compare(Object a,Object
b)
{
Card A=(Card) a;
Card B=(Card) b;
if(A.getShape().getArea()>B.getShape().getArea())
return -1;
else
return 1;
}
}
public class p1 {
public static Scanner input= new Scanner(System.in);
public static void main(String[] args) {
ArrayList<Integer> list= new ArrayList<Integer>();
int num =input.nextInt();
if(num==0){System.out.println("Wrong Format");System.exit(0);}
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.showResult23();
input.close();
}
}
类图:
由于本次题目的需求有所变化,其中包含了输出格式的变化:如需要按照不同类型的图像进行面积的统计,同时还需要在此基础上进行面积的排序,因此还需要增加一个缓冲类,用来提取每一种图形的面积数据,从而进行后续的输出与统计。因此在原有的类的基础上,新加入了一个新的输出类,依次来满足题目的需求。
题目集8——-2:
设计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编号]上存款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
④查询余额业务输出
¥[金额]
金额保留两位小数。
输入样例1:
在这里给出一组输入。例如:
6222081502001312390 88888888 06 -500.00
#
输出样例1:
在这里给出相应的输出。例如:
张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00
输入样例2:
在这里给出一组输入。例如:
6217000010041315709 88888888 02 3500.00
#
输出样例2:
在这里给出相应的输出。例如:
杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥6500.00
输入样例3:
在这里给出一组输入。例如:
6217000010041315715
#
输出样例3:
在这里给出相应的输出。例如:
¥10000.00
输入样例4:
在这里给出一组输入。例如:
6222081502001312390 88888888 06 -500.00
6222081502051320786 88888888 06 1200.00
6217000010041315715 88888888 02 1500.00
6217000010041315709 88888888 02 3500.00
6217000010041315715
#
输出样例4:
在这里给出相应的输出。例如:
张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00
韦小宝在中国工商银行的06号ATM机上取款¥1200.00
当前余额为¥8800.00
杨过在中国建设银行的02号ATM机上取款¥1500.00
当前余额为¥8500.00
杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥5000.00
¥5000.00
源代码:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
public class p1{
public static void main(String[]
args) {
Scanner input = new Scanner(System.in);
UnionPay unionPay = new UnionPay();
Bank ccb = new Bank("1001","中国建设银行",0.02);
Bank icbc = new Bank("1002","中国工商银行",0.03);
Bank rcb = new Bank("1003","中国农业银行",0.04);
unionPay.addBank(ccb);
unionPay.addBank(icbc);
unionPay.addBank(rcb);
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",rcb);
ATM aTM8 = new ATM("08",rcb);
ATM aTM9 = new ATM("09",rcb);
ATM aTM10 = new ATM("10",rcb);
ATM aTM11 = new ATM("11",rcb);
ccb.addATM(aTM1);
ccb.addATM(aTM2);
ccb.addATM(aTM3);
ccb.addATM(aTM4);
icbc.addATM(aTM5);
icbc.addATM(aTM6);
rcb.addATM(aTM7);
rcb.addATM(aTM8);
rcb.addATM(aTM9);
rcb.addATM(aTM10);
rcb.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","张三丰","13025996587");
User linghuchong = new User("3640000010045441009","令狐冲","13025996587");
User qiaofeng = new User("3630000010033431001","乔峰","13025996587");
User hongqigon = new User("3630000010033431008","洪七公","");
Account ccbAcc1 = new DebitAccount("3217000010041315709",10000.00,Yangguo,ccb);
Account ccbAcc2 = new DebitAccount("3217000010041315715",10000.00,Yangguo,ccb);
Account ccbAcc3 = new DebitAccount("3217000010051320007",10000.00,Guojing,ccb);
Account icbcAcc1 = new DebitAccount("3222081502001312389",10000.00,Zhangwuji,icbc);
Account icbcAcc2 = new DebitAccount("3222081502001312390",10000.00,Zhangwuji,icbc);
Account icbcAcc3 = new DebitAccount("3222081502001312399",10000.00,Zhangwuji,icbc);
Account icbcAcc4 = new DebitAccount("3222081502051320785",10000.00,Weixiaobao,icbc);
Account icbcAcc5 = new DebitAccount("3222081502051320786",10000.00,Weixiaobao,icbc);
Account ccbAcc4 = new CreditAccount("3640000010045442002", 10000.00, Zhangsanfeng, ccb);
Account icbcAcc6 = new CreditAccount("3640000010045441009",10000.00, linghuchong, icbc);
Account rcbAcc1 = new CreditAccount("3630000010033431001",10000.00, qiaofeng, rcb);
Account rcbAcc2 = new CreditAccount("3630000010033431008",10000.00,hongqigon,rcb);
ccb.addAccount(ccbAcc1);
ccb.addAccount(ccbAcc2);
ccb.addAccount(ccbAcc3);
icbc.addAccount(icbcAcc1);
icbc.addAccount(icbcAcc2);
icbc.addAccount(icbcAcc3);
icbc.addAccount(icbcAcc4);
icbc.addAccount(icbcAcc5);
ccb.addAccount(ccbAcc4);
icbc.addAccount(icbcAcc6);
rcb.addAccount(rcbAcc1);
rcb.addAccount(rcbAcc2);
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(ccbAcc4);
linghuchong.addAccount(icbcAcc6);
qiaofeng.addAccount(rcbAcc1);
hongqigon.addAccount(rcbAcc2);
Card ccbCard1 = new BankCard("6217000010041315709","88888888",ccbAcc1);
Card ccbCard2 = new BankCard("6217000010041315715","88888888",ccbAcc1);
Card ccbCard3 = new BankCard("6217000010041315718","88888888",ccbAcc2);
Card ccbCard4 = new BankCard("6217000010051320007","88888888",ccbAcc3);
Card icbcCard1 = new BankCard("6222081502001312389","88888888",icbcAcc1);
Card icbcCard2 = new BankCard("6222081502001312390","88888888",icbcAcc2);
Card icbcCard3 = new BankCard("6222081502001312399","88888888",icbcAcc3);
Card icbcCard4 = new BankCard("6222081502001312400","88888888",icbcAcc3);
Card icbcCard5 = new BankCard("6222081502051320785","88888888",icbcAcc4);
Card icbcCard6 = new BankCard("6222081502051320786","88888888",icbcAcc5);
Card ccbCard5 = new CreditCard("6640000010045442002","88888888",ccbAcc4);
Card ccbCard6 = new CreditCard("6640000010045442003","88888888",ccbAcc4);
Card icbcCard7 = new CreditCard("6640000010045441009","88888888",icbcAcc6);
Card rcbCard1 = new CreditCard("6630000010033431001","88888888",rcbAcc1);
Card rcbCard2 = new CreditCard("6630000010033431008","88888888",rcbAcc2);
ccbAcc1.addCard(ccbCard1);
ccbAcc1.addCard(ccbCard2);
ccbAcc2.addCard(ccbCard3);
ccbAcc3.addCard(ccbCard4);
icbcAcc1.addCard(icbcCard1);
icbcAcc2.addCard(icbcCard2);
icbcAcc3.addCard(icbcCard3);
icbcAcc3.addCard(icbcCard4);
icbcAcc4.addCard(icbcCard5);
icbcAcc5.addCard(icbcCard6);
ccbAcc4.addCard(ccbCard5);
ccbAcc4.addCard(ccbCard6);
icbcAcc6.addCard(icbcCard7);
rcbAcc1.addCard(rcbCard1);
rcbAcc2.addCard(rcbCard2);
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 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();
}
}
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 boolean verify() {
/**
* 校验该卡是否存在
*/
Card card = ValidateData.getCardbyCardNO(unionPay, cardNO);
if(card == null) {
System.out.println("Sorry,this
card does not exist.");
return false;
}
/**
* 校验ATM是否存在
*/
ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID);
if(aTM == null) {
System.out.println("Sorry,the
ATM's id is wrong.");
return false;
}
Account account = Account.getAmountbyCardNO(cardNO);
double balance = account.getBalance();
/**
* 校验卡密码是否正确
*/
if(!card.getCardPassword().equals(cardPassword)) {
System.out.println("Sorry,your
password is wrong.");
return false;
}
/**
* 校验取款金额是否大于余额
*/if(!account.maxwithdrawal(amount, aTM.getBank())){
System.out.println("Sorry,your
account balance is insufficient.");
return false;
}
return true;
}
public void withdraw() {
if(!verify())
{
System.exit(0);
}
Card card = ValidateData.getCardbyCardNO(unionPay, cardNO);
ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID);
Account account = Account.getAmountbyCardNO(cardNO);
card.access(amount,aTM.getBank());
if(amount
>= 0) {
showResult(account,1, aTM);
}else
{
showResult(account,0, aTM);
}
}
public void showResult(Account account,int flag, ATM aTM) {
String type = "";
if(flag == 1) {
type = "取款";
}else
{
type = "存款";
amount *= -1;
}
String userName =
account.getUser().getName();
String bankName = aTM.getBank().getBankName();
System.out.printf("业务:%s ",type);
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 BankCard extends
Card {
public BankCard(String cardNO, String
cardPassword, Account ccbAcc) {
super(cardNO,cardPassword,ccbAcc);
}
public void access(double money,Bank bank) {//对金额操作的函数
if(money
> 0) {
super.getAccount().withdrawal(money,bank);
}else
{
super.getAccount().deposit(-money);
}
}
}
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 access(double money) {
}
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;
}
public void withdrawal(double money , Bank bank) { //取款函数
// TODO 自动生成的方法存根
this.setBalance(this.balance - money);
}
public void deposit(double money) { //存款函数,传入的数据为正数
this.setBalance(this.balance
+ money);
}
public boolean maxwithdrawal(double num,Bank bank) {
return false;
}
}
class CreditCard extends
Card {//信用卡
public CreditCard(String cardNO, String
cardPassword, Account rcbAcc1) {
super(cardNO,cardPassword,rcbAcc1);
}
public void access(double money,Bank bank){
if(money
> 0) {
super.getAccount().withdrawal(money, bank);
}else
{
super.getAccount().deposit(-money);
}
}
}
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);
}
public void access(double money,Bank bank) {
this.account.setBalance(this.account.getBalance() - money);
}
}
class CreditAccount extends Account {
/**
*贷记账户
*/
private double credit = 50000; //信用,默认为五万
final String
name = "贷记账户";
public double getBalance(){
if(super.getBalance() > 0) {
return super.getBalance();
}
else {
return credit
- 50000;
}
}
public boolean maxwithdrawal(double money,Bank bank) {
double money_1
= super.getBalance();
if(bank.getBankName().equals(getBank().getBankName())) {
if(money
> money_1) {
if(credit - (money - money_1) * 1.05 <
0) {
return false;
}
}
else {
if(money_1
- money < 0) {
return false;
}
}
}else {
double sum
= 0;
if(money > money_1) {
sum = money + (money - money_1)*0.05 + money * bank.getServicecharge();
if(credit - (sum - money_1)
< 0)
return false;
}else
{
sum = money + money *
bank.getServicecharge();
if(sum > money_1) {
if(credit - (sum - money_1) < 0)
return false;
}else
{
if((money_1 - sum) < 0){
return false;
}
}
}
}
return true;
//
if(bank.getBankName().equals(getBank().getBankName())) {
// if(money >
super.getBalance()) {
// if((money -
super.getBalance()) * 0.05 + money * (1 + bank.getServicecharge()) > credit
+ super.getBalance()) {
//
System.out.println("Sorry,your account balance is
insufficient.");
// return false;
// }else {
// return true;
// }
// }else {
// return true;
// }
// }else {
// if(money >
super.getBalance()) {
// if((money -
super.getBalance()) * 0.05 + money > credit + super.getBalance()) {
//
System.out.println("Sorry,your account balance is
insufficient.");
// return false;
// }else {
// return true;
// }
// }else {
// return true;
// }
// }
}
public CreditAccount(String accountNO, double balance, User user, Bank bank){
super(accountNO,balance,user,bank);
}
public void withdrawal(double money,Bank bank) { //取款函数
// TODO 自动生成的方法存根
double money_1 = super.getBalance();
if(bank.getBankName().equals(getBank().getBankName())) {
if(money
> money_1) {
super.setBalance(0);
this.setCredit(credit -
(money - money_1) * 1.05);
}else
{
super.setBalance(
money_1 - money );
}
}else {
double sum
= 0;
if(money > money_1) {
sum = money + (money -
money_1)*0.05 + money * bank.getServicecharge();
super.setBalance(0);
this.setCredit(credit -
(sum - money_1));
}else
{
sum = money + money *
bank.getServicecharge();
if(sum > money_1) {
super.setBalance(0);
this.setCredit(credit -
(sum - money_1));
}else
{
super.setBalance((money_1 - sum));
}
}
//
if(money > money_1) {
// super.setBalance(0);
// this.setCredit(credit -
(money - money_1) * 1.05 - money * bank.getServicecharge());
// }else {
// if(money * (1 +
bank.getServicecharge()) < money_1) {
// super.setBalance( money_1
- money * (1 + bank.getServicecharge()));
// }else {
// super.setBalance(0);
// this.setCredit(credit -
(money * (1 + bank.getServicecharge()) - money_1));
//
// }
// }
}
}
public double getCredit() {
return credit;
}
public void setCredit(double credit) {
this.credit = credit;
}
}
class DebitAccount extends Account {//借记账户
final String
name = "借记账户";
DebitAccount (String accountNO,
double balance, User user, Bank bank){
super(accountNO,balance,user,bank);
}
public void withdrawal(double money,Bank bank) { //取款函数
if(bank.getBankName().equals(getBank().getBankName()))
{
super.setBalance(super.getBalance() - money);
}else
{
super.setBalance(super.getBalance() - money * (1 +
bank.getServicecharge()));
}
}
public void deposit(double money) { //存款函数
super.setBalance(super.getBalance() + money);
}
public boolean maxwithdrawal(double money,Bank bank) {
double money1
= super.getBalance();
if(super.getBank().getBankName().equals(bank.getBankName()))
{
if(money
> money1) {
return false;
}
}else {
if(money
*(1 + bank.getServicecharge()) > money1) {
return false;
}
}
return true;
}
}
class UnionPay {
//中国银联
private ArrayList<Bank>
bankList = new ArrayList<Bank>();
public double servicecharge;
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 Bank {
private double servicecharge = 0;
private String bankNO;
private String bankName;
private ArrayList<Account> accountList = new ArrayList<Account>();
private ArrayList<ATM> ATMList = new ArrayList<ATM>();
public Bank() {
super();
}
public double getServicecharge() {
return servicecharge;
}
public void setServicecharge(double servicecharge) {
this.servicecharge = servicecharge;
}
public Bank(String bankNO, String
bankName , double servicecharge ) {
super();
this.bankNO
= bankNO;
this.bankName
= bankName;
this.servicecharge
= servicecharge;
}
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 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 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);
}
}
类图:

浙公网安备 33010602011771号