0% found this document useful (0 votes)
3 views

op

The document defines a banking system with classes for BankAccount, SavingsAccount, and CurrentAccount. The BankAccount class includes basic functionalities like deposit and balance management, while the SavingsAccount adds features such as interest, minimum balance, and withdrawal penalties. The CurrentAccount includes service charges and transaction fees for withdrawals.

Uploaded by

fa23-bse-210
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

op

The document defines a banking system with classes for BankAccount, SavingsAccount, and CurrentAccount. The BankAccount class includes basic functionalities like deposit and balance management, while the SavingsAccount adds features such as interest, minimum balance, and withdrawal penalties. The CurrentAccount includes service charges and transaction fees for withdrawals.

Uploaded by

fa23-bse-210
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

NAME : ABDUL WASAY

REG NO. FA23-BSE-210

// Base class for Bank Account


class BankAccount {
private String accountHolderName;
private double balance;

public BankAccount(String accountHolderName, double balance) {


this.accountHolderName = accountHolderName;
this.balance = balance;
}

// Getter and Setter for accountHolderName


public String getAccountHolderName() {
return accountHolderName;
}

public void setAccountHolderName(String accountHolderName) {


this.accountHolderName = accountHolderName;
}

// Getter and Setter for balance


public double getBalance() {
return balance;
}

public void setBalance(double balance) {


this.balance = balance;
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
System.out.println("Deposited Rs " + amount + " to account. New
balance: Rs " + balance);
} else {
System.out.println("Invalid deposit amount.");
}
}
}

// Savings Account subclass


class SavingsAccount extends BankAccount {
private final double interestRate = 0.05; // 5% interest rate
private final double minimumBalance = 20000;
private final double earlyWithdrawalPenalty = 0.2; // 20% penalty on
withdrawn amount
private final double maxWithdrawalPercent = 0.8; // Can withdraw at
most 80% of the balance
private final int minimumYearsToHold = 5;
private int yearsHeld;

public SavingsAccount(String accountHolderName, double balance, int


yearsHeld) {
super(accountHolderName, balance);
this.yearsHeld = yearsHeld;
}

// Getter and Setter for yearsHeld


public int getYearsHeld() {
return yearsHeld;
}

public void setYearsHeld(int yearsHeld) {


this.yearsHeld = yearsHeld;
}

public void withdraw(double amount) {


if (getBalance() < minimumBalance) {
System.out.println("Cannot withdraw. Minimum balance of Rs " +
minimumBalance + " is required.");
return;
}

if (amount > getBalance() * maxWithdrawalPercent) {


System.out.println("Cannot withdraw more than 80% of the
balance.");
return;
}

if (yearsHeld < minimumYearsToHold) {


double penalty = amount * earlyWithdrawalPenalty;
setBalance(getBalance() - (amount + penalty));
System.out.println("Early withdrawal penalty imposed. Withdrawn
Rs " + amount + " with penalty Rs " + penalty + ". New balance: Rs " +
getBalance());
} else {
setBalance(getBalance() - amount);
System.out.println("Withdrawn Rs " + amount + ". New balance: Rs
" + getBalance());
}
}

public void addInterest() {


double interest = getBalance() * interestRate;
setBalance(getBalance() + interest);
System.out.println("Interest added: Rs " + interest + ". New balance:
Rs " + getBalance());
}
}

// Current Account subclass


class CurrentAccount extends BankAccount {
private final double serviceChargePercent = 0.06; // 6% service charge
on withdrawal
private final double transactionFee = 50; // Rs 50 per transaction

public CurrentAccount(String accountHolderName, double balance) {


super(accountHolderName, balance);
}

public void withdraw(double amount) {


double serviceCharge = amount * serviceChargePercent;
double totalDeduction = amount + serviceCharge + transactionFee;

if (totalDeduction > getBalance()) {


System.out.println("Insufficient balance for withdrawal and
charges.");
return;
}

setBalance(getBalance() - totalDeduction);
System.out.println("Withdrawn Rs " + amount + " with service
charges of Rs " + serviceCharge + " and transaction fee of Rs " +
transactionFee + ". New balance: Rs " + getBalance());
}
}

// Testing the classes


public class BankTest {
public static void main(String[] args) {
// Creating a Savings Account
SavingsAccount savingsAccount = new SavingsAccount("Alice",
25000, 3);
System.out.println("Account Holder: " +
savingsAccount.getAccountHolderName());
System.out.println("Initial Balance: Rs " +
savingsAccount.getBalance());

savingsAccount.addInterest();
savingsAccount.withdraw(15000);

// Creating a Current Account


CurrentAccount currentAccount = new CurrentAccount("Bob", 30000);
System.out.println("\nAccount Holder: " +
currentAccount.getAccountHolderName());
System.out.println("Initial Balance: Rs " +
currentAccount.getBalance());

currentAccount.withdraw(10000);
}
}

Class Definitions

1. BankAccount Class

o Purpose: Represents a basic bank account with essential


functionalities such as deposit and balance management.

o Attributes:

 accountHolderName: The name of the account holder


(String).

 balance: The current balance in the account (double).

o Methods:

 deposit(double amount): Adds money to the account


balance.

 getBalance(): Returns the current balance of the account.

 setBalance(double balance): Sets the account balance.

 getAccountHolderName(): Returns the name of the


account holder.

 setAccountHolderName(String accountHolderName):
Sets the account holder’s name.

2. SavingsAccount Class (Subclass of BankAccount)


o Purpose: Represents a savings account with special features
such as interest, minimum balance, early withdrawal penalties,
and withdrawal restrictions.

o Attributes:

 interestRate: The interest rate applied to the account (5%


per annum).

 minimumBalance: The minimum balance required (Rs


20,000).

 earlyWithdrawalPenalty: Penalty imposed if money is


withdrawn before 5 years (20%).

 maxWithdrawalPercent: Maximum percentage of the


account balance that can be withdrawn (80%).

 minimumYearsToHold: The minimum number of years the


account must be held (5 years).

 yearsHeld: The number of years the account has been


held.

o Methods:

 withdraw(double amount): Allows the user to withdraw


money, subject to restrictions and penalties.

 addInterest(): Adds interest to the balance at the defined


interest rate.

 getYearsHeld(): Returns the number of years the account


has been held.

 setYearsHeld(int yearsHeld): Sets the number of years


the account has been held.

3. CurrentAccount Class (Subclass of BankAccount)

o Purpose: Represents a current account with features such as


service charges and transaction fees.

o Attributes:

 serviceChargePercent: Service charge percentage on


withdrawals (6%).
 transactionFee: A fixed fee for each transaction (Rs 50).

o Methods:

 withdraw(double amount): Allows withdrawal with


service charges and transaction fees deducted.

You might also like