patterncsharpMinor
Temperature conversion application
Viewed 0 times
conversiontemperatureapplication
Problem
I'm two months in a programming course and our instructor told us we should think about building real-world applications as to gain valuable experience. I got the idea of doing a simple temperature conversion app.
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TempratureProject
{
class Program
{
static void Main(string[] args)
{
//Ask for the temprature that the user wants to convert
Console.WriteLine("What is the temprature you wish to convert?");
var RInput = Console.ReadLine();
double dInput = Convert.ToDouble(RInput);
//Ask for what type of conversion
Console.WriteLine("If you wish to convert Fahrenheit to Celsius, Press 1");
Console.WriteLine("If you wish to convert Fahrenheit to Kelvin, Press 2");
Console.WriteLine("If you wish to convert Celsius to Fahrenheit, Press 3");
Console.WriteLine("If you wish to convert Celsius to Kelvin, Press 4");
Console.WriteLine("If you wish to convert Kelvin to Fahrenheit, Press 5");
Console.WriteLine("If you wish to convert Kelvin to Celsius, Press 6");
//Set YInput equal to the choice made
var YInput = Console.ReadLine();
int iInput = Convert.ToInt32(YInput);
//Use of a a series of If statements to call the methods
if (iInput == 1)
{
var aAnswer = FtoCelsius(dInput);
Console.WriteLine("The value of {0} degrees Fahrenheit is equall to {1} degrees Celsius", dInput, aAnswer);
}
else if (iInput == 2)
{
var bAnswer = FtoKelvin(dInput);
Console.WriteLine("The value of {0} degrees Fahrenheit is equall to {1} degrees Kelvin", dInput, bAnswer);
}
else if (iInput == 3)
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TempratureProject
{
class Program
{
static void Main(string[] args)
{
//Ask for the temprature that the user wants to convert
Console.WriteLine("What is the temprature you wish to convert?");
var RInput = Console.ReadLine();
double dInput = Convert.ToDouble(RInput);
//Ask for what type of conversion
Console.WriteLine("If you wish to convert Fahrenheit to Celsius, Press 1");
Console.WriteLine("If you wish to convert Fahrenheit to Kelvin, Press 2");
Console.WriteLine("If you wish to convert Celsius to Fahrenheit, Press 3");
Console.WriteLine("If you wish to convert Celsius to Kelvin, Press 4");
Console.WriteLine("If you wish to convert Kelvin to Fahrenheit, Press 5");
Console.WriteLine("If you wish to convert Kelvin to Celsius, Press 6");
//Set YInput equal to the choice made
var YInput = Console.ReadLine();
int iInput = Convert.ToInt32(YInput);
//Use of a a series of If statements to call the methods
if (iInput == 1)
{
var aAnswer = FtoCelsius(dInput);
Console.WriteLine("The value of {0} degrees Fahrenheit is equall to {1} degrees Celsius", dInput, aAnswer);
}
else if (iInput == 2)
{
var bAnswer = FtoKelvin(dInput);
Console.WriteLine("The value of {0} degrees Fahrenheit is equall to {1} degrees Kelvin", dInput, bAnswer);
}
else if (iInput == 3)
Solution
UI experience
Your menue text is hard to read and not intuitively clear. I'd prefer to see something like that:
Also outputting the text that way would make the code more readable.
Taking input from console
Your statements
can be simplified to
consider to catch exceptions or use a
Rather use
You should replace
with
That makes your code more readable and concise.
Write generic code, if it's prone to be extended
At least I'd try to make all of that more generic, using a
dictionary pair. Where the tuple key serves the possible combinations of
The command line input should look like this then
1)BTW you rather want to deal with
Your menue text is hard to read and not intuitively clear. I'd prefer to see something like that:
Console.WriteLine(
@"Choose to convert temperature values, enter a key please:\n
1 - Fahrenheit to Celsius
2 - Fahrenheit to Kelvin
3 - Celsius to Fahrenheit
4 - Celsius to Kelvin
5 - Kelvin to Fahrenheit
6 - Kelvin to Celsius, Press 6
> ");Also outputting the text that way would make the code more readable.
Taking input from console
Your statements
var YInput = Console.ReadLine();
int iInput = Convert.ToInt32(YInput);can be simplified to
int iInput = Convert.ToInt32(Console.ReadLine());consider to catch exceptions or use a
String and TryParse() to check for invalid input.1Rather use
switch than if else cascadesYou should replace
if (iInput == 1)
{
var aAnswer = FtoCelsius(dInput);
Console.WriteLine("The value of {0} degrees Fahrenheit is equall to {1} degrees Celsius", dInput, aAnswer);
}
else if (iInput == 2)
{
var bAnswer = FtoKelvin(dInput);
Console.WriteLine("The value of {0} degrees Fahrenheit is equall to {1} degrees Kelvin", dInput, bAnswer);
}with
switch(iInput) {
case 1:
var aAnswer = FtoCelsius(dInput);
Console.WriteLine("The value of {0} degrees Fahrenheit is equall to {1} degrees Celsius", dInput, aAnswer);
break;
case 2:
var bAnswer = FtoKelvin(dInput);
Console.WriteLine("The value of {0} degrees Fahrenheit is equall to {1} degrees Kelvin", dInput, bAnswer);
break;
}That makes your code more readable and concise.
Write generic code, if it's prone to be extended
At least I'd try to make all of that more generic, using a
public delegate double ConversionDelegate(double x);
Dictionary,ConversionDelegate) = {
{ {"Fahrenheit","Celsius"}, FtoCelsius } ,
{ {"Fahrenheit","Kelvin"}, FtoKelvin} ,
// ...
{ {"Kelvin", "Celsius" }, KtoCelsius ,
};dictionary pair. Where the tuple key serves the possible combinations of
Fahrenheit to Celsius, Kelvin to Fahrenheit, etc. You can use the functions from Dictionary to match the key, and call the delegate and render the output accordingly.The command line input should look like this then
> Enter temperature and
> 39.5 celsius kelvin
> The value of 39.5 degrees celsius is equal to 312,65 degrees Kelvin1)BTW you rather want to deal with
double values IMO.Code Snippets
Console.WriteLine(
@"Choose to convert temperature values, enter a key please:\n
1 - Fahrenheit to Celsius
2 - Fahrenheit to Kelvin
3 - Celsius to Fahrenheit
4 - Celsius to Kelvin
5 - Kelvin to Fahrenheit
6 - Kelvin to Celsius, Press 6
> ");var YInput = Console.ReadLine();
int iInput = Convert.ToInt32(YInput);int iInput = Convert.ToInt32(Console.ReadLine());if (iInput == 1)
{
var aAnswer = FtoCelsius(dInput);
Console.WriteLine("The value of {0} degrees Fahrenheit is equall to {1} degrees Celsius", dInput, aAnswer);
}
else if (iInput == 2)
{
var bAnswer = FtoKelvin(dInput);
Console.WriteLine("The value of {0} degrees Fahrenheit is equall to {1} degrees Kelvin", dInput, bAnswer);
}switch(iInput) {
case 1:
var aAnswer = FtoCelsius(dInput);
Console.WriteLine("The value of {0} degrees Fahrenheit is equall to {1} degrees Celsius", dInput, aAnswer);
break;
case 2:
var bAnswer = FtoKelvin(dInput);
Console.WriteLine("The value of {0} degrees Fahrenheit is equall to {1} degrees Kelvin", dInput, bAnswer);
break;
}Context
StackExchange Code Review Q#157958, answer score: 8
Revisions (0)
No revisions yet.