4
\$\begingroup\$

I'm new here and this is my first simple console application that calculates the number of hours left. There's a lot to improve and if you have any suggestions on what things to add or refactor that would be awesome! It's 486 because it's the usual number of hours an internship has.

The user presses 1 to enter the number of days, and 2 gets the current date. The output shows the accumulated number of hours, the remaining hours and days left before the 486 hours are up.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reuseable
{
    //calculates the accumulated hours, remaining hours, and remaining days, where total hours = 486.
    public static class ReuseableClass
    {
        static int AccumulatedHours(int dayhours, int input)
        {
            int result = dayhours * input;
            Console.WriteLine("Accumulated hours: " + result);
            return result;
        }

        static int RemainingHours(int totalhours, int accumulatedhours)
        {
            int result = totalhours - accumulatedhours;
            Console.Write("Remaining hours: " + result);
            return result;
        }

        static int RemainingDays(int remaininghours, int dayhours)
        {
            int result = remaininghours / dayhours;
            Console.Write("\nRemaining days: " + result);
            return result;
        }

        /// <returns>Number of business days during the 'span'</returns>
        public static double GetBusinessDays(DateTime startD, DateTime endD)
        {

            double calcBusinessDays =
                1 + ((endD - startD).TotalDays * 5 -
                (startD.DayOfWeek - endD.DayOfWeek) * 2) / 7;

            if (endD.DayOfWeek == DayOfWeek.Saturday) calcBusinessDays--;
            if (startD.DayOfWeek == DayOfWeek.Sunday) calcBusinessDays--;

            return calcBusinessDays;
        }

        public static void NumberOne()
        {
            //sets the hours
            int dayHours = 9;
            int totalHours = 486;
            int input;

            Console.WriteLine("\nEnter days");

            bool isNumeric = int.TryParse(Console.ReadLine(), out input);

            if (isNumeric == false) //checks if input is ok or not
            {
                Console.WriteLine("Invalid input");
                Console.ReadLine();
                System.Environment.Exit(1);
            }

            //gets input from user
            int accumHours = AccumulatedHours(dayHours, input);
            int remainingHours = RemainingHours(totalHours, accumHours);
            RemainingDays(remainingHours, dayHours);
        }

        public static void NumberTwo()
        {
            int dayHours = 9;
            int totalHours = 486;

            DateTime startDate = new DateTime(2016, 06, 20);
            DateTime endDate = DateTime.Today;

            int input = Convert.ToInt32(GetBusinessDays(startDate, endDate));

            int accumHours = AccumulatedHours(dayHours, input);
            int remainingHours = RemainingHours(totalHours, accumHours);
            RemainingDays(remainingHours, dayHours);

        }
        public static void Main()
        {
            Console.WriteLine("1. Enter No. of Days \n2. Use current day");
            ConsoleKeyInfo button = Console.ReadKey();
            if (button.Key == ConsoleKey.D1)
            {
                NumberOne();
            }
            else if (button.Key == ConsoleKey.D2)
            {
                NumberTwo();
            }
            else
            {
                Console.WriteLine("Invalid input");
            }

            Console.ReadLine();
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ The title is reasonably descriptive, but this question could use a more fleshed out problem statement in the body. What input does this take? What output should result? Days left? How do you know how many hours are used? \$\endgroup\$ – mdfst13 Aug 26 '16 at 2:34
  • \$\begingroup\$ You must not edit the question when there are any reviews already. I rolled back your changes. \$\endgroup\$ – t3chb0t Aug 26 '16 at 6:19
  • \$\begingroup\$ Please see What should I do when someone answers my question? \$\endgroup\$ – t3chb0t Aug 26 '16 at 6:24
2
\$\begingroup\$

ReuseableClass

Each class is (ok, might be) reusable so naming it like this does not have much value. It calculates something... name it simply IntershipCalculator.

And... it's not really reusable either because you write to the console there. You should write to the console somewhere else and let the methods just calculate the results.

NumberOne

This looks to me like a class but it's a method. You should name it appropriatly ie ReadNumberOne.

        //gets input from user
        int accumHours = AccumulatedHours(dayHours, input);
        int remainingHours = RemainingHours(totalHours, accumHours);
        RemainingDays(remainingHours, dayHours);

The comment is misleading. You are not getting here anything from the user but writing results.


Console.WriteLine("1. Enter No. of Days \n2. Use current day");

Why not like so?

Console.WriteLine("1. Enter No. of Days");
Console.WriteLine("2. Use current day");
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Added your suggestions to the code, thanks a lot. \$\endgroup\$ – Asferisterness Aug 26 '16 at 6:17
1
\$\begingroup\$

Some simple rules are:

  1. Don't ever write the same code twice.
  2. Separate input and output from data processing.
  3. Minimize the use of static functions. In your code the only function that should be static is Main() (this is an arguable opinion ).
  4. Make the code as flexible (reusable) as possible by for instance not hardcode values/properties (dayHours, totalHours etc.).

Below find a solution that follow these rules:

1. The calculator:

  public class InternshipCalculator
  {
    private int m_dayHours;
    private int m_totalHours;

    public InternshipCalculator(int dayHours = 9, int totalHours = 486)
    {
      m_dayHours = dayHours;
      m_totalHours = totalHours;
    }

    public int AccumulatedHours { get; private set; }
    public int RemainingHours { get; private set; }
    public int RemainingDays { get; private set; }

    public void CalculateFromNow(DateTime startDate)
    {
      Calculate(GetBusinessDays(startDate, DateTime.Today));
    }

    public void CalculateFromDays(int days)
    {
      Calculate(days);
    }

    private int GetBusinessDays(DateTime start, DateTime end)
    {
      double calcBusinessDays =
          1 + ((end - start).TotalDays * 5 -
          (start.DayOfWeek - end.DayOfWeek) * 2) / 7;

      if (end.DayOfWeek == DayOfWeek.Saturday) calcBusinessDays--;
      if (start.DayOfWeek == DayOfWeek.Sunday) calcBusinessDays--;

      return Convert.ToInt32(calcBusinessDays);
    }

    private void Calculate(int days)
    {
      AccumulatedHours = m_dayHours * days;
      RemainingHours = m_totalHours - AccumulatedHours;
      RemainingDays = RemainingHours / m_dayHours;
    }

    public override string ToString()
    {
      return $"Accumulated Hours: {AccumulatedHours}\nRemaining Hours: {RemainingHours}\nRemaining Days: {RemainingDays}";
    }
  }

1. The Userinterface:

  public static class Program
  {
    static bool GetOption(out ConsoleKey choise)
    {
      Console.WriteLine("1. Calculate Days.");
      Console.WriteLine("2. Calculate for Current Day.");
      Console.Write("Enter Option: ");
      choise = Console.ReadKey().Key;

      return choise == ConsoleKey.D1 || choise == ConsoleKey.D2;
    }

    public static void Main()
    {
      ConsoleKey choise;

      while (GetOption(out choise))
      {
        InternshipCalculator calculator = new InternshipCalculator();

        switch (choise)
        {
          case ConsoleKey.D1:
            Console.WriteLine();
            Console.Write("Enter days: ");
            int days;
            if (int.TryParse(Console.ReadLine(), out days))
            {
              calculator.CalculateFromDays(days);
            }
            else
            {
              Console.WriteLine("Invalid Input");
              Console.WriteLine();
              continue;
            }
            break;
          case ConsoleKey.D2:
            calculator.CalculateFromNow(new DateTime(2016, 06, 20));
            Console.WriteLine();
            break;

        }

        Console.WriteLine();
        Console.WriteLine("Result: ");
        Console.WriteLine(calculator.ToString());
        Console.WriteLine();
      }

      Console.ReadLine();
    }
  }
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.