1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
public class Ligne
{
public int Id { get; private set; }
public double Montant { get; private set; }
public Ligne(int id, double value)
{
this.Id = id;
this.Montant = value;
}
}
public class Cumul
{
private SortedDictionary<int, Ligne> _dicLignes;
private readonly int _idEcr;
public IEnumerable<Ligne> Lignes
{
get { return this._dicLignes.Values; }
}
public int IdEcr
{
get { return this._idEcr; }
}
public double Montant
{
get
{
return this._dicLignes.Values.Sum(l => l.Montant);
}
}
public Cumul(int idEcr)
{
this._idEcr = idEcr;
_dicLignes = new SortedDictionary<int, Ligne>();
}
public void Add(Ligne ligne)
{
if (this._dicLignes.ContainsKey(ligne.Id))
throw new Exception("Ligne déjà présente");
_dicLignes.Add(ligne.Id, ligne);
}
}
public class Ecritures
{
SortedDictionary<int, Ligne> _sdicLignes = new SortedDictionary<int, Ligne>();
public double Montant
{
get
{
return this._sdicLignes.Values.Sum(l => l.Montant);
}
}
public void Add(Ligne ligne)
{
if (this._sdicLignes.ContainsKey(ligne.Id))
throw new Exception("Déja présente");
this._sdicLignes.Add(ligne.Id, ligne);
}
public List<Cumul> GenererCumul()
{
List<Cumul> _lstCumul = new List<Cumul>();
int iCumul = 1;
Cumul cumulCurrent = null;
foreach (Ligne l in this._sdicLignes.Values)
{
if (cumulCurrent == null || cumulCurrent.Montant == 0)
{
//soit c'est le premier soit on doit changer de cumul
cumulCurrent = new Cumul(iCumul);
_lstCumul.Add(cumulCurrent);
iCumul++;
}
cumulCurrent.Add(l);
}
return _lstCumul;
}
}
class Program
{
static void Main(string[] args)
{
Dictionary<int, double> _dico = new Dictionary<int, double>()
{
{20384 , 2392178.69 },
{20386 , -2392178.69 },
{20388 , 9770.89},
{20390 , 765.98},
{20392 , 1188.2},
{20394 , -11725.07},
{20396 , 8922.17},
{20398 , 765.98},
{20400 , 1018.45},
{20402 , -10706.6},
{20404 , 8922.17},
{20406 , 765.98},
{20408 , 1018.45},
{20410 , -10706.6}
};
//On affiche
foreach (KeyValuePair<int, double> kvp in _dico)
{
Console.WriteLine(kvp.Key + " -> " + kvp.Value);
}
//Pause
Console.ReadKey();
//On construit nos objects.
Ecritures ecritures = new Ecritures();
foreach (KeyValuePair<int, double> kvp in _dico)
{
Ligne l = new Ligne(kvp.Key, kvp.Value);
ecritures.Add(l);
}
//Toutes les lignes sont dans Ecritures.
//je vais générer les cumuls
var lstCumul = ecritures.GenererCumul();
foreach (Cumul c in lstCumul.OrderBy(c => c.IdEcr))
{
foreach (Ligne l in c.Lignes)
{
Console.WriteLine(l.Id + " -> " + l.Montant + " -> " + c.IdEcr);
}
}
Console.ReadKey();
}
} |
Partager