# Calculates the number of hours and days left, where the total number of hours is 486

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;

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)
{

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

}

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");
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 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");
if (button.Key == ConsoleKey.D1)
{
NumberOne();
}
else if (button.Key == ConsoleKey.D2)
{
NumberTwo();
}
else
{
Console.WriteLine("Invalid input");
}

}
}
}

• 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? Aug 26, 2016 at 2:34
• You must not edit the question when there are any reviews already. I rolled back your changes. Aug 26, 2016 at 6:19
• Aug 26, 2016 at 6:24

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");


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)
{
}

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

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

}

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: ");

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;
{
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();
}