-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
48 lines (40 loc) · 1.28 KB
/
Account.java
File metadata and controls
48 lines (40 loc) · 1.28 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
package com.dj;
import java.util.ArrayList;
public class Account {
private String accountName;
private int balance = 0;
private ArrayList<Integer> transactions;
public Account(String accountName) {
this.accountName = accountName;
this.transactions = new ArrayList<Integer>();
}
public int getBalance() {
return balance;
}
public void deposit(int amount) {
if (amount > 0){
transactions.add(amount);
this.balance += amount;
System.out.println(amount + " deposited. Balance is now " + this.balance);
}else {
System.out.println("Can not deposit negative sums");
}
}
public void withdrawal(int amount){
int withdrawal = -amount;
if (withdrawal < 0){
this.transactions.add(withdrawal);
this.balance += withdrawal;
System.out.println(amount + " withdrawn. Balance is now " + this.balance);
}else {
System.out.println("Can not withdraw negative sums");
}
}
public void calculateBalance(){
this.balance = 0;
for (int i : this.transactions){
this.balance += i;
}
System.out.println("Calculated balance is " + this.balance);
}
}