Example Solutions: The Time Sheet |
|
A time sheet is a list that holds the time values worked by an employee or a contractor over a time period. In many companies, employees or contractors are usually asked to use a piece of paper on which they should record the necessary information for one or two weeks worth of the time they spent performing a duty. Nowadays, most companies use an electronic means of collecting the time. In an Intranet, employees may have to access an application from a shared drive, open the appropriate document, fill out their time sheet, and submit it. |
A paper time sheet is a physical list of values on a piece of paper. An employee would usually keep the paper that he or she can pick up at any time and modify it various times during the time period. If people from the accounting or payroll department wants to examine the time sheet of one particular employee, t hey would have to call the employee and request it. This is can be difficult or problematic if the employee and the company's accounting are not physically close, which is not uncommon nowadays (it is not surprising anymore for somebody located in New York to work for an employer who resides in San Francisco, or for a contractor in Adelaide to work for a company in Sydney). When the time period is over, such as at the end of the week or at the end of the two-week period, the employee can submit the time sheet. The time sheet is then sent to the accounting or payroll department.
An electronic time sheet is accessed using a computer. Usually, all employees time sheets are stored somewhere in a database on a computer. As stated earlier, an employee can access it any time, so can the accounting or payroll department. This means that, at any time, a supervisor can check the time sheet, for any reason. To make this possible, an application, namely a database is created, stored somewhere in a common computer such as a server, and given access to those who can use it. One of the advantages of using paper time sheet is that, since the employee keeps the time sheet, there is no risk of having a duplicate time sheet. On the other hand, if you create an electronic table of time sheets, when an employee who wants to fill out his or her time sheet opens it, you need to make sure that the right time sheet is opened. In the same way, if somebody from the payroll department wants to check one particular employee's time sheet, you need to make it possible and easy to locate the right time sheet. In the solution we are going to apply, we will create a table of employees and the time sheet they can fill out. In our time sheet, we will create a certain column, named TimeSheetCode, that will hold a unique number. We will come back to the role of this column.
To address our problem of an electronic time, we will create a time sheet in which two pieces of information are required: an employee's number and a starting period. After an employee has opened a time sheet:
After the the employee or contractor has entered a valid employee number and a start date, we will create a number called a time sheet code, represented in the TimeSheet as the TimeSheetCode column. This number is created as follows: 0000000000000 The first 5 digits represent the employee's number. The second 4 digits represent the year of the start date. The next 2 digits represent the month, and the last 2 digits represent the day. This number must be unique so that there would not be a duplicate number throughout the time sheet. To make sure the value of the TimeSheetCode is unique for each record, after the employee has provided a valid employee number and a start date, we will create the time sheet code and check if that number exists in the TimeSheet table already:
|
#pragma once namespace YNBTimeSheet1 { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Data::SqlClient; using namespace System::Drawing; /// <summary> /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } } . . . No Change private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components; bool ValidTimeSheet; bool bNewRecord; String ^ strTimeSheetCode; #pragma region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { . . . No Change } #pragma endregion private: System::Void txtEmployeeNumber_Leave(System::Object^ sender, System::EventArgs^ e) { if( this->txtEmployeeNumber->Text == L"" ) { ValidTimeSheet = false; return; } String ^ strSelect = String::Concat(L"SELECT * FROM Employees WHERE EmployeeNumber = '", this->txtEmployeeNumber->Text, L"';"); SqlConnection ^ conDatabase = gcnew SqlConnection(L"Data Source=(local);Database='ynb1';" L"Integrated Security=true"); SqlCommand ^ cmdDatabase = gcnew SqlCommand(strSelect, conDatabase); DataSet ^dsEmployees = gcnew DataSet(); SqlDataAdapter ^ sda = gcnew SqlDataAdapter(); sda->SelectCommand = cmdDatabase; sda->Fill(dsEmployees); try { DataRow ^ recEmployee = dsEmployees->Tables[0]->Rows[0]; if( recEmployee->IsNull(L"EmployeeNumber") ) { throw gcnew System::IndexOutOfRangeException(L"Bad Employee Number!"); ValidTimeSheet = false; return; } else { String ^ strFullName = dynamic_cast<String ^>(recEmployee[L"FirstName"]) + " " + dynamic_cast<String ^>(recEmployee[L"LastName"]); lblFullName->Text = L"Welcome " + strFullName; ValidTimeSheet = true; } } catch(IndexOutOfRangeException ^) { ValidTimeSheet = false; lblFullName->Text = L""; MessageBox::Show(L"There is no employee with that number!"); txtEmployeeNumber->Text = L""; btnClose->Focus(); } dtpStartDate->Value = DateTime::Today; txtWeek1Monday->Text = L"0.00"; txtWeek1Tuesday->Text = L"0.00"; txtWeek1Wednesday->Text = L"0.00"; txtWeek1Thursday->Text = L"0.00"; txtWeek1Friday->Text = L"0.00"; txtWeek1Saturday->Text = L"0.00"; txtWeek1Sunday->Text = L"0.00"; txtWeek2Monday->Text = L"0.00"; txtWeek2Tuesday->Text = L"0.00"; txtWeek2Wednesday->Text = L"0.00"; txtWeek2Thursday->Text = L"0.00"; txtWeek2Friday->Text = L"0.00"; txtWeek2Saturday->Text = L"0.00"; txtWeek2Sunday->Text = L"0.00"; conDatabase->Close(); } private: System::Void dtpStartDate_CloseUp(System::Object^ sender, System::EventArgs^ e) { lblEndDate->Text = dtpStartDate->Value.AddDays(14).ToString(); if( txtEmployeeNumber->Text->Equals(L"") ) { ValidTimeSheet = false; return; } String ^ strMonth; String ^ strDay; int iMonth; int iDay; DateTime dteStart; dteStart = dtpStartDate->Value; iMonth = dteStart.Month; iDay = dteStart.Day; if( iMonth < 10 ) strMonth = dteStart.Year + "0" + iMonth.ToString(); else strMonth = dteStart.Year + iMonth.ToString(); if( iDay < 10 ) strDay = strMonth + "0" + iDay.ToString(); else strDay = strMonth + iDay.ToString(); strTimeSheetCode = txtEmployeeNumber->Text + strDay; SqlConnection ^ conTimeSheet = nullptr; String ^ strSQL = String::Concat(L"SELECT * FROM dbo.TimeSheets WHERE TimeSheetCode = '", strTimeSheetCode, "';"); conTimeSheet = gcnew SqlConnection(L"Data Source=(local);Database='ynb1';" L"Integrated Security=true"); SqlCommand ^ cmdTimeSheet = gcnew SqlCommand(strSQL, conTimeSheet); DataSet ^dsTimeSheet = gcnew DataSet(L"TimeSheetSet"); SqlDataAdapter ^ sdaTimeSheet = gcnew SqlDataAdapter(); sdaTimeSheet->SelectCommand = cmdTimeSheet; sdaTimeSheet->Fill(dsTimeSheet); conTimeSheet->Close(); try { DataRow ^ recTimeSheet = dsTimeSheet->Tables[0]->Rows[0]; strTimeSheetCode = dynamic_cast<String ^>(recTimeSheet[L"TimeSheetCode"]); if( recTimeSheet->IsNull(L"TimeSheetCode") ) { throw gcnew System::IndexOutOfRangeException( L"No TimeSheet with that number exists!"); bNewRecord = true; return; } else { txtWeek1Monday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week1Monday"]); txtWeek1Tuesday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week1Tuesday"]); txtWeek1Wednesday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week1Wednesday"]); txtWeek1Thursday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week1Thursday"]); txtWeek1Friday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week1Friday"]); txtWeek1Saturday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week1Saturday"]); txtWeek1Sunday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week1Sunday"]); txtWeek2Monday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week2Monday"]); txtWeek2Tuesday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week2Tuesday"]); txtWeek2Wednesday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week2Wednesday"]); txtWeek2Thursday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week2Thursday"]); txtWeek2Friday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week2Friday"]); txtWeek2Saturday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week2Saturday"]); txtWeek2Sunday->Text = dynamic_cast<String ^>(recTimeSheet[L"Week2Sunday"]); bNewRecord = false; } } catch(IndexOutOfRangeException ^) { txtWeek1Monday->Text = L"0.00"; txtWeek1Tuesday->Text = L"0.00"; txtWeek1Wednesday->Text = L"0.00"; txtWeek1Thursday->Text = L"0.00"; txtWeek1Friday->Text = L"0.00"; txtWeek1Saturday->Text = L"0.00"; txtWeek1Sunday->Text = L"0.00"; txtWeek2Monday->Text = L"0.00"; txtWeek2Tuesday->Text = L"0.00"; txtWeek2Wednesday->Text = L"0.00"; txtWeek2Thursday->Text = L"0.00"; txtWeek2Friday->Text = L"0.00"; txtWeek2Saturday->Text = L"0.00"; txtWeek2Sunday->Text = L"0.00"; bNewRecord = true; } } private: System::Void btnTimeSheet_Click(System::Object^ sender, System::EventArgs^ e) { String ^ strTimeSheet = L""; // If this is new record, then create a new time sheet if( bNewRecord == true ) { strTimeSheet = String::Concat(L"INSERT INTO dbo.TimeSheets(TimeSheetCode, ", L"EmployeeNumber, StartDate, Week1Monday, Week1Tuesday, ", L"Week1Wednesday, Week1Thursday, Week1Friday, Week1Saturday, Week1Sunday, ", L"Week2Monday, Week2Tuesday, Week2Wednesday, Week2Thursday, Week2Friday, ", L"Week2Saturday, Week2Sunday, Notes) ", L"VALUES('", strTimeSheetCode, L"', '", txtEmployeeNumber->Text, L"', '", dtpStartDate->Value.ToString(L"MM/dd/yyyy"), L"', '", txtWeek1Monday->Text, L"', '", txtWeek1Tuesday->Text, L"', '", txtWeek1Wednesday->Text, L"', '", txtWeek1Thursday->Text, L"', '", txtWeek1Friday->Text, L"', '", txtWeek1Saturday->Text, L"', '", txtWeek1Sunday->Text, L"', '", txtWeek2Monday->Text, L"', '", txtWeek2Tuesday->Text, L"', '", txtWeek2Wednesday->Text, L"', '", txtWeek2Thursday->Text, L"', '", txtWeek2Friday->Text, L"', '", txtWeek2Saturday->Text, L"', '", txtWeek2Sunday->Text, L"', '", txtNotes->Text, "');"); } // If this is an existing record, then, only update it if( bNewRecord == false ) { strTimeSheet = String::Concat(L"UPDATE TimeSheets SET Week1Monday = '", txtWeek1Monday->Text, L"', Week1Tuesday = '", txtWeek1Tuesday->Text, L"', Week1Wednesday = '", txtWeek1Wednesday->Text, L"', Week1Thursday = '", txtWeek1Thursday->Text, L"', Week1Friday = '", txtWeek1Friday->Text, L"', Week1Saturday = '", txtWeek1Saturday->Text, L"', Week1Sunday = '", txtWeek1Sunday->Text, L"', Week2Monday = '", txtWeek2Monday->Text, L"', Week2Tuesday = '", txtWeek2Tuesday->Text, L"', Week2Wednesday = '", txtWeek2Wednesday->Text, L"', Week2Thursday = '", txtWeek2Thursday->Text, L"', Week2Friday = '", txtWeek2Friday->Text, L"', Week2Saturday = '", txtWeek2Saturday->Text, L"', Week2Sunday = '", txtWeek2Sunday->Text, L"', Notes = '", txtNotes->Text, L"' WHERE TimeSheetCode = '", strTimeSheetCode, L"';"); } if( this->ValidTimeSheet == true ) { SqlConnection ^ conTimeSheet = gcnew SqlConnection(L"Data Source=(local);Database='ynb1';" L"Integrated Security=true"); SqlCommand ^ cmdTimeSheet = gcnew SqlCommand(strTimeSheet, conTimeSheet); conTimeSheet->Open(); cmdTimeSheet->ExecuteNonQuery(); conTimeSheet->Close(); MessageBox::Show(L"Your time sheet has been submitted"); } else { MessageBox::Show(L"The time sheet is not valid\n" L"either you didn't enter a valid employee number, " L"or you didn't a valid start date\n" L"The time sheet will not be saved"); } } private: System::Void btnClose_Click(System::Object^ sender, System::EventArgs^ e) { Close(); } }; }
|
||
Home | Copyright © 2007-2013, FunctionX | |
|