Wednesday, February 20, 2013

Manhandle user inputs in a C# console app.

Today I made a silly console app that converts Fahrenheit values to Celsius values.

using System;
using Temperature_Calculator.Core;
namespace TemperatureCalculator
{
   class Program
   {
      static void Main(string[] args)
      {
         Console.Write("Type a number and then press enter: ");
         string concatenatedKeystrokes = "";
         ConsoleKeyInfo currentKeystroke;
         do
         {
            currentKeystroke = Console.ReadKey(true);
            concatenatedKeystrokes = Filter.Interpret(currentKeystroke,
                  concatenatedKeystrokes);
         }
         while (currentKeystroke.Key != ConsoleKey.Enter);
         Console.WriteLine("\r\n");
         Console.WriteLine(concatenatedKeystrokes + " in Fahrenheit converted to Celsius
               is...");
         Console.WriteLine(Converter
               .ToCelsius(Convert.ToDouble(concatenatedKeystrokes)));
         Console.WriteLine("\r\n");
         Console.WriteLine("Press any key to exit.");
         Console.ReadLine();
      }
   }
}

 
 

I teased a lot of the logic out to two static classes. The most interesting of which was this one, based upon this, which interprets user inputs and restricts them to digits and backspaces.

using System;
namespace Temperature_Calculator.Core
{
   public static class Filter
   {
      public static string Interpret(ConsoleKeyInfo keystroke, string history)
      {
         if (keystroke.Key != ConsoleKey.Backspace)
         {
            history = Add(keystroke, history);
         } else {
            history = Backspace(history);
         }
         return history;
      }
      
      private static string Add(ConsoleKeyInfo keystroke, string history)
      {
         double dummyDigit = 0;
         bool isDigit = double.TryParse(keystroke.KeyChar.ToString(), out dummyDigit);
         if (isDigit)
         {
            history += keystroke.KeyChar;
            Console.Write(keystroke.KeyChar);
         }
         return history;
      }
      
      private static string Backspace(string history)
      {
         if (history.Length > 0)
         {
            history = history.Substring(0, (history.Length - 1));
            Console.Write("\b \b");
         }
         return history;
      }
   }
}

 
 

Less interesting is...

namespace Temperature_Calculator.Core
{
   public class Converter
   {
      public static double ToCelsius(double fahrenheitValue)
      {
         double alteration = SubtractThirtyTwo(fahrenheitValue);
         alteration = MultiplyByFive(alteration);
         alteration = DivideByNine(alteration);
         return alteration;
      }
      
      private static double SubtractThirtyTwo(double value)
      {
         return value - 32;
      }
      
      private static double MultiplyByFive(double value)
      {
         return value * 5;
      }
      
      private static double DivideByNine(double value)
      {
         return value / 9;
      }
   }
}

 
 

Note that I did not make the Converter class static itself. It is only a "static" class in that it holds static methods. It is not truly a static class and I could just jam stuff like this into it:

public double LatestValue { get; set; }
 
public void SetState(double fahrenheitValue)
{
   LatestValue = ToCelsius(fahrenheitValue);
}

 
 

I could then use the instance method and the getsetter like so:

Converter converter = new Converter();
converter.SetState(Convert.ToDouble(concatenatedKeystrokes));
Console.WriteLine(converter.LatestValue);

No comments:

Post a Comment