Open In App

Smart Contract to Store Employee Details

Last Updated : 26 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Solidity is a high-level language. The structure of smart contracts in solidity is very similar to the structure of classes in object-oriented languages. The solidity file has an extension .sol. 

What are Smart Contracts?

Solidity’s code is encapsulated in contracts which means a contract in Solidity is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. A contract is a fundamental block of building an application on the Ethereum. 

Example: In the below example, we aim at creating a smart contract to demonstrate how to store and retrieve the details of the employees of an organization using structure, arrays, and functions.

Solution:

  1. Create a smart contract, StructDemo having structure Employee with data members as empid, name, department, designation. Create a dynamic array of Employee as emps.
  2. Create a function addEmployee() which takes the data of the employee and stores into a dynamic array called emps.
  3. Create a function getEmployee() which takes the employee id, searches the record in the emps array, and returns the details like name, department, and designation.

Implementation:

Step 1: Open Remix-IDE.

Step 2: Select File Explorer from the left side icons and select Solidity in the environment. Click on New option below the Solidity environment. Enter the file name as StructDemo.sol and Click on the OK button.

Create a new .sol file

Step 3: Enter the following Solidity Code. Select the same solidity version as in your code.

Solidity




// Solidity program 
// to store 
// Employee Details
pragma solidity ^0.6.8;
  
// Creating a Smart Contract
contract StructDemo{
  
   // Structure of employee
   struct Employee{
       
       // State variables
       int empid;
       string name;
       string department;
       string designation;
   }
   
   Employee []emps;
  
   // Function to add 
   // employee details
   function addEmployee(
     int empid, string memory name, 
     string memory department, 
     string memory designation
   ) public{
       Employee memory e
         =Employee(empid,
                   name,
                   department,
                   designation);
       emps.push(e);
   }
  
  // Function to get
  // details of employee
   function getEmployee(
     int empid
   ) public view returns(
     string memory, 
     string memory, 
     string memory){
       uint i;
       for(i=0;i<emps.length;i++)
       {
           Employee memory e
             =emps[i];
           
           // Looks for a matching 
           // employee id
           if(e.empid==empid)
           {
                  return(e.name,
                      e.department,
                      e.designation);
           }
       }
       
     // If provided employee 
     // id is not present
     // it returns Not 
     // Found
     return("Not Found",
            "Not Found",
            "Not Found");
   }
}


Step 4: Compile the file StructDemo.sol from the Solidity Compiler tab.

Compile the code

Step 5: Deploy the smart contract from the Deploy and Run Transaction tab.

Deploy the code

Step 6: Then add the employee details through addEmployee() then after that, you can view details of any employee using employee id through getEmployee().



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads