4

I am developing a university management system using ethereum. So,for that I need an entity "Student" and I have created a smart contract with some basic functionalities. Here is my code snippet for student.sol

pragma solidity ^0.5.0;

contract student
 {
    string private name;
    string private birthdate;
    string private department;
    string private location;
    string private email;
    uint256 private id;
    uint256 private mobile_no;
    uint256 counter=0;

    function setName(string memory myname) public 
    {
        name=myname;
    }
    function getName() public view returns(string memory )
    {
        return name;
    }

     constructor ()public
    {
        id=counter+1;
        counter=counter+1;
    }
    function getid() public view returns(uint256 id1)
    {
        id1=id;
        return id1;
    }

    function setbirthdate(string memory _birthdate) public 
    {
        birthdate = _birthdate;
    }

    function getbirthdate() public view returns(string memory )
    {
        return birthdate;
    }

    function setdepartment(string memory mydepartment) public
    {
        department = mydepartment;   
    }
    function getdepartment() public view returns(string memory )
    {
        return department;
    }
    function setlocation(string memory mylocation) public
    {
        location= mylocation;   
    }
    function getlocation() public view returns(string memory )
    {
        return location;
    }

    function setmobileno(uint256 myphonenumber) public
    {
        mobile_no = myphonenumber;
    }
    function getmobileno() public view  returns(uint256 )
    {
        return mobile_no;
    }


 }

I have used web3.js to interact between the smart contract and web page. Now I want to create an array of student object and use them whenever I need to create a student. How can I do it? Please help me

3

1 Answer 1

5

You can use structs instead and use two functions, one to register all the data for each student and one to read the data. See the code below. The id is used to index the student

pragma solidity 0.5.1;

contract Management{

struct studentInfo {
    string name;
    string birthdate;
    string department;
    string location;
    string email;
    uint256 mobile_no;
}

mapping(uint256 => studentInfo) students;
uint256[] public studentIds;

    function registerStudent(string memory name, string memory birthdate, string memory department, 
                             string memory location, string memory email, uint256 id, uint256 mobile_no) public {
        studentInfo storage newStudent = students[id];
        newStudent.name = name;
        newStudent.birthdate = birthdate;
        newStudent.department = department;
        newStudent.location = location;
        newStudent.email = email;
        newStudent.mobile_no = mobile_no;
        studentIds.push(id);
    }
    
    function getStudent(uint256 id) public view returns (string memory, string memory, string memory, 
                                                    string memory, string memory, uint256){
        studentInfo storage s = students[id];
        return (s.name,s.birthdate,s.department,s.location,s.email,s.mobile_no);
    }
}

#IMPORTANT In Europe saving information of people in the blockchain will likely put you in problems as the data cannot be removed later. This is probably the case in many other parts of the world. In general, avoid putting in the blockchain any data that allows people to identify the users. (Avoid using personal data)

1
  • Thank you for helping me. I am developing the university management system with blockchain. So,please help me in future. Have a goo d day Jaime. Commented Apr 24, 2019 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.