abstract class CustomException extends Exception
{
abstract public String toString();
abstract public String getMessage();
}
interface SimpleInterestCalculator
{
public void setPrincipalAmount(int principalAmount) throws CustomException;
public int getPrincipalAmount();
public void setRateOfInterest(int rateOfInterest) throws CustomException;
public int getRateOfInterest();
public void setTime(int Time) throws CustomException;
public int getTime();
public int getSimpleInterest();
}
interface CompoundInterestCalculator
{
public void setPrincipalAmount(int principalAmount) throws CustomException;
public int getPrincipalAmount();
public void setRateOfInterest(int rateOfInterest) throws CustomException;
public int getRateOfInterest();
public void setTime(int Time) throws CustomException;
public int getTime();
public int getCompoundInterest();
}
class SimpleInterestCalculationException extends CustomException
{
String message;
SimpleInterestCalculationException(String message)
{
this.message = message;
}
SimpleInterestCalculationException()
{
this.message = null;
}
public String toString()
{
if (this.message == null)
{
return "Simple Interest Calculation Exception";
}
else
{
return "Simple Interest Calculation Exception : " + this.message;
}
}
public String getMessage()
{
return this.message;
}
}
class CompoundInterestCalculationException extends CustomException
{
String message;
CompoundInterestCalculationException(String message)
{
this.message = message;
}
CompoundInterestCalculationException()
{
this.message = null;
}
public String toString()
{
if (this.message == null)
{
return "Compound Interest Calculation Exception";
}
else
{
return "Compound Interest Calculation Exception : " + this.message;
}
}
public String getMessage()
{
return this.message;
}
}
class InterestCalculator implements SimpleInterestCalculator, CompoundInterestCalculator
{
private int principalAmount, rateOfInterest, time;
InterestCalculator()
{
this.principalAmount = 0;
this.rateOfInterest = 0;
this.time = 0;
}
InterestCalculator(int principalAmount, int rateOfInterest, int time)
{
this.principalAmount = principalAmount;
this.rateOfInterest = rateOfInterest;
this.time = time;
}
public void setPrincipalAmount(int principalAmount) throws SimpleInterestCalculationException
{
if (principalAmount < 0)
{
throw new SimpleInterestCalculationException("Principal Amount Cannot be Negative");
}
if (principalAmount == 0)
{
throw new SimpleInterestCalculationException("Principal Amount Cannot be Zero");
}
this.principalAmount = principalAmount;
}
public int getPrincipalAmount()
{
return this.principalAmount;
}
public void setRateOfInterest(int rateOfInterest) throws SimpleInterestCalculationException
{
if (rateOfInterest < 0)
{
throw new SimpleInterestCalculationException("Rate Of Interest Cannot be Negative");
}
if (rateOfInterest == 0)
{
throw new SimpleInterestCalculationException("Rate Of Interest Cannot be Zero");
}
this.rateOfInterest = rateOfInterest;
}
public int getRateOfInterest()
{
return this.rateOfInterest;
}
public void setTime(int time) throws SimpleInterestCalculationException
{
if (time < 0)
{
throw new SimpleInterestCalculationException("Time Cannot be Negative");
}
if (time == 0)
{
throw new SimpleInterestCalculationException("Time Cannot be Zero");
}
this.time = time;
}
public int getTime()
{
return this.time;
}
public int getSimpleInterest()
{
return (this.principalAmount * this.rateOfInterest * this.time) / 100;
}
public int getCompoundInterest()
{
int x, y, z;
x = (this.rateOfInterest / 100) + 1;
y = this.time;
z = 1;
while (y > 0)
{
z = z * x;
y--;
}
z = z * this.principalAmount;
return z;
}
}
class calculatepsp
{
public static void main(String gg[])
{
InterestCalculator simpleInterest = new InterestCalculator();
InterestCalculator compoundInterest = new InterestCalculator();
try
{
simpleInterest.setPrincipalAmount(-100);
simpleInterest.setRateOfInterest(5);
simpleInterest.setTime(2);
int simpleinterest = interestCalculator.getSimpleInterest();
System.out.println("Simple Interest : " + simpleinterest);
compoundInterest.setPrincipalAmount(1000);
compoundInterest.setRateOfInterest(-8);
compoundInterest.setTime(4);
int compoundinterest = interestCalculator.getCompoundInterest();
System.out.println("Compound Interest : " + compoundinterest);
}
catch (SimpleInterestCalculationException sice)
{
System.out.println(sice);
}
catch (CompoundInterestCalculationException cice)
{
System.out.println(cice);
}
}
}
我想抛出类的异常,如果我正在计算单利,那么它应该是 SimpleInterestCalculationException,如果我正在计算复利,那么它应该是 CompundInterestCalculationException。
请对此提供帮助。