银行卡系统
- 一、功能描述
- 二、实现代码
- 1、定义银联接口
- 2、定义工商银行接口,继承银联接口
- 3、定义农业银行接口,继承银联接口
- 4、定义银行类,实现银联接口
- 5、定义工商银行类,继承银行类,实现工商银行接口
- 6、定义农业银行类,继承银行类,实现农业银行接口
- 演示效果图
一、功能描述
- 要求实现银行卡系统的银联接口,详细要求如下
- 银联接口,用于描述银联统一制定的规则,该接口提供检查密码,取钱以及查询余额方法
- 工商银行接口,用于描述工行发行的卡片功能,在满足银联接口的规则基础上,增加了在线支付功能。
- 农业银行接口,用于描述农行发行的卡片功能,在满足银联接口的规则基础上,增加了支付电话费的功能。另外,农行的卡可以透支5000元。
- 实现工行接口和农行接口,并进行测试
二、实现代码
1、定义银联接口
UnionPay.java
package com.feonix;
public interface UnionPay {
boolean auth(String passwd);
void drawMoney(double money, String passwd);
void queryBalance();
}
2、定义工商银行接口,继承银联接口
ICBC.java
package com.feonix;
public interface ICBC extends UnionPay {
void onlinePay(double money, String passwd);
}
3、定义农业银行接口,继承银联接口
ABC.java
package com.feonix;
public interface ABC extends UnionPay {
public static final double OVERDRAFT = 5000;
void recharge(double money, String passwd);
}
4、定义银行类,实现银联接口
Bank.java
package com.feonix;
import java.util.Scanner;
public abstract class Bank implements UnionPay {
private int id;
private String passwd;
private double balance;
public Bank(int id, String passwd, double balance) {
this.id = id;
this.passwd = passwd;
this.balance = balance;
}
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
boolean flag = true;
String passwd;
double money;
Bank icbc = null, abc = null;
while (flag) {
System.out.println("***************欢迎使用银行业务系统****************");
System.out.println("请选择银行:(工商银行请扣1 农业银行请扣2 退出请扣0)");
int bank = key.nextInt();
if (bank == 0) {
break;
}
System.out.println("请输入卡号:");
int id = key.nextInt();
System.out.println("请输入密码:");
passwd = key.next();
boolean flag2 = true;
switch (bank) {
case 1:
icbc = new ICBCBank(id, "qwer123", 5000);
if (!icbc.auth(passwd)) {
System.out.println("密码错误,请重试\n");
continue;
}
while (flag2) {
System.out.println("请选择操作:(余额查询请扣1 取款请扣2 在线支付请扣3 返回上一级菜单请扣0)");
int code = key.nextInt();
switch (code) {
case 1:
icbc.queryBalance();
break;
case 2:
System.out.println("请输入取款金额:");
money = key.nextDouble();
System.out.println("请输入密码:");
passwd = key.next();
icbc.drawMoney(money, passwd);
break;
case 3:
System.out.println("请输入在线支付金额:");
money = key.nextDouble();
System.out.println("请输入密码:");
passwd = key.next();
icbc.onlinePay(money, passwd);
break;
case 0:
flag2 = false;
break;
default:
System.out.println("指令无效,请重新输入\n");
}
}
break;
case 2:
abc = new ABCBank(id, "qwer456", 3000);
if (!abc.auth(passwd)) {
System.out.println("密码错误,请重试\n");
continue;
}
while (flag2) {
System.out.println("请选择操作:(余额查询请扣1 取款请扣2 话费充值请扣3 返回上一级菜单请扣0)");
int code = key.nextInt();
switch (code) {
case 1:
abc.queryBalance();
break;
case 2:
System.out.println("请输入取款金额:");
money = key.nextDouble();
System.out.println("请输入密码:");
passwd = key.next();
abc.drawMoney(money, passwd);
break;
case 3:
System.out.println("请输入充值话费金额:");
money = key.nextDouble();
System.out.println("请输入密码:");
passwd = key.next();
abc.recharge(money, passwd);
break;
case 0:
flag2 = false;
break;
default:
System.out.println("指令无效,请重新输入\n");
}
}
break;
}
}
key.close();
}
abstract void onlinePay(double money, String passwd);
protected abstract void recharge(double money, String string);
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public boolean auth(String passwd) {
if (this.passwd.equals(passwd)) {
return true;
}
return false;
}
@Override
public void drawMoney(double money, String passwd) {
double balance = getBalance();
if (!auth(passwd)) {
System.out.println("银行卡密码输入错误,取款失败\n");
return;
}
if (balance - money <= 0) {
System.out.println("银行卡余额不足,取款失败\n");
return;
}
setBalance(balance - money);
System.out.println("卡号" + getId() + "取款" + money + "元成功,剩余余额" + getBalance() + "\n");
}
@Override
public void queryBalance() {
System.out.printf("卡号:%d\n余额:%.2f\n\n", getId(), getBalance());
}
}
5、定义工商银行类,继承银行类,实现工商银行接口
ICBCBank.java
package com.feonix;
public class ICBCBank extends Bank implements ICBC {
public ICBCBank(int id, String passwd, double balance) {
super(id, passwd, balance);
}
@Override
public void onlinePay(double money, String passwd) {
if (!auth(passwd)) {
System.out.println("银行卡密码输入错误,在线支付失败\n");
return;
}
if (getBalance() - money <= 0) {
System.out.println("银行卡余额不足,在线支付失败\n");
return;
}
setBalance(getBalance() - money);
System.out.println("卡号" + getId() + "进行了网上支付,支付金额为:" + money + "\n");
}
@Override
protected void recharge(double money, String string) {
}
}
6、定义农业银行类,继承银行类,实现农业银行接口
ABCBank.java
package com.feonix;
public class ABCBank extends Bank implements ABC {
private double overdraft = OVERDRAFT;
public ABCBank(int id, String passwd, double balance) {
super(id, passwd, balance);
}
@Override
public void recharge(double money, String passwd) {
if (!auth(passwd)) {
System.out.println("银行卡密码输入错误,话费充值失败\n");
return;
}
if (getBalance() + overdraft - money <= 0) {
System.out.println("银行卡余额不足,话费充值失败\n");
return;
}
calcBanlance(money);
System.out.println("卡号" + getId() + "进行了话费充值,充值金额为:" + money + "\n");
}
@Override
public void drawMoney(double money, String passwd) {
double balance = getBalance();
if (!auth(passwd)) {
System.out.println("银行卡密码输入错误,取款失败\n");
return;
}
if (balance + overdraft - money <= 0) {
System.out.println("银行卡余额不足,取款失败\n");
return;
}
calcBanlance(money);
System.out.println("卡号" + getId() + "取款" + money + "元成功,剩余余额" + getBalance() + ",可透支余额:" + overdraft + "\n");
}
@Override
public void queryBalance() {
System.out.printf("卡号:%d\n余额:%.2f\n可透支额度:%.2f\n\n", getId(), getBalance(), overdraft);
}
@Override
protected void onlinePay(double money, String passwd) {
}
private void calcBanlance(double money) {
if (money > getBalance()) {
money = money - getBalance();
setBalance(0);
overdraft -= money;
} else {
setBalance(getBalance() - money);
}
}
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
}