添加corejava第11版源码
日期:2019-5-22
This commit is contained in:
1 parent
e199db7c36
commit
af9d779c86
525 files changed
+91425
No files matched your search
@@ -0,0 +1,61 @@
|
||||
package threads;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* A bank with a number of bank accounts.
|
||||
*/
|
||||
public class Bank
|
||||
{
|
||||
private final double[] accounts;
|
||||
|
||||
/**
|
||||
* Constructs the bank.
|
||||
* @param n the number of accounts
|
||||
* @param initialBalance the initial balance for each account
|
||||
*/
|
||||
public Bank(int n, double initialBalance)
|
||||
{
|
||||
accounts = new double[n];
|
||||
Arrays.fill(accounts, initialBalance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfers money from one account to another.
|
||||
* @param from the account to transfer from
|
||||
* @param to the account to transfer to
|
||||
* @param amount the amount to transfer
|
||||
*/
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the sum of all account balances.
|
||||
* @return the total balance
|
||||
*/
|
||||
public double getTotalBalance()
|
||||
{
|
||||
double sum = 0;
|
||||
|
||||
for (double a : accounts)
|
||||
sum += a;
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of accounts in the bank.
|
||||
* @return the number of accounts
|
||||
*/
|
||||
public int size()
|
||||
{
|
||||
return accounts.length;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user