📄 banking.java
字号:
package itso.webserv.model;
import itso.bank.exception.AccountDoesNotExistException;
import itso.bank.exception.BankException;
import itso.bank.exception.InsufficientFundsException;
import itso.bank.exception.ZeroAmountException;
import itso.bank.model.Account;
import itso.bank.model.TransRecord;
import java.math.BigDecimal;
/**
* Facade to access the Account, perform credits and debits.
*
* @author Ian Brown, modified by Ueli Wahli
*/
public class Banking {
/**
* Deposit funds into a bank account
*/
public void deposit(String accountId, BigDecimal amount)
throws AccountDoesNotExistException, ZeroAmountException {
AccountDB accountDB = AccountDB.getInstance();
accountDB.credit(accountId, amount);
}
/**
* Withdraw funds from a bank account
*/
public void withdraw(String accountId, BigDecimal amount)
throws AccountDoesNotExistException, ZeroAmountException, InsufficientFundsException, BankException {
AccountDB accountDB = AccountDB.getInstance();
Account account = accountDB.getAccount(accountId);
if (account.getBalance().compareTo(amount) == -1)
throw new InsufficientFundsException();
accountDB.debit(accountId, amount);
}
/**
* Transfer funds from a bank account to another
*/
public void transfer(String accountId1, String accountId2, BigDecimal amount)
throws AccountDoesNotExistException, ZeroAmountException, InsufficientFundsException, BankException {
AccountDB accountDB = AccountDB.getInstance();
Account account1 = accountDB.getAccount(accountId1);
Account account2 = accountDB.getAccount(accountId2);
if (account1.getBalance().compareTo(amount) == -1)
throw new InsufficientFundsException();
accountDB.debit(accountId1, amount);
accountDB.credit(accountId2, amount);
}
/**
* Get details about a bank account
*/
public Account getAccount(String accountId)
throws AccountDoesNotExistException, BankException {
AccountDB accountDB = AccountDB.getInstance();
Account account = accountDB.getAccount(accountId);
return account;
}
/**
* Get transaction records for a bank account
*/
public TransRecord[] getTransactions(String accountId)
throws BankException {
AccountDB accountDB = AccountDB.getInstance();
TransRecord[] tr = accountDB.getTransactions(accountId);
return tr;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -