Last active
February 4, 2023 02:39
-
-
Save ginika-chinonso/5a8f3a019ccd96a58f94f400023d5aff to your computer and use it in GitHub Desktop.
Student Record Keeping Smart Contract
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
/* | |
Student record Assignment | |
*/ | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract studentRecords{ | |
address public admin_address; | |
Student[] public student_details; | |
struct Student { | |
string name; | |
uint age; | |
string gender; | |
} | |
constructor (address _admin) { | |
admin_address = _admin; | |
} | |
modifier onlyOwner { | |
require(msg.sender == admin_address, "Only Admin can call this contract"); | |
_; | |
} | |
function store_details(string memory _name,uint8 _age, string memory _gender) onlyOwner external{ | |
Student memory _student = Student({ | |
name: _name, | |
age: _age, | |
gender: _gender | |
}); | |
student_details.push(_student); | |
} | |
function get_name(string memory _name) public view returns(Student memory) { | |
for (uint i = 0; i < student_details.length; i++) { | |
if (keccak256(abi.encodePacked(_name)) == keccak256(abi.encodePacked(student_details[i].name ))){ | |
return student_details[i]; | |
} | |
} | |
} | |
function get_names() public view returns(Student[] memory) { | |
return student_details; | |
} | |
function deleteStudent(string memory _name) public onlyOwner returns (bool) { | |
for (uint i = 0; i < student_details.length; i++) { | |
if (keccak256(abi.encodePacked(_name)) == keccak256(abi.encodePacked(student_details[i].name ))){ | |
student_details[i] = Student({ | |
name: "", | |
age: 0, | |
gender: "" | |
}); | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment