pragma solidity ^0.4.23;
contract Owned {
address owner;
function owned() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
}
contract Edureka is Owned {
struct Instructor {
uint age;
bytes name;
bytes subject;
}
mapping (address => Instructor) instructors;
address[] public instructorAccts;
event instructorInfo (
bytes name,
bytes subject,
uint age
);
function setInstructor(address _address, uint _age, bytes _subject, bytes _name) {
**var instructor =instructors[_address];**
instructor.subject = _subject;
instructor.name = _name;
instructor.age = _age;
}
function getInstructor()view public returns(address[]){
return instructorAccts;
}
function getInstructor(address _address)view public returns(uint, bytes, bytes) {
**return(instructors[_address].subject, instructors[_address].name, instructors[_address].age)**
;
}
function countInstructurs() view public returns(uint){
**return instructors.length;**
}
}
Asked
Active
Viewed 32 times
2
Rob Hitchens
- 55,151
- 11
- 89
- 145
Pato Salinas
- 21
- 2
1 Answers
0
This will get you part-way there.
pragma solidity ^0.4.23;
contract Owned {
address owner;
function owned() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
}
contract Edureka is Owned {
struct Instructor {
uint age;
bytes32 name;
bytes32 subject;
}
mapping (address => Instructor) instructors;
address[] public instructorAccts;
event instructorInfo (
bytes name,
bytes subject,
uint age
);
function setInstructor(address _address, uint _age, bytes32 _subject, bytes32 _name) public onlyOwner {
instructors[_address].subject = _subject;
instructors[_address].name = _name;
instructors[_address].age = _age;
}
function getInstructor(address _address)view public returns(bytes32, bytes32, uint) {
return(instructors[_address].subject, instructors[_address].name, instructors[_address].age);
}
}
It's not possible to enumerate the instructors but you can set them an retrieve them. Only the owner (admin) is allowed to set.
For some ideas about extending the capabilities, have a look over here: Are there well-solved and simple storage patterns for Solidity?
Hope it helps.
Rob Hitchens
- 55,151
- 11
- 89
- 145
bytes32.bytesis an array of bytes of any length.bytes32is exactly what it sounds like. Values go in and come out in hex. I left it the way it was to avoid straying too far from the question. I have updated the answer. – Rob Hitchens Sep 10 '18 at 01:48