📄 bankbusinessbean.java
字号:
package com.aptech.accp.bank.ejb.session;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.NamingException;
import com.aptech.accp.bank.ejb.entity.AccountHome;
import com.aptech.accp.bank.ejb.entity.Account;
import javax.naming.InitialContext;
import javax.ejb.FinderException;
import java.math.BigDecimal;
/**
*
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author Michael Luo
* @version 1.0
*/
public class BankBusinessBean implements SessionBean {
/**
* BankBusinessBean
*/
public BankBusinessBean() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* sessionContext
*/
SessionContext sessionContext;
/**
*
* @throws CreateException e
*/
public void ejbCreate() throws CreateException {
}
/**
* ejbRemove
*/
public void ejbRemove() {
}
/**
* ejbActivate
*/
public void ejbActivate() {
}
/**
* ejbPassivate
*/
public void ejbPassivate() {
}
/**
*
* @param sessionContext SessionContext
*/
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
/**
*
* @param accountNo String
* @param money int
* @return int
* @throws RemoteException
*/
public int saveMoney(String accountNo, int money) {
int result = 0;
BigDecimal moneyBDC = new BigDecimal(money);
try {
Context context = new InitialContext();
AccountHome accountrHome = (AccountHome) context.lookup("Account");
Account account = accountrHome.findByPrimaryKey(accountNo);
if (account != null) {
account.setBalance(account.getBalance().add(moneyBDC));
result = 1;
}
} catch (NamingException e) {
e.printStackTrace();
} catch (FinderException e) {
e.printStackTrace();
} catch (Exception e) {
this.sessionContext.setRollbackOnly();
e.printStackTrace();
}
return result;
}
/**
*
* @param accountNo String
* @param money int
* @return int -1 表示余额不足, 0表示操作不成功, 1表示操作成功
* @throws RemoteException
*/
public int takeMoney(String accountNo, int money) {
int result = 0;
BigDecimal moneyBDC = new BigDecimal(money);
try {
Context context = new InitialContext();
AccountHome accountrHome = (AccountHome) context.lookup("Account");
Account account = accountrHome.findByPrimaryKey(accountNo);
if (account != null) {
if (account.getBalance().compareTo(moneyBDC) < 0) {
result = -1;
} else {
account.setBalance(account.getBalance().subtract(moneyBDC));
result = 1;
}
}
} catch (NamingException e) {
e.printStackTrace();
} catch (FinderException e) {
e.printStackTrace();
} catch (Exception e) {
this.sessionContext.setRollbackOnly();
e.printStackTrace();
}
return result;
}
/**
*
* @param accountNo String
* @return double
*/
public BigDecimal searchAccount(String accountNo) {
BigDecimal result = new BigDecimal(0);
try {
Context context = new InitialContext();
AccountHome accountrHome = (AccountHome) context.lookup("Account");
Account account = accountrHome.findByPrimaryKey(accountNo);
if (account != null) {
result = account.getBalance();
}
} catch (NamingException e) {
e.printStackTrace();
} catch (FinderException e) {
e.printStackTrace();
} catch (Exception e) {
this.sessionContext.setRollbackOnly();
e.printStackTrace();
}
return result;
}
/**
*
* @param from String
* @param to String
* @param money int
* @return int -3 表示目标帐户被冻结,-2表示目标帐户不存在 -1 表示转帐帐户余额不足,0表示转帐失败,1表示转帐成功
* @throws RemoteException
*/
public int transfer(String from, String to, int money) {
int result = 0;
BigDecimal moneyBDC = new BigDecimal(money);
try {
Context context = new InitialContext();
AccountHome accountrHome = (AccountHome) context.lookup("Account");
Account accountFrom = accountrHome.findByPrimaryKey(from);
Account accountTo = accountrHome.findByPrimaryKey(to);
if (accountTo == null) {
result = -2;
return result;
} else {
if (accountTo.getStatus().equals("0")) {
return -3;
}
if (accountFrom != null) {
if (accountFrom.getBalance().compareTo(moneyBDC) < 0) {
result = -1;
} else {
accountFrom.setBalance(accountFrom.getBalance().
subtract(
moneyBDC));
accountTo.setBalance(accountTo.getBalance().add(
moneyBDC));
result = 1;
}
}
}
} catch (NamingException e) {
e.printStackTrace();
} catch (FinderException e) {
e.printStackTrace();
result = -2;
return result;
} catch (Exception e) {
this.sessionContext.setRollbackOnly();
e.printStackTrace();
}
return result;
}
/**
* jbInit
* @throws Exception e
*/
private void jbInit() throws Exception {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -