如何修改computeBalance()以确保在JTextField中输入的金额是正数(我认为我是这样做的)并且没有美分?例如:100.19和-49.8是不可接受的。

public class ATMbizlogic {

    private double totalBalance;
    private boolean rightPinEntered;



    /**
     * Creates a new instance of ATMbizlogic
     */
    public ATMbizlogic()
    {
        totalBalance = 0.0;
        rightPinEntered =  true;
    }

    public void computeBalance(double withdraw, double deposit)
    throws ATMexception
    {
        if(withdraw <=0)
            throw new ATMexception("Negative withdraw not allowed");


        if(deposit <=0)
            throw new ATMexception("Negative deposit not allowed");


         double balance = deposit - withdraw;

        totalBalance = totalBalance + balance;



    }



    public void checkPin(double pin)
    throws ATMexception
    {
        if(pin <=0)
            throw new ATMexception("Negative pin not allowed");
        if(rightPinEntered == false)
            throw new ATMexception("Can not take another pin");
        if(pin<1111 || pin>9999)
            throw new ATMexception("Enter a valid pin");
        rightPinEntered = true;


    }




    public double getBalance()
    {
        return totalBalance;
    }
}

最佳答案


这样会将数字四舍五入,然后检查它们是否仍然相同。如果不是,则表示它有一个小数。

关于java - 如何修改calculate Balance()方法以确保金额​​为正且没有美分?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9691524/

10-09 13:03