Advertisement
Guest User

sdf

a guest
May 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 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. using System.IO;
  11.  
  12. namespace api42_Homework2 {
  13. public partial class frmEmployee : Form {
  14. public frmEmployee() {
  15. InitializeComponent();
  16. }
  17.  
  18. private void btnFindEmployee_Click(object sender, EventArgs e) {
  19. string strPath = @"C:\Users\Adrian\Desktop\company.txt";
  20. string[] strArray = File.ReadAllLines(strPath);
  21. int employeeNum;
  22. bool inputExists = false;
  23.  
  24. if (txtEmployee.Text.Trim() == "") return;
  25.  
  26. if (int.TryParse(txtEmployee.Text, out employeeNum)) {
  27. foreach (string employeeStr in strArray) {
  28. string[] employeeDetails = employeeStr.Split(',');
  29.  
  30. if (int.Parse(employeeDetails[0]) == employeeNum) {
  31. lblEmployeeOutput.Text = string.Format("Employee ID: {0}\nFirst Name: {1}\nLast Name: {2}\nDepartment ID: {3}", employeeDetails[0], employeeDetails[2], employeeDetails[3], employeeDetails[1]);
  32. inputExists = true;
  33. }
  34. }
  35. } else {
  36. foreach (string employeeStr in strArray) {
  37. string[] employeeDetails = employeeStr.Split(',');
  38. string[] inputSplit = txtEmployee.Text.Split(' ');
  39.  
  40. if (inputSplit.Length >= 2 && employeeStr.Contains(inputSplit[0]) && employeeStr.Contains(inputSplit[1])) {
  41. lblEmployeeOutput.Text = string.Format("Employee ID: {0}\nFirst Name: {1}\nLast Name: {2}\nDepartment ID: {3}", employeeDetails[0], employeeDetails[2], employeeDetails[3], employeeDetails[1]);
  42. inputExists = true;
  43. }
  44. if (!inputExists) {
  45. lblEmployeeOutput.Text = "Unable to find employee: " + txtEmployee.Text;
  46. }
  47. }
  48. }
  49. }
  50.  
  51. private void btnUpdateDept_Click(object sender, EventArgs e) {
  52. string strPath = @"C:\Users\Adrian\Desktop\company.txt";
  53. string[] strArray = File.ReadAllLines(strPath);
  54. int num;
  55.  
  56. if (!lblEmployeeOutput.Text.Contains("Employee ID:")) return;
  57. if (!int.TryParse(txtNewDeptID.Text, out num) || num < 1 || num > 7){
  58. MessageBox.Show("Please enter a number between 1 and 7 inclusive.");
  59. }
  60.  
  61. string[] employeeDetails = lblEmployeeOutput.Text.Split('\n');
  62.  
  63. for (int i = 0; i < employeeDetails.Length; i++) {
  64. employeeDetails[i] = employeeDetails[i].Split(new string[] { ": " }, StringSplitOptions.None)[1];
  65. }
  66.  
  67. employeeDetails[3] = txtNewDeptID.Text;
  68. using (StreamWriter writer = new StreamWriter(strPath, false)) {
  69. foreach (string line in strArray) {
  70. if (line.Contains(employeeDetails[0])) {
  71. string newLine = string.Format("{0},{1},{2},{3}", employeeDetails[0], employeeDetails[3], employeeDetails[1], employeeDetails[2]);
  72. writer.WriteLine(newLine);
  73. } else {
  74. writer.WriteLine(line);
  75. }
  76. }
  77. }
  78. }
  79.  
  80. private void btnExit_Click(object sender, EventArgs e) {
  81. this.Close();
  82. }
  83.  
  84. private void btnAddEmployee_Click(object sender, EventArgs e) {
  85. string[] employeeNew = {txtEmployeeNum.Text, txtDeptId.Text, txtFirstName.Text, txtLastName.Text};
  86. string strPath = @"C:\Users\Adrian\Desktop\company.txt";
  87. string[] strArray = File.ReadAllLines(strPath);
  88. int num;
  89.  
  90.  
  91. foreach (string newDetail in employeeNew) {
  92. if (newDetail.Trim() == "") {
  93. MessageBox.Show("Please fill out all the fields");
  94. return;
  95. }
  96. }
  97.  
  98. foreach (string line in strArray) {
  99. if (line.Contains(employeeNew[0])) {
  100. MessageBox.Show("This employee number is already in use.");
  101. return;
  102. }
  103. }
  104. if (employeeNew[0].Length != 9) {
  105. MessageBox.Show("Employee numbers must be 9 digits long.");
  106. return;
  107. }
  108. if (!int.TryParse(employeeNew[1], out num) || num < 1 || num > 7) {
  109. MessageBox.Show("Department ID must be between 1 and 7 inclusive.");
  110. return;
  111. }
  112.  
  113. using (StreamWriter writer = new StreamWriter(strPath, false)) {
  114. foreach (string line in strArray) {
  115. writer.WriteLine(line);
  116. }
  117. writer.WriteLine(string.Format("{0},{1},{2},{3}", employeeNew[0], employeeNew[1], employeeNew[2], employeeNew[3]));
  118. }
  119. }
  120.  
  121. private void btnClear_Click(object sender, EventArgs e) {
  122. txtDeptId.Text = "";
  123. txtEmployee.Text = "";
  124. txtEmployeeNum.Text = "";
  125. txtFirstName.Text = "";
  126. txtLastName.Text = "";
  127. txtNewDeptID.Text = "";
  128. lblEmployeeOutput.Text = "";
  129. txtEmployee.Focus();
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement