Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

C# Question You've landed a job with a brokerage firm (Dewy, Hewy and Howe, Inc.). You've been asked to review the code that determines the discount on trades. The company wants to charge a 5% fee on transactions of more than 1 million dollars, 7% on transactions between $999,999.99 and %500,000, 10% on transactions less than $50,000 and 8% on all other transactions. Does anyone know how to go about using a switch statement to create this? I believe I have done this correctly with the if-else statement, but am struggling with the switch statement.

OpenStudy (anonymous):

I'm not sure how to create this without millions of lines of code for the switch statement, but the instructor said it can be done.

OpenStudy (anonymous):

if (transaction > 999999.99) { Fee = transaction * 0.05; } else if (transaction > 500000.00) { Fee = transaction * 0.07; } else if (transaction < 50000.00) { Fee = transaction* 0.10; } else { Fee = transaction * 0.08; } This is my current if-else code. I'm stuck after this.

OpenStudy (espex):

Your if statement is good, for your switch you will need to set a variable and then set a case for each possible value, very cumbersome. The alternative will be to use your if to set a variable to one of 4 possible values and then switch. So far as I know you cannot switch a conditional expression.

OpenStudy (anonymous):

Thanks for the response. This is where I get confused. So I use my previous code, and create four different variables to store the ranges in the if statement in? Then, I somehow use it in the switch statement? Sorry, I'm pretty poor at programming.

OpenStudy (espex):

Yes, you could create an int variable 'rate' and inside your if, rather than having Fee = transaction * 0.05;, you have rate = 1; etc. etc. Then you will switch rate and put a case for 1 through 4.

OpenStudy (espex):

You could also make rate a char, it is not really significant which you choose.

OpenStudy (anonymous):

Okay, thank you. One sec, lemme see if I got this correct.

OpenStudy (anonymous):

if (transaction > 999999.99) { rate = 1; } else if (transaction > 500000.00) { rate = 2; } else if (transaction < 50000.00) { rate = 3; } else { rate = 4; }

OpenStudy (anonymous):

something like this?

OpenStudy (espex):

Exactly.

OpenStudy (anonymous):

Hmmm I see. That was tricky at first.Thank you!

OpenStudy (espex):

You're welcome.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!