Skip to content
Snippets Groups Projects
Select Git revision
  • bd66ebd799fdb46167024c8bf1c0dc37aef9d3fb
  • master default protected
2 results

AccountDatabase.java

Blame
  • AccountDatabase.java 1.11 KiB
    package ch.hepia.account;
    
    import java.util.*;
    
    import ch.hepia.account.Account;
    
    public class AccountDatabase {
        
        List<Account> database;
    
        public AccountDatabase(){
            this.database = new ArrayList<>();
        }
    
        public void addAccount(Account a){
            database.add(a);
        }
    
        public Optional<Account> getById(int idAccount){
            return this.database.stream().filter(d -> d.id() == idAccount).findFirst();
        }
    
        public boolean accountPresent(int idAccount){
            return this.getById(idAccount).isPresent();
        }
    
        public int nextID(){
            return this.database.size()+1;
        }
    
        public void creditAnAccount(int idAccount, double amount){
            this.database.get(this.database.indexOf(getById(idAccount).get())).addAmount(amount);
        }
    
        public boolean hasEnoughMoney(int idAccount, double price){
            return getById(idAccount).get().credit() >= price;
        }
    
        public void withdrawCreditAccount(int idAccount, double credit){
            System.out.println("ID:" + idAccount);
            this.database.get(this.database.indexOf(getById(idAccount).get())).withdraw(credit);
        }
    }