当我的提款额大于现金余额时,如何打印错误消息?

时间:2020-03-01 12:49:12

标签: java structure bank

我的Java银行程序有问题,用户可以从现金余额中提取更大的金额。与我的其他功能(“付款”和“汇款”)相同。我如何像使用它一样使用此代码和'else if'语句或?我感到难过。请帮忙。谢谢。

case "Withdraw": //withdraw money from account
            if(people.get(accIndex).getMoney()<=0) { //if the user does not have enough funds
                JOptionPane.showMessageDialog(null, "You Dont Have Enough Funds In Your Account!", "Javank", JOptionPane.WARNING_MESSAGE);
            }
            else {
            System.out.print("Input amount to withdraw: ");
            double withmoney = in.nextDouble();
            people.get(accIndex).setMoney(people.get(accIndex).getMoney()-withmoney); //gets info from accIndex and withdraws money from account
            JOptionPane.showMessageDialog(null, "Successfully withdrew: "+withmoney,"Javank",JOptionPane.INFORMATION_MESSAGE);
            LocalDateTime myDateObj2 = LocalDateTime.now(); //create local date and time object
            String formattedDate2 = myDateObj2.format(myFormatObj); //store it to a string (formatted)
            checktrans.push("Withdrew "+withmoney+" @ "+formattedDate2); //add to transaction stack
            }
            break;

1 个答案:

答案 0 :(得分:1)

如果用户一开始拥有正余额,我认为这无关紧要。 取而代之的是,如果请求的金额已经提取,我将检查余额是否低于零。

类似

double withmoney = in.nextDouble();
if((people.get(accIndex).getMoney() - withmoney) < 0.0) {
    //the user does not have enough funds
}
else {
    //allow withdrawal
}