Last active
February 4, 2023 11:20
-
-
Save marvelous-007/e4d2b9de2c9d9ea0ad43b7c76ab9adfb to your computer and use it in GitHub Desktop.
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 | |
// @author Johnson Marvelous | |
// @title Student Records | |
pragma solidity ^0.8.0; | |
contract School { | |
// @notice this data type holds the information of students | |
struct Student { | |
string name; | |
uint32 age; | |
string gender; | |
} | |
// @notice this maps the student to strecord | |
mapping(uint32 => Student) public STRecord; | |
// @notice this holds the address of the admin | |
// @dev only the admin can access the student record | |
address adminAddress; | |
// @notice this stores the number of students in the record | |
uint32 public noOfStudents = 0; | |
// @notice this initialize the address to the admin | |
constructor(address _adminAddress) { | |
adminAddress = _adminAddress; | |
} | |
// @notice this function stores the student details | |
// @params _name is the student name | |
// @params _gender is the gender type of the student | |
// @params _age is the current age of the student | |
// @dev only the admin can strore studentDetails | |
function storeDetails(string memory _name, string memory _gender, uint32 _age) external onlyOwner { | |
Student storage myRecord = STRecord[noOfStudents]; | |
myRecord.name = _name; | |
myRecord.gender = _gender; | |
myRecord.age = _age; | |
noOfStudents += 1; | |
} | |
// @notice this function gets the name, gender and age of a particular student | |
// @params _id is the id of the student | |
// @return this returns a particular student info | |
// @dev everyone has access to the student info | |
function getName(uint32 _id) external view returns(Student memory) { | |
return STRecord[_id]; | |
} | |
// @notice this retricts access to only the owner | |
// @dev the person who deployed the contract is by default the admin | |
modifier onlyOwner() { | |
require(msg.sender == adminAddress, "Only the admin has access to this"); | |
_; | |
} | |
// @notice this function get the names, gender and age of all the student in the student record | |
// @return this returns all the student info | |
// @dev everyone has access to the student info | |
function getNames() external view returns(Student[] memory) { | |
Student[] memory studentList = new Student[](noOfStudents); | |
for (uint32 i = 0; i < noOfStudents; i++) { | |
studentList[i] = STRecord[i]; | |
} | |
return studentList; | |
} | |
// @notice this function removes a student for the student record | |
// @params _id is the id of the student you want to remove | |
// @dev only the admin can remove a student | |
function deleteStudent(uint32 _id) external onlyOwner { | |
Student storage myRecord = STRecord[_id]; | |
myRecord.name = STRecord[noOfStudents - 1].name; | |
myRecord.gender = STRecord[noOfStudents - 1].gender; | |
myRecord.age = STRecord[noOfStudents - 1].age; | |
Student storage myRecordL = STRecord[noOfStudents - 1]; | |
myRecordL.name = ""; | |
myRecordL.gender = ""; | |
myRecordL.age = 0; | |
noOfStudents -= 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment