Aldin_SXR

ElectiveCoursesIBU.sol

Mar 19th, 2023
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.86 KB | Cryptocurrency | 0 0
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.18;
  3.  
  4. import "@openzeppelin/contracts/utils/Strings.sol";
  5.  
  6. contract ElectiveCoursesIBU {
  7.     // Course mappings
  8.     mapping(uint => uint) private courseCapacity;
  9.  
  10.     mapping(uint =>  mapping(address => bool)) private courseStudents;
  11.  
  12.     string[] private courses = [
  13.         "Advanced Web3 Concepts", // 0
  14.         "Introduction to DevOps", // 1
  15.         "Secure Software System Development", // 2
  16.         "Software Engineering", // 3
  17.         "Introduction to Machine Learning", // 4
  18.         "Introduction to Mobile and Wireless Networking", // 5
  19.         "Human-Computer Interaction" // 6
  20.     ];
  21.  
  22.     uint[] private maxCap = [
  23.         35, 45, 75, 75, 105, 35, 40
  24.     ];
  25.  
  26.     function getCourse(uint id) public view returns (string memory) {
  27.         return courses[id];
  28.     }
  29.  
  30.     function getCourseWithCapacity(uint id) public view returns (string memory) {
  31.         return string.concat(courses[id], " (", Strings.toString(courseCapacity[id]), "/", Strings.toString(maxCap[id]), ")");
  32.     }
  33.  
  34.     function registerCourses(address student, uint[] calldata courseIds) external {
  35.         for (uint i = 0; i < courseIds.length; i++) {
  36.             // Check max capacity for a course
  37.             require(courseIds[i] >= 0 && courseIds[i] <= 6, "You are choosing a non-existing course!");
  38.             require(courseCapacity[courseIds[i]] < maxCap[courseIds[i]], string.concat("Course ", getCourse(courseIds[i]), " filled up!"));
  39.             require(courseStudents[courseIds[i]][student] != true, string.concat("You already chose ", getCourse(courseIds[i]), "!"));
  40.            
  41.             courseCapacity[courseIds[i]]++;
  42.             courseStudents[courseIds[i]][student] = true;
  43.         }
  44.     }
  45.  
  46.     function unregisterCourses(address student, uint[] calldata courseIds) external {
  47.         for (uint i = 0; i < courseIds.length; i++) {
  48.             // Check max capacity for a course
  49.             require(courseIds[i] >= 0 && courseIds[i] <= 6, "You are selecting a non-existing course!");
  50.             require(courseStudents[courseIds[i]][student] == true, string.concat("You are not enrolled to ", getCourse(courseIds[i]), "!"));
  51.            
  52.             courseCapacity[courseIds[i]]--;
  53.             courseStudents[courseIds[i]][student] = false;
  54.         }
  55.     }
  56.  
  57.     function getStudentCourses(address student) external view returns (string[] memory) {
  58.         uint size = 0;
  59.         for (uint i = 0; i < 7; i++) {
  60.             if (courseStudents[i][student] == true) {
  61.                 size++;
  62.             }
  63.         }
  64.        
  65.         string[] memory sc = new string[](size);
  66.         uint count = 0;
  67.         for (uint i = 0; i < 7; i++) {
  68.             if (courseStudents[i][student] == true) {
  69.                 sc[count] = getCourse(i);
  70.                 count++;
  71.             }
  72.         }
  73.         return sc;
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment