Wednesday, April 13, 2011

Converting values using a text file

I'm trying to write a console application in C# which allows a user to enter the following information in the format x,pound,ounce where X is the value to be converted.

I've created a text file which contains the conversion factors in the following format.

pound,ounce,16.0

What I want the application to do is to grab the user input and then find the correct conversion factor within the text file and then calculate the result.

I've tried using a Node class to cycle through the list of conversion factors but it doesn't seem to work =(

Can you help?

From stackoverflow
  • Reading the file line by line is easy with a stream reader. You can then grab the three bits by splitting the string. This looks something like this:

    string line;
    StreamReader reader = new StreamReader(filePath);
    while(null != (line = reader.Read())
    {
       //You have to create the constant COMMA_SEP
       string split = line.Split(COMMA_SEP);
    }
    

    Making the corrrect calculation is something you have to create. Knowing start and end, which you have in your list, you can create a state machine type of class that returns the correct class to call, for example. You then create a factory class to return a class that contains the algorithm you desire.

    public class CalculatorFactory
    {
        public ICalculator GetCalculator(string origin, string result)
        {
              //Implementation to get correct calculator
        }
    }
    

    Then you can call the Calculate method on this interface and get your answer.

    Make sense?

    centralhawk3 : That makes sense, I should probably use a certain conversion factor when the input from the user is equal to a line in the text file. e.g. 5,pound,ounce finds pound,ounce,16.0 and then calculates the result.
  • A flat file for conversion factors is not enterprisey enough. What you need is to store these factors in XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <ConversionFactors>
      <ConversionFactor>
        <From>Pounds</From>
        <To>Ounce</From>
        <Factor>16</Factor>
      </ConversionFactor>
      <ConversionFactor>
        <From>Fortnight</From>
        <To>Day</From>
        <Factor>14</Factor>
      </ConversionFactor>
      <ConversionFactor>
        <From>KesselRun</From>
        <To>Parsec</From>
        <Factor>12</Factor>
      </ConversionFactor>
    </ConversionFactors>
    

    This way you can create an XmlDocument object and select your conversion factor using an XPath expression.

    centralhawk3 : I can try that if I can't find a simpler solution. Thanks anyway! =)
    itsmatt : Technically, it was under twelve parsecs. ;-)
  • There are many ways to solve this problem. Here's a simple way that would solve the problem with little code:

    Create a Dictionary to hold the conversions; For each of the conversion factor lines in the file you could do a conversionFactors.Add(from-to, value), where from-to is the concatenation of those two units - it's just a lookup key, so it doesn't matter much how you format it. The assumption is that the user's going to enter the same text strings.

    Then take the user's input, construct the from-to "key", grab the conversion factor "value" from the Dictionary by doing a dictionary[from_to] and do the multiplication.

    Certainly not elegant but it's probably a dozen or so lines of code. Sounds like homework, so I'll leave my solution at that.

  • I managed to solve it using an array :)

0 comments:

Post a Comment