UnionPay.java
package com.feonix;
/**
* 银联接口
*
* @author FeoniX
*
*/
public interface UnionPay {
/**
* 密码认证
*
* @param passwd
*/
boolean auth(String passwd);
/**
* 取钱
*
* @param money
*/
void drawMoney(double money, String passwd);
/**
* 查询余额
*/
void queryBalance();
}
ICBC.java
package com.feonix;
/**
* 工商银行接口
*
* @author FeoniX
*
*/
public interface ICBC extends UnionPay {
/**
* 在线支付
*
* @param money
* @param passwd
*/
void onlinePay(double money, String passwd);
}
ABC.java
package com.feonix;
/**
* 农业银行接口
*
* @author FeoniX
*
*/
public interface ABC extends UnionPay {
/** 透支额度 */
public static final double OVERDRAFT = 5000;
/**
* 充值话费
* @param money
*/
void recharge(double money, String passwd);
}
Bank.java
package com.feonix;
import java.util.Scanner;
/**
* 银行类 实现 银联 接口
*
* @author FeoniX
*
*/
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());
}
}
ICBCBank.java
package com.feonix;
/**
* 工商银行类 继承 银行类 实现 工商银行接口
*
* @author 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) {
}
}
ABCBank.java
package com.feonix;
/**
* 农业银行类 继承 银行类 实现 农业银行接口
*
* @author 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) {
}
/**
* 扣款后计算并更新余额
*
* @param money
*/
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;
}
}