@@ -3,38 +3,57 @@ pragma solidity ^0.8.0;
3
3
4
4
contract Assignment5 {
5
5
// 1. Declare a public unsigned integer variable called `counter`
6
+ uint public counter;
6
7
7
8
// 2. Declare a public boolean variable called `isActive`
9
+ bool public isActive;
8
10
9
11
// 3. Create a public mapping from an address to a string called `userNames`
12
+ mapping (address => string ) public UserNames;
10
13
11
14
// 4. Define a struct `User` with fields: `id` (uint), `name` (string)
15
+ struct User
16
+ {
17
+ uint id;
18
+ string name;
19
+ }
12
20
13
21
// 5. Create a public array of `User` called `users`
22
+ User[] public users;
14
23
15
24
// Constructor
16
25
// Initialize `isActive` to true
17
26
constructor () {
18
27
// Fill in the logic
19
-
28
+ isActive = true ;
20
29
}
21
30
22
31
// Function to increment the counter
23
32
// This function should:
24
33
// - Be external
25
34
// - Use a for loop to increment `counter` by 1 for 5 iterations
26
- function incrementCounter () {
35
+ function incrementCounter () external {
27
36
// Fill in the logic
28
-
37
+ for (uint i = 0 ; i < 5 ; i++ )
38
+ {
39
+ counter++ ;
40
+ }
29
41
}
30
42
31
43
// Function to toggle `isActive`
32
44
// This function should:
33
45
// - Be public
34
46
// - Use an if statement to toggle `isActive` between true and false
35
- function toggleActive () {
47
+ function toggleActive () public {
36
48
// Fill in the logic
37
-
49
+ if (isActive == false )
50
+ {
51
+ isActive = true ;
52
+ }
53
+ else if (isActive == true )
54
+ {
55
+ isActive = false ;
56
+ }
38
57
}
39
58
40
59
// Function to add a user
@@ -45,21 +64,22 @@ contract Assignment5 {
45
64
// The memory keyword will be explained later
46
65
// - Add a new user to the `users` array
47
66
// - Update the `userNames` mapping with the address and name
48
- function addUser () {
67
+ function addUser (uint id , string memory name , address userAdress ) public {
49
68
// Fill in the logic
50
-
69
+ User memory user = User (id, name);
70
+ users.push (user);
71
+ UserNames[userAdress] = name;
51
72
// Hint: you have to use the keyword `memory` to define the struct
52
73
// For example: User memory user
53
74
// This keyword will be explained later
54
-
55
75
}
56
76
57
77
// Function to get the total number of users
58
78
// This function should:
59
79
// - Be public and view
60
80
// - Return the length of the `users` array
61
- function getUserCount () {
81
+ function getUserCount () public view returns ( uint ) {
62
82
// Fill in the logic
63
-
83
+ return users. length ;
64
84
}
65
85
}
0 commit comments