Katrin1337
Katrin1337

Reputation: 21

Replace the Switch construction with a method that gives the same results.C#

I need to replace the switch construction with a method in order to optimize the code, but with the same results. I've been trying to do this for a long time, but I can't do it. I will be very grateful if you can help! Here's what my teacher wrote to me: "Katrin in switch, you pass a value, and an integer. It can be considered in the simplest case as an array index. In the more complex case, as an element of a General expression that gives different results for this value. If You can write a function that gives the same results as switch-case, then the record will be compact (as if it consists of a single case block), so there is no need for switch-case"

string[] array = new string[68]{(68 elements)};//The array of professions
string[] array2 = new string[44]{(44 elements)};//Array of qualities: HEALTH status
string[] array3 = new string[46] {(46 elements)};//Array of qualities : Character traits.
string[] array4 = new string[40] {(40 elements)};//Array of qualities : a HOBBY.
string[] array5 = new string[41] {(41 elements)};//Array of qualities : PHOBIAS.
string[] array6 = new string[50] {(50 elements)};//Array of Luggage.
string[] array8 = new string[39] {(39 elements)};//An array of player's cards.
string[] array7 = new string[50] {(50 elements)};//Array of Additional information.

int start = 0;
Console.WriteLine("Choose what to generate: \n[1] - PROFESSION \n[2] - ACTION cards \n[3] - PHOBIAS \n[4] - HEALTH status \n[5] - ADD.Information \n[6] - LUGGAGE \n[7] - HOBBIES \n[8] - CHARACTER ");
start = Convert.ToInt32(Console.ReadLine());

switch (start)
{
    case 1:

        int p;
        int sycle2;
        
        Console.WriteLine("How many cards with Professions can I generate?: ");
        p = Convert.ToInt32(Console.ReadLine());

        for (sycle2 = 0; sycle2 < p; sycle2++)
        {
            Console.WriteLine("enter the names of the players to generate cards for: ");
            string name = Console.ReadLine();
            name += ".txt";

            string str1 = sycle2 + 1 + "bynker.txt";
            StreamWriter sw2 = File.CreateText(name);
            int a7 = rnd.Next(0, 68);
            sw2.WriteLine($"Profession : {array[a7]}");
            sw2.Close();
        }
        break;

    case 2:

        int p1;
        int sycle3;
        
        Console.WriteLine("how many cards with additional maps to generate?: ");
        p1 = Convert.ToInt32(Console.ReadLine());

        for (sycle3 = 0; sycle3 < p1; sycle3++)
        {
            Console.WriteLine("enter the names of the players to generate cards for: ");
            string name = Console.ReadLine();
            name += ".txt";

            string str2 = sycle3 + 1 + "bynker.txt";
            StreamWriter sw3 = File.CreateText(name);

            int b3 = rnd.Next(0, 39);
            int b4 = rnd.Next(0, 39);
            sw3.WriteLine($"action Map: {array8[b3]}");
            sw3.WriteLine($"action Map: {array8[b4]}");
            sw3.Close();
        }
        break;

    case 3:
    
        int p2;
        int sycle4;
        
        Console.WriteLine("How many cards with phobias to generate : ");
        p2 = Convert.ToInt32(Console.ReadLine());
        
        for (sycle4 = 0; sycle4 < p2; sycle4++)
        {
            Console.WriteLine("enter the names of the players to generate cards for: ");
            string name = Console.ReadLine();
            name += ".txt";

            string str1 = sycle4 + 1 + "bynker.txt";
            StreamWriter sw4 = File.CreateText(name);
            int b = rnd.Next(0, 41);
            sw4.WriteLine($"Phobia : {array5[b]}");
            sw4.Close();
        }
        break;

    case 4:
    
        int p3;
        int sycle5;
        
        Console.WriteLine("How many cards with Diseases(State of Health) to generate?: ");
        p3 = Convert.ToInt32(Console.ReadLine());
        
        for (sycle5 = 0; sycle5 < p3; sycle5++)
        {
            Console.WriteLine("enter the names of the players to generate cards for: ");
            string name = Console.ReadLine();
            name += ".txt";

            string str1 = sycle5 + 1 + "bynker.txt";
            StreamWriter sw5 = File.CreateText(name);
            int b6 = rnd.Next(5, 80);
            int a8 = rnd.Next(0, 44);
            sw5.WriteLine($"health status : {array2[a8]} ({b6}%)");
            sw5.Close();
        }
        break;

    case 5:
    
        int p4;
        int sycle6;
        
        Console.WriteLine("How many cards with Extra.What should I generate?: ");
        p4 = Convert.ToInt32(Console.ReadLine());
        
        for (sycle6 = 0; sycle6 < p4; sycle6++)
        {
            Console.WriteLine("enter the names of the players to generate cards for: ");
            string name = Console.ReadLine();
            name += ".txt";

            string str1 = sycle6 + 1 + "bynker.txt";
            StreamWriter sw6 = File.CreateText(name);
            int b2 = rnd.Next(0, 50);
            sw6.WriteLine($"Add.Information: {array7[b2]}");
            sw6.Close();
        }
        break;

    case 6:
    
        int p5;
        int sycle7;
        
        Console.WriteLine("How many Baggage cards can I generate?: ");
        p5 = Convert.ToInt32(Console.ReadLine());
        
        for (sycle7 = 0; sycle7 < p5; sycle7++)
        {
            Console.WriteLine("enter the names of the players to generate cards for: ");
            string name = Console.ReadLine();
            name += ".txt";

            string str1 = sycle7 + 1 + "bynker.txt";
            StreamWriter sw7 = File.CreateText(name);
            int b1 = rnd.Next(0, 50);
            sw7.WriteLine($"baggage:{array6[b1]}");
            sw7.Close();
        }
        break;

    case 7:
        
        int p6;
        int sycle8;
        
        Console.WriteLine("How many Hobby cards can I generate?: ");
        p6 = Convert.ToInt32(Console.ReadLine());
        
        for (sycle8 = 0; sycle8 < p6; sycle8++)
        {
            Console.WriteLine("enter the names of the players to generate cards for: ");
            string name = Console.ReadLine();
            name += ".txt";

            string str1 = sycle8 + 1 + "bynker.txt";
            StreamWriter sw8 = File.CreateText(name);
            int a10 = rnd.Next(0, 40);
            sw8.WriteLine($") : {array4[a10]}");
            sw8.Close();
        }           
        break;

    case 8:
    
        int p7;
        int sycle9;
        
        Console.WriteLine("How many Character cards can I generate?: ");
        p7 = Convert.ToInt32(Console.ReadLine());
        
        for (sycle9 = 0; sycle9 < p7; sycle9++)
        {
            Console.WriteLine("enter the names of the players to generate cards for: ");
            string name = Console.ReadLine();
            name += ".txt";

            string str1 = sycle9 + 1 + "bynker.txt";
            StreamWriter sw9 = File.CreateText(name);
            int a9 = rnd.Next(0, 46);
            sw9.WriteLine($"--Trait : {array3[a9]}");
            sw9.Close();
        }           
        break;
}

Thank you in advance!

Upvotes: 0

Views: 64

Answers (1)

Jasper Kent
Jasper Kent

Reputation: 3676

I don't want to solve the entire program for you, but here's the process just looking at the first two cases.

  1. Wrap the clauses in curly braces, which means you can reuse variable names.
  2. Make the variable names consistent
  3. Mark the lines that are different.

This gives:

  case 1:
  {
      int p;
      int sycle2;
            
      Console.WriteLine("How many cards with Professions can I generate?: ");
      p = Convert.ToInt32(Console.ReadLine());
    
      for (sycle2 = 0; sycle2 < p; sycle2++)
      {
          Console.WriteLine("enter the names of the players to generate cards for: ");
          string name = Console.ReadLine();
          name += ".txt";
    
          string str1 = sycle2 + 1 + "bynker.txt";
          StreamWriter sw2 = File.CreateText(name);
          int a7 = rnd.Next(0, 68);
          sw2.WriteLine($"Profession : {array[a7]}");
          sw2.Close();
      }
      break;
  }

  case 2:
  {
      int p;
      int sycle2;
        
      // Different
      Console.WriteLine("how many cards with additional maps to generate?: ");

      p = Convert.ToInt32(Console.ReadLine());

      for (sycle2 = 0; sycle2 < p; sycle2++)
      {
          Console.WriteLine("enter the names of the players to generate cards for: ");
          string name = Console.ReadLine();
          name += ".txt";

          string str1 = sycle2 + 1 + "bynker.txt";
          StreamWriter sw2 = File.CreateText(name);

          // Different
          int b3 = rnd.Next(0, 39);
          int b4 = rnd.Next(0, 39);
          sw2.WriteLine($"action Map: {array8[b3]}");
          sw2.WriteLine($"action Map: {array8[b4]}");
          // End different
          
          sw2.Close();
      }
      break;
  }

The first difference is just the text of the message, so this can be solved by an array of strings keyed on start.

The second difference is more complex. This can be solved in various ways. One is the Template Method design pattern, but I'd use an array of actions:

void WriteMethod1(StreamWriter sw)
{
     int a7 = rnd.Next(0, 68);
     sw2.WriteLine($"Profession : {array[a7]}");
}

void WriteMethod2(StreamWriter sw)
{
     int b3 = rnd.Next(0, 39);
     int b4 = rnd.Next(0, 39);
     sw2.WriteLine($"action Map: {array8[b3]}");
     sw2.WriteLine($"action Map: {array8[b4]}");
}

Action<StreamWriter>[] writers = {WriteMethod1,WriteMethod2, ...}

So now you can replace the whole switch with:

  int p;
  int sycle2;
                
  Console.WriteLine(messages[start]);
  p = Convert.ToInt32(Console.ReadLine());
        
  for (sycle2 = 0; sycle2 < p; sycle2++)
  {
       Console.WriteLine("enter the names of the players to generate cards for: ");
       string name = Console.ReadLine();
       name += ".txt";
        
       string str1 = sycle2 + 1 + "bynker.txt";
       StreamWriter sw2 = File.CreateText(name);
       writers[start](sw2);
       sw2.Close();
  }

Apologies for any errors. The important thing is the process, no the detail.

Upvotes: 1

Related Questions