How to Store Employees Data in Structure as Array

This is the best program for storing and learning the different types of data structures. We can use the structures as an array to store the data of multiple employees. We can store the age (int) name (string) grade (char) of one employee in one variable as a group in one structure variable. If we have to store the records of multiple employees in one variable, in this case we will use the array of the structures.

Source Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace employeeDetails
{
class Program
{
//Structure
struct employee
{
public string name;
public int id;
public string Address;
public string phoneNum;
public string status;
public int salary;
public char grade;
};
//Struct Instance
employee[] empInstance;

//Globel variables
int size;

static void Main(string[] args)
{
Console.Write("\t\a\n\n\n"
+ "\t*******************************************************" + "\n"
+ "\t** **" + "\n"
+ "\t** Welcome To **" + "\n"
+ "\t** **" + "\n"
+ "\t** Employee ARRAY and STRUCTURES Demo **" + "\n"
+ "\t** **" + "\n"
+ "\t*******************************************************" + "\n"
+ "\n"
+ "\n");
Program m = new Program();
m.empInstance = new employee[50];
m.size = 1;
Console.Write("\t\t\t\t");
employee_menu(m);
int s = Convert.ToInt32(Console.ReadLine());
return;
}

private static void employee_menu(Program m)
{

int employee_menu_choice;
Console.Write("\t\n" + "\n"
+ "\n" + "\n" + "\n"
+ "\a\t *******************************************************" + "\n"
+ "\t ** **" + "\n"
+ "\t ** You Are In **" + "\n"
+ "\t ** **" + "\n"
+ "\t ** Employee Menu **" + "\n"
+ "\t ** **" + "\n"
+ "\t *******************************************************" + "\n"
+ "\n");


Console.Write("\t\t\t What do you want to do for employee " + "\n"
+ "\t\t ===================================" + "\n"
+ "\n"
+ "\n\n\t 1 => Add employee "
+ "\n\n\t 2 => employee menu Exit \n"
+ "\t=== ------------------ " + "\n"
+ "\t\t\t\tYou Select : ");

employee_menu_choice = Convert.ToInt32(Console.ReadLine());

if (employee_menu_choice > 0)
{
switch (employee_menu_choice)
{
case 1:
{
add_employee(m);//Function
break;
}
case 2:
{
return;
}
default:
{
return;
}
}//end switch
}//end if
else
{
Console.Clear();

}//end else
}

private static void add_employee(Program m)
{
Console.Clear();
Console.Write("\t\n" + "\n" + "\n"
+ "\n" + "\n" + "\n"
+ "\a\t *******************************************************" + "\n"
+ "\t ** **" + "\n"
+ "\t ** Adding Employee **" + "\n"
+ "\t ** **" + "\n"
+ "\t *******************************************************" + "\n");

Console.Write("\t\n"
+ "\t\tCOLLECTING DATA FOR EMPLOYEE NO : " + m.size
+ "\n"
+ "\n\tEnter name : ");

m.empInstance[m.size].name = Console.ReadLine();//taking name

Console.Write("\t \n\tEnter Address : ");
m.empInstance[m.size].Address = Console.ReadLine();//taking Address

Console.Write("\t \n\tEnter Phone Number : ");
m.empInstance[m.size].phoneNum = Console.ReadLine();

Console.Write("\t \n\tEnter Status : ");
m.empInstance[m.size].status = Console.ReadLine();//taking status


Console.Write("\t \n\tEnter Salary : ");
m.empInstance[m.size].salary = Convert.ToInt32(Console.ReadLine());

Console.Write("\t \n\tEnter Grade : ");
m.empInstance[m.size].grade = Convert.ToChar(Console.ReadLine());//taking grade

m.empInstance[m.size].id = m.size;//settind customer id

Console.Write("\t\n"
+ "\tCustomer added with following data" + "\n"
+ "\n"
+ "\tName : " + m.empInstance[m.size].name + "\n"
+ "\tAdress : " + m.empInstance[m.size].Address + "\n"
+ "\tPhone Number : " + m.empInstance[m.size].phoneNum + "\n"
+ "\tGrade : " + m.empInstance[m.size].grade + "\n"
+ "\tSalary : " + m.empInstance[m.size].salary + "\n"
+ "\tStatus : " + m.empInstance[m.size].status + "\n"
+ "\tID : " + m.empInstance[m.size].id + "\n");


m.size++;
}
}
}

Output of the Program

Store Employees Data in Structure as Array

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment