Full-Stack freelance Umbraco developer in the UK.

George Phillipson - Freelance Umbraco developer

Temperature Converter

using System.ComponentModel.DataAnnotations;
namespace TempConverter.Models
{
    public class TemperatureConverterViewModel
    {
       public List<string> Temp         { get; set; }
        [Required(ErrorMessage = "Please select a conversion option")]
        [DisplayName("Conversion Option")]
        public string ConversionType    { get; set; }
        [Required(ErrorMessage = "Please enter a starting Temperature")]
        [DisplayName("Starting Temp")]
        public string StartingTemp      { get; set; }
    }
}
namespace TempConverter.Infrastructure
{
    public static class CalculateTemperatureHelper
    {
        public static List<string> TempList(int value, string option)
        {
            List<string> lstOfTemps = new List<string>();
            switch (option)
            {
                case "1":
                    for (int i = value; i < value + 100; i += 1)
                    {
                        lstOfTemps.Add(
                            $"The temperature at {i:N}°C is {TemperatureConversionFormula.CelsiusToFahrenheit(i):N}°F");
                    }
                    break;
                case "2":
                    for (int i = value; i < 100; i += 1)
                    {
                        lstOfTemps.Add(
                            $"The temperature at {i:N}°F is {TemperatureConversionFormula.FahrenheitToCelsius(i):N}°C");
                    }
                    break;
                default:
                    lstOfTemps.Add($"The temperature conversion option you selected is not valid.");
                    break;
            }
            return lstOfTemps;
        }
        private static class TemperatureConversionFormula
        {
            public static double CelsiusToFahrenheit(int value)
            {
                return ((9.0 / 5.0) * value) + 32;
            }
            public static double FahrenheitToCelsius(int value)
            {
                return (5.0 / 9.0) * (value - 32);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using TempConverter.Infrastructure;
using TempConverter.Models;
namespace TempConverter.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            ViewBag.conversionType = LstConversionOptions();
            var model = new TemperatureConverterViewModel();
            return View("Index", model);
        }
        [HttpPost]
        public ActionResult Index(string startingTemp, string conversionType)
        {
            try
            {
                int value;
                if (!int.TryParse(startingTemp, out value))
                {
                    ModelState.AddModelError("Error", "Starting Temp can only be whole numbers");
                    ViewBag.conversionType = LstConversionOptions();
                    return View("Index");
                }
                if (ModelState.IsValid)
                {
                    var data = CalculateTemperatureHelper.TempList(Convert.ToInt32(startingTemp), conversionType);
                    var model = new TemperatureConverterViewModel
                    {
                        Temp = data
                    };
                    ViewBag.conversionType = LstConversionOptions();
                    return View("Index", model);
                }
                ViewBag.conversionType = LstConversionOptions();
                return View("Index");
            }
            catch (Exception)
            {
                ViewBag.Message = "Sorry an error has occured";
                return View("Error");
            }
        }
        private static List<SelectListItem> LstConversionOptions()
        {
            List<SelectListItem> conversionOption = new List<SelectListItem>
            {
                new SelectListItem {Text = "Convert Celsius to Fahrenheit", Value = "1"},
                new SelectListItem {Text = "Convert Fahrenheit to Celsius", Value = "2"}
            };
            return conversionOption;
        }
    }
}
@model TempConverter.Models.TemperatureConverterViewModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
<div>
    <form action="@Url.Action("Index", "Home")" method="post">
        @Html.ValidationSummary(false)
        @Html.LabelFor(x => x.StartingTemp)<br/>
        @Html.TextBoxFor(x => x.StartingTemp)<br/>
        @Html.LabelFor(x => x.ConversionType)<br/>
        @Html.DropDownList("conversionType")<br/>
        <input type="submit" value="Convert">
    </form>
    @if(Model?.Temp != null)
    {
        <ul>
            @foreach (var t in Model.Temp)
            {
                <li>@t.ToString()</li>
            }
        </ul>
    }
</div>
</body>
</html>

Please enter your comment.