Anna
Anna

Reputation: 35

Variable doesn't change its value in response to input

I'm actually quite new to programming and maybe I'm just tired but I can't find the problem here. The result doesn't seem to change its value. As you can see, the first input is product, second is city and lastly it's the amount of that product we want. The program has to print the final price (result).

static void Main(string[] args)
{
    string product = Console.ReadLine().ToLower();
    string city = Console.ReadLine().ToLower();
    double amount = double.Parse(Console.ReadLine());
    double result;
    if (city == "Sofia")
    {
        if (product == "coffee")
        {
            result = amount * 0.50;
            Console.WriteLine(result);

        }
        else if (product == "water")
        {
            result = amount * 0.80;
            Console.WriteLine(result);

        }
        else if (product == "beer")
        {
            result = amount * 1.20;
            Console.WriteLine(result);

        }
        else if (product == "sweets")
        {
            result = amount * 1.45;
            Console.WriteLine(result);

        }
        else if (product == "peanuts")
        {
            result = amount * 1.60;
            Console.WriteLine(result);

        }
    }
    else if (city == "Plovdiv")
    {
        if (product == "coffee")
        {
            result = amount * 0.40;
            Console.WriteLine(result);

        }
        else if (product == "water")
        {
            result = amount * 0.70;
            Console.WriteLine(result);

        }
        else if (product == "beer")
        {
            result = amount * 1.15;
            Console.WriteLine(result);

        }
        else if (product == "sweets")
        {
            result = amount * 1.30;
            Console.WriteLine(result);

        }
        else if (product == "peanuts")
        {
            result = amount * 1.50;
            Console.WriteLine(result);

        }
    }
    else if (city == "Varna")
    {
        if (product == "coffee")
        {
            result = amount * 0.45;
            Console.WriteLine(result);

        }
        else if (product == "water")
        {
            result = amount * 0.70;
            Console.WriteLine(result);

        }
        else if (product == "beer")
        {
            result = amount * 1.10;
            Console.WriteLine(result);

        }
        else if (product == "sweets")
        {
            result = amount * 1.35;
            Console.WriteLine(result);

        }
        else if (product == "peanuts")
        {
            result = amount * 1.55;
            Console.WriteLine(result);

        }
        else
        {
            Console.WriteLine("Invalid");
        }
    }

Upvotes: 1

Views: 104

Answers (4)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186688

Can you see how combersome the code is? Let's split the implementation into model and business logic (C# 6.0):

// Dictionary of dictionary; a better solution is to implement a custom class for it 
private static Dictionary<string, Dictionary<string, double>> s_Model = 
  new Dictionary<string, Dictionary<string, double>>(StringComparer.OrdinalIgnoreCase) {
    { "Sofia", new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase) {
      { "coffee", 0.80},
      { "beer", 1.20},
      { "water", 0.50} } },

    { "Plovdiv", new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase) {
      { "coffee", 0.70},
      { "water", 0.45} } },
  };

Than just use the model; try obtaining products for the provided city; then looking for price for the required product:

static void Main(string[] args)
{
    // It's the Model that should take case into account
    string product = Console.ReadLine();
    string city = Console.ReadLine();
    double amount = double.Parse(Console.ReadLine());

    Dictionary<string, double> products = null;
    double price;

    if (!s_Model.TryGetValue(city, out products)) 
      Console.WriteLine("Incorrect city"); 
    else if (products.TryGetValue(products, out price))
      Console.WriteLine("Incorrect product");
    else
      Console.Write((amount * price).ToString("F2"));
}  

Upvotes: 1

EpicKip
EpicKip

Reputation: 4043

As Bathsheba said, you need to change your comparison to lowercase too as the comparison is exact. Also I would recommend putting a Console.ReadKey(); at the end so it doesn't close before you get a result.

Example:

    Console.Write("Product: ");
    string product = Console.ReadLine().ToLower();
    Console.Write("City: ");
    string city = Console.ReadLine().ToLower();
    Console.Write("Price: ");
    double amount = Convert.ToDouble(Console.ReadLine());
    double result;
    if (city == "sofia")
    {
        if (product == "coffee")
        {
            result = amount * 0.50;
            Console.WriteLine(result);
        }
    }
    Console.ReadKey();
    }
}

I put the Console.Write();there for readability when running so I could test it.

Also with the many if's I would recommend a switch statement.

eg.

 switch(city)
        {
            case "sofia":
                switch (product)
                {
                    case "coffee":
                        result = amount * 0.50;
                        Console.WriteLine(result);
                        break;
                }
                break;

            case "plovdiv":
                switch (product)
                {
                    case "coffee":
                        result = amount * 0.40;
                        Console.WriteLine(result);
                        break;
                } 
                break;
        }

Upvotes: 0

Wheels73
Wheels73

Reputation: 2890

Someones seen the lowercase issue already. but also as an alternative to the nesting that you have. In the interest of clean code, change to a switch statement to reduce nesting and to improve the readability of your code.

 var product = "coffee";
            var city = "VarNa";
            var amount = 10.00;
            double result;

            switch (city.ToLower())
            {
                case "sofia":
                    switch (product)
                    {
                        case "coffee":
                            result = amount * 0.50;
                            Console.WriteLine(result);
                            break;
                        case "water":
                            result = amount * 0.80;
                            Console.WriteLine(result);
                            break;
                        case "beer":
                            result = amount * 1.20;
                            Console.WriteLine(result);
                            break;
                        case "sweets":
                            result = amount * 1.45;
                            Console.WriteLine(result);
                            break;
                        case "peanuts":
                            result = amount * 1.60;
                            Console.WriteLine(result);
                            break;
                    }
                    break;

                case "plovdiv":
                    switch (product)
                    {
                        case "coffee":
                            result = amount * 0.40;
                            Console.WriteLine(result);
                            break;
                        case "water":
                            result = amount * 0.70;
                            Console.WriteLine(result);
                            break;
                        case "beer":
                            result = amount * 1.15;
                            Console.WriteLine(result);
                            break;
                        case "sweets":
                            result = amount * 1.30;
                            Console.WriteLine(result);
                            break;
                        case "peanuts":
                            result = amount * 1.50;
                            Console.WriteLine(result);
                            break;
                    }
                    break;

                case "varna":
                    switch (product)
                    {
                        case "coffee":
                            result = amount * 0.45;
                            Console.WriteLine(result);
                            break;
                        case "water":
                            result = amount * 0.70;
                            Console.WriteLine(result);
                            break;
                        case "beer":
                            result = amount * 1.10;
                            Console.WriteLine(result);
                            break;
                        case "sweets":
                            result = amount * 1.35;
                            Console.WriteLine(result);
                            break;
                        case "peanuts":
                            result = amount * 1.55;
                            Console.WriteLine(result);
                            break;
                        default:
                            Console.WriteLine("Invalid");
                            break;
                    }
                    break;
            }

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234705

You convert city to lower case, then compare using an upper case initial letter! That will not end well.

i.e. you need

if (city == "sofia")

etc.

Your line by line debugger could be used to verify this. Do spend some time learning how to use that. Debugging more important than being able to type code.

Upvotes: 5

Related Questions