[go: up one dir, main page]

0% found this document useful (0 votes)
13 views5 pages

Java Exp 1.2

The document outlines an experiment for a Java application that calculates interest for Fixed Deposits (FDs) and Recurring Deposits (RDs) using inheritance. It includes class definitions, an algorithm for operations, and a code implementation that handles user input and exceptions. Learning outcomes focus on object-oriented programming, program flow management, and user-friendly system design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Java Exp 1.2

The document outlines an experiment for a Java application that calculates interest for Fixed Deposits (FDs) and Recurring Deposits (RDs) using inheritance. It includes class definitions, an algorithm for operations, and a code implementation that handles user input and exceptions. Learning outcomes focus on object-oriented programming, program flow management, and user-friendly system design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENT OF

COMPUTER SCIENCE &


ENGINEERING
Experiment 1.3

Student Name: Prince Kumar UID: 22BCS10715


Branch: CSE Section:IOT_616/B
Semester: 6th DOP:19/01/25
Subject: Java Subject Code: 22CSH-359

Aim: Create an application to calculate interest for FDs, RDs based on certain conditions using
inheritance.

Objective Create an application to calculate interest for Fixed Deposits (FDs) and
Recurring Deposits (RDs) using inheritance. The system will ensure accurate calculations
based on specific conditions, support modularity for future enhancements, and maintain a
user-friendly interface for seamless operation.

Algorithm:
  Define Classes:

 Video: Attributes: videoID, title, genre, isAvailable. Methods: markRented(),


markReturned().

 Inventory: Maintain a list of videos. Methods: addVideo(), listAvailableVideos(),


isVideoAvailable().

 Customer: Attributes: customerID, name, rentedVideos. Methods: rentVideo(),


returnVideo().

 RentalSystem: Attributes: inventory, customers. Methods: listVideos(),


rentVideoToCustomer(), returnVideoFromCustomer().

  Menu-Driven Operations:

 Add a new video to the inventory.

 List available videos.

 Rent a video to a customer.

 Return a video from a customer.

 Exit the program.

 Use the RentalSystem methods to perform actions based on user input.

 Handle invalid inputs with exceptions or validation.

 End the loop when the user chooses to exit.


DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
Code:
import java.util.Scanner;

abstract class Account {


protected double rate;
protected double amount;

public Account(double rate, double amount) {


this.rate = rate;
this.amount = amount;
}

public abstract double calculateInterest();


}
class InvalidInput extends Exception {
public InvalidInput(String msg) {
super(msg);
}
}
class SBAccount extends Account {
private String type;
public SBAccount(double amount, String type) throws InvalidInput {
super(type.equalsIgnoreCase("Normal") ? 4 : type.equalsIgnoreCase("NRI") ? 6 : -1, amount);
if (this.rate == -1 || amount < 0) throw new InvalidInput("Invalid account type or amount!");
this.type = type;
}

@Override
public double calculateInterest() {
return (amount * rate) / 100;
}
}

class FDAccount extends Account {


private int days;
private int age;

public FDAccount(double amount, int days, int age) throws InvalidInput {


super(0, amount);
if (amount < 0 || days <= 0) throw new InvalidInput("Invalid FD amount or days!");
this.days = days;
this.age = age;

if (amount < 10000000) {


if (days <= 14) rate = age >= 60 ? 5 : 4.5;
else if (days <= 29) rate = age >= 60 ? 5.25 : 4.75;
else if (days <= 45) rate = age >= 60 ? 6 : 5.5;
else if (days <= 60) rate = age >= 60 ? 7.5 : 7;
else if (days <= 184) rate = age >= 60 ? 8 : 7.5;
else if (days <= 365) rate = age >= 60 ? 8.5 : 8;
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
else throw new InvalidInput("Invalid number of days!");
} else {
if (days <= 14) rate = 6.5;
else if (days <= 29) rate = 6.75;
else if (days <= 45) rate = 6.75;
else if (days <= 60) rate = 8;
else if (days <= 184) rate = 8.5;
else if (days <= 365) rate = 10;
else throw new InvalidInput("Invalid number of days!");
}
}

@Override
public double calculateInterest() {
return (amount * rate) / 100;
}
}

class RDAccount extends Account {


private int months;
private int age;

public RDAccount(double monthlyDeposit, int months, int age) throws InvalidInput {


super(0, monthlyDeposit * months);
if (monthlyDeposit < 0 || months <= 0) throw new InvalidInput("Invalid deposit or months!");
this.months = months;
this.age = age;

if (months == 6) rate = age >= 60 ? 8 : 7.5;


else if (months == 9) rate = age >= 60 ? 8.25 : 7.75;
else if (months == 12) rate = age >= 60 ? 8.5 : 8;
else if (months == 15) rate = age >= 60 ? 8.75 : 8.25;
else if (months == 18) rate = age >= 60 ? 9 : 8.5;
else if (months == 21) rate = age >= 60 ? 9.25 : 8.75;
else throw new InvalidInput("Invalid number of months!");
}

@Override
public double calculateInterest() {
return (amount * (months + 1) / 2) * (rate / 100) * (1.0 / 12);
}
}

public class exp3 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

while (true) {
System.out.println("... Bank Management System......\n");
System.out.println("...code by Prince Kumar...\n");
System.out.println("1. SB Interest\n2. FD Interest\n3. RD Interest\n4. Exit");
int option = sc.nextInt();
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
try {
if (option == 1) {

System.out.println("Enter amount:");
double amt = sc.nextDouble();
System.out.println("Type (Normal/NRI):");
sc.nextLine();
String type = sc.nextLine();
SBAccount sb = new SBAccount(amt, type);
System.out.println("Interest: Rs. " + sb.calculateInterest());
} else if (option == 2) {
System.out.println("Enter amount:");
double amt = sc.nextDouble();
System.out.println("Days:");
int days = sc.nextInt();
System.out.println("Age:");
int age = sc.nextInt();
FDAccount fd = new FDAccount(amt, days, age);
System.out.println("Interest: Rs. " + fd.calculateInterest());
} else if (option == 3) {
System.out.println("Monthly deposit:");
double deposit = sc.nextDouble();
System.out.println("Months:");
int months = sc.nextInt();
System.out.println("Age:");
int age = sc.nextInt();
RDAccount rd = new RDAccount(deposit, months, age);
System.out.println("Interest: Rs. " + rd.calculateInterest());
} else if (option == 4) {
System.out.println("Exiting...");
break;
} else {
System.out.println("Invalid option!");
}
} catch (InvalidInput e) {
System.out.println(e.getMessage());
}
}

sc.close();
}
}

Output:
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING

Learning Outcomes:

  Understand how to create and use classes to represent real-world entities and implement inheritance
in object-oriented programming.

 Develop skills in using loops, conditionals, and methods to manage program flow and perform specific tasks like
interest calculations

 Learn to handle dynamic data efficiently by integrating data structures like ArrayList for managing
multiple objects.

  Design user-friendly systems with menu-driven interfaces and robust error handling through custom
exceptions.

You might also like