Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

for stackoverflow

By: a guest on Aug 23rd, 2012  |  syntax: C#  |  size: 4.43 KB  |  views: 54  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Feettoinches
  7. {
  8.   public class UnitConverter
  9.   {
  10.     double ratio;
  11.     public UnitConverter(double unitratio) { ratio = unitratio; }
  12.     public double Convert(double unit) { return unit * ratio; }
  13.   }
  14.  
  15.        
  16.  
  17.  
  18.  
  19.   class Test
  20.   {
  21.     public static void Main()
  22.     {
  23.       //Grams
  24.       UnitConverter feetToInchesConverter = new UnitConverter(12);
  25.       UnitConverter milesToFeetConverter = new UnitConverter(5280);
  26.       UnitConverter kmsToMilesConverter = new UnitConverter(1.609);
  27.       UnitConverter centToInchesConverter = new UnitConverter(2.54);
  28.       UnitConverter ozToGramsConverter = new UnitConverter(28.349);
  29.       UnitConverter cupsTolitConverter = new UnitConverter(4.226);
  30.  
  31.       /*                                                 
  32.            
  33.        Console.WriteLine(feetToInchesConverter.Convert(30)); //360
  34.        Console.WriteLine(feetToInchesConverter.Convert(milesToFeetConverter.Convert(1)));
  35.        Console.WriteLine(feetToInchesConverter.Convert(100)); //1200
  36.        Console.WriteLine(kmsToMilesConverter.Convert(2)); //3.218
  37.        Console.WriteLine(centToInchesConverter.Convert(2));//5.08        
  38.          
  39.        */
  40.  
  41.  
  42.  
  43.  
  44.       string type;
  45.       double amount;
  46.       int done;
  47.  
  48.       done = 0;
  49.      
  50.       while (done == 0)
  51.  
  52.       {
  53.  
  54.         {
  55.  
  56.           type = Console.ReadLine();
  57.  
  58.           if (type == "centi")
  59.  
  60.           {
  61.  
  62.             amount = Centimeters(centToInchesConverter, ref done);
  63.  
  64.           }
  65.  
  66.           else if (type == "feet")
  67.  
  68.           {
  69.  
  70.             amount = Feet(feetToInchesConverter, ref done);
  71.  
  72.           }
  73.  
  74.           else if (type == "km")
  75.  
  76.           {
  77.  
  78.             amount = kilometersToMiles(kmsToMilesConverter, ref done);
  79.  
  80.           }
  81.  
  82.           else if (type == "miles")
  83.  
  84.           {
  85.  
  86.             amount = MilesToFeet(milesToFeetConverter, ref done);
  87.  
  88.           }
  89.  
  90.           else if (type == "grams")
  91.  
  92.           {
  93.  
  94.             amount = gramsToOunces(ozToGramsConverter, ref done);
  95.  
  96.           }
  97.  
  98.           else if (type == "cups")
  99.  
  100.           {
  101.  
  102.             Console.WriteLine("Enter a number of liters to be converted into cups.");
  103.             amount = Convert.ToDouble(Console.ReadLine());
  104.             Console.WriteLine(cupsTolitConverter.Convert(amount) + " centimeters to said number of inches.");
  105.  
  106.           }
  107.  
  108.  
  109.           else if (type == "end")
  110.  
  111.           {
  112.  
  113.             done = 1;
  114.  
  115.           }
  116.         }
  117.       }
  118.     }
  119.  
  120.     private static double gramsToOunces(UnitConverter ozToGramsConverter, ref int done)
  121.  
  122.     {
  123.  
  124.       double amount;
  125.       Console.WriteLine("Enter a number of ounces to be converted to grams.");
  126.       amount = Convert.ToDouble(Console.ReadLine());
  127.       Console.WriteLine(ozToGramsConverter.Convert(amount));
  128.      
  129.       return amount;
  130.  
  131.     }
  132.  
  133.     private static double MilesToFeet(UnitConverter milesToFeetConverter, ref int done)
  134.  
  135.     {
  136.  
  137.       double amount;
  138.       Console.WriteLine("Enter a number of miles to be converted to feet.");
  139.       amount = Convert.ToDouble(Console.ReadLine());
  140.       Console.WriteLine(milesToFeetConverter.Convert(amount) + " feet to said number of miles.");
  141.       done
  142.       return amount;
  143.  
  144.     }
  145.  
  146.     private static double kilometersToMiles(UnitConverter kmsToMilesConverter, ref int done)
  147.  
  148.     {
  149.  
  150.       double amount;
  151.       Console.WriteLine("Enter a number of kilometers to be converted into miles.");
  152.       amount = Convert.ToDouble(Console.ReadLine());
  153.       Console.WriteLine(kmsToMilesConverter.Convert(amount) + " miles to said number of kilometers.");
  154.      
  155.       return amount;
  156.  
  157.     }
  158.  
  159.     private static double Feet(UnitConverter feetToInchesConverter, ref int done)
  160.  
  161.     {
  162.  
  163.       double amount;
  164.       Console.WriteLine("Enter a number of feet to be converted into inches.");
  165.       amount = Convert.ToDouble(Console.ReadLine());
  166.       Console.WriteLine(feetToInchesConverter.Convert(amount) + " inches to said number of feet.");      
  167.       return amount;
  168.  
  169.     }
  170.  
  171.     private static double Centimeters(UnitConverter centToInchesConverter, ref int done)
  172.  
  173.     {
  174.  
  175.       double amount;
  176.       Console.WriteLine("Enter a number of centimeters to be converted into inches.");
  177.       amount = Convert.ToDouble(Console.ReadLine());
  178.       Console.WriteLine(centToInchesConverter.Convert(amount) + " centimeters to said number of inches.");
  179.      
  180.       return amount;
  181.  
  182.     }
  183.   }
  184. }