static void Main(string[] args) { Console.WriteLine("***** Fun with Encapsulation *****\n"); //Employee emp = new Employee("Marvin", 456, 30000, 368925583); //emp.GiveBonus(1000); //emp.DisplayStats(); ////use get/set to interact w object's name //emp.Name="Marv";//can't use () like a method, needs this syntax //Console.WriteLine("Employee is named: {0}", emp.Name); ////reset and then get the name property //emp.Name="Marv"; //Console.WriteLine("Employee is named: {0}", emp.Name); //Employee joe = new Employee(); //joe.Age++; Employee lucas = new Employee("Lucas", 32,234, 45000, 368925583.ToString()); lucas.DisplayStats(); Console.ReadLine(); ////Error! can't access private object members! //emp.empName = "Marv"; }
//Add new employee to the database. public bool addEmployee(Employee employee) { //SQL to add a new employee to the database. string DDL = "insert into Employee(Employee_Forename, Employee_Surname, Employee_Username, Employee_Password, Employee_TelephoneNumber) values('" + employee.getSetEmployeeForename + "','" + employee.getSetEmployeeSurname + "','" + employee.getSetEmployeeUsername + "','" + encryptionController.encryptOrDecrypt(employee.getSetEmployeePassword) + "','" +employee.getSetEmployeeTelephoneNumber + "')"; OleDbCommand command = new OleDbCommand(DDL, database.getSetCon()); //Checks to see if it passes validations. if (validateForename(employee) && validateSurname(employee) && validateUsername(employee) && validatePassword(employee) && validateTelephoneNumber(employee)) { //Checks to see if the username already exists if (checkDuplicateUsername(employee)) { database.runCommand(command); //Executes the DDL and adds the employee. return true; } //Username exists else { return false; } } //Validation failed else { return false; } }
static void Main(string[] args) { Console.WriteLine("**** Fun wiht Encapsulation ****\n"); Employee emp = new Employee("Marvin", 456, 30000); emp.GiveBonus(1000); emp.DisplayStats(); emp.Name = "Marv"; Console.WriteLine("Employee is named: {0}", emp.Name); Employee emp2 = new Employee(); emp2.SetName("Xena the wrairre princess"); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Encapsulation ***** \n"); Employee emp = new Employee("Marvin", 456, 3000); emp.GiveBonus(1000); emp.DisplayStats(); // Set and get the Name property. emp.Name = "Marv"; Console.WriteLine("Employee is named {0}", emp.Name); Employee joe = new Employee(); joe.Age++; Console.WriteLine("Joe's age is {0}", joe.Age); Console.ReadKey(); }
static void Main( string[] args ) { Console.WriteLine("***** Fun with Encapsulation *****\n"); Employee emp = new Employee("Marvin", 456, 30000); emp.GiveBonus(1000); emp.DisplayStats(); // Set and get the Name property. emp.Name = "Marv"; Console.WriteLine("Employee is named: {0}", emp.Name); // Longer than 15 characters! Error will print to console. Employee emp2 = new Employee(); emp2.SetName("Xena the warrior princess"); Console.ReadLine(); }
//Validate employee forename. private bool validateForename(Employee employee) { //Ensures that it the field isn't empty. if (employee.getSetEmployeeForename != "") { //Checks to see the length isn't greater than 30. if (employee.getSetEmployeeForename.Length > 30) { MetroMessageBox.Show(form, "Employee forename can only be 30 characters long.\nPlease refer to the user manual for guidelines.", "Invalid Forename", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } //Length is greater than 30 else { return true; } } //Empty field. else { MetroMessageBox.Show(form, "Enter a forename.\nPlease refer to the user manual for guidelines.", "Invalid Forename", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } }
//Checks to see if the username is unique private bool checkDuplicateUsername(Employee employee) { List<string> usernames = new List<string>(); string DDL = "SELECT employee_username from employee"; //Gets all employee usernames OleDbCommand command = new OleDbCommand(DDL, database.getSetCon()); usernames = database.getEmployeeUsername(command); //Adds all employee usernames to the list. //Loop through each item in the list foreach (string username in usernames) { //Checks to see if any usernames in the list match the username entered. if (username == employee.getSetEmployeeUsername) { //Username exists in the list. MetroMessageBox.Show(form, "Duplicate user name entered. Please enter another username.", "Duplicate username", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } //Username is unique return true; }
//Deletes a certain row in the grid. public void deleteData(Employee employee, int rowIndex) { //Removes the row from the grid. grid.Rows.RemoveAt(rowIndex); //Deletes a row from the employee database. string DDL = "DELETE FROM employee where employee_id = " + employee.getSetEmployeeID; OleDbCommand command = new OleDbCommand(DDL, database.getSetCon()); database.runCommand(command); //Execute the SQL statement }
//Validate employee telephone number. private bool validateTelephoneNumber(Employee employee) { //Ensures that it the field isn't empty. if (employee.getSetEmployeeTelephoneNumber != "") { //Checks to see if the telephone number is 11 digits long.. if(employee.getSetEmployeeTelephoneNumber.Length != 11) { MetroMessageBox.Show(form, "Telephone number has to be 11 digits long. Please re-enter your telephone number.\nPlease refer to the user manual for guidelines.", "Invalid telephone number", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } //Length is exactly 11 digits long else { //Checks to see if it is numbers only. try { Int64.Parse(employee.getSetEmployeeTelephoneNumber); return true; } //Telephone number is not only integers. catch(Exception ex) { MessageBox.Show(ex.Message) ; MetroMessageBox.Show(form, "Telephone number has to be numbers. Please re-enter your telephone number.\nPlease refer to the user manual for guidelines.", "Invalid telephone number", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } } //Empty telephone number else { MetroMessageBox.Show(form, "Enter a telephone number. \nPlease refer to the user manual for guidelines.", "Invalid telephone number", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } }