Skip to content

Instantly share code, notes, and snippets.

@DapperFox
Created July 19, 2013 04:44
Show Gist options
  • Save DapperFox/6035645 to your computer and use it in GitHub Desktop.
Save DapperFox/6035645 to your computer and use it in GitHub Desktop.
C# - TimeSheet Application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//http://collabedit.com/pg9sd
namespace Lab1
{
class Driver
{
/// <summary>
/// allUsers sets up the list of Users that will contain Work Weeks (hours worked, date information, etc.)
/// </summary>
static List<Users> allUsers = new List<Users>();
/// <summary>
/// Opening menu screen, allows the user to make a selection (add user, add hours, exit etc.)
/// The try catch takes in the user's selections, makes an int coversions, and if the conversion is viable, no exception is thrown
/// Switch statement goes through each option and calls appropriate methods
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
int userSelection = 99;
Console.WriteLine("Time Card Pro");
while (userSelection != 0)
{
Console.WriteLine("1. Add new user");
if (allUsers.Count > 0)
{
Console.WriteLine("2. Select user to add hours");
Console.WriteLine("3. Select user to edit hours");
}
Console.WriteLine("0. Exit the application");
Console.WriteLine("Please make a selection");
try
{
userSelection = Convert.ToInt32(Console.ReadLine());
}
catch (FormatException e)
{
Console.WriteLine("Please input a number");
userSelection = 99;
}
switch (userSelection)
{
case 1:
CreateUser();
break;
case 2:
SelectUsers(true);
break;
case 3:
SelectUsers(false);
break;
case 0: //do nothing
break;
default: Console.WriteLine("Input not recognized. Try again.");
break;
}
}
}
/// <summary>
/// When you are in the add or edit function, you get a list of users(employees) that have
/// been entered into the system. They will be labled 1. 2. 3. etc. Based on the number you enter
/// it will go to that particular user, and you can add/change hours accordingly
/// </summary>
/// <param name="option">true is add hours, false is edit</param>
private static void SelectUsers(bool option)
{
for (int i = 0; i < allUsers.Count; i++)
{
int selectionNum = i + 1;
Console.WriteLine(selectionNum + ". : " + allUsers[i].FirstName + " " + allUsers[i].LastName);
}
Console.WriteLine("Select user via number.");
int userSelection = 0;
try
{
userSelection = Convert.ToInt32(Console.ReadLine());
while (userSelection < 1 || userSelection > allUsers.Count)
{
Console.WriteLine("Input incorrect. Please select a user by number");
userSelection = Convert.ToInt32(Console.ReadLine());
}
if (option)
{
InputHours(allUsers[userSelection - 1]);
}
else
{
EditHours(allUsers[userSelection - 1]);
}
}
catch (FormatException e)
{
SelectUsers(option);
}
}
/// <summary>
/// Allows the user to select an emlpoyee and edit the hours they have previously entered
/// </summary>
private static void EditHours(Users currentUser)
{
if (currentUser.WorkList.Count == 0)
{
Console.WriteLine("Work hours empty. Entering add hours mode.");
InputHours(currentUser);
}
else
{
Console.WriteLine("Select week to change.");
for (int i = 0; i < currentUser.WorkList.Count; i++)
{
int selectionNum = i + 1;
Console.WriteLine(selectionNum + ".: " + currentUser.WorkList[i].WorkedHours.Date);
}
try
{
int selection = Convert.ToInt32(Console.ReadLine()) - 1;
Console.WriteLine("Input new hours: ");
int changedHours = Convert.ToInt32(Console.ReadLine());
currentUser.WorkList[selection].Hours = changedHours;
Console.WriteLine("Hours changed to: " + changedHours);
}
catch (FormatException e)
{
Console.WriteLine("Input incorrect.");
EditHours(currentUser);
}
}
}
/// <summary>
/// InputHours() is called after an Employee has been added to the time sheet application. You are able to select a user from a
/// provided list, enter in the day, month, and year, and then you enter in the hours worked on that day.
/// </summary>
/// <param name="currentUser">Stores and keeps track of the current user to input hours for.</param>
private static void InputHours(Users currentUser)
{
//boolean to check if any of the dates are wrong
bool isIncorrect = true;
while (isIncorrect)
{
isIncorrect = false;
//Has the most issues with exception
Console.WriteLine("Enter the first day of the week (day, month, year) e.g. 24, 02, 2013");
string userInput = Console.ReadLine();
string[] splitDate = userInput.Split(',');
int[] dateArray = new int[3];
for (int i = 0; i < splitDate.Length; i++)
{
try
{
dateArray[i] = Convert.ToInt32(splitDate[i]);
}
catch(FormatException e)
{
break;
}
}
if (dateArray[0] < 1 || dateArray[0] > 31)
{
isIncorrect = true;
Console.WriteLine("Day format incorrect.");
}
if (dateArray[1] < 1 || dateArray[1] > 12)
{
isIncorrect = true;
Console.WriteLine("Month format incorrect");
}
if (dateArray[2] < 1 || dateArray[2] > DateTime.Now.Year + 2)
{
isIncorrect = true;
Console.WriteLine("Year format incorrect");
}
if (!isIncorrect)
{
DateTime weekBegin = new DateTime(dateArray[2], dateArray[1], dateArray[0], 0, 0, 0);
//boolean to check if any are equal to the what has already been recorded
bool isDuplicate = false;
for (int i = 0; i < currentUser.WorkList.Count; i++)
{
if (currentUser.WorkList[i].WorkedHours == weekBegin)
{
isDuplicate = true;
Console.WriteLine("This week has already been entered. Entering edit mode.");
EditHours(currentUser);
}
}
if (!isDuplicate)
{
Console.WriteLine("Enter amount of hours worked: ");
int hoursWorked = -1;
while (hoursWorked == -1)
{
try
{
hoursWorked = Convert.ToInt32(Console.ReadLine());
}
catch (FormatException e)
{
Console.WriteLine("Numbers only please.");
hoursWorked = -1;
}
if (hoursWorked > 168 || hoursWorked < 0)
{
Console.WriteLine("Hours outside of workable range. Try again.");
hoursWorked = -1;
}
}
WorkWeek inputWeek = new WorkWeek(weekBegin, hoursWorked);
currentUser.WorkList.Add(inputWeek);
Console.WriteLine(hoursWorked + " added to " + weekBegin.Day + "/" + weekBegin.Month + "/" + weekBegin.Year);
}
}
}
}
/// <summary>
/// Create's a new user from the information input in the console and add's it to the allUsers list.
/// </summary>
private static void CreateUser()
{
Console.WriteLine("Please input user's First Name");
string fName = Console.ReadLine();
Console.WriteLine("Please input user's Last Name");
string lName = Console.ReadLine();
Console.WriteLine("Please input user's title");
string title = Console.ReadLine();
Users addedUser = new Users(fName, lName, title);
allUsers.Add(addedUser);
Console.WriteLine("User: " + fName + " " + lName + " added.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment