Skip to content

Commit

Permalink
refactor: Argent.montant is NOT publicly readable
Browse files Browse the repository at this point in the history
feat: Argent equality does not take minor units into account
  • Loading branch information
hei-teacher committed Nov 3, 2024
1 parent 5231d90 commit daefbc8
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 43 deletions.
54 changes: 50 additions & 4 deletions src/main/java/school/hei/patrimoine/modele/Argent.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
package school.hei.patrimoine.modele;

import static java.lang.Math.abs;
import static school.hei.patrimoine.modele.Devise.EUR;
import static school.hei.patrimoine.modele.Devise.MGA;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import lombok.experimental.Accessors;

public record Argent(int montant, Devise devise) implements Serializable {
@ToString
@AllArgsConstructor
public class Argent implements Serializable {
private final double montant;

public static Argent euro(int montant) {
@Accessors(fluent = true)
@Getter
private final Devise devise;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Argent argent = (Argent) o;
return Objects.equals(devise, argent.devise)
&&
// we are only interested in major unit equality
// eg: cents in EUR does NOT interest us
abs(montant - argent.montant) < 1;
}

@Override
public int hashCode() {
return Objects.hash(montant, devise);
}

public static Argent euro(double montant) {
return new Argent(montant, EUR);
}

Expand All @@ -18,11 +48,11 @@ public static Argent ariary(int montant) {

public Argent convertir(Devise autreDevise, LocalDate t) {
return new Argent(
(int) ((montant * devise.valeurEnAriary(t)) / autreDevise.valeurEnAriary(t)), autreDevise);
(montant * devise.valeurEnAriary(t)) / autreDevise.valeurEnAriary(t), autreDevise);
}

public Argent mult(double d) {
return new Argent((int) (montant * d), devise);
return new Argent(montant * d, devise);
}

public Argent minus(Argent that, LocalDate t) {
Expand All @@ -32,4 +62,20 @@ public Argent minus(Argent that, LocalDate t) {
public Argent add(Argent that, LocalDate t) {
return new Argent(montant + that.convertir(devise, t).montant, devise);
}

public boolean gt(double n) {
return montant > n;
}

public boolean lt(double n) {
return montant < n;
}

public boolean le(double n) {
return montant <= n;
}

public String ppMontant() {
return montant + "";
}
}
2 changes: 1 addition & 1 deletion src/main/java/school/hei/patrimoine/modele/Devise.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public record Devise(
public static final Devise EUR =
new Devise("EURO", "€", LocalDate.of(2024, JULY, 3), 4_821, -0.1);
public static final Devise CAD =
new Devise("CAD", "CAD", LocalDate.of(2024, JULY, 8), 3286, -0.1);
new Devise("CAD", "CAD", LocalDate.of(2024, JULY, 8), 3_286, -0.1);

public double valeurEnAriary(LocalDate now) {
var joursEcoules = DAYS.between(t, now);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ private static void fluxDuJour(LocalDate date, Compte compte, HashSet<FluxJourna

public Set<FluxJournalier> fluxJournaliersImpossibles() {
return fluxJournaliers.stream()
.filter(
fj -> !(fj.compte() instanceof Dette) && fj.compte().valeurComptable().montant() < 0)
.filter(fj -> !(fj.compte() instanceof Dette) && fj.compte().valeurComptable().lt(0))
.collect(toSet());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package school.hei.patrimoine.modele.evolution;

import static java.util.stream.Collectors.joining;
import static school.hei.patrimoine.modele.evolution.SerieComptableTemporelle.parseMontant;

import java.time.LocalDate;
import java.util.Set;
Expand All @@ -14,7 +15,7 @@ public String toString() {
"[%s][%s=%d%s] %s",
date,
compte.getNom(),
compte.valeurComptable().montant(),
parseMontant(compte.valeurComptable()),
compte.valeurComptable().devise().symbole(),
toFluxJournalierString(flux));
}
Expand All @@ -24,6 +25,6 @@ private String toFluxJournalierString(Set<FluxArgent> flux) {
}

private String toFluxJournalierString(FluxArgent flux) {
return String.format("(%s, %d)", flux.getNom(), flux.getFluxMensuel().montant());
return String.format("(%s, %d)", flux.getNom(), parseMontant((flux.getFluxMensuel())));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package school.hei.patrimoine.modele.evolution;

import static java.lang.Double.parseDouble;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -8,6 +10,7 @@
import java.util.Map;
import java.util.function.Predicate;
import lombok.AllArgsConstructor;
import school.hei.patrimoine.modele.Argent;
import school.hei.patrimoine.modele.Devise;
import school.hei.patrimoine.modele.possession.FluxArgent;
import school.hei.patrimoine.modele.possession.Possession;
Expand Down Expand Up @@ -38,12 +41,12 @@ public Map<Possession, List<Integer>> serieValeursComptablesParPossession() {
.forEach(
d ->
serie.add(
ep.getEvolutionJournaliere()
.get(d)
.possessionParNom(possession.getNom())
.valeurComptable()
.convertir(devise, d)
.montant()));
parseMontant(
ep.getEvolutionJournaliere()
.get(d)
.possessionParNom(possession.getNom())
.valeurComptable()
.convertir(devise, d))));
map.put(possession, serie);
}
return map;
Expand Down Expand Up @@ -82,11 +85,18 @@ public List<Integer> serieValeursComptablesPatrimoine() {
.forEach(
d ->
serie.add(
ep.getEvolutionJournaliere()
.get(d)
.getValeurComptable()
.convertir(devise, d)
.montant()));
parseMontant(
ep.getEvolutionJournaliere()
.get(d)
.getValeurComptable()
.convertir(devise, d))));
return serie;
}

public static int parseMontant(Argent a) {
// Argent::montant is PURPOSEFULLY private so that people do NOT manipulate it directly.
// Indeed, operations such as those on Devise can only be handled correctly internally.
// ppMontant explicitely indicates that it should only be used for printing purpose.
return (int) parseDouble(a.ppMontant());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public String prettyPrint() {
objectivable.nom(),
objectif.t(),
aAtteindre.devise().symbole(),
aAtteindre.montant(),
objectivable.valeurAObjectifT(objectif.t()).montant());
aAtteindre.ppMontant(),
objectivable.valeurAObjectifT(objectif.t()).ppMontant());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public final class Creance extends Compte {

public Creance(String nom, LocalDate t, Argent valeurComptable) {
super(nom, t, valeurComptable);
if (valeurComptable.montant() < 0) {
if (valeurComptable.lt(0)) {
throw new IllegalArgumentException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public final class Dette extends Compte {

public Dette(String nom, LocalDate t, Argent valeurComptable) {
super(nom, t, valeurComptable);
if (valeurComptable.montant() > 0) {
if (valeurComptable.gt(0)) {
throw new IllegalArgumentException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ public GroupePossession(String nom, Devise devise, LocalDate t, Set<Possession>
super(
nom,
t,
new Argent(
possessions.stream()
.mapToInt(p -> p.getValeurComptable().convertir(devise, t).montant())
.sum(),
devise));
possessions.stream()
.map(Possession::getValeurComptable)
.reduce(new Argent(0, devise), (a1, a2) -> a1.add(a2, t)));
this.possessions = possessions;
typeAgregat(possessions); // sanity check: fails if set is inconsistent
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package school.hei.patrimoine.modele.possession;

import static java.lang.Math.max;
import static java.time.temporal.ChronoUnit.DAYS;
import static school.hei.patrimoine.modele.possession.TypeAgregat.IMMOBILISATION;

Expand Down Expand Up @@ -33,15 +32,13 @@ public Possession projectionFuture(LocalDate tFutur) {
tauxDAppreciationAnnuelle);
}
var joursEcoules = DAYS.between(t, tFutur);
double valeurAjouteeJournaliere =
valeurComptable.montant() * (tauxDAppreciationAnnuelle / 365.);
int valeurComptableFuture =
max(0, (int) (valeurComptable.montant() + valeurAjouteeJournaliere * joursEcoules));
var valeurAjouteeJournaliere = valeurComptable.mult((tauxDAppreciationAnnuelle / 365.));
var valeurFutureUnbound = valeurComptable.add(valeurAjouteeJournaliere.mult(joursEcoules), t);
return new Materiel(
nom,
dateAcquisition,
tFutur,
new Argent(valeurComptableFuture, valeurComptable.devise()),
valeurFutureUnbound.lt(0) ? new Argent(0, devise()) : valeurFutureUnbound,
tauxDAppreciationAnnuelle);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private Patrimoine patrimoineDeZetyLe26Octobre2025() {
var projeté = patrimoineDeZetyAu3Jul.projectionFuture(LocalDate.of(2024, SEPTEMBER, 17));

assertEquals(ariary(3_600_000), patrimoineDeZetyAu3Jul.getValeurComptable());
assertEquals(ariary(2_978_848), projeté.getValeurComptable());
assertEquals(ariary(2_978_849), projeté.getValeurComptable());
}

@Test
Expand All @@ -58,12 +58,13 @@ void zety_s_endette() {
var patrimoineDeZetySendette = patrimoineDeZetySendette();

var differenceEntreLesDeuxPatrimoines =
patrimoineDu17Septembre.getValeurComptable().montant()
- patrimoineDeZetySendette.getValeurComptable().montant();
patrimoineDu17Septembre
.getValeurComptable()
.minus(patrimoineDeZetySendette.getValeurComptable(), LocalDate.MIN);

assertEquals(ariary(2_978_848), patrimoineDu17Septembre.getValeurComptable());
assertEquals(ariary(1_976_464), patrimoineDeZetySendette.getValeurComptable());
assertEquals(1_002_384, differenceEntreLesDeuxPatrimoines);
assertEquals(ariary(2_978_849), patrimoineDu17Septembre.getValeurComptable());
assertEquals(ariary(1_976_465), patrimoineDeZetySendette.getValeurComptable());
assertEquals(ariary(1_002_384), differenceEntreLesDeuxPatrimoines);
}

@Test
Expand All @@ -79,7 +80,7 @@ void zety_s_endette() {
LocalDate tFutur = dayOfFailureFrom18September.plusDays(i);
var argentEnEspècesProjeté = argentEnEspècesDeZetyEn20242025.projectionFuture(tFutur);
log.debug("à t={} montant = {}", argentEnEspècesProjeté.valeurComptable(), tFutur);
if (argentEnEspècesProjeté.valeurComptable().montant() <= 0) {
if (argentEnEspècesProjeté.valeurComptable().le(0)) {
dayOfFailureFrom18September = tFutur;
break;
}
Expand All @@ -94,7 +95,7 @@ void zety_s_endette() {
var patrimoineDeZetyLe14Fevrier2025 =
patrimoineDeZetyLe14Fev2025().projectionFuture(AU_14_FEVRIER_2025);

assertEquals(ariary(-1_528_686), patrimoineDeZetyLe14Fevrier2025.getValeurComptable());
assertEquals(ariary(-1_528_684), patrimoineDeZetyLe14Fevrier2025.getValeurComptable());
}

@Test
Expand Down

0 comments on commit daefbc8

Please sign in to comment.