-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.java
More file actions
36 lines (33 loc) · 1.11 KB
/
Copy pathBankAccount.java
File metadata and controls
36 lines (33 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: " + amount);
} else {
System.out.println("Invalid deposit amount.");
}
} protected void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Invalid withdrawal amount or insufficient balance.");
}
}
void checkBalance() {
System.out.println("Current Balance: " + balance);
}
public static void main(String[] args) {
// Creating an object of BankAccount
BankAccount account = new BankAccount(1000);
// Accessing public method
account.deposit(500);
// Accessing protected method
account.withdraw(200);
account.checkBalance();
}
}