settingsAccountsettings
Menusettings

Q: Problem solving in C# Task - Dream Item

+5 votes

Trifon has a dream item. He is a good programmer and started working last month, but he needs help with his salary. You are given the month when Trifon is working, the money he is making per hour, number of hours per day he is working and the price of his dream item. Assume February has 28 days and every month has exactly 10 holidays when Trifon is not working. All other months have either 31 or 30 (check a calendar if you’re unsure about the number of days in a given month). Also if Trifon makes more than 700 leva this month, he is promised a bonus of 10% of the total money (e.g. if he makes 800 lv, his bonus will be 80 and the total money he would earn is 880 lv).

Your task is to write a program that calculates whether Trifon can buy his dream item.

Input

The input data consists of one line coming from the console. Check the examples below.

The format is: Month\Money per hour\Hours per day\Price of the item.

The input data will always be valid and in the format described. There is no need to check it explicitly.

Output

  • The output data must be printed on the console.
  • On the only output line you must print whether the item can be bought -
    "Money left = {0} leva." or "Not enough money. {0} leva needed."
  •  The money must be rounded to the second digit after the decimal point.

Constraints

  • Month - "Jan", "Feb", "March", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov" or "Dec".
  • Money per hour - floating number [-7.9 x 1028 … 7.9 x 1028].
  • Hours per day - integer in range [1 to 24].
  • Item price - floating number in range [0... +7.9 x 1028].
  • Allowed memory: 16 MB. Allowed time: 0.1 seconds.

Examples:

dream item c sharp - screenshot 4

dream item c sharp - screenshot 1

dream item c sharp - screenshot 2

dream item c sharp - screenshot 3

asked in C# category by user matthew44
edited by user golearnweb

1 Answer

+2 votes
 
Best answer

These type of tasks are best solvable if they can be devided into smaller and more understandable task - then glue them together :-) Here's what I did with this one:

  1. First you need to read the comments carefully.
  2. You need to present the input as an array and know exactly which element has what data in any particular element (of course parse the numerical data value type)!
  3. Use switch case for the 3 different types of numbers of days in months.
  4. Do the calculations - extract the holidays days, calculate the daily earnings, etc.
  5. With if case add the 10% bonus if the sum is bigger than 700 leva;
  6. Print the output in the console - and use {0:f2} as it is required in the output;
  7. Last but not least - do not forget that you can also have this case: earningsPerMonth == priceOfTheItem

Here is the code itself:

using System;

class DreamItem
{
    static void Main()
    {
        string[] arrayData = Console.ReadLine().Split('\\');
        string month = arrayData[0];
        decimal moneyPerHour = decimal.Parse(arrayData[1]);
        decimal hoursPerDay = decimal.Parse(arrayData[2]);
        decimal priceOfTheItem = decimal.Parse(arrayData[3]);

        decimal workingDays = 0;

        switch (month)
        {
            case "Feb":
                workingDays += 28; break;

            case "Apr":
            case "June":
            case "Sept":
            case "Nov":
                workingDays += 30; break;

            case "Jan":
            case "March":
            case "May":
            case "July":
            case "Aug":
            case "Oct":
            case "Dec":
                workingDays += 31; break;
        }
        decimal workingDaysWihtoutHolidays = workingDays - 10;

        decimal moneyPerDay = moneyPerHour * hoursPerDay;
        decimal earningsPerMonthNoBonus = workingDaysWihtoutHolidays * moneyPerDay;

        decimal earningsPerMonth = 0;
        if (earningsPerMonthNoBonus > 700)
        {
            earningsPerMonth = earningsPerMonthNoBonus + (earningsPerMonthNoBonus * 0.1m);
        }
        else if (earningsPerMonthNoBonus < 700)
        {
            earningsPerMonth = earningsPerMonthNoBonus;
        }

        if (priceOfTheItem > earningsPerMonth)
        {
            Console.WriteLine("Not enough money. {0:f2} leva needed.", priceOfTheItem - earningsPerMonth);
        }
        else if (earningsPerMonth > priceOfTheItem)
        {
            Console.WriteLine("Money left = {0:f2} leva.", earningsPerMonth - priceOfTheItem);
        }
        else if (earningsPerMonth == priceOfTheItem)
        {
            Console.WriteLine("Money left = {0:f2} leva.", earningsPerMonth - priceOfTheItem);
        }
    }
}

 

answered by user eiorgert
edited by user golearnweb
Good job mate! Nicely done!
OH, and this is very important - Split('\\'); the escape case in \\ - they must be 2 - other wise - no splitting of the strings!
...