Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WorkTime_Calculator
- {
- public partial class mainForm : Form
- {
- public Form frm2;
- private int [] numLinesPerType= new int[9];
- private int LineCount;
- String strMonthSeparator = "---------------------";
- String strNewWeekSeparator = "<---->";
- public String[] WorkPerDaySupport = new String[10];
- public String[] WorkPerDayTesting = new String[10];
- private int[] intWorkPerDaySupport = new int[10];
- private int[] intWorkPerDayTesting = new int[10];
- private int WeeklySupportMins;
- private int WeeklyTestingMins;
- String[] MonthsRoman = new String[12];
- int curDayOfWeek = 0;
- int intDaysTotal = 0;
- public DateTime dtFirstDay;
- public DateTime dtCurrentDay;
- enum lineTypes
- {
- LineEmpty, NewWeekSeparator, NewMonthSeparator, WorkDay, LineSupport, LineTesting, LineUnknown, numLineReserved
- };
- public int CalcDayOfWeek(DateTime CalcDate) // Convert Sunday-start day of Week to Monday
- {
- return ((int)CalcDate.DayOfWeek + 6) % 7;
- }
- private void ResetData() // Reset data values for new calculation(s)
- {
- LineCount = 0; //reset Line Counter
- WeeklySupportMins = 0;
- WeeklyTestingMins = 0;
- curDayOfWeek = 0;
- intDaysTotal = 0;
- for (int i = 0; i < 9; i++)
- {
- numLinesPerType[i] = 0;
- }
- for (int i = 0; i < 10; i++)
- {
- WorkPerDaySupport[i] = "=";
- WorkPerDayTesting[i] = "=";
- intWorkPerDaySupport[i] = 0;
- intWorkPerDayTesting[i] = 0;
- }
- return;
- }
- public int Roman_Parse(char[] roman) // Parse Roman numbers into Arabic ones
- {
- Dictionary<char, short> lookup = new Dictionary<char, short>();
- lookup.Add('M', 1000);
- lookup.Add('C', 100);
- lookup.Add('L', 50);
- lookup.Add('X', 10);
- lookup.Add('V', 5);
- lookup.Add('I', 1);
- lookup.Add('m', 1000);
- lookup.Add('c', 100);
- lookup.Add('l', 50);
- lookup.Add('x', 10);
- lookup.Add('v', 5);
- lookup.Add('i', 1);
- int arabic = 0;
- for (int i = 0; i < roman.Count(); i++)
- {
- //
- // return 0 if not valid roman numeral
- //
- if (!lookup.ContainsKey(roman[i]))
- return 0;
- if (i == roman.Count() - 1)
- {
- arabic += lookup[roman[i]];
- }
- else
- {
- if (lookup[roman[i + 1]] > lookup[roman[i]]) arabic -= lookup[roman[i]];
- else arabic += lookup[roman[i]];
- }
- }
- return arabic;
- }
- private int CalcTimePerLine(String line) // Calculates minutes per work item (support OR testing)
- {
- int i = 0;
- int spaces = 0;
- line = line.Trim();
- if(line.Substring(line.Length-4)=="mins" || line.Substring(line.Length - 3) == "min")
- {
- //means line has work in minutes, no need for extra calculation, just locate digits
- i = line.Length - 1;
- while (true)
- {
- if (line.Substring(i, 1) == " ")
- spaces++;
- if (spaces == 2)
- break;
- i--;
- }
- if (Properties.Settings.Default.DEBUG)
- {
- MessageBox.Show("Work line detected - " + line.Substring(i + 1, 2).Trim() + " mins");
- MessageBox.Show("Min or mins detected! [" + line.Substring(i+1, 2) + "] ---- Full line: "+ line);
- }
- return Convert.ToInt32(line.Substring(i+1, 2)); // FIXed on 03.I.2016 -- was Substring(i, 2), should be Substring (i+1, 2)
- }
- else if (Char.IsNumber(line, line.Length - 2) && Char.IsNumber(line, line.Length-1) && line.Substring(line.Length - 3, 1) == ":")
- {
- // means line has work in hour:min--hour:min, must calculate difference
- // If 2nd time is less than 1st, means it passed midnight
- DateTime StartWork;
- DateTime EndWork;
- StartWork = Convert.ToDateTime(line.Substring(line.Length - 11, 5));
- EndWork = Convert.ToDateTime(line.Substring(line.Length - 5, 5));
- if (Properties.Settings.Default.DEBUG)
- MessageBox.Show("Start: " + StartWork.ToShortTimeString() + " --- End: " + EndWork.ToShortTimeString());
- if (EndWork < StartWork)
- {
- EndWork = EndWork.AddDays(1);
- //MessageBox.Show("End of work is past 00:00! "+EndWork.ToShortDateString()+" --- "+ EndWork.ToShortTimeString());
- //MessageBox.Show("Start of work: " + StartWork.ToShortDateString() + " --- " + StartWork.ToShortTimeString());
- }
- return (int)(EndWork - StartWork).TotalMinutes;
- }
- else // Wrong/uncalculatable line detected
- {
- return -1; // Means could not calculate time
- }
- }
- private bool CleanUpWorkString (int dayOfWeekToClean)
- {
- // Remove trailing + character, add 0 character in case no data
- // from Support
- if (WorkPerDaySupport[dayOfWeekToClean].Substring(WorkPerDaySupport[dayOfWeekToClean].Length - 1, 1) == "+")
- WorkPerDaySupport[dayOfWeekToClean] = WorkPerDaySupport[dayOfWeekToClean].Substring(0, WorkPerDaySupport[dayOfWeekToClean].Length - 1);
- // ..and then from Testing
- if (WorkPerDayTesting[dayOfWeekToClean].Substring(WorkPerDayTesting[dayOfWeekToClean].Length - 1, 1) == "+")
- WorkPerDayTesting[dayOfWeekToClean] = WorkPerDayTesting[dayOfWeekToClean].Substring(0, WorkPerDayTesting[dayOfWeekToClean].Length - 1);
- // If no data for day, add a zero
- if (WorkPerDaySupport[dayOfWeekToClean] == "=")
- WorkPerDaySupport[dayOfWeekToClean] += "0";
- if (WorkPerDayTesting[dayOfWeekToClean] == "=")
- WorkPerDayTesting[dayOfWeekToClean] += "0";
- return true;
- }
- private DateTime CalculateDate(String strDate)
- {
- DateTime resultDate;
- int numDate = 1, numMonth = 1, numYear = 1970;
- int MonthStart = 0;
- int RomanMonthLength = 1;
- int YearStart = 0;
- // resultDate = new System.DateTime(numYear, numMonth, numDate);
- if (Char.IsNumber(strDate[0]) & Char.IsNumber(strDate[1])) // Two-character date
- numDate = Convert.ToInt32(strDate.Substring(0, 2));
- if (Char.IsNumber(strDate[0]) & strDate.Substring(1, 1) == ".") // One-character date
- numDate = Convert.ToInt32(strDate.Substring(0, 1));
- // TODO - check how many Roman numerals/chars in date
- // TODO -- check where Year starts, then set Month and Year
- MonthStart = strDate.IndexOf(".", 0) + 1;
- YearStart = strDate.IndexOf(".", MonthStart + 1) + 1;
- RomanMonthLength = YearStart - MonthStart - 1;
- numYear = Convert.ToInt32(strDate.Substring(YearStart, 2)) + 2000;
- numMonth = Roman_Parse(strDate.Substring(MonthStart, RomanMonthLength).ToCharArray());
- resultDate = new System.DateTime(numYear, numMonth, numDate);
- curDayOfWeek =CalcDayOfWeek(resultDate); /// SET exact week day - Monday/Tuesday/etc.
- if (intDaysTotal == 0)
- dtFirstDay = resultDate; // First day in data detected - set var to it
- dtCurrentDay = resultDate;
- if (intDaysTotal > 0) // Remove Trailing Plus sign -- from Support+Testing
- CleanUpWorkString(intDaysTotal-1);
- intDaysTotal++; // NEW day/date detected, increment DaysTotal counter
- if(Properties.Settings.Default.DEBUG)
- MessageBox.Show("Date: " + resultDate + " -- Day of week: "+resultDate.DayOfWeek);
- return resultDate;
- }
- public mainForm()
- {
- InitializeComponent();
- frm2 = new Form2(this);
- }
- private int ColorLine(int lineNum, Color lineColor)
- {
- richTextBox1.SelectionStart = richTextBox1.GetFirstCharIndexFromLine(lineNum);
- richTextBox1.SelectionLength = richTextBox1.Lines[lineNum].Length;
- richTextBox1.SelectionColor = lineColor;
- return 1;
- }
- private lineTypes AnalyzeLine(String LineText) // AnalyzeLine type - work/date/empty/week separator/month separator
- {
- // Set default to be Unknown
- lineTypes LineType= lineTypes.LineUnknown;
- // Attempt to detect Line Type
- if (LineText.Trim() == "")
- LineType = lineTypes.LineEmpty;
- else if (LineText == strNewWeekSeparator)
- LineType = lineTypes.NewWeekSeparator;
- else if (LineText == strMonthSeparator)
- LineType = lineTypes.NewMonthSeparator;
- else if (Char.IsNumber(LineText[0]))
- {
- if (Char.IsNumber(LineText[1]) | LineText.Substring(1, 1) == ".")
- {
- if (LineText.Contains("day"))
- {
- //MessageBox.Show("Workday with word day found: " + LineText);
- };
- LineType = lineTypes.WorkDay;
- // MessageBox.Show("Workday found: " + LineText);
- LineType = lineTypes.WorkDay;
- }
- else
- LineType = lineTypes.LineUnknown;
- }
- else if (LineText.Contains("testing") | LineText.Contains("Skype") | LineText.Contains("Asana") && !LineText.Contains("support"))
- {
- LineType = lineTypes.LineTesting;
- }
- else if (LineText.Contains("support") | LineText.Contains("Slack") | LineText.Contains("work") | LineText.Contains("progress") | LineText.Contains("check"))
- LineType = lineTypes.LineSupport;
- else
- LineType = lineTypes.LineUnknown;
- numLinesPerType[(int) LineType]++;
- LineCount++;
- return LineType;
- }
- private void btnStart_Click(object sender, EventArgs e)
- {
- String strData;
- String firstLine;
- String lastLine;
- //String strText;
- int int4thLineChar;
- int i;
- ResetData(); // Reset counter and other data to defaults
- //MessageBox.Show( ((int)lineTypes.LineEmpty).ToString()+" ---- "+ ((int)lineTypes.NewWeekSeparator).ToString());
- strData = richTextBox1.Text.Trim();
- if (strData=="") // 1st check - check if empty
- {
- MessageBox.Show("Textbox with work data can not be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- // MessageBox.Show("OK for now...");
- if(richTextBox1.Lines.Count()<10)
- {
- MessageBox.Show("Textbox has too few lines. Please add more than 10 lines!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- firstLine = richTextBox1.Lines[0];
- //MessageBox.Show("First line: "+firstLine+" Lines total:" + richTextBox1.Lines.Count());
- lastLine = richTextBox1.Lines[richTextBox1.Lines.Count() - 1];
- if (lastLine.Trim() == "")
- lastLine = richTextBox1.Lines[richTextBox1.Lines.Count() - 2];
- //MessageBox.Show("Last line: "+lastLine +" Lines total:" + richTextBox1.Lines.Count());
- if (firstLine != strNewWeekSeparator && lastLine !=strNewWeekSeparator)
- {
- MessageBox.Show("New week separator missing! Please check input text.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- MessageBox.Show("OK for now - text not empty, has 10 or more lines, has new week separator!","All fine", MessageBoxButtons.OK, MessageBoxIcon.Information);
- /*
- int length = richTextBox1.TextLength; // at end of text
- richTextBox1.SelectionStart = 0;
- richTextBox1.SelectionLength = 23;
- richTextBox1.SelectionColor = Color.Red;*/
- int4thLineChar = richTextBox1.GetFirstCharIndexFromLine(4);
- for (i = 0; i < richTextBox1.Lines.Count(); i++)
- {
- lineTypes curLine;
- curLine=AnalyzeLine(richTextBox1.Lines[i]);
- if (curLine == lineTypes.WorkDay)
- {
- richTextBox1.SelectionStart = richTextBox1.GetFirstCharIndexFromLine(i);
- richTextBox1.SelectionLength = richTextBox1.Lines[i].Length;
- System.Drawing.Font currentFont = richTextBox1.SelectionFont;
- System.Drawing.FontStyle newFontStyle = FontStyle.Bold;
- richTextBox1.SelectionFont = new Font(
- currentFont.FontFamily,
- currentFont.Size,
- newFontStyle
- );
- // TODO -- analyze WorkDay line and produce Date
- CalculateDate(richTextBox1.Lines[i]);
- if (intDaysTotal>7) // TODO - calculate work time for 1/2/3/4+ weeks
- {
- MessageBox.Show("Too many work days! Stopping on 7.");
- break;
- }
- }
- else if (curLine == lineTypes.LineEmpty)
- ; //do nothing
- else if (curLine == lineTypes.NewMonthSeparator | curLine == lineTypes.NewWeekSeparator)
- ColorLine(i, Color.Navy);
- else if (curLine == lineTypes.LineTesting || curLine == lineTypes.LineSupport)
- {
- // TODO -- Analyze line and calculate Testing time, then add to Date/day work
- if (curLine == lineTypes.LineTesting)
- ColorLine(i, Color.Purple);
- if (curLine == lineTypes.LineSupport)
- ColorLine(i, Color.Blue);
- int curLineResult;
- curLineResult = CalcTimePerLine(richTextBox1.Lines[i]);
- if (curLineResult == -1)
- MessageBox.Show("Unknown minutes format - cannot calculate!"+"Line says: "+ richTextBox1.Lines[i], "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- else if(curLine==lineTypes.LineTesting)
- {
- // Add work minutes to Integer vars + Add same to Text vars
- intWorkPerDayTesting[CalcDayOfWeek(dtCurrentDay)] += curLineResult;
- WorkPerDayTesting[CalcDayOfWeek(dtCurrentDay)] += curLineResult.ToString() + "+";
- }
- else if(curLine==lineTypes.LineSupport)
- {
- // Add same to proper other Integer vars & Text vars
- intWorkPerDaySupport[CalcDayOfWeek(dtCurrentDay)] += curLineResult;
- WorkPerDaySupport[CalcDayOfWeek(dtCurrentDay)] += curLineResult.ToString() + "+";
- }
- }
- else if (curLine == lineTypes.LineUnknown)
- {
- ColorLine(i, Color.Red);
- MessageBox.Show("Alert! Unknown line found in text. Line #: " + i.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
- }
- }
- // Cleanup last day detected
- CleanUpWorkString(CalcDayOfWeek(dtCurrentDay));
- //MessageBox.Show("First line starts at: " + richTextBox1.GetFirstCharIndexFromLine(0) + " And 4th line starts at: " + int4thLineChar);
- // ColorLine(0, Color.Red);
- // ColorLine(1, Color.Blue);
- // ColorLine(2, Color.Yellow);
- // ColorLine(3, Color.Green);
- // ColorLine(richTextBox1.Lines.Count()-1, Color.Cyan);
- //MessageBox.Show("Text has: " + richTextBox1.Lines.Count() + " ---- Lines processed: " + LineCount);
- String strResults;
- strResults= "---- Lines detected: "+ LineCount + " ------" + Environment.NewLine + "Empty lines: " + numLinesPerType[(int)lineTypes.LineEmpty] +
- Environment.NewLine + "New week separators: " + numLinesPerType[(int)lineTypes.NewWeekSeparator] +
- Environment.NewLine + "Work days: " + numLinesPerType[(int)lineTypes.WorkDay] +
- Environment.NewLine + "Support entries: " + numLinesPerType[(int)lineTypes.LineSupport] +
- Environment.NewLine + "Testing entries: " + numLinesPerType[(int)lineTypes.LineTesting]+ Environment.NewLine +
- Environment.NewLine + "Unkown entries: " + numLinesPerType[(int)lineTypes.LineUnknown] + Environment.NewLine;
- strResults += "Day 1 Support: " + WorkPerDaySupport[0]+ Environment.NewLine;
- strResults += "Day 2 Support: " + WorkPerDaySupport[1]+ Environment.NewLine;
- strResults += "Day 3 same: " + WorkPerDaySupport[2]+ Environment.NewLine;
- strResults += "Day 4 same: " + WorkPerDaySupport[3]+ Environment.NewLine;
- strResults += "Day 5 same: " + WorkPerDaySupport[4]+ Environment.NewLine;
- tbResults.Text = strResults;
- this.Text = Application.ProductName + " --- Data from " + dtFirstDay.ToShortDateString() + " to " + dtCurrentDay.ToShortDateString();
- // Show 2nd form, which should read & show calculated data
- frm2.Show();
- }
- private void mainForm_Load(object sender, EventArgs e)
- {
- // Initialize defaults
- MonthsRoman[0] = "I";
- MonthsRoman[1] = "II";
- MonthsRoman[2] = "III";
- MonthsRoman[3] = "IV";
- MonthsRoman[4] = "V";
- MonthsRoman[5] = "VI";
- MonthsRoman[6] = "VII";
- MonthsRoman[7] = "VIII";
- MonthsRoman[8] = "IX";
- MonthsRoman[9] = "X";
- MonthsRoman[10] = "XI";
- MonthsRoman[11] = "XII";
- this.Text = Application.ProductName; // Set form title/caption to show Project name
- }
- private void button2_Click(object sender, EventArgs e)
- {
- if (frm2.Visible)
- frm2.BringToFront();
- else
- frm2.Show();
- //
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement