🧪 Building a Simple Bank System using Inheritance and Encapsulation #100
akash-coded
started this conversation in
Tasks
Replies: 2 comments
-
Task 1 - Sayan Dey
// Base Class: Account
class Account {
private String accountNumber;
protected double balance;
public Account(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Insufficient balance!");
}
}
public double getBalance() {
return balance;
}
}
// Subclass: SavingsAccount
class SavingsAccount extends Account {
private double interestRate;
public SavingsAccount(String accountNumber, double initialBalance, double interestRate) {
super(accountNumber, initialBalance);
this.interestRate = interestRate;
}
public void addInterest() {
balance += balance * interestRate / 100;
System.out.println("Interest added.");
}
}
// Subclass: JuniorSavingsAccount
class JuniorSavingsAccount extends SavingsAccount {
private String nomineeName;
public JuniorSavingsAccount(String accountNumber, double initialBalance, double interestRate, String nomineeName) {
super(accountNumber, initialBalance, interestRate);
this.nomineeName = nomineeName;
}
@Override
public void withdraw(double amount) {
System.out.println("Nominee approval needed for money withdrawal.");
}
}
public class Main {
public static void main(String[] args) {
Account acc = new Account("12345", 1000);
SavingsAccount savings = new SavingsAccount("67890", 2000, 5);
JuniorSavingsAccount junior = new JuniorSavingsAccount("13579", 1500, 3, "Sayan Dey");
acc.deposit(500);
acc.withdraw(300);
System.out.println("Account balance: " + acc.getBalance());
savings.addInterest();
System.out.println("Savings Account balance: " + savings.getBalance());
junior.withdraw(200);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
PRASETHA N public class Account {
private String accountHolder;
private String accountNumber;
protected double balance;
public Account(String accountHolder, String accountNumber, double initialDeposit) {
this.accountHolder = accountHolder;
this.accountNumber = accountNumber;
this.balance = initialDeposit;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: $" + amount);
} else {
System.out.println("Deposit amount must be positive.");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: $" + amount);
} else {
System.out.println("Invalid withdrawal amount.");
}
}
public double getBalance() {
return balance;
}
public String getAccountInfo() {
return "Account Holder: " + accountHolder +
", Account Number: " + accountNumber +
", Balance: $" + balance;
}
}
public class SavingsAccount extends Account {
private double interestRate;
public SavingsAccount(String accountHolder, String accountNumber, double initialDeposit, double interestRate) {
super(accountHolder, accountNumber, initialDeposit);
this.interestRate = interestRate;
}
public void applyInterest() {
double interest = balance * (interestRate / 100);
deposit(interest);
System.out.println("Interest applied: $" + interest);
}
}
public class JuniorSavingsAccount extends SavingsAccount {
private static final double MAX_WITHDRAWAL = 100.0;
public JuniorSavingsAccount(String accountHolder, String accountNumber, double initialDeposit, double interestRate) {
super(accountHolder, accountNumber, initialDeposit, interestRate);
}
@Override
public void withdraw(double amount) {
if (amount > MAX_WITHDRAWAL) {
System.out.println("Withdrawal exceeds junior account limit of $" + MAX_WITHDRAWAL);
} else {
super.withdraw(amount);
}
}
}
public class BankDemo {
public static void main(String[] args) {
System.out.println("=== Regular Account ===");
Account regular = new Account("Ram", "1", 500.0);
regular.deposit(200);
regular.withdraw(100);
System.out.println(regular.getAccountInfo());
System.out.println("\n=== Savings Account ===");
SavingsAccount savings = new SavingsAccount("Mohan", "2", 1000.0, 5.0);
savings.applyInterest();
savings.withdraw(50);
System.out.println(savings.getAccountInfo());
System.out.println("\n=== Junior Savings Account ===");
JuniorSavingsAccount junior = new JuniorSavingsAccount("Beth", "3", 300.0, 4.0);
junior.withdraw(150);
junior.withdraw(80);
junior.applyInterest();
System.out.println(junior.getAccountInfo());
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
🧪 Title: Building a Simple Bank System using Inheritance and Encapsulation
Filename:
SimpleBankSystem.javaGoal: Reinforce class relationships, constructors, encapsulated fields, inheritance, and clean code practices.
🧾 Background:
You're part of a small fintech startup. The product is a basic bank account system. You need to implement:
Accountfor all types of accountsSavingsAccountthat inherits fromAccountJuniorSavingsAccountthat inherits fromSavingsAccountBankDemothat uses all the classes💡 Instructions:
You’ll complete the TODOs in the code below. Follow the JavaDoc-style comments to understand the purpose of each method.
Use meaningful variable names, and follow Java conventions (
camelCase,PascalCasefor class names).🧑💻 Starter Code –
SimpleBankSystem.java🏗 Key Concepts Reinforced:
SavingsAccount→Account,JuniorSavingsAccount→SavingsAccountsuper(...)📌 Suggested Student Practice (after this task):
setInterestRate()to allow changing interest.Accountobjects.FixedDepositAccount(inherit fromAccount) with alockInPeriodfield.withdraw()so it returns boolean to check success/failure (no print inside).Beta Was this translation helpful? Give feedback.
All reactions