1 編寫(xiě)智能合約
每個(gè)學(xué)生分別部署合約Student.sol ,保證只有自己可以修改姓名。老師部署合約StudentScore.sol,用于錄入學(xué)生成績(jī),查詢(xún)學(xué)生信息。查詢(xún)學(xué)生信息時(shí),需要調(diào)用學(xué)生部署的合約Student.sol。
student.sol合約,用于學(xué)生對(duì)自己信息進(jìn)行管理。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-465875.html
- 學(xué)生的基本信息作為狀態(tài)變量:
pragma solidity ^0.4.0;
contract Student{
string studentID;
string studentName;
address owner;
}
- 聲明構(gòu)造函數(shù),在構(gòu)造函數(shù)中將owner設(shè)置為調(diào)用者的address:
constructor(string _studentID, string _stdentName) public{
studentID = _studentID;
studentName = _stdentName;
owner = msg.sender;
}
- 聲明函數(shù)修飾器,可以將其加在函數(shù)中,在函數(shù)執(zhí)行前進(jìn)行判斷,來(lái)檢查調(diào)用者是否為學(xué)生本人,只有本人才能調(diào)用該函數(shù):
modifier onlyOwner(){
require(msg.sender == owner,"only owner can call this function");
_;
}
- get和set方法,其中set方法加入了函數(shù)修飾器:
function getStudentIDandName() public constant returns(string,string) {
return (studentID,studentName);
}
function setStudentIDandName(string _studentID,string _studentName) public onlyOwner {
studentID = _studentID;
studentName = _studentName;
}
Teacher.sol合約,用于錄入、查詢(xún)學(xué)生成績(jī)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-465875.html
- 導(dǎo)入Student合約:
pragma solidity ^0.4.11;
import "./Student.sol";
- 學(xué)生的相關(guān)成績(jī)作為狀態(tài)變量,totalScore為全班總成績(jī),studentCount全班學(xué)生數(shù)量,owner部署合約的賬戶(hù)地址。同時(shí)聲明映射studentAddrMap,studentScoreMap,將學(xué)生ID分別映射為學(xué)生地址和學(xué)生成績(jī)。事件studentNotExistsEvent用于存儲(chǔ)學(xué)生ID號(hào)
contract StudentScore{
uint totalScore;
uint studentCount;
address owner;
mapping(string => address) studentAddrMap;
mapping(string => uint) studentScoreMap;
event studentNotExistsEvent(string studentID);
}
- 聲明自定義修飾符onlyOwner,只有老師地址才能執(zhí)行這些函數(shù)。
modifier onlyOwner(){
require(msg.sender == owner,"only owner can call this function");
_;
}
- 函數(shù)addStudentScore,modifyScoreByStudentId分別用于老師添加或修改學(xué)生成績(jī):
function addStudentScore(string studentID, address student, uint score) public onlyOwner {
studentAddrMap[studentID] = student;
studentScoreMap[studentID] = score;
totalScore += score;
studentCount ++;
}
function modifyScoreByStudentId(string studentID, uint score) public onlyOwner{
if(!studentExists(studentID)) {
studentNotExistsEvent(studentID);
return;
}
totalScore -= studentScoreMap[studentID];
studentScoreMap[studentID] = score;
totalScore += score;
}
- 函數(shù)getAvgScore用于查詢(xún)?nèi)鄬W(xué)生平均成績(jī),函數(shù)studentExists用于判斷學(xué)生ID是否存在,函數(shù)getScoreByStudentID用于學(xué)生查詢(xún)自己的成績(jī)。
function getAvgScore() public view returns(uint){
return totalScore / studentCount;
}
function studentExists(string studentID) public view returns(bool){
address student = Student(studentAddrMap[studentID]);
if(student == 0x00){
return false;
}else{
return true;
}
}
function getScoreByStudentID(string studentID) public constant returns(string, string, uint){
if(!studentExists(studentID)) {
studentNotExistsEvent(studentID);
return;
}
Student student = Student(studentAddrMap[studentID]);
var studentName = student.getStudentName();
uint score = studentScoreMap[studentID];
return(studentID, studentName, score);
}
到了這里,關(guān)于以太坊智能合約開(kāi)發(fā)(五):Solidity成績(jī)錄入智能合約實(shí)驗(yàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!