-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
66 lines (54 loc) · 1.35 KB
/
Account.java
File metadata and controls
66 lines (54 loc) · 1.35 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
package ver2;
public class Account {
// Instance variable, only available inside this class.
private double balance;
//New comment
private String name;
public Account(String name, double balance) {
this.name = name;
this.balance = balance;
}
// A "getter" method that simply returns the balance.
public double getBalance() {
return balance;
}
public String getName() {
String msg = "name=" + name + ", balance=$" + balance;
return name;
}
public void setName(String name) {
this.name = name;
}
// A method that increases the balance by amount
public void deposit(double amount) {
if(amount>0) {
balance += amount;
}
}
public void withdraw(double amount) {
if(amount>0) {
balance -= amount;
}
}
public void mergeAccount(Account a) {
if(this.getName().equals(a.getName())) {
this.balance += a.getBalance();
}
}
@Override
public String toString() {
String msg = "balance=$" + balance;
return msg;
}
// Informal test code
public static void main(String[] args) {
Account a1 = new Account(1000.0);
System.out.println("Balance=$" + a1.getBalance());
a1.deposit(500.0);
System.out.println("Balance=$" + a1.getBalance());
a1.withdraw(200.0);
System.out.println("Balance=$" + a1.getBalance());
System.out.println(a1.toString());
System.out.println(a1);
}
}