Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hey there, i want to write an algorithm for a pizza shop (c#)

i wrote and i showed it to my friend, my friend said: your algorithm is wrong. and he wrote another algorithm.

and right now i want to know which one of our algorithm is better?

tell me which one is better and correct? (with reason)

What I have tried:

Console.WriteLine("Welcome! We have 3 different sizes, which do you want?: 1.Small - 2.Meduim - 3.Large");
            Console.Write("Enter Your Selection: ");
            int userSelection = int.Parse(Console.ReadLine());
            int price = 0;
            //there is my code:

            switch (userSelection)
            {
                case 1:
                    price += 5;
                    Console.WriteLine("You are selected Small size, We'll send you :)");
                    Console.WriteLine("Price: " + price + "$");
                    break;

                case 2:
                    price += 10;
                    Console.WriteLine("You are selected Medium size, We'll Send you :)");
                    Console.WriteLine("Price: " + price + "$");
                    break;

                case 3:
                    price += 15;
                    Console.WriteLine("You are selected Large size, We'll Send you :)");
                    Console.WriteLine("Price: " + price + "$");
                    break;

                default:
                    Console.WriteLine("Please enter the number of size that you want! (1 - 3)");
                    break;

//this is my friend's code:

switch (userSelection)
            {
                case 1:
                    price += 5;
                    break;

                case 2:
                    price += 5;
                    goto case 1;

                case 3:
                    price += 5;
                    goto case 1;
                default:
                    Console.WriteLine("please enter number of your selection");
                    break;
            }

            if (price != 0)
            {
                Console.WriteLine("Your selection is " + userSelection + ", we'll send you");
                Console.WriteLine("Price: " + price);
            }
            }
C#

Posted
Updated 16-Feb-17 8:19am
v2
Comments
[no name] 16-Feb-17 11:52am    
"please download the zip file", ha ha ha ..... no
Amir Hossein Hmd 16-Feb-17 12:32pm    
oh! excuse me! i forgot it :)
[no name] 16-Feb-17 12:55pm    
Your friend wins. Anyone brave enough to use the universally despised "goto" must be a coding genius.
Amir Hossein Hmd 16-Feb-17 15:26pm    
LOL...!!! thanks
Patrice T 16-Feb-17 11:54am    
No! list the 2 pieces of code in the question
also, describe what is supposed to do that algorithm.

Well, there is no algorithm there in either case - it's a "style thing" and your mate's version would fail most code reviews just for using goto unnecessarily.

Me? I'd set the price of each as a constant value above the method, and set it once inside a switch instead of adding values on. I'd also use a string "human readable" description which I'd put into a variable so that the Console output is done in one place, and one place only.
 
Share this answer
 
Comments
[no name] 16-Feb-17 12:53pm    
And use TryParse
second code is definitively worse. Using goto's for such a simple problem is a sign that your friend need to improve its understanding of programming.
Your code is barely better, but both of your codes are overly complicated.
minimum code would look like this, assuming user input is correct:
C#
string pizza_size[]= {"Small", "Medium", "Large"};
int pizza_price[]= {5, 10, 15};
int userSelection = int.Parse(Console.ReadLine());
string size= pizza_size[userSelection-1];
int price= pizza_price[userSelection-1];
Console.WriteLine("You are selected "+ size+ " size, We'll send you :)");
Console.WriteLine("Price: " + price + "$");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900