0
I have the total of a purchase list and need to divide this total by the amount of emails
which I have on my second list and return a dictionary. However, not all division is exact, so I need to add the rest of divisions not exact to the last Value
dictionary.
Is there any method to take the last value and make the change?
using System;
using System.Collections.Generic;
namespace listas {
class Program {
static void Main(string[] args) {
List<Itens> itens = new List<Itens>();
itens.Add(new Itens() {
Item = "Pendrive", Quantidade = 2, ValorUND = 20
});
itens.Add(new Itens() {
Item = "Fones de ouvido", Quantidade = 2, ValorUND = 20
});
itens.Add(new Itens() {
Item = "SSD 10GB", Quantidade = 1, ValorUND = 20
});
List<string> emails = new List<string>();
emails.Add("[email protected]");
emails.Add("[email protected]");
emails.Add("[email protected]");
Calcula(itens, emails);
}
static Dictionary<string,int> Calcula(List<Itens> lista1, List<string> lista2)
{
int total = 0;
int resto;
Dictionary<string, int> dict = new Dictionary<string, int>();
for (int i = 0; i < lista1.Count; i++) {
total += (lista1[i].Quantidade * lista1[i].ValorUND);
}
total = total / lista2.Count;
resto = total % lista2.Count;
lista2.ForEach((string email) => {
dict.Add(email, total);
});
if (resto != 0) {
}
foreach(KeyValuePair < string, int > d in dict) {
Console.WriteLine($"{d.Key}: {d.Value.ToString("
C ")}");
}
return dict;
}
}
}
There’s no way to know the last value of a dictionary, it’s a cluttered collection. Source: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2 "For purposes of enumeration, each item in the Dictionary is treated as a Keyvaluepair<Tkey,Tvalue> Structure Representing a value and its key. The order in which the items are returned is Undefined."
– Natan Fernandes