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.
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.
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.
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.
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.
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.
You could also make rate a char, it is not really significant which you choose.
Okay, thank you. One sec, lemme see if I got this correct.
if (transaction > 999999.99) { rate = 1; } else if (transaction > 500000.00) { rate = 2; } else if (transaction < 50000.00) { rate = 3; } else { rate = 4; }
something like this?
Exactly.
Hmmm I see. That was tricky at first.Thank you!
You're welcome.
Join our real-time social learning platform and learn together with your friends!