填写测试代码:(黑体部分)
package compoundinterestagent;
import java.io.*;
import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
public class CompoundInterestAgentTestClient
{
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private CompoundInterestAgentHome compoundInterestAgentHome = null;
private CompoundInterestAgent compoundInterestAgent = null;
private static BufferedReader in;
..................
private static String getInputString(String prompt)throws IOException
{
System.out.println(prompt);
return in.readLine();
}
//Main method
public static void main(String[] args)
{
CompoundInterestAgentTestClient client = new CompoundInterestAgentTestClient();
CompoundInterestAgent agent;
boolean valid;
String answer;
double tmpDbl=0.0;
int tmpInt=0;
in = new BufferedReader(new InputStreamReader(System.in));
try
{
agent=client.create();
System.out.println("/nWelcome to the Compound Interest Calculation:");
do
{
answer=getInputString("Enter a starting balance:$ ");
try
{
tmpDbl=Double.parseDouble(answer);
agent.setStartingBalance(tmpDbl);
valid=true;
}catch(NumberFormatException nfe)
{
System.out.println("Invalid Entry!");
valid=false;
}
}while(valid==false);
do
{
System.out.println("How often will the interest compound? ");
answer=getInputString("(1)monthly,(2)querterly,(3)semi_annually ");
try
{
tmpInt=Integer.parseInt(answer);
if(tmpInt>3)
throw new NumberFormatException();
agent.setFrequency(tmpInt);
valid=true;
}catch(NumberFormatException nfe)
{
System.out.println("Invalid Entry!");
valid=false;
}
}while(valid==false);
do
{
System.out.println("How much will you be contributing ");
switch(tmpInt)
{
case 1:
answer=getInputString("each month?");
break;
case 2:
answer=getInputString("each quarter?");
break;
case 3:
answer=getInputString("every six month?");
break;
}
try
{
tmpDbl=Double.parseDouble(answer);
agent.setContribution(tmpDbl);
valid=true;
}catch(NumberFormatException nfe)
{
System.out.println("Invalid Entry!");
valid=false;
}
}while(valid==false);
do
{
answer=getInputString("What is the ammount's annual yield? ");
try
{
tmpDbl=Double.parseDouble(answer);
agent.setAnnualYield(tmpDbl);
valid=true;
}catch(NumberFormatException nfe)
{
System.out.println("Invalid Entry!");
valid=false;
}
}while(valid==false);
do
{
answer=getInputString("How many years will you maintain this account?");
try
{
tmpInt=Integer.parseInt(answer);
agent.setTime(tmpInt);
valid=true;
}catch(NumberFormatException nfe)
{
System.out.println("Invalid Entry!");
valid=false;
}
}while(valid==false);
do
{
answer=getInputString("What rate of inflation rate are you anticipating? ");
try
{
tmpDbl=Double.parseDouble(answer);
agent.setInflation(tmpDbl);
valid=true;
}catch(NumberFormatException nfe)
{
System.out.println("Invalid Entry!");
valid=false;
}
}while(valid==false);
tmpDbl=agent.calculateReturn();
System.out.println("The return on this account would be $ "+tmpDbl);
}catch(RemoteException re)
{
re.printStackTrace();
}catch(IOException ioe)
{
ioe.printStackTrace();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
博客给出了复利计算代理测试客户端的代码。代码中包含多个import语句,定义了常量和变量,有获取输入字符串的方法。在main方法里,创建客户端对象,通过循环获取用户输入的起始余额、复利频率等信息,处理异常,最后计算并输出账户回报。
281

被折叠的 条评论
为什么被折叠?



