Smart Contract to Store Employee Details

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
   }
}

chevron_right


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().




My Personal Notes arrow_drop_up

I am a passionate learner with an innovative mind I am curious about new technologies and always try to learn them and make something productive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.