UID:18BCS2051
Subject : Project Based Learning in Java
Practical No.3
Calculate interest based on the type of the account and the status of the account holder.
The rates of interest changes according to the amount (greater than or less than 1 crore),
age of account holder (General or Senior citizen) and number of days if the type of account
is FD or RD.
Program Code:
package acc;
import java.util.*;
abstract class acc{
double interest;
double amount;
double interestAmount;
protected abstract double calculateInterest();
}
class FDAccount extends acc{
private int noOfDays;
private int ageOfACHolder;
FDAccount(int am,int days,int age){
amount = am;
noOfDays = days;
ageOfACHolder = age;
}
@Override
protected double calculateInterest() {
if (amount < 10000000) {
if(ageOfACHolder < 65) {
if(noOfDays >= 7 && noOfDays <= 14) {
interest = 4.50;
}
else if(noOfDays >= 15 && noOfDays <= 29) {
interest = 4.75;
}
else if(noOfDays >= 30 && noOfDays <= 45) {
interest = 5.50;
}
else if(noOfDays >= 46 && noOfDays <= 60) {
interest = 7.00;
}
else if(noOfDays >= 61 && noOfDays <= 184) {
interest = 7.50;
}
else if(noOfDays >= 185 && noOfDays <= 365) {
interest = 8.00;
}
}
else {
if(noOfDays >= 7 && noOfDays <= 14) {
interest = 5.00;
}
else if(noOfDays >= 15 && noOfDays <= 29) {
interest = 5.25;
}
else if(noOfDays >= 30 && noOfDays <= 45) {
interest = 6.00;
}
else if(noOfDays >= 46 && noOfDays <= 60) {
interest = 7.50;
}
else if(noOfDays >= 61 && noOfDays <= 184) {
interest = 8.00;
}
else if(noOfDays >= 185 && noOfDays <= 365) {
interest = 8.50;
}
}
}
else {
if(noOfDays >= 7 && noOfDays <= 14) {
interest = 6.50;
}
else if(noOfDays >= 15 && noOfDays <= 29) {
interest = 6.75;
}
else if(noOfDays >= 30 && noOfDays <= 45) {
interest = 6.75;
}
else if(noOfDays >= 46 && noOfDays <= 60) {
interest = 8.00;
}
else if(noOfDays >= 61 && noOfDays <= 184) {
interest = 8.50;
}
else if(noOfDays >= 185 && noOfDays <= 365) {
interest = 10.00;
}
interestAmount = interest * amount/100;
return interestAmount;
}
}
class SBAccount extends acc{
SBAccount(int am){
amount = am;
}
@Override
protected double calculateInterest() {
interest= 4.0;
interestAmount =interest * amount/100;
return interestAmount;
}
}
class RDAccount extends acc{
int noOfMonths;
double monthlyAmount;
private int ageOfACHolder;
RDAccount( int am, int months, int age){
monthlyAmount = am;
ageOfACHolder = age;
noOfMonths = months;
}
@Override
protected double calculateInterest() {
if(ageOfACHolder < 65) {
if(noOfMonths >= 7 && noOfMonths <= 14) {
interest = 4.50;
}
else if(noOfMonths >= 15 && noOfMonths <= 29) {
interest = 4.75;
}
else if(noOfMonths >= 30 && noOfMonths <= 45) {
interest = 5.50;
}
else if(noOfMonths >= 46 && noOfMonths <= 60) {
interest = 7.00;
}
else if(noOfMonths >= 61 && noOfMonths <= 184) {
interest = 7.50;
}
else if(noOfMonths >= 185 && noOfMonths <= 365) {
interest = 8.00;
}
}
else {
if(noOfMonths <= 6) {
interest = 8.00;
}
else if(noOfMonths <= 9) {
interest = 8.25;
}
else if(noOfMonths <= 12) {
interest = 8.50;
}
else if(noOfMonths <= 15) {
interest = 8.75;
}
else if(noOfMonths <= 18) {
interest = 9.00;
}
else if(noOfMonths > 18) {
interest = 9.25;
}
}
interestAmount = 0;
for(int i = 0;i < interest;i++) {
interestAmount += interest * monthlyAmount/100;
}
return interestAmount;
}
}
public class account {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=0;
int am;
while(n < 4) {
System.out.println("Select the option:");
System.out.println("1. Interest Calculator – SB");
System.out.println( "2. Interest Calculator – FD");
System.out.println("3. Interest Calculator – RD");
System.out.println("4. Exit");
n = sc.nextInt();
switch(n)
{
case 1:
{
System.out.print("Enter the Average amount in your account:");
am = sc.nextInt();
SBAccount sb =new SBAccount( am );
System.out.println("Interest Gained :" + sb.calculateInterest());
break;
}
case 2:
{
System.out.print("Enter the FD amount:");
am = sc.nextInt();
System.out.print(" the number of days:");
int ndays = sc.nextInt();
System.out.print("Enter your age:");
int age = sc.nextInt();
if(ndays > 0) {
FDAccount fd = new FDAccount( am, ndays, age );
System.out.println("Interest gained is:" +
fd.calculateInterest());
}
else {
System.out.println("Invalid Number of days! Please enter
correct values.");
}
break;
}
case 3:
{
System.out.print("Enter the RD monthly amount:");
am = sc.nextInt();
System.out.print(" the number of Months:");
int nmonths = sc.nextInt();
System.out.print("Enter your age:");
int age = sc.nextInt();
RDAccount rd = new RDAccount( am, nmonths, age );
System.out.println("Interest gained is:" +
rd.calculateInterest());
break;
}
case 4:
{
break;
}
}
}
sc.close();
}
}
Output Window:
1. SB:
2. FD(Senior Citizen):
3. FD(General Citizen):
4. FD(invalid days):
5. RD(Senior Citizen):
6. RD(General Citizen):
7. Exit: