C# Selecting Correct Carton Based On Qty
up vote
0
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? than I would required 2 A485.
Update
[Test]
private static CartonType? GetCartonType(int qty)
{
foreach (CartonType cartonType in Enum.GetValues(typeof(CartonType)))
{
if (qty <=300)
{
return cartonType;
}
if (qty <=600)
{
return cartonType;
}
if (qty <= 1200)
{
return cartonType;
}
if (qty <= 1800)
{
return cartonType;
}
if (qty <= 3000)
{
return cartonType;
}
if (qty => 5000)
{
return cartonType;
}
}
return null;
}
c#
add a comment |
up vote
0
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? than I would required 2 A485.
Update
[Test]
private static CartonType? GetCartonType(int qty)
{
foreach (CartonType cartonType in Enum.GetValues(typeof(CartonType)))
{
if (qty <=300)
{
return cartonType;
}
if (qty <=600)
{
return cartonType;
}
if (qty <= 1200)
{
return cartonType;
}
if (qty <= 1800)
{
return cartonType;
}
if (qty <= 3000)
{
return cartonType;
}
if (qty => 5000)
{
return cartonType;
}
}
return null;
}
c#
add a comment |
up vote
0
down vote
favorite
up vote
0
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? than I would required 2 A485.
Update
[Test]
private static CartonType? GetCartonType(int qty)
{
foreach (CartonType cartonType in Enum.GetValues(typeof(CartonType)))
{
if (qty <=300)
{
return cartonType;
}
if (qty <=600)
{
return cartonType;
}
if (qty <= 1200)
{
return cartonType;
}
if (qty <= 1800)
{
return cartonType;
}
if (qty <= 3000)
{
return cartonType;
}
if (qty => 5000)
{
return cartonType;
}
}
return null;
}
c#
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? than I would required 2 A485.
Update
[Test]
private static CartonType? GetCartonType(int qty)
{
foreach (CartonType cartonType in Enum.GetValues(typeof(CartonType)))
{
if (qty <=300)
{
return cartonType;
}
if (qty <=600)
{
return cartonType;
}
if (qty <= 1200)
{
return cartonType;
}
if (qty <= 1800)
{
return cartonType;
}
if (qty <= 3000)
{
return cartonType;
}
if (qty => 5000)
{
return cartonType;
}
}
return null;
}
c#
c#
edited 7 mins ago
asked 29 mins ago
user1234433222
18810
18810
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
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;
}
// ...
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
10 mins ago
1
@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
7 mins ago
Thanks for that, working backwards might be the key to this.
– user1234433222
6 mins ago
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209788%2fc-selecting-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
1
down vote
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;
}
// ...
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
10 mins ago
1
@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
7 mins ago
Thanks for that, working backwards might be the key to this.
– user1234433222
6 mins ago
add a comment |
up vote
1
down vote
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;
}
// ...
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
10 mins ago
1
@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
7 mins ago
Thanks for that, working backwards might be the key to this.
– user1234433222
6 mins ago
add a comment |
up vote
1
down vote
up vote
1
down vote
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;
}
// ...
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;
}
// ...
answered 20 mins ago
janos
96.7k12124350
96.7k12124350
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
10 mins ago
1
@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
7 mins ago
Thanks for that, working backwards might be the key to this.
– user1234433222
6 mins ago
add a comment |
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
10 mins ago
1
@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
7 mins ago
Thanks for that, working backwards might be the key to this.
– user1234433222
6 mins 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
10 mins 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
10 mins ago
1
1
@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
7 mins 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
7 mins ago
Thanks for that, working backwards might be the key to this.
– user1234433222
6 mins ago
Thanks for that, working backwards might be the key to this.
– user1234433222
6 mins ago
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209788%2fc-selecting-correct-carton-based-on-qty%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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