Smart Contract to Store Employee Details

26 Aug, 2020
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. 
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:
Create a smart contract, StructDemo having structure Employee with data members as empid, name, department, designation. Create a dynamic array of Employee as emps.Create a function addEmployee() which takes the data of the employee and stores into a dynamic array called emps.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.
Create a smart contract, StructDemo having structure Employee with data members as empid, name, department, designation. Create a dynamic array of Employee as emps.
Create a function addEmployee() which takes the data of the employee and stores into a dynamic array called emps.
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.
Step 3: Enter the following Solidity Code. Select the same solidity version as in your code.
Solidity
// Solidity program // to store // Employee Detailspragma solidity ^0.6.8;  // Creating a Smart Contractcontract 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.
Step 5: Deploy the smart contract from the Deploy and Run Transaction tab.
Step 6: Then add the employee details through addEmployee() then after that, you can view details of any employee using employee id through getEmployee().
Solidity
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Solidity - Libraries
Solidity - Programming ERC-20 Token
Solidity - Break and Continue Statements
Solidity - Assembly
Creating Ownable Contracts in Solidity
What is Decentralized Voting Application (DApps)?
Solidity - Special Variables
Solidity - Basics of Contracts
Application Binary Interface(ABI) in Ethereum Virtual Machine
Solidity - Decision Making Statements