Repository navigation
Expand file tree
/
Copy pathAccount.java
More file actions
130 lines (109 loc) · 2.92 KB
/
Copy pathAccount.java
File metadata and controls
130 lines (109 loc) · 2.92 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package polymorphism;
public class Account {
/**
* TODO: create attributes.
*
* This class has 3 attributes: number : String, private. holderId : String,
* private. balance: double, private.
*/
private String number;
private String holderId;
private double balance;
/**
* TODO: create constructor.
*
* This constructor receives 3 values and initializes the 3 attributes
* accordingly.
*/
public Account(String number, String holderId, double balance) {
this.number = number;
this.holderId = holderId;
this.balance = balance;
}
/**
* TODO: create getters and setters. These getters and setters expose the
* private attributes.
*
* Note: number should be retrieve-only, i.e. can not be set from outside.
*/
public String getNumber() {
return this.number;
}
public String getHolderId() {
return this.holderId;
}
public double getBalance() {
return this.balance;
}
public void setHolderId(String holderId) {
this.holderId = holderId;
}
public void setBalance(double balance) {
this.balance = balance;
}
/**
* TODO: create method display.
*
* This method prints out the number, holder id and balance of the account.
*/
public void display() {
System.out.printf("Account number %s, holder %s, balance %.1f\n", this.number, this.holderId, this.balance);
}
/**
* . TODO: implement method deposit().
*
* This method increases the account balance by the given amount.
*/
public void deposit(double amount) {
this.balance += amount;
}
/**
* TODO: implement method withdraw().
*
* This method decreases the account balance by the given amount. Balance must
* NOT be negative. It makes sure that there is enough balance to withdraw.
*
* Return true if the withdraw is successful and false otherwise.
*/
public boolean withdraw(double amount) {
if (this.balance < amount) {
return false;
}
this.balance -= amount;
return true;
}
/**
* TODO: implement method transferTo()
*
* This method transfers a given amount to another given account. It makes sure
* that this sender has enough balance before transferring.
*
* Return true if there is enough balance to transfer and false otherwise
*/
public boolean transferTo(double amount, Account toAccount) {
boolean isWithdrawOk = withdraw(amount);
if (!isWithdrawOk) {
return false;
}
toAccount.deposit(amount);
return true;
}
public void creditInterest() {
double interest = calculateInterest();
deposit(interest);
}
public double calculateInterest() {
return 0.0;
}
/**
* TODO: override method toString().
*
* This method returns the representation of the current object in the form of
* the combination of values of its attributes in a more readable format.
*
*/
@Override
public String toString() {
return "Account number " + number + ", holder " + holderId + ", balance " + balance;
}
}