[Solved] Abstract Class FundTransfer with Java

Abstract Class FundTransfer: Let’s try an application like a fund transfer for our larger application. So, in fund transfer, there are 3 types of NEFT/IMPS/RGTS. We can create an abstract class FundTransfer. And extend it in the child classes. Create an abstract method transfer and implement it in all the child classes.

Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, attribute names and method names should be the same as specified in the problem statement.

Create an abstract class FundTransfer with following private attributes,

AttributesDatatype
accountNumberString
balanceDouble

 
Include the following methods in the class FundTransfer.

MethodDescription
Boolean validate(Double transfer)To check if the accountNumber is 10 digits, the transfer amount is non-negative and less than balance.
If all cases are satisfied then return true, if not return false
Boolean transfer(Double transfer)It is an abstract method with no definition


Create a class NEFTTransfer which extends FundTransfer.

Include the following method in the class NEFTTransfer.

MethodDescription
Boolean transfer(Double transfer)To check whether the transfer amount+5% of the transfer amount is less than balance.
If then subtracts transfer amount and 5% service charge from balance and return true, if not return false


Create a class IMPSTransfer which extends FundTransfer.

Include the following method in the class IMPSTransfer.

MethodDescription
Boolean transfer(Double transfer)To check whether transfer amount+2% of transfer amount is less than balance.
If then subtracts transfer amount and 2% service charge from balance and return true, if not return false.


Create a class RTGSTransfer which extends FundTransfer.

Include the following method in the class RTGSTransfer.

MethodDescription
Boolean transfer(Double transfer)To check whether the transfer amount is greater than Rs.10000.
If then subtracts transfer amount from balance and return true, if not return false.

 
Include appropriate getters/setters, constructors with super() to create objects. Write a driver class Main to test them.

Input format:
Refer to sample Input and Output for the details and for the formatting specifications.

Output format:
Print “Transfer occurred successfully” in the main method. If the transfer function returns true.
Print “Account number or transfer amount seems to be wrong” in the main method. If validate function returns false.
Print “Transfer could not be made” in the main method. If the transfer function returns false.
Refer to sample Input and Output for formatting specifications.

Note: All Texts in bold corresponds to the input and rest are output

Sample Input and Output 1:

Enter your account number:
1234567890
Enter the balance of the account:
10000
Enter the type of transfer to be made:
1.NEFT
2.IMPS
3.RTGS
1
Enter the amount to be transferred:
2000
Transfer occurred successfully
Remaining balance is 7900.0

Sample Input and Output 2:

Enter your account number:
1111111
Enter the balance of the account:
10000
Enter the type of transfer to be made:
1.NEFT
2.IMPS
3.RTGS
2
Enter the amount to be transferred:
1000
Account number or transfer amount seems to be wrong

Sample Input and Output 3:

Enter your account number:
1234567890
Enter the balance of the account:
50000
Enter the type of transfer to be made:
1.NEFT
2.IMPS
3.RTGS
3
Enter the amount to be transferred:
7500
Transfer could not be made

Solution


import java.util.*;
abstract class FundTransfer {
    private String accountNumber;
    private Double balance;
    public FundTransfer(String accountNumber, Double balance) {
        this.accountNumber = accountNumber;
        this.balance = balance;
    }
    public String getAccountNumber() {
        return accountNumber;
    }
    //sauravhathi
    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }
    public Double getBalance() {
        return balance;
    }
    public void setBalance(Double balance) {
        this.balance = balance;
    }
    public abstract Boolean validate(Double transfer);
    public abstract Boolean transfer(Double transfer);
}

class NEFTTransfer extends FundTransfer {
    public NEFTTransfer(String accountNumber, Double balance) {
        super(accountNumber, balance);
    }
    public Boolean validate(Double transfer) {
        if(this.getAccountNumber().length()==10 && transfer>0 && transfer<this.getBalance()) {
            return true;
        }
        return false;
    }
    public Boolean transfer(Double transfer) {
        if(this.validate(transfer)) {
            this.setBalance(this.getBalance()-transfer-transfer*0.05);
            return true;
        }
        //sauravhathi
        return false;
    }
}

class IMPSTransfer extends FundTransfer {
    public IMPSTransfer(String accountNumber, Double balance) {
        super(accountNumber, balance);
    }
    public Boolean validate(Double transfer) {
        if(this.getAccountNumber().length()==10 && transfer>0 && transfer<this.getBalance()) {
            return true;
        }
        return false;
    }
    public Boolean transfer(Double transfer) {
        if(this.validate(transfer)) {
            this.setBalance(this.getBalance()-transfer-transfer*0.02);
            return true;
        }
        //sauravhathi
        return false;
    }
}

class RTGSTransfer extends FundTransfer {
    public RTGSTransfer(String accountNumber, Double balance) {
        super(accountNumber, balance);
    }
    public Boolean validate(Double transfer) {
        if(this.getAccountNumber().length()==10 && transfer>0 && transfer<this.getBalance()) {
            return true;
        }
        return false;
//sauravhathi
    }
    public Boolean transfer(Double transfer) {
        if(this.validate(transfer) && transfer>10000) {
            this.setBalance(this.getBalance()-transfer);
            return true;
        }
        return false;
    }
}
//sauravhathi
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your account number:");
        String accountNumber = sc.nextLine();
        System.out.println("Enter the balance of the account:");
        Double balance = sc.nextDouble();
        System.out.println("Enter the type of transfer to be made:");
        System.out.println("1.NEFT");
        System.out.println("2.IMPS");
        System.out.println("3.RTGS");
        int choice = sc.nextInt();
        System.out.println("Enter the amount to be transferred:");
        Double transfer = sc.nextDouble();
        FundTransfer ft = null;
//sauravhathi
        if(choice==1) {
            ft = new NEFTTransfer(accountNumber, balance);
        }
        else if(choice==2) {
            ft = new IMPSTransfer(accountNumber, balance);
        }
        else if(choice==3) {
            ft = new RTGSTransfer(accountNumber, balance);
        }
//sauravhathi
        if(ft.validate(transfer)) {
            if(ft.transfer(transfer)) {
                System.out.println("Transfer occurred successfully");
                System.out.println("Remaining balance is "+ft.getBalance());
            }
            else {
                System.out.println("Transfer could not be made");
            }
        }
        //sauravhathi
        else {
            System.out.println("Account number or transfer amount seems to be wrong");
        }

    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *