73 lines
2 KiB
Java
73 lines
2 KiB
Java
package banque;
|
|
|
|
import java.util.*;
|
|
|
|
public abstract class Compte {
|
|
private int code;
|
|
private double solde;
|
|
private List<Operation> listeDesOperations;
|
|
|
|
// Constructeur
|
|
Compte(int code) {
|
|
// Le solde est initialement à zéro
|
|
|
|
this.code = code;
|
|
this.solde = 0;
|
|
this.listeDesOperations = new ArrayList<Operation>();
|
|
}
|
|
|
|
// Getters et Setters
|
|
public double getSolde() {
|
|
return this.solde;
|
|
}
|
|
public void setSolde(double nouveauSolde) {
|
|
this.solde = nouveauSolde;
|
|
}
|
|
|
|
// Méthodes de la classe
|
|
public void verser(double montant) {
|
|
Versement versement = new Versement(montant);
|
|
this.ajouterOperation(versement);
|
|
this.solde += montant;
|
|
}
|
|
public void retirer(double montant) {
|
|
// Redéfini dans les classes filles
|
|
}
|
|
public void virement(double montant, Compte compte) {
|
|
// Retirer le montant de ce compte
|
|
this.retirer(montant);
|
|
|
|
// Verser ce montant sur le compte distant
|
|
compte.verser(montant);
|
|
}
|
|
public double consulterSolde() {
|
|
return this.solde;
|
|
}
|
|
public abstract void updateSolde();
|
|
public List<Operation> listeOperations() {
|
|
return this.listeDesOperations;
|
|
}
|
|
public void ajouterOperation(Operation operation) {
|
|
int dernierIndex = this.listeDesOperations.size();
|
|
operation.setNumero(dernierIndex);
|
|
this.listeDesOperations.add(operation);
|
|
}
|
|
public double totalVersements() {
|
|
double total = 0;
|
|
for (Operation versement: this.listeDesOperations) {
|
|
if (Versement.class.isInstance(versement)) {
|
|
total += versement.getMontant();
|
|
}
|
|
}
|
|
return total;
|
|
}
|
|
public double totalRetraits() {
|
|
double total = 0;
|
|
for (Operation retrait: this.listeDesOperations) {
|
|
if (Retrait.class.isInstance(retrait)) {
|
|
total += retrait.getMontant();
|
|
}
|
|
}
|
|
return total;
|
|
}
|
|
}
|