Home > Software design >  How do you keep adding a double until your limit has been reached?
How do you keep adding a double until your limit has been reached?

Time:12-13

For example, I have a bank account which has a limit of let's say 500 euros. First I add 400 euros and after that I add 150 euros. How can I change my code in order to add the 100 euros of my last addition and then print for example; "Your limit has been reached, (leftover 50 euros) of your (original 150 euros) addition cannot be added."

My code as it is right now is like this;

private double limit;
private String name;
double deposit = 0;
double balance = 0;
int numberOfDeposits = 0;
double afhaling = 0;

public Bankaccount(String name, double limit) // constructor
{
    this.name = name;
    this.limit = limit;
}

public boolean addDeposit(double deposit){
    if(deposit < 0){
        balance = balance;
        return false;
    }
    
    if(deposit > 0){
        balance = balance   deposit;
        numberOfDeposits   ;
        return true;
    }
    
    if(balance > limit){
        balance = balance;
        return false;
    }
    return false;
}

CodePudding user response:

Like I already wrote in the comment. You need to check whether the limit is reached, before you return. Here is a better function:

public boolean addDeposit(double deposit){
        if(deposit <= 0){ // include the case for deposit == 0
            balance = balance; // is redundant
            return false;
        }else{
            balance = balance   deposit;

            if(balance > limit){
                balance = balance;
                System.out.print("Can not deposit because limit is reached.");
                return false;
            }else {
                numberOfDeposits  ;
                return true;
            }
        }
    }

CodePudding user response:

I have done a few changes to your code. let me know if this works for you.


  public class Bankaccount {
    private double limit;
    private String name;
    double deposit = 0;
    double balance = 0;
    int numberOfDeposits = 0;
    double afhaling = 0;

    public Bankaccount(String name, double limit) // constructor
    {
        this.name = name;
        this.limit = limit;
    }

    public boolean addDeposit(double deposit) {
        if (deposit <= 0) {
            return false;
        }

        var sum = balance   deposit;
        if (!(balance >= limit)) {
            if (balance != limit && sum > limit) {
                var amount = limit - balance;
                balance  = amount;
                System.out.println("remaining amount "   (deposit - amount));
            } else if (balance != limit && sum < limit) {
                balance = sum; 
            }
            System.out.println("your new balance is "   balance);
            numberOfDeposits  ;
            return true;
        } else {
            return false;
        }
    }

}

CodePudding user response:

There are many ways to do it. Here is one way. I used Locale and the Currency class to display the proper name. If your output device is configured to accept alternate character sets/encodings you can modify to use the symbol. See the Currency class for details. It is unclear if you want to increment numberOfDeposits for a partial deposit.

import java.util.Currency;
import java.util.Locale;

public class BankAccount {
    private double limit;
    private String name;
    double deposit = 0;
    double balance = 0;
    int numberOfDeposits = 0;
    double afhaling = 0;
    String currency = Currency.getInstance(Locale.getDefault())
            .getDisplayName();
    
    public static void main(String[] args) {
        BankAccount ba = new BankAccount("Mine", 500);
        System.out.println(ba.addDeposit(200));
        System.out.println(ba.addDeposit(150));
        System.out.println(ba.addDeposit(175));
    }
    
    public BankAccount(String name, double limit) { 
        this.name = name;
        this.limit = limit;
    }
    
    public boolean addDeposit(double deposit) {
        if (deposit < 0) {
            return false;
        }
        
        if (balance >= limit) {
            System.out.println(
                    "You are over the limit, deposit declined");
            return false;
        }
        
        if (balance   deposit <= limit) {
            balance  = deposit;
            System.out.printf(
                    "%.2f %s deposit accepted.  Your balance is %.2f %ss%n",
                    deposit, currency, balance, currency);
            numberOfDeposits  ;
            return true;
        }
        double actualDeposit = limit - balance;
        System.out.printf("You have reached your limit of %.2f %ss."
                  "%n%.2f of your %.2f %s deposit is accepted.%n"
                  "%.2f %ss is not deposited.%n", limit, currency,
                actualDeposit, deposit, currency,
                deposit - actualDeposit, currency);
        return false;
    }
}

Prints

200.00 US Dollars deposit accepted.  Your balance is 200.00 US Dollars
true
150.00 US Dollar deposit accepted.  Your balance is 350.00 US Dollars
true
You have reached your limit of 500.00 US Dollars.
150.00 of your 175.00 US Dollar deposit is accepted.
25.00 US Dollars is not deposited.
false

  • Related