Repository navigation
Expand file tree
/
Copy pathDemo.java
More file actions
53 lines (43 loc) · 1.63 KB
/
Copy pathDemo.java
File metadata and controls
53 lines (43 loc) · 1.63 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package Lab3;
public class Demo {
public class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account() {}
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public int getId() { return id; }
public double getBalance() { return balance; }
public double getAnnualInterestRate() { return annualInterestRate; }
public void setId(int id) { this.id = id; }
public void setBalance(double balance) { this.balance = balance; }
public void setAnnualInterestRate(double rate) { this.annualInterestRate = rate; }
public double getMonthlyInterestRate() {
return (annualInterestRate / 12) / 100;
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient balance.");
}
}
public void deposit(double amount) {
balance += amount;
}
}
// Client program
public class AccountTest {
public static void main(String[] args) {
Account acc = new Account(1222, 20000, 4.5);
acc.withdraw(2500);
acc.deposit(3000);
System.out.println("Balance: $" + acc.getBalance());
System.out.println("Monthly Interest: $" + (acc.getBalance() * acc.getMonthlyInterestRate()));
}
}
}