0% found this document useful (0 votes)
8 views2 pages

Java Bank and Savings Account Example

Uploaded by

karthan.official
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Java Bank and Savings Account Example

Uploaded by

karthan.official
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//First Program : MyPack/BankAccount.

java

package MyPack;

public class BankAccount {


private String accountHolder;
private double balance;

// Constructor
public BankAccount(String accountHolder, double initialBalance) {
[Link] = accountHolder;
[Link] = initialBalance;
}

// Deposit method
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
[Link]("Deposited: $" + amount);
} else {
[Link]("Invalid deposit amount.");
}
}

// Withdraw method
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
[Link]("Withdrew: $" + amount);
} else {
[Link]("Invalid withdrawal amount.");
}
}

// Get balance method


public double getBalance() {
return balance;
}

// Display account details


public void displayAccountInfo() {
[Link]("Account Holder: " + accountHolder);
[Link]("Balance: $" + balance);
}
}

//Second Program : MyPack/[Link]

package MyPack;

public class SavingsAccount extends BankAccount {


private double interestRate;

// Constructor
public SavingsAccount(String accountHolder, double initialBalance, double
interestRate) {
super(accountHolder, initialBalance);
[Link] = interestRate;
}
// Apply interest
public void applyInterest() {
double interest = getBalance() * (interestRate / 100);
deposit(interest);
[Link]("Interest applied: $" + interest);
}

// Override account info display to include interest rate


@Override
public void displayAccountInfo() {
[Link]();
[Link]("Interest Rate: " + interestRate + "%");
}
}

//Main Program : [Link]

import [Link];
import [Link];

public class MainClass {


public static void main(String[] args) {
// Create a BankAccount object
BankAccount account1 = new BankAccount("Alice", 1000);
[Link]();
[Link](500);
[Link](300);
[Link]();

[Link]();

// Create a SavingsAccount object


SavingsAccount account2 = new SavingsAccount("Bob", 2000, 5); // 5%
interest rate
[Link]();
[Link]();
[Link]();
}
}

You might also like