Skip to content

Instantly share code, notes, and snippets.

@bhavishyagopesh
Created March 17, 2019 00:58
Show Gist options
  • Save bhavishyagopesh/c5fe2296ec77e4f23f3989d000a3ba94 to your computer and use it in GitHub Desktop.
Save bhavishyagopesh/c5fe2296ec77e4f23f3989d000a3ba94 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity 0.5.1;
contract Course {
address admin;
address ManagerContract;
address instructor;
int courseNo;
struct Marks{
int midsem;
int endsem;
int attendance;
}
mapping (int => Marks) student;
mapping (int => bool) isEnrolled;
constructor(int c, address inst, address adm) public {
ManagerContract = msg.sender;
courseNo = c;
instructor = inst;
admin = adm;
}
function kill() public{
//The admin has the right to kill the contract at any time.
//Take care that no one else is able to kill the contract
require(
msg.sender == admin,
"Sender not authorized."
);
address payable tmpaddr = address(uint160(admin));
selfdestruct(tmpaddr);
}
function enroll(int rollNo) public {
//This function can only be called by the ManagerContract
//Enroll a student in the course if not already registered
require(
msg.sender == ManagerContract,
"Sender not authorized."
);
require(
! isEnrolled[rollNo],
"Already enrolled."
);
student[rollNo].midsem = 0;
student[rollNo].endsem = 0;
student[rollNo].attendance = 0;
isEnrolled[rollNo] = true;
}
function markAttendance(int rollNo) public{
//Only the instructor can mark the attendance
//Increment the attendance variable by one
//Make sure the student is enrolled in the course
require(
msg.sender == instructor,
"Sender not authorized."
);
require(
isEnrolled[rollNo],
"Not enrolled."
);
student[rollNo].attendance += 1;
}
function addMidSemMarks(int rollNo, int marks) public{
//Only the instructor can add midsem marks
//Make sure that the student is enrolled in the course
require(
msg.sender == instructor,
"Sender not authorized."
);
require(
isEnrolled[rollNo],
"Not enrolled."
);
student[rollNo].midsem += marks;
}
function addEndSemMarks(int rollNo, int marks) public{
//Only the instructor can add endsem marks
//Make sure that the student is enrolled in the course
require(
msg.sender == instructor,
"Sender not authorized."
);
require(
isEnrolled[rollNo],
"Not enrolled."
);
student[rollNo].endsem += marks;
}
function getMidsemMarks(int rollNo) public view returns(int) {
//Can only be called by the ManagerContract
//return the midSem, endSem and attendance of the student
//Make sure to check the student is enrolled
require(
msg.sender == ManagerContract,
"Sender not authorized."
);
require(
isEnrolled[rollNo],
"Not enrolled."
);
return (student[rollNo].midsem);
}
function getEndsemMarks(int rollNo) public view returns(int) {
//Can only be called by the ManagerContract
//return the midSem, endSem and attendance of the student
//Make sure to check the student is enrolled
require(
msg.sender == ManagerContract,
"Sender not authorized."
);
require(
isEnrolled[rollNo],
"Not enrolled."
);
return (student[rollNo].endsem);
}
function getAttendance(int rollNo) public view returns(int) {
//Can only be called by the ManagerContract
//return the midSem, endSem and attendance of the student
//Make sure to check the student is enrolled
require(
msg.sender == ManagerContract,
"Sender not authorized."
);
require(
isEnrolled[rollNo],
"Not enrolled."
);
return (student[rollNo].attendance);
}
function isEnroll(int rollNo) public view returns(bool){
//Returns if a roll no. is enrolled in a particular course or not
//Can be accessed by anyone
return (isEnrolled[rollNo]);
}
}
pragma solidity 0.5.1;
import "./Course.sol" ;
contract Manager {
//Address of the school administrator
address admin;
mapping (address => int) student;
mapping (address => bool) isStudent;
mapping (int => bool) isCourse;
mapping (int => Course) course;
int rollCount = 19111000;
//Constructor
constructor() public{
admin = msg.sender;
}
function kill() public{
//The admin has the right to kill the contract at any time.
//Take care that no one else is able to kill the contract
require(
msg.sender == admin,
"Sender not authorized."
);
address payable tmpaddr1 = address(uint160(admin));
selfdestruct(tmpaddr1);
}
function addStudent() public{
//Anyone on the network can become a student if not one already
//Remember to assign the new student a unique roll number
require (
!isStudent[msg.sender],
"Already a student."
);
student[msg.sender] = rollCount;
rollCount += 1;
}
function addCourse(int courseNo, address instructor) public{
//Add a new course with course number as courseNo, and instructor at address instructor
//Note that only the admin can add a new course. Also, don't create a new course if course already exists
require(
msg.sender == admin,
"Sender not authorized."
);
require(
!isCourse[courseNo],
"Course already exist."
);
course[courseNo] = new Course(courseNo,instructor, admin);
}
function regCourse(int courseNo) public{
//Register the student in the course if he is a student on roll and the courseNo is valid
require(
isStudent[msg.sender],
"Not a valid student."
);
require(
isCourse[courseNo],
"Not a valid course."
);
// Register
course[courseNo].enroll(student[msg.sender]);
}
function getMyMarks(int courseNo) public view returns(int, int, int) {
//Check the courseNo for validity
//Should only work for valid students of that course
//Returns a tuple (midsem, endsem, attendance)
require(
isCourse[courseNo],
"Not a valid course."
);
require(
course[courseNo].isEnroll(student[msg.sender]),
"Not an enrolled student."
);
int ms = course[courseNo].getMidsemMarks(student[msg.sender]);
int es = course[courseNo].getEndsemMarks(student[msg.sender]);
int at = course[courseNo].getAttendance(student[msg.sender]);
return (ms, es, at);
}
function getMyRollNo() public view returns(int) {
//Utility function to help a student if he/she forgets the roll number
//Should only work for valid students
//Returns roll number as int
require(
isStudent[msg.sender],
"Not a valid student."
);
return (student[msg.sender]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment