-5
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。
请对此提供帮助。

4

1 回答 1

0

三个主要选项:

  1. 制作一个基础异常,将其作为抛出您的方法,使您的其他异常从基础继承
  2. 进行多个异常,在您的方法上使用 throws 作为逗号分隔的列表,例如public String myMethod() throws ExA, ExB, ExC
  3. 使所有异常都扩展可运行,然后您不必声明抛出(但它也不会强制您的调用代码处理它,所以就是这样)
于 2013-09-13T13:01:37.077 回答