如何设置透支限额

时间:2016-03-22 16:38:35

标签: java

我想要做的是设置透支限额,即-150,如果他们的余额为-150,则用户不能再提取。我的代码如下:

提款方法

public void Withdraw(double amount){


    if (amount + 5 > balance ){ //If amount and transaction fee is greater than balance then apply for overdraft
        System.out.println("Insufficent funds");
        System.out.println("Would you like to apply for an ovedraft?");
        System.out.println("1:Yes");
        System.out.println("2:No, return me back to menus");
        Choice = Option.nextLine();
        if (Choice.equalsIgnoreCase("1")){
         if((balance = balance - amount+5)<=-150){   //If balance is grater than 150 , apply for overdraft
             System.out.println("You have exceeded your Overdraft Limit, you will now be returned back to the menus");
         return;

        }
        else{ //if not exceeding bank balance
            balance -= amount + 5; 
            System.out.println("You have withdrawen £" + amount);
            System.err.println("You now have a balance of £" +balance);
            return; 
        }
    }
}

}

它们都在同一个类中,即“帐户”,现在发生的是消息确实发生 - (“您已超出透支限额,现在将返回菜单”)并返回菜单,但当我去检查余额时,钱仍然扣除,并显示超过-150的余额,例如。 -190我怎样才能确保-150是限制而不是扣除。希望这个问题得到理解。

3 个答案:

答案 0 :(得分:4)

问题在于:

if (!IsPostBack)
{
    // your logic here
}

这里的关键是if ((balance = balance - amount + 5) <= -150) { 。这是在支票

之前确定之前的新金额。

这样的事情可以避免这个问题。

=

注意使用 if (balance - amount - 5 < 0) { System.out.println("Insufficent funds"); System.out.println("Would you like to apply for an ovedraft?"); System.out.println("1:Yes"); System.out.println("2:No, return me back to menus"); Choice = Option.nextLine(); if (Choice.equalsIgnoreCase("1")) { if (balance - amount - 5 <= -150) { System.out.println("You have exceeded your Overdraft Limit, you will now be returned back to the menus"); } else { //if not exceeding bank balance balance -= amount + 5; System.out.println("You have withdrawen £" + amount); System.err.println("You now have a balance of £" + balance); } 进行数学和比较的更明确的计算(if (balance - amount - 5 <= -150))。这在口头上更容易理解。

答案 1 :(得分:2)

function uploadFile_multipart (foldername, filename) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onload = function() { var contentType = xmlhttp.getResponseHeader('Content-Type'); var contentLength = xmlhttp.getResponseHeader('Content-Length'); var contentData = new Uint8Array(xmlhttp.response); const boundary = 'foo_bar_baz'; var multipartRequestBody = '--' + boundary + '\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n' + '{ "name": "' + filename + '", "mimeType": "' + contentType + '" }\r\n\r\n--' + boundary + '\r\nContent-Type: ' + contentType + '\r\n\r\n' + contentData + '\r\n--' + boundary + '--'; var request = gapi.client.request({ 'path': '/upload/drive/v3/files', 'method': 'POST', 'params': {'uploadType': 'multipart'}, 'headers': {'Content-Type': 'multipart/related; boundary=' + boundary, 'Content-Length': contentLength}, 'body': multipartRequestBody }); request.execute(function(resp) { alert(resp); }); } xmlhttp.open("GET", foldername + filename, true); xmlhttp.responseType = "arraybuffer"; xmlhttp.send(); } 是你的问题。首先,您没有在括号内写if((balance = balance - amount+5)<=-150),因此它应该是amount+5

其次,您实际上是在这里更改余额(amount-5是分配运算符)。您还写了=而不是<=。你应该写的是以下内容:

<

答案 2 :(得分:0)

除上述所有答案外,我将添加此内容:

如果我有 1 $ 且我透支 50 $ ,那么我的帐户可以在

  

1 $ -55 $ = -54 $

但是这种情况不可能

if (amount + FEES > balance) { // If amount and transaction fee is greater than balance then apply for overdraft
System.out.println("Insufficent funds");