forked from OpenSpace100/blockchain-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchoolBaseArray.sol
More file actions
45 lines (33 loc) · 965 Bytes
/
Copy pathSchoolBaseArray.sol
File metadata and controls
45 lines (33 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
pragma solidity ^0.8.0;
contract SchoolBaseArray {
address[] students;
constructor() {
}
function addStudent(address student ) public {
require(!isStudent(student));
students.push(student);
}
function removeStudent(address student) public {
(bool found, uint256 index) = _getStudentIndex(student);
require(found);
for (uint256 i = index; i<students.length; ++i ) {
students[i-1] = students[i];
}
students.pop();
}
function isStudent(address student) public view returns (bool) {
(bool found, ) = _getStudentIndex(student);
return found;
}
function getStudents() public view returns(address[] memory) {
return students;
}
function _getStudentIndex(address student) internal view returns (bool, uint256) {
for (uint256 i = 0; i< students.length; ++i) {
if (student == students[i]) {
return (true, i);
}
}
return (false, 0);
}
}