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;
- using System.IO;
- namespace api42_Homework2 {
- public partial class frmEmployee : Form {
- public frmEmployee() {
- InitializeComponent();
- }
- private void btnFindEmployee_Click(object sender, EventArgs e) {
- string strPath = @"C:\Users\Adrian\Desktop\company.txt";
- string[] strArray = File.ReadAllLines(strPath);
- int employeeNum;
- bool inputExists = false;
- if (txtEmployee.Text.Trim() == "") return;
- if (int.TryParse(txtEmployee.Text, out employeeNum)) {
- foreach (string employeeStr in strArray) {
- string[] employeeDetails = employeeStr.Split(',');
- if (int.Parse(employeeDetails[0]) == employeeNum) {
- lblEmployeeOutput.Text = string.Format("Employee ID: {0}\nFirst Name: {1}\nLast Name: {2}\nDepartment ID: {3}", employeeDetails[0], employeeDetails[2], employeeDetails[3], employeeDetails[1]);
- inputExists = true;
- }
- }
- } else {
- foreach (string employeeStr in strArray) {
- string[] employeeDetails = employeeStr.Split(',');
- string[] inputSplit = txtEmployee.Text.Split(' ');
- if (inputSplit.Length >= 2 && employeeStr.Contains(inputSplit[0]) && employeeStr.Contains(inputSplit[1])) {
- lblEmployeeOutput.Text = string.Format("Employee ID: {0}\nFirst Name: {1}\nLast Name: {2}\nDepartment ID: {3}", employeeDetails[0], employeeDetails[2], employeeDetails[3], employeeDetails[1]);
- inputExists = true;
- }
- if (!inputExists) {
- lblEmployeeOutput.Text = "Unable to find employee: " + txtEmployee.Text;
- }
- }
- }
- }
- private void btnUpdateDept_Click(object sender, EventArgs e) {
- string strPath = @"C:\Users\Adrian\Desktop\company.txt";
- string[] strArray = File.ReadAllLines(strPath);
- int num;
- if (!lblEmployeeOutput.Text.Contains("Employee ID:")) return;
- if (!int.TryParse(txtNewDeptID.Text, out num) || num < 1 || num > 7){
- MessageBox.Show("Please enter a number between 1 and 7 inclusive.");
- }
- string[] employeeDetails = lblEmployeeOutput.Text.Split('\n');
- for (int i = 0; i < employeeDetails.Length; i++) {
- employeeDetails[i] = employeeDetails[i].Split(new string[] { ": " }, StringSplitOptions.None)[1];
- }
- employeeDetails[3] = txtNewDeptID.Text;
- using (StreamWriter writer = new StreamWriter(strPath, false)) {
- foreach (string line in strArray) {
- if (line.Contains(employeeDetails[0])) {
- string newLine = string.Format("{0},{1},{2},{3}", employeeDetails[0], employeeDetails[3], employeeDetails[1], employeeDetails[2]);
- writer.WriteLine(newLine);
- } else {
- writer.WriteLine(line);
- }
- }
- }
- }
- private void btnExit_Click(object sender, EventArgs e) {
- this.Close();
- }
- private void btnAddEmployee_Click(object sender, EventArgs e) {
- string[] employeeNew = {txtEmployeeNum.Text, txtDeptId.Text, txtFirstName.Text, txtLastName.Text};
- string strPath = @"C:\Users\Adrian\Desktop\company.txt";
- string[] strArray = File.ReadAllLines(strPath);
- int num;
- foreach (string newDetail in employeeNew) {
- if (newDetail.Trim() == "") {
- MessageBox.Show("Please fill out all the fields");
- return;
- }
- }
- foreach (string line in strArray) {
- if (line.Contains(employeeNew[0])) {
- MessageBox.Show("This employee number is already in use.");
- return;
- }
- }
- if (employeeNew[0].Length != 9) {
- MessageBox.Show("Employee numbers must be 9 digits long.");
- return;
- }
- if (!int.TryParse(employeeNew[1], out num) || num < 1 || num > 7) {
- MessageBox.Show("Department ID must be between 1 and 7 inclusive.");
- return;
- }
- using (StreamWriter writer = new StreamWriter(strPath, false)) {
- foreach (string line in strArray) {
- writer.WriteLine(line);
- }
- writer.WriteLine(string.Format("{0},{1},{2},{3}", employeeNew[0], employeeNew[1], employeeNew[2], employeeNew[3]));
- }
- }
- private void btnClear_Click(object sender, EventArgs e) {
- txtDeptId.Text = "";
- txtEmployee.Text = "";
- txtEmployeeNum.Text = "";
- txtFirstName.Text = "";
- txtLastName.Text = "";
- txtNewDeptID.Text = "";
- lblEmployeeOutput.Text = "";
- txtEmployee.Focus();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement