forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncapsulationDemo.java
More file actions
36 lines (29 loc) · 825 Bytes
/
EncapsulationDemo.java
File metadata and controls
36 lines (29 loc) · 825 Bytes
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
package com.thealgorithms.oopconcepts;
class Account {
private String holder;
private double balance;
public String getHolder() {
return holder;
}
public void setHolder(String holder) {
this.holder = holder;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) balance += amount;
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) balance -= amount;
}
}
public class EncapsulationTest {
public static void main(String[] args) {
Account acc = new Account();
acc.setHolder("Suryanshu");
acc.deposit(50000);
acc.withdraw(12000);
System.out.println(acc.getHolder() + " has balance ₹" + acc.getBalance());
}
}