Created
November 5, 2023 12:16
-
-
Save shubham-kshetre/c16930df601bd1044e1e04c1783d5163 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.18+commit.87f61d96.js&optimize=false&runs=200&gist=
This file contains hidden or 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: MIT | |
pragma solidity ^0.8.0; | |
contract StudentData { | |
struct Student { | |
uint256 id; | |
string name; | |
uint256 age; | |
string major; | |
} | |
Student[] public students; | |
event StudentAdded(uint256 id, string name, uint256 age, string major); | |
// Fallback function to accept ether | |
receive() external payable {} | |
function addStudent(uint256 _id, string memory _name, uint256 _age, string memory _major) public { | |
Student memory newStudent = Student(_id, _name, _age, _major); | |
students.push(newStudent); | |
emit StudentAdded(_id, _name, _age, _major); | |
} | |
function getStudentCount() public view returns (uint256) { | |
return students.length; | |
} | |
function getStudent(uint256 _index) public view returns (uint256 id, string memory name, uint256 age, string memory major) { | |
require(_index < students.length, "Student does not exist"); | |
Student storage student = students[_index]; | |
id = student.id; | |
name = student.name; | |
age = student.age; | |
major = student.major; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment