So im writing a small little c# console app, which is supposed to imitate a shopping system of sorts. All the products are sold in Packs (not individually), and the Pack sizes are like 4 items, 10 items or 15 items.
Given a Qty size of like 28, it should return a result of something like: 2 x 10 Packs + 2 x 4 Packs. Or given a Qty size of like 25, it should return a result of something like: 1 x 15 Packs + 1 x 10 Packs And the function needs to be efficient, so it returns the least amount of packs. (obviously ignore scenerio's where it might be an Qty of 3 - as there are no pack sizes that small)
this is my code at the moment:
qty = 28, LargePack = 15, MediumPack = 10, SmallPack = 4
double total = 0.00;
int tmpQty = qty;
while (tmpQty != 0)
{
if ((tmpQty >= LargePack) && ((tmpQty % LargePack) % 1) == 0)
{
tmpQty -= LargePack;
lg += 1;
total =+ (lg * LargePrice);
}
else if ((tmpQty >= MediumPack))
{
tmpQty -= MediumPack;
md += 1;
total =+ (md * MediumPrice);
}
else if ((SmallPack !=0) && (tmpQty >= SmallPack) && (tmpQty < MediumPack))
{
tmpQty -= SmallPack;
sm += 1;
total =+ (sm * SmallPrice);
}
}
My idea here - because with a qty of like 28, i need it to skip the first IF statement, i thought i'd do a check of whether tmpQty (28) / LargePack (15) was an integer number - which it shouldn't be, then it would go to the 2nd IF statement. But for some reason the formula:
(tmpQty % LargePack) %1 == 0
always equals 0 - so its True... when it should be false.
Or if anyone has a better way to work out Pack sizes, im open to suggestions. Note - Pack sizes aren't defined here, as various different products use the same sorting process.