C# – Basic Exam – Fruit Market

On the 14.04.2014 I have applied for the SoftUni – a Software University in Sofia, Bulgaria. There were 5 C# problems and I managed to resolve completely 3 of them, thus gaining 300 points and being ranked in 278 place out of 1200 applicants. To be honest, I could have opted for more, but I was a little tired already and I was sure that the result will grant me a place as a distance student. Furthermore, the last two problems were with binary calculation and char conversion, which were not exactly my favourite ones.

SoftUni

So, back to the point – in this article I will present the first problem, my solution and the original solution. The problem is a little easy to understand and to code, but it still needed some extra attention to some of the details. The original problem is explained here.

As you probably see, there are just two tricky moments, which you should take care of, in order to pass the 16 automated tests successfully – the output number should be in the format “0.00” and one should be careful about the cases and the if-operators. Anyway, I have spent about an hour on this and at the end I came up with something really terrible – 655 lines of code! Really, something worthy for going to www.govnokod.ru, but as far as I had 100 points I have decided to go further.

Here is my “outstanding” result:

using System;

public class Garden
{
    public static void Main()
    {
        System.Threading.Thread.CurrentThread.CurrentCulture =
            System.Globalization.CultureInfo.InstalledUICulture;

        double bananaCost = 1.8;
        double cucumberCost = 2.75;
        double tomatoCost = 3.20;
        double orangeCost = 1.60;
        double appleCost = 0.86;

        double totalCosts = 0;
        double discount = 0;
        double Item1 = 0;
        double Item2 = 0;
        double Item3 = 0;

        double q1 = 0;
        string product1 = "";

        double q2 = 0;
        string product2 = "";

        double q3 = 0;
        string product3 = "";

        string DayOfTheWeek = Console.ReadLine();
        if (DayOfTheWeek == "Friday")
        {

            q1 = double.Parse(Console.ReadLine());
            product1 = Console.ReadLine();

            switch (product1)
            {
                case "banana":
                    Item1 = q1 * bananaCost;
                    break;
                case "cucumber":
                    Item1 = q1 * cucumberCost;
                    break;
                case "tomato":
                    Item1 = q1 * tomatoCost;
                    break;
                case "orange":
                    Item1 = q1 * orangeCost;
                    break;
                case "apple":
                    Item1 = q1 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q2 = double.Parse(Console.ReadLine());
            product2 = Console.ReadLine();
            switch (product2)
            {
                case "banana":
                    Item2 = q2 * bananaCost;
                    break;
                case "cucumber":
                    Item2 = q2 * cucumberCost;
                    break;
                case "tomato":
                    Item2 = q2 * tomatoCost;
                    break;
                case "orange":
                    Item2 = q2 * orangeCost;
                    break;
                case "apple":
                    Item2 = q2 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q3 = double.Parse(Console.ReadLine());
            product3 = Console.ReadLine();
            switch (product3)
            {
                case "banana":
                    Item3 = q3 * bananaCost;
                    break;
                case "cucumber":
                    Item3 = q3 * cucumberCost;
                    break;
                case "tomato":
                    Item3 = q3 * tomatoCost;
                    break;
                case "orange":
                    Item3 = q3 * orangeCost;
                    break;
                case "apple":
                    Item3 = q3 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }
            totalCosts = Item1 + Item2 + Item3;
            discount = totalCosts * 0.1;
            totalCosts = totalCosts - discount;
        }
        else if (DayOfTheWeek == "Sunday")
        {

            q1 = double.Parse(Console.ReadLine());
            product1 = Console.ReadLine();

            switch (product1)
            {
                case "banana":
                    Item1 = q1 * bananaCost;
                    break;
                case "cucumber":
                    Item1 = q1 * cucumberCost;
                    break;
                case "tomato":
                    Item1 = q1 * tomatoCost;
                    break;
                case "orange":
                    Item1 = q1 * orangeCost;
                    break;
                case "apple":
                    Item1 = q1 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q2 = double.Parse(Console.ReadLine());
            product2 = Console.ReadLine();
            switch (product2)
            {
                case "banana":
                    Item2 = q2 * bananaCost;
                    break;
                case "cucumber":
                    Item2 = q2 * cucumberCost;
                    break;
                case "tomato":
                    Item2 = q2 * tomatoCost;
                    break;
                case "orange":
                    Item2 = q2 * orangeCost;
                    break;
                case "apple":
                    Item2 = q2 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q3 = double.Parse(Console.ReadLine());
            product3 = Console.ReadLine();
            switch (product3)
            {
                case "banana":
                    Item3 = q3 * bananaCost;
                    break;
                case "cucumber":
                    Item3 = q3 * cucumberCost;
                    break;
                case "tomato":
                    Item3 = q3 * tomatoCost;
                    break;
                case "orange":
                    Item3 = q3 * orangeCost;
                    break;
                case "apple":
                    Item3 = q3 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }
            //SUNDAY
            totalCosts = Item1 + Item2 + Item3;
            discount = totalCosts * 0.05;
            totalCosts = totalCosts - discount;
        }
        else if (DayOfTheWeek == "Thursday")
        {

            q1 = double.Parse(Console.ReadLine());
            product1 = Console.ReadLine();

            switch (product1)
            {
                case "banana":
                    Item1 = q1 * bananaCost;
                    discount = 0.3 * q1 * bananaCost;
                    Item1 = Item1 - discount;
                    break;
                case "cucumber":
                    Item1 = q1 * cucumberCost;
                    break;
                case "tomato":
                    Item1 = q1 * tomatoCost;
                    break;
                case "orange":
                    Item1 = q1 * orangeCost;
                    break;
                case "apple":
                    Item1 = q1 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q2 = double.Parse(Console.ReadLine());
            product2 = Console.ReadLine();
            switch (product2)
            {
                case "banana":
                    Item2 = q2 * bananaCost;
                    discount = 0.3 * q2 * bananaCost;
                    Item2 = Item2 - discount;
                    break;
                case "cucumber":
                    Item2 = q2 * cucumberCost;
                    break;
                case "tomato":
                    Item2 = q2 * tomatoCost;
                    break;
                case "orange":
                    Item2 = q2 * orangeCost;
                    break;
                case "apple":
                    Item2 = q2 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q3 = double.Parse(Console.ReadLine());
            product3 = Console.ReadLine();
            switch (product3)
            {
                case "banana":
                    Item3 = q3 * bananaCost;
                    discount = 0.3 * q3 * bananaCost;
                    Item3 = Item3 - discount;
                    break;
                case "cucumber":
                    Item3 = q3 * cucumberCost;
                    break;
                case "tomato":
                    Item3 = q3 * tomatoCost;
                    break;
                case "orange":
                    Item3 = q3 * orangeCost;
                    break;
                case "apple":
                    Item3 = q3 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }
            totalCosts = Item1 + Item2 + Item3;
        }
        else if (DayOfTheWeek == "Wednesday")
        {

            q1 = double.Parse(Console.ReadLine());
            product1 = Console.ReadLine();

            switch (product1)
            {
                case "banana":
                    Item1 = q1 * bananaCost;
                    break;
                case "cucumber":
                    Item1 = q1 * cucumberCost;
                    discount = Item1 * 0.1;
                    Item1 = Item1 - discount;
                    break;
                case "tomato":
                    Item1 = q1 * tomatoCost;
                    discount = Item1 * 0.1;
                    Item1 = Item1 - discount;

                    break;
                case "orange":
                    Item1 = q1 * orangeCost;
                    break;
                case "apple":
                    Item1 = q1 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q2 = double.Parse(Console.ReadLine());
            product2 = Console.ReadLine();
            switch (product2)
            {
                case "banana":
                    Item2 = q2 * bananaCost;
                    break;

                case "cucumber":
                    Item2 = q2 * cucumberCost;
                    discount = Item2 * 0.1;
                    Item2 = Item2 - discount;
                    break;

                case "tomato":
                    Item2 = q2 * tomatoCost;
                    discount = Item2 * 0.1;
                    Item2 = Item2 - discount;
                    break;

                case "orange":
                    Item2 = q2 * orangeCost;
                    break;

                case "apple":
                    Item2 = q2 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q3 = double.Parse(Console.ReadLine());
            product3 = Console.ReadLine();
            switch (product3)
            {
                case "banana":
                    Item3 = q3 * bananaCost;
                    break;
                case "cucumber":
                    Item3 = q3 * cucumberCost;
                    discount = Item3 * 0.1;
                    Item3 = Item3 - discount;

                    break;
                case "tomato":
                    Item3 = q3 * tomatoCost;
                    discount = Item3 * 0.1;
                    Item3 = Item3 - discount;

                    break;
                case "orange":
                    Item3 = q3 * orangeCost;
                    break;
                case "apple":
                    Item3 = q3 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }
            totalCosts = Item1 + Item2 + Item3;
        }
        else if (DayOfTheWeek == "Tuesday")
        {

            q1 = double.Parse(Console.ReadLine());
            product1 = Console.ReadLine();

            switch (product1)
            {
                case "banana":
                    Item1 = q1 * bananaCost;
                    discount = Item1 * 0.2;
                    Item1 = Item1 - discount;

                    break;
                case "cucumber":
                    Item1 = q1 * cucumberCost;
                    break;
                case "tomato":
                    Item1 = q1 * tomatoCost;

                    break;
                case "orange":
                    Item1 = q1 * orangeCost;
                    discount = Item1 * 0.2;
                    Item1 = Item1 - discount;

                    break;
                case "apple":
                    Item1 = q1 * appleCost;
                    discount = Item1 * 0.2;
                    Item1 = Item1 - discount;

                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q2 = double.Parse(Console.ReadLine());
            product2 = Console.ReadLine();
            switch (product2)
            {
                case "banana":
                    Item2 = q2 * bananaCost;
                    discount = Item2 * 0.2;
                    Item2 = Item2 - discount;
                    break;

                case "cucumber":
                    Item2 = q2 * cucumberCost;
                    break;

                case "tomato":
                    Item2 = q2 * tomatoCost;
                    break;

                case "orange":
                    Item2 = q2 * orangeCost;
                    discount = Item2 * 0.2;
                    Item2 = Item2 - discount;

                    break;

                case "apple":
                    Item2 = q2 * appleCost;
                    discount = Item2 * 0.2;
                    Item2 = Item2 - discount;

                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q3 = double.Parse(Console.ReadLine());
            product3 = Console.ReadLine();
            switch (product3)
            {
                case "banana":
                    Item3 = q3 * bananaCost;
                    discount = Item3 * 0.2;
                    Item3 = Item3 - discount;

                    break;
                case "cucumber":
                    Item3 = q3 * cucumberCost;

                    break;
                case "tomato":
                    Item3 = q3 * tomatoCost;

                    break;
                case "orange":
                    Item3 = q3 * orangeCost;

                    discount = Item3 * 0.2;
                    Item3 = Item3 - discount;
                    break;
                case "apple":
                    Item3 = q3 * appleCost;

                    discount = Item3 * 0.2;
                    Item3 = Item3 - discount;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }
            totalCosts = Item1 + Item2 + Item3;
        }
        else if (DayOfTheWeek == "Monday")
        {

            q1 = double.Parse(Console.ReadLine());
            product1 = Console.ReadLine();

            switch (product1)
            {
                case "banana":
                    Item1 = q1 * bananaCost;
                    break;
                case "cucumber":
                    Item1 = q1 * cucumberCost;
                    break;
                case "tomato":
                    Item1 = q1 * tomatoCost;
                    break;
                case "orange":
                    Item1 = q1 * orangeCost;
                    break;
                case "apple":
                    Item1 = q1 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q2 = double.Parse(Console.ReadLine());
            product2 = Console.ReadLine();
            switch (product2)
            {
                case "banana":
                    Item2 = q2 * bananaCost;
                    break;
                case "cucumber":
                    Item2 = q2 * cucumberCost;
                    break;
                case "tomato":
                    Item2 = q2 * tomatoCost;
                    break;
                case "orange":
                    Item2 = q2 * orangeCost;
                    break;
                case "apple":
                    Item2 = q2 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q3 = double.Parse(Console.ReadLine());
            product3 = Console.ReadLine();
            switch (product3)
            {
                case "banana":
                    Item3 = q3 * bananaCost;
                    break;
                case "cucumber":
                    Item3 = q3 * cucumberCost;
                    break;
                case "tomato":
                    Item3 = q3 * tomatoCost;
                    break;
                case "orange":
                    Item3 = q3 * orangeCost;
                    break;
                case "apple":
                    Item3 = q3 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }
            totalCosts = Item1 + Item2 + Item3;

        }
        else if (DayOfTheWeek == "Saturday")
        {

            q1 = double.Parse(Console.ReadLine());
            product1 = Console.ReadLine();

            switch (product1)
            {
                case "banana":
                    Item1 = q1 * bananaCost;
                    break;
                case "cucumber":
                    Item1 = q1 * cucumberCost;
                    break;
                case "tomato":
                    Item1 = q1 * tomatoCost;
                    break;
                case "orange":
                    Item1 = q1 * orangeCost;
                    break;
                case "apple":
                    Item1 = q1 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q2 = double.Parse(Console.ReadLine());
            product2 = Console.ReadLine();
            switch (product2)
            {
                case "banana":
                    Item2 = q2 * bananaCost;
                    break;
                case "cucumber":
                    Item2 = q2 * cucumberCost;
                    break;
                case "tomato":
                    Item2 = q2 * tomatoCost;
                    break;
                case "orange":
                    Item2 = q2 * orangeCost;
                    break;
                case "apple":
                    Item2 = q2 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }

            q3 = double.Parse(Console.ReadLine());
            product3 = Console.ReadLine();
            switch (product3)
            {
                case "banana":
                    Item3 = q3 * bananaCost;
                    break;
                case "cucumber":
                    Item3 = q3 * cucumberCost;
                    break;
                case "tomato":
                    Item3 = q3 * tomatoCost;
                    break;
                case "orange":
                    Item3 = q3 * orangeCost;
                    break;
                case "apple":
                    Item3 = q3 * appleCost;
                    break;
                default:
                    Console.WriteLine("Fuck!");
                    break;
            }
            totalCosts = Item1 + Item2 + Item3;

        }

        Console.WriteLine("{0:F2}", totalCosts);

    }
}

And if you are interesting – here is the original solution from the author of the problem – 61 lines of understandable code… :

using System;

public class FruitMarket
{
    public static void Main()
    {
        string dayOfWeek = Console.ReadLine();
        decimal quantity1 = decimal.Parse(Console.ReadLine());
        string product1 = Console.ReadLine();
        decimal quantity2 = decimal.Parse(Console.ReadLine());
        string product2 = Console.ReadLine();
        decimal quantity3 = decimal.Parse(Console.ReadLine());
        string product3 = Console.ReadLine();

        decimal totalPrice =
            quantity1 * GetPrice(product1, dayOfWeek) +
            quantity2 * GetPrice(product2, dayOfWeek) +
            quantity3 * GetPrice(product3, dayOfWeek);
        Console.WriteLine("{0:f2}", totalPrice);
    }

    static decimal GetPrice(string productName, string dayOfWeek)
    {
        // Assign standard price and check for fruit
        decimal price = 0;
        bool isFruit = false;
        switch (productName)
        {
            case "banana": price = 1.80m; isFruit = true; break;
            case "cucumber": price = 2.75m; break;
            case "tomato": price = 3.20m; break;
            case "orange": price = 1.60m; isFruit = true; break;
            case "apple": price = 0.86m; isFruit = true; break;
        }

        // Apply the daily discounts
        switch (dayOfWeek)
        {
            case "Friday": 
                price = price * 0.90m;
                break;
            case "Sunday": 
                price = price * 0.95m;
                break;
            case "Tuesday": 
                if (isFruit) 
                    price = price * 0.80m;
                break;
            case "Wednesday": 
                if (!isFruit) 
                    price = price * 0.90m;
                    break;
            case "Thursday": 
                if (productName == "banana") 
                    price = price * 0.70m;
                break;
        }

        return price;
    }
}

Anyway, in the other two problems, I had better code 🙂