resume.sol 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. pragma solidity ^0.4.0;
  2. // 独立实现
  3. contract resume {
  4. // 众筹项目
  5. struct Student {
  6. //学生名
  7. string name;
  8. // 学生编号
  9. string studentNo;
  10. // 捐赠人数
  11. uint recordCount;
  12. // 是否是本校学生
  13. bool nju;
  14. // 学习记录
  15. mapping(uint => Record) records;
  16. // 原本使用 Record[] records 数组定义
  17. }
  18. // 捐赠记录
  19. struct Record {
  20. // 学习记录id
  21. uint id;
  22. // json格式的学习记录,便于扩展,可以自己定义json格式
  23. string json;
  24. // 添加记录时间
  25. uint time;
  26. }
  27. // 学生列表
  28. Student[] students;
  29. // 获取学生数量
  30. function getStudentCount() public view returns (uint) {
  31. return students.length;
  32. }
  33. // 获取学生
  34. function getStudentInfo(uint studentIndex) public view returns (string, string, uint, bool) {
  35. Student storage student = students[studentIndex];
  36. return (student.name, student.studentNo, student.recordCount, student.nju);
  37. }
  38. // 获取众筹捐赠人数
  39. function getRecordCount(uint studentIndex) public view returns (uint) {
  40. return students[studentIndex].recordCount;
  41. }
  42. // 获取学习记录
  43. // 参数:学生编号 记录编号
  44. function getRecordInfo(uint studentIndex, uint recordIndex) public view returns (uint, string) {
  45. Record storage record = students[studentIndex].records[recordIndex];
  46. return (record.id, record.json);
  47. }
  48. // 为自己发起众筹
  49. function addStudent(string name, string studentNo, bool nju) public {
  50. students.push(Student(name, studentNo, 0, nju));
  51. }
  52. // 新增学习记录
  53. function addRecord(uint studentIndex, uint id, string json) public payable {
  54. // 引用拷贝
  55. Student storage student = students[studentIndex];
  56. //require(student.nju); //如果添加这一行表示是南京大学本校学生才可以添加记录
  57. // 更新众筹信息
  58. student.records[student.recordCount++] = Record(id, json, now);
  59. }
  60. // 回退函数 防止抛出异常
  61. // https://solidity.readthedocs.io/en/latest/contracts.html#fallback-function
  62. // if you want your contract to receive Ether, you have to implement a fallback function.
  63. function() public payable { }
  64. }