-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.java
More file actions
38 lines (28 loc) · 710 Bytes
/
Bank.java
File metadata and controls
38 lines (28 loc) · 710 Bytes
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
package unsynch;
public class Bank {
private final double[] accounts;
public Bank(int n,double initialBalance){
accounts=new double[n];
for(int i=0;i<accounts.length;i++){
accounts[i]=initialBalance;
}
}
public void transfer(int from,int to,double amount){
if(accounts[from]<amount)
return;
System.out.print(Thread.currentThread());
accounts[from]-=amount;
System.out.printf("%10.2f from %d to %d",amount,from,to);
accounts[to]+=amount;
System.out.printf("Total Balance: %10.2f%n",getTotalBalance());
}
public double getTotalBalance(){
double sum=0;
for(double a:accounts)
sum+=a;
return sum;
}
public int size(){
return accounts.length;
}
}