services.java
来自「Practical Java也是一本和J2ME手机游戏开发相关的书」· Java 代码 · 共 68 行
JAVA
68 行
import java.io.IOException;
class MutualFund
{
public void buyMoreShares(double money)
{}
public void sellShares(double money)
{}
//...
}
class Customer
{
private MutualFund[] fundArray;
public Customer()
{}
public MutualFund[] funds()
{
return fundArray;
}
public void updateMutualFund(MutualFund fund) throws
DatabaseException
{}
public void undoMutualFundUpdate(MutualFund fund)
{}
public void writePortfolioChange() throws IOException
{}
//...
}
class DatabaseException extends Exception
{}
class Services
{
public void invest(Customer cust, double money) throws
IOException, DatabaseException
{
MutualFund[] array = cust.funds();
int size = array.length;
for (int i=0; i<size; i++)
{
((MutualFund)array[i]).buyMoreShares(money);
try {
cust.updateMutualFund(array[i]);
}
catch(DatabaseException dbe) //Catch exception and return
{ //object to a valid state.
((MutualFund)array[i]).sellShares(money);
throw dbe;
}
try {
cust.writePortfolioChange();
}
catch(IOException ioe) //Catch exception and return object
{ //to a valid state.
((MutualFund)array[i]).sellShares(money);
cust.undoMutualFundUpdate(array[i]);
throw ioe;
}
}
//...
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?