Created
February 28, 2024 03:57
-
-
Save fassko/5b7e40bb62a1605163a6bf3fd999fbeb to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.24+commit.e11b9ed9.js&optimize=false&runs=200&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity ^0.8.0; | |
contract NestedMapping { | |
struct Employee { | |
string name; | |
uint256 salary; | |
} | |
mapping(string => mapping(address => Employee)) private employees; | |
function getEmployee(string memory department, address _address) external view returns (Employee memory) { | |
Employee memory employee = employees[department][_address]; | |
require(employee.salary != 0, "Employee does not exist"); | |
return employee; | |
} | |
function addEmployee(string memory department, address _address, string memory name, uint256 salary) external { | |
require(_address != address(0), "Invalid address"); | |
require(bytes(name).length > 0, "Name cannot be empty"); | |
require(employees[department][_address].salary == 0, "Employee already exists"); | |
employees[department][_address] = Employee(name, salary); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment