Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package BankServices;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- public class Bank {
- private String n;
- private int num = 1;
- private double v = 0;
- private double prelievo;
- private double soldi;
- Account a;
- List<Double> prelievi = new LinkedList<>();
- List<Double> s = new LinkedList<>();
- HashMap<Integer, Account> accounts = new HashMap<>();
- public Bank(String n) {
- this.n = n;
- }
- public String getName() {
- return n;
- }
- public int createAccount(String name, int date, double initial) {
- int numd = num++;
- a = new Account(numd, name, date, initial);
- // int numd = num++;
- accounts.put(numd, a);
- return numd;
- }
- public Account deleteAccount(int code, int date) throws InvalidCode {
- if (!accounts.containsKey(code)) {
- throw new InvalidCode();
- }
- // s.add(accounts.get(code).getValue());
- s.add(accounts.get(code).getBalance());
- if (date < accounts.get(code).getDate()) {
- date = accounts.get(code).getDate();
- }
- accounts.get(code).setBalance(0);
- return accounts.remove(code);
- }
- public Account getAccount(int code) throws InvalidCode {
- if (!accounts.containsKey(code)) {
- throw new InvalidCode();
- }
- return accounts.get(code);
- }
- public void deposit(int code, int date, double value) throws InvalidCode {
- if (!accounts.containsKey(code)) {
- throw new InvalidCode();
- } else {
- // double v = accounts.get(code).getInitial() + value;
- // a.addValue(v);
- // accounts.get(code).addValue(v);
- // accounts.get(code).addToBalance(value);
- if (date < accounts.get(code).getDate()) {
- date = accounts.get(code).getDate();
- accounts.get(code).addToBalance(date, value);
- }
- }
- }
- public void withdraw(int code, int date, double value) throws InvalidCode,
- InvalidValue {
- if (!accounts.containsKey(code)) {
- throw new InvalidCode();
- }
- // if (value < accounts.get(code).getValue())
- if (value > accounts.get(code).getBalance()) {
- throw new InvalidValue();
- } else {
- // prelievi.add(accounts.get(code).getValue() - value);
- // prelievi.add(accounts.get(code).getBalance() - value);
- // double t = accounts.get(code).getBalance() - value;
- // accounts.get(code).subtractFromBalance(value);
- if (date < accounts.get(code).getDate()) {
- date = accounts.get(code).getDate();
- accounts.get(code).subtractFromBalance(date, value);
- }
- }
- }
- public void transfer(int fromCode, int toCode, int date, double value)
- throws InvalidCode, InvalidValue {
- if (!accounts.containsKey(fromCode) || !accounts.containsKey(toCode)) {
- throw new InvalidCode();
- }
- // double s = accounts.get(fromCode).getValue();
- // double s = accounts.get(fromCode).getBalance();
- // if (s > accounts.get(toCode).getValue()) {
- // if (value > s) {
- if (value > accounts.get(fromCode).getBalance()) {
- throw new InvalidValue();
- } else {
- // double t = accounts.get(toCode).getValue();
- // double t = accounts.get(toCode).getBalance();
- // double t;
- // t = s- value;
- accounts.get(fromCode).subtractFromBalance(date, value);
- // accounts.get(toCode).addToBalance( value);
- accounts.get(toCode).addToBalance(date, value);
- // t += s;
- }
- }
- public double getTotalDeposit() {
- double sum = 0;
- Collection<Account> at = accounts.values();
- Iterator<Account> it = at.iterator();
- while (it.hasNext()) {
- Account a = it.next();
- // sum += a.getValue();
- sum += a.getBalance();
- }
- return sum;
- }
- public List<Account> getAccounts() {
- List<Account> p = new LinkedList<Account>(accounts.values());
- return p;
- }
- public List<Account> getAccountsByBalance(double low, double high) {
- List<Account> sal = new LinkedList<Account>();
- Collection<Account> at = accounts.values();
- Iterator<Account> it = at.iterator();
- while (it.hasNext()) {
- Account a = it.next();
- // if (a.getValue() > low && a.getValue() < high)
- if (a.getBalance() > low && a.getBalance() < high) {
- sal.add(a);
- }
- }
- return sal;
- }
- public double getPerCentHigher(double min) {
- double per;
- double c = 0;
- Collection<Account> at = accounts.values();
- Iterator<Account> it = at.iterator();
- while (it.hasNext()) {
- Account a = it.next();
- // if (a.getValue() > min)
- if (a.getBalance() > min) {
- c++;
- }
- }
- per = c / at.size() * 100;
- return per;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement