Selecting correct carton based on qty











up vote
1
down vote

favorite












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:



/// <summary>
/// Get Carton Type
/// </summary>
/// <param name="qty"></param>
/// <returns></returns>
[Test]
private static CartonType GetCartonType(int qty)
{
if (qty <= 300)
{
return CartonType.SMALL_PASCAL;
}
else if (qty > 300 && qty <= 600)
{
return CartonType.SMALL_PASCAL;
}
else if (qty > 600 && qty <= 1200)
{
return CartonType.BABY_BOX;
}
else if (qty > 1200 && qty <= 1800)
{
return CartonType.A485_1201;
}
else if (qty >1800 && qty <=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.










share|improve this question




























    up vote
    1
    down vote

    favorite












    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:



    /// <summary>
    /// Get Carton Type
    /// </summary>
    /// <param name="qty"></param>
    /// <returns></returns>
    [Test]
    private static CartonType GetCartonType(int qty)
    {
    if (qty <= 300)
    {
    return CartonType.SMALL_PASCAL;
    }
    else if (qty > 300 && qty <= 600)
    {
    return CartonType.SMALL_PASCAL;
    }
    else if (qty > 600 && qty <= 1200)
    {
    return CartonType.BABY_BOX;
    }
    else if (qty > 1200 && qty <= 1800)
    {
    return CartonType.A485_1201;
    }
    else if (qty >1800 && qty <=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.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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:



      /// <summary>
      /// Get Carton Type
      /// </summary>
      /// <param name="qty"></param>
      /// <returns></returns>
      [Test]
      private static CartonType GetCartonType(int qty)
      {
      if (qty <= 300)
      {
      return CartonType.SMALL_PASCAL;
      }
      else if (qty > 300 && qty <= 600)
      {
      return CartonType.SMALL_PASCAL;
      }
      else if (qty > 600 && qty <= 1200)
      {
      return CartonType.BABY_BOX;
      }
      else if (qty > 1200 && qty <= 1800)
      {
      return CartonType.A485_1201;
      }
      else if (qty >1800 && qty <=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.










      share|improve this question















      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:



      /// <summary>
      /// Get Carton Type
      /// </summary>
      /// <param name="qty"></param>
      /// <returns></returns>
      [Test]
      private static CartonType GetCartonType(int qty)
      {
      if (qty <= 300)
      {
      return CartonType.SMALL_PASCAL;
      }
      else if (qty > 300 && qty <= 600)
      {
      return CartonType.SMALL_PASCAL;
      }
      else if (qty > 600 && qty <= 1200)
      {
      return CartonType.BABY_BOX;
      }
      else if (qty > 1200 && qty <= 1800)
      {
      return CartonType.A485_1201;
      }
      else if (qty >1800 && qty <=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.







      c#






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 13 mins ago









      Heslacher

      44.8k460155




      44.8k460155










      asked 5 hours ago









      user1234433222

      19510




      19510






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted











          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 <= 300)
          {
          return CartonType.SMALL_PASCAL;
          }
          if (qty <= 600)
          {
          return CartonType.SMALL_PASCAL;
          }
          if (qty <= 1200)
          {
          return CartonType.BABY_BOX;
          }
          // ...





          share|improve this answer





















          • do you have any suggestion if there is for anything over 5000? so eg the largest box and a small pascal could be required?
            – user1234433222
            5 hours ago






          • 2




            @user1234433222 sure, but we don't implement feature requests here... You could subtract from the quantity the capacity of the selected container, and then call the method again.
            – janos
            5 hours ago










          • Thanks for that, working backwards might be the key to this.
            – user1234433222
            5 hours ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209788%2fselecting-correct-carton-based-on-qty%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted











          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 <= 300)
          {
          return CartonType.SMALL_PASCAL;
          }
          if (qty <= 600)
          {
          return CartonType.SMALL_PASCAL;
          }
          if (qty <= 1200)
          {
          return CartonType.BABY_BOX;
          }
          // ...





          share|improve this answer





















          • do you have any suggestion if there is for anything over 5000? so eg the largest box and a small pascal could be required?
            – user1234433222
            5 hours ago






          • 2




            @user1234433222 sure, but we don't implement feature requests here... You could subtract from the quantity the capacity of the selected container, and then call the method again.
            – janos
            5 hours ago










          • Thanks for that, working backwards might be the key to this.
            – user1234433222
            5 hours ago















          up vote
          3
          down vote



          accepted











          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 <= 300)
          {
          return CartonType.SMALL_PASCAL;
          }
          if (qty <= 600)
          {
          return CartonType.SMALL_PASCAL;
          }
          if (qty <= 1200)
          {
          return CartonType.BABY_BOX;
          }
          // ...





          share|improve this answer





















          • do you have any suggestion if there is for anything over 5000? so eg the largest box and a small pascal could be required?
            – user1234433222
            5 hours ago






          • 2




            @user1234433222 sure, but we don't implement feature requests here... You could subtract from the quantity the capacity of the selected container, and then call the method again.
            – janos
            5 hours ago










          • Thanks for that, working backwards might be the key to this.
            – user1234433222
            5 hours ago













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted







          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 <= 300)
          {
          return CartonType.SMALL_PASCAL;
          }
          if (qty <= 600)
          {
          return CartonType.SMALL_PASCAL;
          }
          if (qty <= 1200)
          {
          return CartonType.BABY_BOX;
          }
          // ...





          share|improve this answer













          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 <= 300)
          {
          return CartonType.SMALL_PASCAL;
          }
          if (qty <= 600)
          {
          return CartonType.SMALL_PASCAL;
          }
          if (qty <= 1200)
          {
          return CartonType.BABY_BOX;
          }
          // ...






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 5 hours ago









          janos

          96.8k12124350




          96.8k12124350












          • do you have any suggestion if there is for anything over 5000? so eg the largest box and a small pascal could be required?
            – user1234433222
            5 hours ago






          • 2




            @user1234433222 sure, but we don't implement feature requests here... You could subtract from the quantity the capacity of the selected container, and then call the method again.
            – janos
            5 hours ago










          • Thanks for that, working backwards might be the key to this.
            – user1234433222
            5 hours ago


















          • do you have any suggestion if there is for anything over 5000? so eg the largest box and a small pascal could be required?
            – user1234433222
            5 hours ago






          • 2




            @user1234433222 sure, but we don't implement feature requests here... You could subtract from the quantity the capacity of the selected container, and then call the method again.
            – janos
            5 hours ago










          • Thanks for that, working backwards might be the key to this.
            – user1234433222
            5 hours ago
















          do you have any suggestion if there is for anything over 5000? so eg the largest box and a small pascal could be required?
          – user1234433222
          5 hours ago




          do you have any suggestion if there is for anything over 5000? so eg the largest box and a small pascal could be required?
          – user1234433222
          5 hours ago




          2




          2




          @user1234433222 sure, but we don't implement feature requests here... You could subtract from the quantity the capacity of the selected container, and then call the method again.
          – janos
          5 hours ago




          @user1234433222 sure, but we don't implement feature requests here... You could subtract from the quantity the capacity of the selected container, and then call the method again.
          – janos
          5 hours ago












          Thanks for that, working backwards might be the key to this.
          – user1234433222
          5 hours ago




          Thanks for that, working backwards might be the key to this.
          – user1234433222
          5 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209788%2fselecting-correct-carton-based-on-qty%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

          Mangá

          Eduardo VII do Reino Unido