Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- kage BankServices;
- import java.util.Collection;
- 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) {
- a = new Account(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());
- if (date < accounts.get(code).getDate()) {
- date = accounts.get(code).getDate();
- }
- 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);
- if (date < accounts.get(code).getDate()) {
- date = accounts.get(code).getDate();
- }
- }
- }
- 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()) {
- throw new InvalidValue();
- } else {
- prelievi.add(accounts.get(code).getValue() - value);
- if (date < accounts.get(code).getDate()) {
- date = accounts.get(code).getDate();
- }
- }
- }
- 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();
- //if (s > accounts.get(toCode).getValue()) {
- if (value > s){
- throw new InvalidValue();
- } else {
- double t = accounts.get(toCode).getValue();
- 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();
- }
- 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) {
- 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) {
- c++;
- }
- }
- per = c / at.size() * 100;
- return per;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement