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

j Worksheet6 Inheritace

The document contains Java classes for a banking system, including a base class 'BankAccount' and derived classes 'SavingBankAccount' and 'CurrentBankAccount'. It outlines methods for depositing, withdrawing, and displaying account balances, as well as calculating interest for savings and penalties for overdrafts. The main class demonstrates the functionality of these accounts through various operations.

Uploaded by

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

j Worksheet6 Inheritace

The document contains Java classes for a banking system, including a base class 'BankAccount' and derived classes 'SavingBankAccount' and 'CurrentBankAccount'. It outlines methods for depositing, withdrawing, and displaying account balances, as well as calculating interest for savings and penalties for overdrafts. The main class demonstrates the functionality of these accounts through various operations.

Uploaded by

Kiran D C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

class BankAccount {

protected String accountHolder;


protected double balance;

public BankAccount(String accountHolder, double balance) {


this.accountHolder = accountHolder;
this.balance = balance;
}

public void deposit(double amount) {


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

public void withdraw(double amount) {


if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Insufficient balance or invalid amount.");
}
}

public void displayBalance() {


System.out.println(accountHolder + "'s Account Balance: " + balance);
}
}

// Derived class: SavingBankAccount


class SavingBankAccount extends BankAccount {
private double interestRate;

public SavingBankAccount(String accountHolder, double balance, double interestRate) {


/*Complete*/
}

public void calculateInterest() {


/*Complete*/
}
}

class CurrentBankAccount extends BankAccount {


private double overdraftLimit;

public CurrentBankAccount(String accountHolder, double balance, double overdraftLimit) {


/*Complete*/
}
@Override
public void withdraw(double amount) {
if (amount > 0 && (balance + overdraftLimit) >= amount) {
/*Complete*/
}

public void calculateInterest() {


double interest = (balance < 0) ? balance * 0.05 : 0; // 5% penalty on negative balance
/*Complete*/
System.out.println("Interest/Penalty Applied: " + interest);
}
}

// Main Class to Test Functionality


public class jInheritanceAccount {
public static void main(String[] args) {
SavingBankAccount savingAcc = new SavingBankAccount("Hari", 5000, 4.5);
CurrentBankAccount currentAcc = new CurrentBankAccount("Hari", 3000, 2000);

// Performing operations
System.out.println("\n--- Savings Account Operations ---");
savingAcc.deposit(2000);
savingAcc.calculateInterest();
savingAcc.withdraw(1000);
savingAcc.displayBalance();

System.out.println("\n--- Current Account Operations ---");


currentAcc.withdraw(4000); // Allows overdraft
currentAcc.calculateInterest();
currentAcc.displayBalance();
/*Upcasting & Downcasting*/
BankAccount Acc = new SavingBankAccount("Hari", 7000, 4.5);
Acc.deposit(2000);
Acc.withdraw(1000);
Acc.displayBalance();
// Acc.calculateInterest();

SavingBankAccount savingAcc = (SavingBankAccount) Acc;


savingAcc.calculateInterest();
}
}
Program when the attributes are Private

class BankAccount {
private String accountHolder;
private double balance;

public BankAccount(String accountHolder, double balance) {


this.accountHolder = accountHolder;
this.balance = balance;
}

public String getAccountHolder() {


return accountHolder;
}

public double getBalance() {


return balance;
}

protected void setBalance(double balance) {


this.balance = balance;
}

public void deposit(double amount) {


if (amount > 0) {
setBalance(getBalance() + amount);
System.out.println("Deposited: " + amount);
} else {
System.out.println("Invalid deposit amount.");
}
}

public void withdraw(double amount) {


if (amount > 0 && amount <= getBalance()) {
setBalance(getBalance() - amount);
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Insufficient balance or invalid amount.");
}
}

public void displayBalance() {


System.out.println(getAccountHolder() + "'s Account Balance: " + getBalance());
}
}

class SavingBankAccount extends BankAccount {


private double interestRate;

public SavingBankAccount(String accountHolder, double balance, double interestRate) {


super(/*Complete*/);
this.interestRate = interestRate;
}

public void calculateInterest() {


/*Complete the Code*/
}

// Derived class: CurrentBankAccount


class CurrentBankAccount extends BankAccount {
private double overdraftLimit;

public CurrentBankAccount(String accountHolder, double balance, double overdraftLimit) {


super(/*Complete*/);
this.overdraftLimit = overdraftLimit;
}

// Overriding withdraw method to allow overdraft


@Override
public void withdraw(double amount) {
if (amount > 0 && (getBalance() + overdraftLimit) >= amount) {
/*Complete*/
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Overdraft limit exceeded or invalid amount.");
}
}

public void calculateInterest() {


double interest = (/*Complete*/ < 0) ? /*Complete*/ * 0.05 : 0;
/*Complete*/ + interest);
System.out.println("Interest/Penalty Applied: " + interest);
}
}

// Main Class to Test Functionality


public class jInheritanceAccountPrivate {
public static void main(String[] args) {
SavingBankAccount savingAcc = new SavingBankAccount("Hari", 5000, 4.5);
CurrentBankAccount currentAcc = new CurrentBankAccount("Hari", 3000, 2000);

System.out.println("\n--- Savings Account Operations ---");


savingAcc.deposit(2000);
savingAcc.calculateInterest();
savingAcc.withdraw(1000);
savingAcc.displayBalance();
System.out.println("\n--- Current Account Operations ---");
currentAcc.withdraw(4000);
currentAcc.calculateInterest();
currentAcc.displayBalance();
}
}

You might also like