Advertisement
amu2002

student

Nov 20th, 2023 (edited)
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. // @custom:oz-upgrades-unsafe-allow
  2. // @custom:dev-run-script ./contracts/3_student.sol
  3. // SPDX-License-Identifier: Unlicensed
  4. pragma solidity >=0.8.0;
  5.  
  6. contract Student {
  7. struct StudentInfo {
  8. uint256 prn;
  9. string name;
  10. string class;
  11. string department;
  12. }
  13.  
  14. uint256 private prnCounter;
  15. mapping(uint256 => StudentInfo) private studentMap;
  16.  
  17. event StudentAdded(uint256 prn, string name, string class, string department);
  18. event StudentDeleted(uint256 prn);
  19.  
  20. function addStudent(
  21. string memory name,
  22. string memory class,
  23. string memory department
  24. ) public {
  25. prnCounter += 1;
  26. studentMap[prnCounter] = StudentInfo(prnCounter, name, class, department);
  27. emit StudentAdded(prnCounter, name, class, department);
  28. }
  29.  
  30. function getStudent(uint256 _id) public view returns (StudentInfo memory) {
  31. require(studentMap[_id].prn != 0, "Student not found");
  32. return studentMap[_id];
  33. }
  34.  
  35. function totalStudents() public view returns (uint256) {
  36. return prnCounter;
  37. }
  38.  
  39. function deleteStudent(uint256 _prn) public {
  40. require(studentMap[_prn].prn != 0, "Student not found");
  41. delete studentMap[_prn];
  42. emit StudentDeleted(_prn);
  43. }
  44.  
  45. fallback() external {
  46. addStudent("Unknown", "FE", "CSE");
  47. }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement