I have a list of transaction history with the corresponding amount.
public class Invoice
{
public Invoice(string TransactionReference, double Amount)
{
this.TransactionReference = TransactionReference;
this.Amount = Amount;
}
public string TransactionReference { get; set; }
public double Amount { get; set; }
}
List<Invoice> items = new List<Invoice>() {
new Invoice("808861", -45.85),
new Invoice("822634", -144.32),
new Invoice("822635", -56.63),
new Invoice("835308", 2970.55),
new Invoice("835309", 869.36),
new Invoice("835310", 3050.73),
new Invoice("835311", 657.74),
new Invoice("835312", 168.42),
new Invoice("835348", 2922.69),
new Invoice("835349", 324.5),
new Invoice("835350", 906.3),
new Invoice("835351", 420.21),
new Invoice("835352", 851.7),
new Invoice("838880", 158.43),
new Invoice("838881", 2062.46),
new Invoice("838882", 567.26),
new Invoice("838883", 40.96),
new Invoice("838884", 207.16),
new Invoice("838889", 2726.13),
new Invoice("838890", 29.25),
new Invoice("838891", 1458.28),
new Invoice("838892", 219.58),
new Invoice("839930", 2791.99),
new Invoice("839931", 455.47),
new Invoice("839932", 514.94),
new Invoice("839934", 666.78),
new Invoice("840758", -341.34),
new Invoice("855741", -113.55),
new Invoice("855973", -85.46),
new Invoice("866848", -39.53),
new Invoice("877471", -58.17),
new Invoice("877472", -58.17),
new Invoice("878790", -459.53),
new Invoice("892869", -6353.36)
};
I want to divide the transaction history into many small groups with the following conditions:
- Maximum for each group will contain 26 transaction history.
- The total amount in each group must be greater than 0.
- The last group contains the remaining histories as long as the condition is greater than 0.
Below I write indiscriminately to calculate the trial total of the transaction, but the final group is still less than 0.
private static void ShowSumOf26Invoice( List<Invoice> list, int from, int to )
{
if (list.Count > 26)
{
Console.WriteLine("Total amount from: " + from + " to " + to + " : " + items.GetRange(0,26).Sum(x => x.Amount));
items.RemoveRange(from - 1, 26);
from = from - 1 + 26;
to = to + 26;
ShowSumOf26Invoice(items, from, to);
}
else
{
Console.WriteLine("Total amount from: " + from + " to " + (to - 26 + items.Count) + " : " + items.Sum(x => x.Amount));
}
}
static void Main(string[] args)
{
Console.WriteLine( "Total amount: " + items.Sum( x => x.Amount ) );
int from = 1;
int to = 26;
ShowSumOf26Invoice( items, from, to );
Console.WriteLine("Press any key to end the program!");
Console.ReadKey();
}
This is a model of Invoice.
public class Invoice
{
public Invoice(string TransactionReference, double Amount)
{
this.TransactionReference = TransactionReference;
this.Amount = Amount;
}
public string TransactionReference { get; set; }
public double Amount { get; set; }
}
Could you please give me some solutions for this problem? Thank you very much!