Account Manipulation – Abstract class Program in java

In this post, we will learn Account Manipulation – Abstract class Program in java Programming language.

Question:

Yzee bank needs to automate the bank transactions.  There are many accounts, like Savings Account, Current Account, Demat Account and so on.

As start up, they need to automate the Savings Account details. 

You are provided with a public class Customer with private attributes :

                                  int  customerId

                                  String customerName

                                  String emailId

       Appropriate public getters and setters are already written.

Write a public 3 argument constructor with arguments – customerId, customerName and emailId.

Write a public class Account with protected attributes :

                                  int accountNumber

                                  Customer customerObj

                                  double balance

       Uncomment the  public getters and setters provided in the template.

Write a public 3 argument constructor with arguments – accountNumber, customerObj and balance.

Write a public method in Account class as,     

            public boolean withdraw(double amount) – Make this method as abstract.

Write  a public class SavingsAccount with private attribute : 

                                 double minimumBalance

              Uncomment the  public getters and setters provided in the template.

 Make this class SavingsAccount to inherit the Account class.

Write a public 4 argument constructor with arguments – accountNumber, customerObj, balance and minimumBalance.

Implement the  withdraw method  as

     public boolean withdraw(double amount) –  This method should return true if withdraw is successful, else return false.

     In this method, check if

               balance – amount   is greater than the minimum balance.

     If yes, perform withdraw.  Reduce the withdraw amount from the balance and return true.

    If not, return false.

Create a public class Main which has the main method.  Check the correctness of the methods written in these classes.

Note :  All class, methods needs to be declared as public

CODE:

Main.java

import java.util.Scanner;
public class Main{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int a2=sc.nextInt();
        String b2=sc.nextLine();
        sc.nextLine();
        String c2=sc.nextLine();
        Customer c=new Customer(a2,b2,c2);
        System.out.println("balance");
        double b=sc.nextDouble();
        System.out.println("min");
        double d=sc.nextDouble();
        System.out.println("amount");
        double f=sc.nextDouble();
        SavingsAccount a1=new SavingsAccount(a,c,b,d);
        boolean e=a1.withdraw(f);
        if(e==true)
        System.out.println("withdraw successful");
        else
        System.out.println("failed");
    }
}

Account.java

abstract public class Account {
protected int accountNumber;
protected Customer customerObj;
protected double balance;
public   Account(int accountNumber,Customer customerObj,double balance)
{
    this.accountNumber=accountNumber;
    this.customerObj=customerObj;
    this.balance=balance;
}
    public int getAccountNumber() {
		return accountNumber;
	}
	public void setAccountNumber(int accountNumber) {
		this.accountNumber = accountNumber;
	}
	public Customer getCustomerObj() {
		return customerObj;
	}
	public void setCustomerObj(Customer customerObj) {
		this.customerObj = customerObj;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
    abstract public boolean withdraw(double a);
}   

Customer.java

public class Customer {
	//Attributes
	private int  customerId;
	private String customerName;
	private String emailId;
	//Constructor
	public Customer(int customerId, String customerName, String emailId) {
		super();
		this.customerId = customerId;
		this.customerName = customerName;
		this.emailId = emailId;
	}
    //Getters and Setters
	public int getCustomerId() {
		return customerId;
	}
	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public String getEmailId() {
		return emailId;
	}
	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}
}

SavingAccount.java

public class SavingsAccount extends Account{
    private double minimumBalance;
    public SavingsAccount(int accountNumber,Customer customerobj,double balance, double MinimumBalance)
    {
     super(accountNumber,customerobj,balance);
     minimumBalance=MinimumBalance;
    }
    public double getMinimumBalance() {
		return minimumBalance;
	}
	public void setMinimumBalance(double minimumBalance) {
		this.minimumBalance = minimumBalance;
	}
   public boolean withdraw(double amount)
   {
       if((balance-amount)>minimumBalance)
       {
           balance=(balance-amount);
           return true;
       }
       else
       return false;
   }
}
  1. Employee Loan Eligibility – Polymorphism
Sharing Is Caring

Leave a Comment