3D-kreativ
3D-kreativ

Reputation: 9297

About less than and negative values?

I just have a small beginners question, and it's about the code below. I want to check if the amount that the customer deduct from a credit account + the current balance don't getting lower than -5000 ? It's not working, and I'm wondering what I have done wrong? Is it about negative values? Help preciated!

// deduct credit account
if(type == "credit") {
    if((amountOut + creditAccountList.get(index).getAccountBalance()) < -5000) {
       System.out.println("Sorry! No deduct");
    }
}

EDIT: The amountOut is a positive number that the customer enters. I still don't get it to work!? Could it be done in some other way? It seems so simple, byt yet so comlicated for me right now! I just want to prevent the customer to deduct a value if the balance is -5000? The problem is that the balance is a negative number.

Upvotes: 0

Views: 3007

Answers (5)

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25445

I think you need to subtract amountOut although its unclear what value you store in there.

if((creditAccountList.get(index).getAccountBalance() - amountOut) < -5000)
                                                     ^
                                              Changed from plus

Upvotes: 1

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

I think you have to rewrite the condition. If I understood your case is the customer can withdraw an 5000 more than that of diposited amount. Example If customer has balance of 2000rs he/she can withdraw maximum 7000 rupees. If he/she wants to withdraw morw then your application will show "Sorry! No deduct" message. If this is the case then you can re-write the logic as given below

int thresholdMoney = 5000;
        if(((creditAccountList.get(index).getAccountBalance()+ thresholdMoney) - Math.abs(amountOut)) < 0){
            System.out.println("Sorry! No deduct");

        }

Upvotes: 1

duffymo
duffymo

Reputation: 308763

Negative numbers work the same as they always have; it's more likely your logic that's off.

if((amountOut + creditAccountList.get(index).getAccountBalance()) < -5000){

If amountOut is positive, you'll never decrease the account balance. Either make it negative or subtract it from the current balance to get the balance after withdrawal.

Upvotes: 1

nidhin
nidhin

Reputation: 6920

It should be

// deduct credit account
if(type == "credit") {
    if((amountOut + creditAccountList.get(index).getAccountBalance()) > -5000){
   System.out.println("Sorry! No deduct");
}
}

Upvotes: 0

solendil
solendil

Reputation: 8468

Replace

if(type == "credit") {

with

if ("credit".equals(type)) {

Apart from that, the code seems to be correct, in order to double-check, I would run it through a debugger, or put the following error messages for quick and easy debug:

System.out.printf("out %d balance %d\n", amountOut, creditAccountList.get(index).getAccountBalance());

Upvotes: 4

Related Questions