Advertisement
ejectedmatrix

Simple program

Nov 18th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.23 KB | None | 0 0
  1. using System; // This is the core namespace, this let's us do stuff with strings, and write to the Console.
  2. using System.IO; // This let's us interact with IO (Input/Output), aka File IO.
  3. using System.Collections.Generic; // This let's us create Dictionaries (collections), and work with them.
  4.  
  5. /*
  6.     ---
  7.         * PURPOSE OF THIS PROGRAM *
  8.        
  9.         This program was created for educational purposes.
  10.         This program reads a file line-by-line given in by a user.
  11.         For each line, it splits it by the equals sign, and stores the value into a key-value pair.
  12.         For example, if a line was "apple=good fruit" then it would store it with the key being "apple" and value being "good fruit."
  13.         Once it is done reading the file, a user can request the value of a key by giving the application input.
  14.         If the value exists, it'll give it, if it doesn't, it'll notify the user.
  15.        
  16.         This program is commented in detail to explain how it works. Have fun learning!
  17.         This program was created by bin to teach pixel core C# functions.
  18.     ---
  19. */
  20.  
  21. namespace MyApp // Our namespace is decalred
  22. {
  23.     public class Program // The program's namespace is declared.
  24.     {
  25.         // These are the variables of the program, used globally inside it.
  26.         static string file_name; //This is the filename that we will be reading
  27.         static Dictionary<string, string> values = new Dictionary<string, string>();
  28.         /*
  29.             This is a Dictionary. Dictionary is a special type of List with a key-value pair
  30.             For example, you can have the key "apples" link to "good fruit." Dictionaries are an extremely useful class
  31.             used throughout C# coding for all kinds of coding, from tasks, to storing quick data, to this !
  32.             Have fun playing with Dictionaries !    
  33.         */
  34.        
  35.         public static void Main(string[] args) // The entry point of the program, where all the stuff happens.
  36.         {
  37.             Console.WriteLine("Enter the file name you would like to read:"); //These next couple lines of code are self explanatory.
  38.             Console.Write("> ");
  39.             file_name = Console.ReadLine(); // We take the file name as an input from the Console, and store it in the file_name variable.
  40.            
  41.             if(string.IsNullOrWhiteSpace(file_name)) // Now we check if it's null or just spaces, and if so, we'll change it back to the default file name
  42.                 file_name = "items.txt"; // which is done here.
  43.            
  44.             if(!File.Exists(file_name)) // Now we check if the file exists, if it doesn't we create a new file with the same name as file_name
  45.             {
  46.                 File.Create(file_name).Close(); // Here as you can see, the file is created, and then the FileStream returned by .Create() is closed.
  47.                 Console.WriteLine("Error! File did not exist and has been created!"); //We notify the user
  48.                 return; //Then we use the "meh" method to exit the program (atleast only usable in this situation, in the main program).
  49.                
  50.                 //Up here we are using the *meh* method to exit the program.
  51.             }
  52.            
  53.             string[] file_data = File.ReadAllLines(file_name); // We read the file by **lines** and store it an array, each value of the array is a different line
  54.             int line_number = 1; // We take a note of the line number, it starts from 1 in this case for ease of use, but can be 0 as well.
  55.            
  56.             foreach(string data in file_data) // This is a foreach loop, it executes that block of code for each value in the array, and assigns it to a temp variable called "data"
  57.             {
  58.  
  59.                 if(!string.IsNullOrWhiteSpace(data)) // We check if the data is null/empty or not.
  60.                 {
  61.                     string[] key_value = data.Split('='); //string.Split(data, '=');
  62.                     //Here we split the data by the equals sign.
  63.                    
  64.                     /* Side note:
  65.                         This program does not account for a second equals sign, it will just ignore it and not take it in as an input.
  66.                     */
  67.                    
  68.                     //Here we check to make sure that there are actually two values, one key, and one value for the key.
  69.                     if(key_value.Length >= 2)
  70.                     {
  71.                         if(string.IsNullOrWhiteSpace(key_value[0])) // Now we check if the key is blank or not, if it is, it's invalid.
  72.                             Console.WriteLine("Invalid data key on line " + line_number); // Notify the user.
  73.                         else if(string.IsNullOrWhiteSpace(key_value[1])) // Now we check if the data value for the key is blank, if it is, it's invalid
  74.                             Console.WriteLine("Invalid data value on line " + line_number); // Notify the user.
  75.                         else // If it's not invalid, this gets called.
  76.                             values.Add(key_value[0], key_value[1]); // and we add the key-value pair to the dictionary.
  77.                     }
  78.                     else // If there aren't two values, or something blank, or some other shit, we notify the user
  79.                         Console.WriteLine("Invalid data provided on line " + line_number); // that the data is invalid.
  80.                 }
  81.                 else
  82.                     Console.WriteLine("Invalid data provided on line " + line_number); // We should do it here, just incase if it's null or white-spaced.
  83.                
  84.                 line_number++; // Here we make the line number go up by one so we have a refrence to what line we are on, to notify the user.
  85.                
  86.                 // Then the loop iterates until it has gone through every value in the array.
  87.             }
  88.            
  89.             Console.WriteLine("Finished reading file '" + file_name + "'"); // We notify the user that it's done.
  90.             Console.WriteLine("Enter a value, or a command. Use -h for commands or -e to exit.\n"); // We notify the user of the possible commands, and etc.
  91.            
  92.             while(true) // Now we go into a forever while loop, this goes on forever till the program exits or is killed.
  93.             {
  94.                 Console.Write("> "); // We write a cool command-hackery-looking thing down here.
  95.                 string input = Console.ReadLine(); // We read the input
  96.                
  97.                 if(!string.IsNullOrWhiteSpace(input)) // We check to make sure that it's not null or spaces, and if it is, we just keep looping.
  98.                 {
  99.                     input = input.Trim(); // We trim the input from extra spaces and etc.0
  100.                     string cmd = input.ToLower(); // We create a lowered version of the input to check for commands (just incase if user types -E instead of -e).
  101.                    
  102.                     if(cmd == "-h") // If it's the help command, just notify the user with a list of commands.
  103.                         Console.WriteLine("Commands:\n-h : Lists commands.\n-v : Lists all values\n-e : Exits ths program.");
  104.                     else if(cmd == "-e") // If it's the exit command:
  105.                     {
  106.                         Console.WriteLine("Now exiting..."); // Notify the user we are exiting
  107.                        
  108.                         // There are two ways to exit the program in this situation:
  109.                         // We can exit the normal way, OR we can just end the Main() function and boom.
  110.                         // For science, we will do it the proper way.
  111.                        
  112.                         Environment.Exit(0); // Exit the program with exit code 0, which means execution succesfull.
  113.                        
  114.                         // The unproper way:
  115.                         // return;
  116.                     }
  117.                     else if(cmd == "-v") // if it's the list all values command then we list all the values.
  118.                     {
  119.                         string output_text = "All values:\n"; // We create a template variable, start it with same basic values:
  120.                        
  121.                         foreach(var pair in values) // Iterate through all the keys
  122.                         {
  123.                             output_text += $"'{pair.Key}'\n"; // Add '{KEY_NAME}' to the string above
  124.                             // The += means:
  125.                             // output_text = output_text + new_value;
  126.                         }
  127.                        
  128.                         Console.WriteLine(output_text); // We output the text
  129.                     }
  130.                     else // If it's not a command, we'll take the input and check if it's a key in our memory.
  131.                     {
  132.                         if(values.ContainsKey(input)) // If it is
  133.                             Console.WriteLine($"Value for '{input}' is: '{values[input]}'"); // We'll just output it here
  134.                         else
  135.                             Console.WriteLine($"Value not found for '{input}'"); // If it isn't then we'll notify the user
  136.                     }
  137.                    
  138.                     // We don't do anything, the while loop iterates forever until the program exits, or is killed.
  139.                 }
  140.             }
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement