JAVA 1. A bank offers two types of account for its customers, a savings account
ID: 3704320 • Letter: J
Question
JAVA
1. A bank offers two types of account for its customers, a savings account and a checking account. The savings account provides compound interest* and transfers money between accounts. The checking account provides withdrawal but no interest. Checking account holders should also maintain a minimum balance of $300 while a savings account should have a minimum balance of $100. If the balance falls below the minimum requirement, a service charge is imposed.
Create a class Account that stores customer name, account number and type of account. From this derive two classes CheckingAccount and SavingsAccount. Include appropriate methods/instance variables to achieve the following tasks:
(a).Deposit amount to an account (checking or savings)
(b).Display the balance of an account (checking or savings)
(c).Compute and deposit compound interest of a savings account
(d).Permit withdrawal and update the balance accordingly (checking or savings). Check that the withdrawal should not be more than the account balance available. Impose a service charge, if the balance falls below the minimum balance required. Also, give user an option to transfer funds from the savings account (if sufficient balance available) when withdrawing from a checking account if there are insufficient funds available in the checking account. Update the savings balance accordingly.
*Compound Interest is calculated as:
CI = P (1 + r/n) (nt)
Where:
CI = Compound Interest
P = the principal investment amount (the initial deposit) r = the annual interest rate (decimal)
n = the number of times that interest is compounded per year t = the number of years the money is invested or borrowed for
Ref: https://www.thecalculatorsite.com/articles/finance/compound-interest-formula.php
You may get input from user for each function. When your program starts, create a checking account object and a savings account object, and displays a menu of four functions that a user can select.
Explanation / Answer
public class SavingsAccount
{
private double balance;
private double interest;
Private string name;
private string accountType;
private long accNo;
public SavingsAccount(string name, string accountType, long accNo)
{
this.name=name;
this.accountType=account Type;
this.accNo=accNo;
balance = 0;
interest = 0;
}
public SavingsAccount(double initialBalance, double initialInterest, string name, string accountType, long accNo)
{
this.name=name;
this.accountType=accountType;
this.accNo=accNo;
balance = initialBalance;
interest = initialInterest;
}
public void deposit(double amount)
{
if (amount < 0) // deposit value is negative
{
System.out.println ();
System.out.println ("Error: Deposit amount is invalid.");
System.out.println (accNo + " " + fmt.format(amount));
}
else
balance = balance + amount;
return balance;
}
public void withdraw(double amount)
{
int fee =60;
amount += fee; // optional- if you don't want to apply service //charge remove this statement
if (amount < 0) // withdraw value is negative
{
System.out.println ();
System.out.println ("Error: Withdraw amount is invalid.");
System.out.println ("Account: " + accNo); System.out.println ("Requested: " + fmt.format(amount));
}
if (accountType=="Saving")
// for saving accounts amount withdrawal
{
if (amount >( balance-100)) // withdraw value exceeds balance
{
System.out.println ();
System.out.println ("Error: Insufficient funds.");
System.out.println ("Account: " + accNo); System.out.println ("Requested: " + fmt.format(amount));
System.out.println ("Available: " + fmt.format(balance));
}
else balance = balance - amount;
}
else // for checking accounts amount withdrawal
{
if (amount >( balance-300)) // withdraw value exceeds balance
{
System.out.println ();
System.out.println ("Error: Insufficient funds.");
System.out.println ("Account: " + accNo); System.out.println ("Requested: " + fmt.format(amount));
System.out.println ("Available: " + fmt.format(balance));
}
else
balance = balance - amount;
}
return balance;
}
public void addInterest(double r, double t, double n, double p) {
double CI = P *(1 + r/n) *(n*t)
}
public double getBalance() {
return balance;
}
}
public class SavingsAccountTester
{
public static void main(String[] args)
{
SavingsAccount obj = new SavingsAccount(1000, 0.10,"sam", "Saving",345678);
SavingsAccount obj1= new SavingsAccount (2000,0.8,"Tim","Saving", 6788 );
obj.withdraw(250);
obj.deposit(400);
obj.addInterest(10, 2, 7, 455);
System.out.println(obj.getBalance());
SavingAccountTester t=new SavingAccountTester;
t.transfer(obj, obj1,500);
System.out.println(obj.getBalance());
}
public static void transfer (SavingsAccount from, SavingsAccount to, double amount)
{
from.withdraw(amount);
to.deposit(amount);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.