⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transferfundscommand.java

📁 100多M的J2EE培训内容
💻 JAVA
字号:
package examples.command;

import examples.dualpersistent.AccountHome;
import examples.dualpersistent.Account;
import examples.dualpersistent.ProcessingErrorException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.ejb.FinderException;
import java.rmi.RemoteException;
import java.io.Serializable;

public class TransferFundsCommand extends Command implements Serializable {

    String withdrawAccountID;
    String depositAccountID;
    double transferAmount;

    double withdrawAccountBalance;
    double depositAccountBalance;

    public void setWithdrawAccountID(String withdrawAccountID) {
        this.withdrawAccountID = withdrawAccountID;
    }

    public void setDepositAccountID(String depositAccountID) {
        this.depositAccountID = depositAccountID;
    }

    public void setTransferAmount(double transferAmount) {
        this.transferAmount = transferAmount;
    }


    public double getDepositAccountBalance() {
        return depositAccountBalance;
    }

    public double getWithdrawAccountBalance() {
        return withdrawAccountBalance;
    }

    public void execute() throws CommandException
    {
        //at this point we are inside the EJB Server
        try {

            InitialContext ctx = new InitialContext();
            AccountHome home = (AccountHome) PortableRemoteObject.narrow(ctx.lookup("dualPersistent"), AccountHome.class);

            //locate accounts and perform transfer
            Account account1 = home.findByPrimaryKey(withdrawAccountID);
            Account account2 = home.findByPrimaryKey(depositAccountID);

            account1.withdraw(this.transferAmount);
            account2.deposit(this.transferAmount);

            //populate command with final balances
            this.depositAccountBalance = account2.balance();
            this.withdrawAccountBalance = account1.balance();
        }
        catch (Exception e)
        {
            //wrap the exception as a command execption and throw to client
            throw new CommandException(e);
        }
    }

    public TransferFundsCommand()
    {
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -