-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
83 lines (65 loc) · 1.98 KB
/
Account.java
File metadata and controls
83 lines (65 loc) · 1.98 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.Date;
class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private Date dateCreated;
public Account() {
dateCreated = new Date();
}
public Account(int id, double balance) {
this();
this.id = id;
this.balance = balance;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public double getMonthlyInterestRate() {
return annualInterestRate / 12;
}
public void withdraw(double amount) {
if(amount > balance) {
System.out.println("Insufficient balance!");
return;
}
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
}
class TestAccount {
public static void main(String[] args) {
Account account = new Account(1234, 100);
account.setAnnualInterestRate(6.7);
// Try to withdraw more than the account balance
account.withdraw(150);
// Withdraw $50 from the account
account.withdraw(50);
// Deposit $1000 in the account
account.deposit(1000);
// Print the balance, annual interest rate, and the date that the account was created
System.out.println("Balance: " + account.getBalance());
System.out.println("Annual Interest Rate: " + account.getAnnualInterestRate() + "%");
System.out.println("Date Created: " + account.getDateCreated());
}
}