Click here to Skip to main content
15,566,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one Interface, an abstract class that implement interface and two (Male and Female) clases that inherit abstract class and override one of methods. In main, when I make a person either female or male (e.g. IPerson p1 = new Female();). And i need console app menu, I used (nested) switch case but I don't like bcuse of low readability, I looking for another solution, please help, sorry in advace I'm new in c#..Code is bellow, ugly...

What I have tried:

C#
IPerson p1 = new Female();

            byte choice, choice1, choice2;
            DisplayMenu();
            choice = byte.Parse(ReadLine());
            Clear();


            while (choice != 4)
            {
                

                switch (choice)
                {
                    case 1:
                        f1.LiftLeg();
                        Thread.Sleep(1000);
                        Write("Press 1 to lover a leg: ");
                        choice1 = byte.Parse(ReadLine());

                        switch (choice1)
                        {
                            case 1:
                                f1.LowerLeg();
                                Thread.Sleep(2000);
                                Clear();
                                break;

                        }
                        break;           
                    case 2:
                        f1.LiftArm();
                        Write("Press 1 to lower arm: ");
                        choice2 = byte.Parse(ReadLine());
                        switch (choice2)
                        {
                            case 1:
                                f1.LowerArm();
                                Thread.Sleep(2000);
                                Clear();
                                break;
                            default:
                                WriteLine("Invalid input.");
                                break;
                        }
                        
                    case 3:
                        f1.Pee();
                        break;

                    default:

                        WriteLine("You entered " + choice + " please only use numbers from 1 to 4.");
                        
                        break;

                }
Posted
Updated 15-Nov-22 5:51am
v2
Comments
PIEBALDconsult 15-Nov-22 19:19pm    
Replace the outer switch with an array of delegates.
 
Replace the inner switches with ifs.
 
Get thirty years' experience.

A simple way would be moving the code in the (outer switch) cases in dedicated methods, e.g.
C#
switch (choice)
{
case 1:
  ProcessLiftLeg(f1);
  break; 
//..
with
C#
ProcessLiftLeg(F f) //'F', because, in your code, there is no hint about 'f1' type
{
  f.LiftLeg();
  Thread.Sleep(1000);
  Write("Press 1 to lover a leg: ");
  var choice = byte.Parse(ReadLine());
  if ( choice == 1)
  {
     f.LowerLeg();
     Thread.Sleep(2000);
     Clear();
  }
}


Note, your while iterates indefinitely with the same choice value.
 
Share this answer
 
Comments
janko petrovic 15-Nov-22 14:21pm    
Okay, I have few questions, sorry for bothering. What is 'f1' in ProcessLiftLeg(f1)?
What are these arguments in ProcessLiftLeg method (F f), and wich one is access-modifier and return type?
And how to fix while loop, I need to iterate until user press exit or nuber 4, it used to work, I changed something but I'm completly lost and couldn't find the solution. Thank you very much, and sorry again..
CPallini 16-Nov-22 4:49am    
f1 is the same variable you used in the posted code (is there possibly a type and you meant to use p1 instead?).
To fix the while you have to ask the user for the choice inside it. Maybe a do .. while loop is more appropriate.
First off, Something must be missing because your while loop isn't closed and even if it was, nothing changes choice so it's infinite. Also, your case 2 is missing a break in the first switch case. So let's assume those things are there.

If you are looking for readability, then I might suggest using constants for your choice case values. This would allow them to be recognizable right off instead of having to read the code to see what each does. The inner switch, (choice1) would be better served by if statements instead of a switch. This lets us know that there is a delineation of choices, that the inner is different from the outer.

Otherwise, I'm not seeing anything else that's obviously unreadable.
 
Share this answer
 
v3

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