Produce a java class to describe a BankAccount object. A an account is described
ID: 669219 • Letter: P
Question
Produce a java class to describe a BankAccount object. A an account is described with:
the account owner's name
an account ID stored as text
the balance in the account
interest rate
The BankAccount class must include:
instance data variables
two constructors (one takes an inital balance, the second opens an account with no money)
getter and setters (include appropriate value checks when applicable)
a toString method (use console for output)
a deposit method (incluide appropriate value checks)
a withdrawal method (include appropriate value checks)
a method to adjust the interest rate and add interest
Write a driver program to demonstrate the class including:
creating bank account objects using both constructors
making deposits and withdrawls (with both valid and invalid numbers)
demonstrate interest rate functionality
invoking other methods
(the driver program does not have to be interactive just needs to create objects and invoke the methods to demonstrate functonality.)
Use constants if needed
Explanation / Answer
program:
public class BankAccount {
int balance;
int interest_rate=0;
public BankAccount() {
balance=0;
}
public BankAccount(int initial_balance){
balance=initial_balance;
System.out.println("Intial balance is "+ initial_balance);
}
public void deposite(int amount)
{
if(amount<=0)
System.out.println("please enter valid amount");
else
{
balance+=amount;
System.out.println("Balance after deposite of "+amount + " is "+ balance );
}
}
public void withdrawal(int amount)
{
if(amount<=0)
System.out.println("please enter valid amount");
else if(balance<amount)
System.out.println("balance is less than amount which you want to withdrawal");
else
{
balance-=amount;
System.out.println("Balance after withdrawal of "+amount + " is "+ balance );
}
}
public void display()
{
System.out.println("Current Balance is " + balance );
}
public void apply_interest()
{
balance=balance*(1+interest_rate/100);
System.out.println("Current balance after applying interest of "+interest_rate +"% is "+ balance );
}
public void change_interest(int i)
{
interest_rate=i;
System.out.println("New interest rate is "+ interest_rate);
}
public static void main(String str[])
{
BankAccount driver=new BankAccount(1000);
driver.deposite(3000);
driver.withdrawal(2800);
driver.display();
driver.apply_interest();
driver.change_interest(14);
driver.apply_interest();
}
}
Result
Intial balance is 1000
Balance after deposite of 3000 is 4000
Balance after withdrawal of 2800 is 1200
Current Balance is 1200
Current balance after applying interest of 0% is 1200
New interest rate is 14
Current balance after applying interest of 14% is 1200
if wrong input
Balance after deposite of 4000 is 5000
balance is less than amount which you want to withdrawal
Current Balance is 5000
Current balance after applying interest of 0% is 5000
New interest rate is 14
Current balance after applying interest of 14% is 5000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.