Selecting correct carton based on qty I'm attempting to create a function that will select the correct carton based on the qty of the carton content.

Here are my cartons that have the number of items they can hold:


SMALL_PASCAL = 300
BIG_PASCAL = 600
BABY_BOX = 1200
A485_1201 = 1800
A4140_1901 = 3000
A485 = 5000



And here is the method that will return the CartonType:

/// &lt;summary&gt;
/// Get Carton Type
/// &lt;/summary&gt;
/// &lt;param name="qty"&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
[Test]
private static CartonType GetCartonType(int qty)
{
     if (qty &lt;= 300)
     {
         return CartonType.SMALL_PASCAL;
     }
     else if (qty &gt; 300 &amp;&amp; qty &lt;= 600)
     {
         return CartonType.SMALL_PASCAL;
     }
     else if (qty &gt; 600 &amp;&amp; qty &lt;= 1200)
     {
         return CartonType.BABY_BOX;
     }
     else if (qty &gt; 1200 &amp;&amp; qty &lt;= 1800)
     {
         return CartonType.A485_1201;
     }
     else if (qty &gt;1800 &amp;&amp; qty &lt;=3000)
     {
         return CartonType.A4140_1901;
     }
     else // 5000 or more.
     {
         return CartonType.A485;
     }
}


Calling the method like so:

int qty = 1540;
Console.WriteLine(GetCartonType(qty));


Output:

A485_1201


Is there a better way to achieve this rather than an if statement? Also, I just had a thought that what if the qty is like 10,000? I would then require 2 A485.
Is there a better way to achieve this rather than an If statement?


Yes. You could have an enumeration of the container types and their capacities, in increasing order by capacity,
loop over in order,
and as soon as you find one that's big enough, return it.

It's perhaps easier to see after you simplify the if-else chain,
by removing redundant conditions, for example:

 if (qty &lt;= 300)
 {
     return CartonType.SMALL_PASCAL;
 }
 if (qty &lt;= 600)
 {
     return CartonType.SMALL_PASCAL;
 }
 if (qty &lt;= 1200)
 {
     return CartonType.BABY_BOX;
 }
 // ...