Simple balance transfer java program

In this example we are going to code a simple balance transfer java program. Here is how the code looks like. You can try and pass the different parameters for initialBalance and transfer amount and check the result also, you can pass the different accNumber, input values and analyze how the result output is changing.

package com.mybank.transfer;

public class BalanceTransfer {

    private double initialBalance;
    private double remainingBalance;
    private double transfer;

    BalanceTransfer() {
        initialBalance = 5000.0;
        transfer = 20000;
    }

    public BalanceTransfer(double initialBalance, double transfer) {
        super();
        this.initialBalance = initialBalance;
        this.transfer = transfer;
    }

    private double makeTransfer(boolean isActive) {
        if (accountExists(101)) {
            System.out.println("We found your account...");
            if (isActive) {
                System.out
                .println("Congratulations!!! your account is active...");
                if (initialBalance >= transfer) {
                    remainingBalance = initialBalance - transfer;
                    System.out.println("Your remaining balance is: "
                            + remainingBalance);
                } else {
                    System.out
                    .println("Your total balance must be greter than or equal to the transfer amount");
                }
            } else {
                System.out
                .println("Your account isn't active! Please visit the nearest branch...");
            }
        } else {
            System.out.println("Sorry account doesn't exist!");
        }
        return remainingBalance;
    }
   
    private boolean accountExists(int input) {
        int accNumber[] = { 101, 102, 103, 104, 105 };
        boolean found = false;
        for (int index : accNumber) {
            if(index == input)
            found = true;
        }
        return found;
    }

    public static void main(String[] args) {

        BalanceTransfer bt = new BalanceTransfer(1000, 500);
        bt.makeTransfer(true);
    }
}


Output:

We found your account...
Congratulations!!! your account is active...
Your remaining balance is: 500.0

No comments:

Post a Comment