liartie

Untitled

Sep 11th, 2022
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. {
  2.     public class Sort
  3.     {
  4.         public static bool CheckCommand(string command)
  5.         {
  6.             switch (command)
  7.             {
  8.                 case "sort":
  9.                     return true;
  10.                 case "Sort":
  11.                     Console.WriteLine("I don't know this command, sorry to say that. Is there a chance you meant 'sort'?");
  12.                     return false;
  13.                 default:
  14.                     Console.WriteLine("I don't know this command, sorry to say that. And I have no idea what you meant...");
  15.                     return false;
  16.             }
  17.         }
  18.  
  19.         public static List<String> ReadFile(string name)
  20.         {
  21.             List<String> lines = new List<String>();
  22.             try
  23.             {
  24.                 foreach (string line in File.ReadLines(name))
  25.                 {
  26.                     lines.Add(line);
  27.                 }
  28.             }
  29.             catch (FileNotFoundException)
  30.             {
  31.                 Console.WriteLine("I am so sorry to say, but I cannot find such file. I know this might be very difficult," +
  32.                                   " but please check if you have entered the name correctly. I believe in you.");
  33.             }
  34.  
  35.             return lines;
  36.         }
  37.         public static void Main(string[] args)
  38.         {
  39.             var command = Console.ReadLine();
  40.             String[] flags = command.Split(" ");
  41.             if(!CheckCommand(flags[0])) return;
  42.  
  43.             var fileName = flags[^1];
  44.             var linesArray = ReadFile(fileName);
  45.             linesArray.Sort();
  46.             foreach (var line in linesArray)
  47.             {
  48.                 Console.WriteLine(line);
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment