java模拟银行存取款业务

 我来答
钱传杰ha
推荐于2017-12-15 · TA获得超过304个赞
知道小有建树答主
回答量:314
采纳率:0%
帮助的人:165万
展开全部
public class Account {

protected String accId;
protected String name;
protected double money;

public Account (String accId,String name){
this(accId,name,0);
}

public Account (String accId,String name,double money){
this.accId = accId;
this.name = name;
this.money = money;
}

public void saveMoney(double money){
if(money <= 0){
System.out.println("存款金额必须大于0");
}
this.money += money;
System.out.println("存款成功");
}

public double getMoney(double money){
if(money <= 0){
System.out.println("取款金额必须大于0");
return 0;
}
if(this.money <= money){
System.out.println("余额不足,无法取款");
return 0;
}
this.money -= money;
System.out.println("取款成功");
return money;
}

public double getBalance(){
return this.money;
}

protected double getOverdraft(){
return 0;
}

// 实现了equals方法,list比较时才能正确
@Override
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(this == obj){
return true;
}
if(obj instanceof Account){
return this.accId.equals(((Account)obj).accId);
}
return false;
}

@Override
public String toString() {
return "账户=" + accId + ",名字=" + name + ",余额=" + money;
}

}
public class Bank {

// Account实现了equals方法,list查找时才能正确
private List<Account> usersAccounts;

public Bank() {
usersAccounts = new ArrayList<Account>();
}

public void addAccount(Account account) {
if (usersAccounts.contains(account)) {
System.out.println("添加失败,不能添加同样的账户");
return;
}
usersAccounts.add(account);
}

public boolean delAccount(Account account) {
return usersAccounts.remove(account);
}

public boolean delAccount(String accId) {
return delAccount(new Account(accId, null));
}

public boolean existAccount(Account account) {
return usersAccounts.contains(account);
}

public boolean existAccount(String accId) {
return existAccount(new Account(accId, null));
}

public Account getAccount(String accId){
return usersAccounts.get(usersAccounts.indexOf(new Account(accId, null)));
}

public double getAllMoney() {
// 不考虑是否溢出,只是把所有用户余额相加
double result = 0;
for (Account account : usersAccounts) {
result += account.getBalance();
}
return result;
}

public double getAllOverdraft() {
// 不考虑是否溢出
double result = 0;
for (Account account : usersAccounts) {
result += account.getOverdraft();
}
return result;
}

public int getAccountNum() {
return usersAccounts.size();
}

public int getCreditAccountNum() {
int num = 0;
for (Account account : usersAccounts) {
// instanceof 性能没有简单的方法快。
if (account instanceof CreditAccount) {
num++;
}
}
return num;
}

public int getSavingAccountNum() {
int num = 0;
for (Account account : usersAccounts) {
if (account instanceof SavingAccount) {
num++;
}
}
return num;
}

public List<Account> getAllAccount() {
return usersAccounts;
}
}
public class CreditAccount extends Account{

private double overdraft;

public CreditAccount(String accId,String name){
super(accId, name);
this.overdraft = 1000;
}

public CreditAccount(String accId,String name,double money){
this(accId, name,money,1000);
}

public CreditAccount(String accId,String name,double money,double overdraft){
super(accId, name,money);
this.overdraft = overdraft;
}

@Override
public double getMoney(double money) {
if(money <= 0){
System.out.println("取款金额必须大于0");
return 0;
}
if(this.money + overdraft <= money){
System.out.println("余额不足,无法取款");
return 0;
}
this.money -= money;
System.out.println("取款成功");
return money;
}

@Override
public double getOverdraft(){
return overdraft;
}

@Override
public String toString() {
return "账户=" + accId + ",名字=" + name + ",余额=" + money + ",透支=" + overdraft;
}
}
public class SavingAccount extends Account {

public SavingAccount(String accId, String name) {
super(accId, name);
}

public SavingAccount(String accId, String name, double money) {
super(accId, name, money);
}

@Override
public double getMoney(double money) {
return super.getMoney(money);
}

@Override
public double getOverdraft() {
return super.getOverdraft();
}

}
public class Test {

private static Bank bank = new Bank();
public static void main(String[] args) {
Test.genAccount();
// 开户
Account a1 = new CreditAccount("1", "1", 200, 2000);
Account a2 = new SavingAccount("2", "2", 300);
Account a3 = new SavingAccount("3", "3", 400);
Account a4 = new CreditAccount("4", "4", 500, 2000);
Account a5 = new CreditAccount("4", "5", 600, 2000); // 帐号4重
bank.addAccount(a1);
bank.addAccount(a2);
bank.addAccount(a3);
bank.addAccount(a4);
bank.addAccount(a5);
showNowAccount();
// 销户
bank.delAccount("1");
bank.delAccount("2");
showNowAccount();
// 存款
if(bank.existAccount("3")){
Account acc = bank.getAccount("3");
acc.saveMoney(100);
}
showNowAccount();
// 取款
if(bank.existAccount("3")){
Account acc = bank.getAccount("3");
System.out.println("余额=" + acc.getBalance());
acc.getMoney(100);
System.out.println("余额=" + acc.getBalance());
acc.getMoney(1000);
System.out.println("余额=" + acc.getBalance());
}
if(bank.existAccount("4")){
Account acc = bank.getAccount("4");
System.out.println("余额=" + acc.getBalance());
acc.getMoney(100);
System.out.println("余额=" + acc.getBalance());
acc.getMoney(1000);
System.out.println("余额=" + acc.getBalance());
acc.getMoney(10000);
System.out.println("余额=" + acc.getBalance());
}
System.out.println(bank.getAccountNum());
System.out.println(bank.getAllMoney());
System.out.println(bank.getAllOverdraft());
System.out.println(bank.getCreditAccountNum());
System.out.println(bank.getSavingAccountNum());
}

public static void genAccount(){
String s = "1000 0000 0000 000";
Account a = null;
for(int i = 1; i < 11; i ++){
if((i & 2) == 0){
a = new CreditAccount(s + String.valueOf(i), "账户" + String.valueOf(i));
} else {
a = new SavingAccount(s + String.valueOf(i), "账户" + String.valueOf(i));
}
bank.addAccount(a);
}
}

public static void showNowAccount(){
for(Account account : bank.getAllAccount()){
System.out.println(account);
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式