Advertisement
Naki78

Untitled

Dec 30th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11.  
  12. namespace WorkTime_Calculator
  13. {
  14.     public partial class mainForm : Form
  15.     {
  16.        
  17.         public Form frm2;
  18.         private int [] numLinesPerType= new int[9];
  19.         private int LineCount;
  20.         String strMonthSeparator = "---------------------";
  21.         String strNewWeekSeparator = "<---->";
  22.         public String[] WorkPerDaySupport = new String[10];
  23.         public String[] WorkPerDayTesting = new String[10];
  24.         private int[] intWorkPerDaySupport = new int[10];
  25.         private int[] intWorkPerDayTesting = new int[10];
  26.         private int WeeklySupportMins;
  27.         private int WeeklyTestingMins;
  28.         String[] MonthsRoman = new String[12];
  29.         int curDayOfWeek = 0;
  30.         int intDaysTotal = 0;
  31.         public DateTime dtFirstDay;
  32.         public DateTime dtCurrentDay;
  33.  
  34.         enum lineTypes
  35.         {
  36.             LineEmpty, NewWeekSeparator, NewMonthSeparator, WorkDay, LineSupport, LineTesting, LineUnknown, numLineReserved
  37.         };
  38.         public int CalcDayOfWeek(DateTime CalcDate) // Convert Sunday-start day of Week to Monday
  39.         {
  40.             return ((int)CalcDate.DayOfWeek + 6) % 7;
  41.         }
  42.  
  43.         private void ResetData() // Reset data values for new calculation(s)
  44.         {
  45.             LineCount = 0; //reset Line Counter
  46.             WeeklySupportMins = 0;
  47.             WeeklyTestingMins = 0;
  48.             curDayOfWeek = 0;
  49.             intDaysTotal = 0;
  50.  
  51.             for (int i = 0; i < 9; i++)
  52.             {
  53.                 numLinesPerType[i] = 0;
  54.             }
  55.             for (int i = 0; i < 10; i++)
  56.             {
  57.                 WorkPerDaySupport[i] = "=";
  58.                 WorkPerDayTesting[i] = "=";
  59.                 intWorkPerDaySupport[i] = 0;
  60.                 intWorkPerDayTesting[i] = 0;
  61.             }
  62.                
  63.             return;
  64.         }
  65.         public int Roman_Parse(char[] roman) // Parse Roman numbers into Arabic ones
  66.         {
  67.             Dictionary<char, short> lookup = new Dictionary<char, short>();
  68.             lookup.Add('M', 1000);
  69.             lookup.Add('C', 100);
  70.             lookup.Add('L', 50);
  71.             lookup.Add('X', 10);
  72.             lookup.Add('V', 5);
  73.             lookup.Add('I', 1);
  74.             lookup.Add('m', 1000);
  75.             lookup.Add('c', 100);
  76.             lookup.Add('l', 50);
  77.             lookup.Add('x', 10);
  78.             lookup.Add('v', 5);
  79.             lookup.Add('i', 1);
  80.  
  81.             int arabic = 0;
  82.  
  83.             for (int i = 0; i < roman.Count(); i++)
  84.             {
  85.                 //
  86.                 // return 0 if not valid roman numeral
  87.                 //
  88.                 if (!lookup.ContainsKey(roman[i]))
  89.                     return 0;
  90.  
  91.                 if (i == roman.Count() - 1)
  92.                 {
  93.                     arabic += lookup[roman[i]];
  94.                 }
  95.                 else
  96.                 {
  97.                     if (lookup[roman[i + 1]] > lookup[roman[i]]) arabic -= lookup[roman[i]];
  98.                     else arabic += lookup[roman[i]];
  99.                 }
  100.             }
  101.             return arabic;
  102.         }
  103.         private int CalcTimePerLine(String line) // Calculates minutes per work item (support OR testing)
  104.         {
  105.             int i = 0;
  106.             int spaces = 0;
  107.             line = line.Trim();
  108.             if(line.Substring(line.Length-4)=="mins" || line.Substring(line.Length - 3) == "min")
  109.             {
  110.                 //means line has work in minutes, no need for extra calculation, just locate digits
  111.  
  112.                 i = line.Length - 1;
  113.                 while (true)
  114.                 {
  115.                     if (line.Substring(i, 1) == " ")
  116.                         spaces++;
  117.                     if (spaces == 2)
  118.                         break;
  119.                     i--;
  120.                 }
  121.                 if (Properties.Settings.Default.DEBUG)
  122.                 {
  123.                     MessageBox.Show("Work line detected - " + line.Substring(i + 1, 2).Trim() + " mins");
  124.                     MessageBox.Show("Min or mins detected! [" + line.Substring(i+1, 2) + "] ---- Full line: "+ line);
  125.                 }
  126.                 return Convert.ToInt32(line.Substring(i+1, 2)); // FIXed on 03.I.2016 -- was Substring(i, 2), should be Substring (i+1, 2)
  127.             }
  128.             else if (Char.IsNumber(line, line.Length - 2) && Char.IsNumber(line, line.Length-1) && line.Substring(line.Length - 3, 1) == ":")
  129.             {
  130.                 // means line has work in hour:min--hour:min, must calculate difference
  131.                 // If 2nd time is less than 1st, means it passed midnight
  132.                 DateTime StartWork;
  133.                 DateTime EndWork;
  134.                
  135.                 StartWork = Convert.ToDateTime(line.Substring(line.Length - 11, 5));
  136.                 EndWork = Convert.ToDateTime(line.Substring(line.Length - 5, 5));
  137.                 if (Properties.Settings.Default.DEBUG)
  138.                     MessageBox.Show("Start: " + StartWork.ToShortTimeString() + " --- End: " + EndWork.ToShortTimeString());
  139.  
  140.                 if (EndWork < StartWork)
  141.                 {
  142.                     EndWork = EndWork.AddDays(1);
  143.                     //MessageBox.Show("End of work is past 00:00! "+EndWork.ToShortDateString()+" --- "+ EndWork.ToShortTimeString());
  144.                     //MessageBox.Show("Start of work: " + StartWork.ToShortDateString() + " --- " + StartWork.ToShortTimeString());        
  145.                    
  146.                 }
  147.                
  148.  
  149.                 return (int)(EndWork - StartWork).TotalMinutes;
  150.  
  151.  
  152.             }
  153.             else // Wrong/uncalculatable line detected
  154.             {
  155.                 return -1; // Means could not calculate time
  156.             }
  157.                
  158.            
  159.  
  160.         }
  161.         private bool CleanUpWorkString (int dayOfWeekToClean)
  162.         {
  163.             // Remove trailing + character, add 0 character in case no data
  164.  
  165.             // from Support
  166.             if (WorkPerDaySupport[dayOfWeekToClean].Substring(WorkPerDaySupport[dayOfWeekToClean].Length - 1, 1) == "+")
  167.                 WorkPerDaySupport[dayOfWeekToClean] = WorkPerDaySupport[dayOfWeekToClean].Substring(0, WorkPerDaySupport[dayOfWeekToClean].Length - 1);
  168.             // ..and then from Testing
  169.             if (WorkPerDayTesting[dayOfWeekToClean].Substring(WorkPerDayTesting[dayOfWeekToClean].Length - 1, 1) == "+")
  170.                 WorkPerDayTesting[dayOfWeekToClean] = WorkPerDayTesting[dayOfWeekToClean].Substring(0, WorkPerDayTesting[dayOfWeekToClean].Length - 1);
  171.             // If no data for day, add a zero
  172.             if (WorkPerDaySupport[dayOfWeekToClean] == "=")
  173.                 WorkPerDaySupport[dayOfWeekToClean] += "0";
  174.             if (WorkPerDayTesting[dayOfWeekToClean] == "=")
  175.                 WorkPerDayTesting[dayOfWeekToClean] += "0";
  176.             return true;
  177.         }
  178.         private DateTime CalculateDate(String strDate)
  179.         {
  180.             DateTime resultDate;
  181.             int numDate = 1, numMonth = 1, numYear = 1970;
  182.             int MonthStart = 0;
  183.             int RomanMonthLength = 1;
  184.             int YearStart = 0;
  185.  
  186.  
  187.  
  188.  
  189.             // resultDate = new System.DateTime(numYear, numMonth, numDate);
  190.  
  191.             if (Char.IsNumber(strDate[0]) & Char.IsNumber(strDate[1])) // Two-character date
  192.                 numDate = Convert.ToInt32(strDate.Substring(0, 2));
  193.  
  194.             if (Char.IsNumber(strDate[0]) & strDate.Substring(1, 1) == ".") // One-character date
  195.                 numDate = Convert.ToInt32(strDate.Substring(0, 1));
  196.  
  197.  
  198.             // TODO - check how many Roman numerals/chars in date
  199.             // TODO -- check where Year starts, then set Month and Year
  200.             MonthStart = strDate.IndexOf(".", 0) + 1;
  201.             YearStart = strDate.IndexOf(".", MonthStart + 1) + 1;
  202.             RomanMonthLength = YearStart - MonthStart - 1;
  203.             numYear = Convert.ToInt32(strDate.Substring(YearStart, 2)) + 2000;
  204.             numMonth = Roman_Parse(strDate.Substring(MonthStart, RomanMonthLength).ToCharArray());
  205.  
  206.             resultDate = new System.DateTime(numYear, numMonth, numDate);
  207.             curDayOfWeek =CalcDayOfWeek(resultDate); /// SET exact week day - Monday/Tuesday/etc.
  208.             if (intDaysTotal == 0)
  209.                 dtFirstDay = resultDate; // First day in data detected - set var to it
  210.  
  211.             dtCurrentDay = resultDate;
  212.             if (intDaysTotal > 0)              // Remove Trailing Plus sign -- from Support+Testing
  213.                 CleanUpWorkString(intDaysTotal-1);
  214.            
  215.             intDaysTotal++; // NEW day/date detected, increment DaysTotal counter
  216.             if(Properties.Settings.Default.DEBUG)
  217.                 MessageBox.Show("Date: " + resultDate + " -- Day of week: "+resultDate.DayOfWeek);
  218.            
  219.  
  220.             return resultDate;
  221.         }
  222.        
  223.  
  224.         public mainForm()
  225.         {
  226.             InitializeComponent();
  227.             frm2 = new Form2(this);
  228.         }
  229.         private int ColorLine(int lineNum, Color lineColor)
  230.         {
  231.            
  232.             richTextBox1.SelectionStart = richTextBox1.GetFirstCharIndexFromLine(lineNum);
  233.             richTextBox1.SelectionLength = richTextBox1.Lines[lineNum].Length;
  234.             richTextBox1.SelectionColor = lineColor;
  235.            
  236.             return 1;
  237.         }
  238.         private lineTypes AnalyzeLine(String LineText) // AnalyzeLine type - work/date/empty/week separator/month separator
  239.         {
  240.             // Set default to be Unknown
  241.             lineTypes LineType= lineTypes.LineUnknown;
  242.            
  243.  
  244.             // Attempt to detect Line Type
  245.             if (LineText.Trim() == "")
  246.                 LineType = lineTypes.LineEmpty;
  247.             else if (LineText == strNewWeekSeparator)
  248.                 LineType = lineTypes.NewWeekSeparator;
  249.             else if (LineText == strMonthSeparator)
  250.                 LineType = lineTypes.NewMonthSeparator;
  251.             else if (Char.IsNumber(LineText[0]))
  252.             {
  253.                 if (Char.IsNumber(LineText[1]) | LineText.Substring(1, 1) == ".")
  254.                 {
  255.                     if (LineText.Contains("day"))
  256.                     {
  257.                         //MessageBox.Show("Workday with word day found: " + LineText);
  258.                     };
  259.                     LineType = lineTypes.WorkDay;
  260.                    // MessageBox.Show("Workday found: " + LineText);            
  261.            
  262.                     LineType = lineTypes.WorkDay;
  263.                 }
  264.                 else
  265.                     LineType = lineTypes.LineUnknown;
  266.  
  267.             }
  268.             else if (LineText.Contains("testing") | LineText.Contains("Skype") | LineText.Contains("Asana") && !LineText.Contains("support"))
  269.             {
  270.                 LineType = lineTypes.LineTesting;
  271.             }
  272.             else if (LineText.Contains("support") | LineText.Contains("Slack") | LineText.Contains("work") | LineText.Contains("progress") | LineText.Contains("check"))
  273.                 LineType = lineTypes.LineSupport;
  274.            
  275.             else
  276.                 LineType = lineTypes.LineUnknown;
  277.                
  278.  
  279.  
  280.             numLinesPerType[(int) LineType]++;
  281.             LineCount++;
  282.             return LineType;
  283.         }
  284.  
  285.        
  286.  
  287.         private void btnStart_Click(object sender, EventArgs e)
  288.         {
  289.             String strData;
  290.             String firstLine;
  291.             String lastLine;
  292.             //String strText;
  293.            
  294.  
  295.             int int4thLineChar;
  296.             int i;
  297.  
  298.             ResetData(); // Reset counter and other data to defaults
  299.  
  300.  
  301.            //MessageBox.Show( ((int)lineTypes.LineEmpty).ToString()+" ---- "+ ((int)lineTypes.NewWeekSeparator).ToString());
  302.  
  303.             strData = richTextBox1.Text.Trim();
  304.             if (strData=="") // 1st check - check if empty
  305.             {
  306.                 MessageBox.Show("Textbox with work data can not be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  307.                 return;
  308.             }
  309.  
  310.             // MessageBox.Show("OK for now...");
  311.  
  312.             if(richTextBox1.Lines.Count()<10)
  313.             {
  314.                 MessageBox.Show("Textbox has too few lines. Please add more than 10 lines!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  315.                 return;
  316.  
  317.             }
  318.            
  319.             firstLine = richTextBox1.Lines[0];
  320.             //MessageBox.Show("First line: "+firstLine+" Lines total:" + richTextBox1.Lines.Count());
  321.             lastLine = richTextBox1.Lines[richTextBox1.Lines.Count() - 1];
  322.             if (lastLine.Trim() == "")
  323.                 lastLine = richTextBox1.Lines[richTextBox1.Lines.Count() - 2];
  324.            
  325.             //MessageBox.Show("Last line: "+lastLine +" Lines total:" + richTextBox1.Lines.Count());
  326.             if (firstLine != strNewWeekSeparator && lastLine !=strNewWeekSeparator)
  327.             {
  328.                 MessageBox.Show("New week separator missing! Please check input text.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  329.                 return;
  330.             }
  331.  
  332.             MessageBox.Show("OK for now - text not empty, has 10 or more lines, has new week separator!","All fine", MessageBoxButtons.OK, MessageBoxIcon.Information);
  333.  
  334.             /*
  335.             int length = richTextBox1.TextLength;  // at end of text
  336.             richTextBox1.SelectionStart = 0;
  337.             richTextBox1.SelectionLength = 23;
  338.             richTextBox1.SelectionColor = Color.Red;*/
  339.  
  340.             int4thLineChar = richTextBox1.GetFirstCharIndexFromLine(4);
  341.             for (i = 0; i < richTextBox1.Lines.Count(); i++)
  342.             {
  343.                 lineTypes curLine;
  344.                 curLine=AnalyzeLine(richTextBox1.Lines[i]);
  345.                 if (curLine == lineTypes.WorkDay)
  346.                 {
  347.                     richTextBox1.SelectionStart = richTextBox1.GetFirstCharIndexFromLine(i);
  348.                     richTextBox1.SelectionLength = richTextBox1.Lines[i].Length;
  349.                     System.Drawing.Font currentFont = richTextBox1.SelectionFont;
  350.                     System.Drawing.FontStyle newFontStyle = FontStyle.Bold;
  351.  
  352.                     richTextBox1.SelectionFont = new Font(
  353.                        currentFont.FontFamily,
  354.                        currentFont.Size,
  355.                        newFontStyle
  356.                     );
  357.                     // TODO -- analyze WorkDay line and produce Date
  358.  
  359.                     CalculateDate(richTextBox1.Lines[i]);
  360.                     if (intDaysTotal>7) // TODO - calculate work time for 1/2/3/4+ weeks
  361.                     {
  362.                         MessageBox.Show("Too many work days! Stopping on 7.");
  363.                         break;
  364.                     }
  365.  
  366.                 }
  367.                 else if (curLine == lineTypes.LineEmpty)
  368.                     ;    //do nothing                
  369.                 else if (curLine == lineTypes.NewMonthSeparator | curLine == lineTypes.NewWeekSeparator)
  370.                     ColorLine(i, Color.Navy);
  371.                 else if (curLine == lineTypes.LineTesting || curLine == lineTypes.LineSupport)
  372.                 {
  373.                    
  374.                     // TODO -- Analyze line and calculate Testing time, then add to Date/day work
  375.                     if (curLine == lineTypes.LineTesting)
  376.                         ColorLine(i, Color.Purple);
  377.                     if (curLine == lineTypes.LineSupport)
  378.                         ColorLine(i, Color.Blue);
  379.  
  380.                     int curLineResult;
  381.                     curLineResult = CalcTimePerLine(richTextBox1.Lines[i]);
  382.                     if (curLineResult == -1)
  383.                         MessageBox.Show("Unknown minutes format - cannot calculate!"+"Line says: "+ richTextBox1.Lines[i], "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  384.                     else if(curLine==lineTypes.LineTesting)
  385.                     {
  386.                         // Add work minutes to Integer vars + Add same to Text vars
  387.                         intWorkPerDayTesting[CalcDayOfWeek(dtCurrentDay)] += curLineResult;
  388.                         WorkPerDayTesting[CalcDayOfWeek(dtCurrentDay)] += curLineResult.ToString() + "+";
  389.                     }
  390.                     else if(curLine==lineTypes.LineSupport)
  391.                     {
  392.                         // Add same to proper other Integer vars & Text vars
  393.                         intWorkPerDaySupport[CalcDayOfWeek(dtCurrentDay)] += curLineResult;
  394.                         WorkPerDaySupport[CalcDayOfWeek(dtCurrentDay)] += curLineResult.ToString() + "+";
  395.  
  396.                     }
  397.  
  398.                    
  399.                 }                
  400.                 else if (curLine == lineTypes.LineUnknown)
  401.                 {
  402.                     ColorLine(i, Color.Red);
  403.                     MessageBox.Show("Alert! Unknown line found in text. Line #: " + i.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  404.                 }
  405.  
  406.             }
  407.  
  408.             // Cleanup last day detected
  409.             CleanUpWorkString(CalcDayOfWeek(dtCurrentDay));
  410.  
  411.             //MessageBox.Show("First line starts at: " + richTextBox1.GetFirstCharIndexFromLine(0) + " And 4th line starts at: " + int4thLineChar);
  412.             // ColorLine(0, Color.Red);
  413.             // ColorLine(1, Color.Blue);
  414.             // ColorLine(2, Color.Yellow);
  415.             // ColorLine(3, Color.Green);
  416.             // ColorLine(richTextBox1.Lines.Count()-1, Color.Cyan);
  417.             //MessageBox.Show("Text has: " + richTextBox1.Lines.Count() + " ---- Lines processed: " + LineCount);
  418.             String strResults;
  419.             strResults= "---- Lines detected: "+ LineCount + " ------" + Environment.NewLine + "Empty lines: " + numLinesPerType[(int)lineTypes.LineEmpty] +
  420.                 Environment.NewLine + "New week separators: " + numLinesPerType[(int)lineTypes.NewWeekSeparator] +
  421.                 Environment.NewLine  + "Work days: " + numLinesPerType[(int)lineTypes.WorkDay] +
  422.                 Environment.NewLine + "Support entries: " + numLinesPerType[(int)lineTypes.LineSupport] +
  423.                 Environment.NewLine + "Testing entries: " + numLinesPerType[(int)lineTypes.LineTesting]+ Environment.NewLine +
  424.                 Environment.NewLine + "Unkown entries: " + numLinesPerType[(int)lineTypes.LineUnknown] + Environment.NewLine;
  425.  
  426.             strResults += "Day 1 Support: " + WorkPerDaySupport[0]+ Environment.NewLine;
  427.             strResults += "Day 2 Support: " + WorkPerDaySupport[1]+ Environment.NewLine;
  428.             strResults += "Day 3 same: " + WorkPerDaySupport[2]+ Environment.NewLine;
  429.             strResults += "Day 4 same: " + WorkPerDaySupport[3]+ Environment.NewLine;
  430.             strResults += "Day 5 same: " + WorkPerDaySupport[4]+ Environment.NewLine;
  431.             tbResults.Text = strResults;
  432.  
  433.             this.Text = Application.ProductName + " --- Data from " + dtFirstDay.ToShortDateString() + " to " + dtCurrentDay.ToShortDateString();
  434.             // Show 2nd form, which should read & show calculated data
  435.             frm2.Show();
  436.            
  437.  
  438.         }
  439.  
  440.         private void mainForm_Load(object sender, EventArgs e)
  441.         {
  442.             // Initialize defaults
  443.             MonthsRoman[0] = "I";
  444.             MonthsRoman[1] = "II";
  445.             MonthsRoman[2] = "III";
  446.             MonthsRoman[3] = "IV";
  447.             MonthsRoman[4] = "V";
  448.             MonthsRoman[5] = "VI";
  449.             MonthsRoman[6] = "VII";
  450.             MonthsRoman[7] = "VIII";
  451.             MonthsRoman[8] = "IX";
  452.             MonthsRoman[9] = "X";
  453.             MonthsRoman[10] = "XI";
  454.             MonthsRoman[11] = "XII";
  455.             this.Text = Application.ProductName; // Set form title/caption to show Project name
  456.         }
  457.  
  458.         private void button2_Click(object sender, EventArgs e)
  459.         {
  460.             if (frm2.Visible)
  461.                 frm2.BringToFront();
  462.             else
  463.                 frm2.Show();
  464.             //
  465.            
  466.         }
  467.     }
  468. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement