Skip to content

Commit 9067f37

Browse files
committed
Complete Assignment5 implementation-raef hany
1 parent 0b4935d commit 9067f37

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/Assignment5.sol

+30-10
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,57 @@ pragma solidity ^0.8.0;
33

44
contract Assignment5 {
55
// 1. Declare a public unsigned integer variable called `counter`
6+
uint public counter;
67

78
// 2. Declare a public boolean variable called `isActive`
9+
bool public isActive;
810

911
// 3. Create a public mapping from an address to a string called `userNames`
12+
mapping(address => string) public UserNames;
1013

1114
// 4. Define a struct `User` with fields: `id` (uint), `name` (string)
15+
struct User
16+
{
17+
uint id;
18+
string name;
19+
}
1220

1321
// 5. Create a public array of `User` called `users`
22+
User[] public users;
1423

1524
// Constructor
1625
// Initialize `isActive` to true
1726
constructor() {
1827
// Fill in the logic
19-
28+
isActive = true;
2029
}
2130

2231
// Function to increment the counter
2332
// This function should:
2433
// - Be external
2534
// - Use a for loop to increment `counter` by 1 for 5 iterations
26-
function incrementCounter() {
35+
function incrementCounter() external{
2736
// Fill in the logic
28-
37+
for(uint i = 0; i < 5; i++)
38+
{
39+
counter++;
40+
}
2941
}
3042

3143
// Function to toggle `isActive`
3244
// This function should:
3345
// - Be public
3446
// - Use an if statement to toggle `isActive` between true and false
35-
function toggleActive() {
47+
function toggleActive() public{
3648
// Fill in the logic
37-
49+
if(isActive == false)
50+
{
51+
isActive = true;
52+
}
53+
else if (isActive == true)
54+
{
55+
isActive = false;
56+
}
3857
}
3958

4059
// Function to add a user
@@ -45,21 +64,22 @@ contract Assignment5 {
4564
// The memory keyword will be explained later
4665
// - Add a new user to the `users` array
4766
// - Update the `userNames` mapping with the address and name
48-
function addUser() {
67+
function addUser(uint id, string memory name, address userAdress) public{
4968
// Fill in the logic
50-
69+
User memory user = User(id, name);
70+
users.push(user);
71+
UserNames[userAdress] = name;
5172
// Hint: you have to use the keyword `memory` to define the struct
5273
// For example: User memory user
5374
// This keyword will be explained later
54-
5575
}
5676

5777
// Function to get the total number of users
5878
// This function should:
5979
// - Be public and view
6080
// - Return the length of the `users` array
61-
function getUserCount() {
81+
function getUserCount() public view returns(uint) {
6282
// Fill in the logic
63-
83+
return users.length;
6484
}
6585
}

0 commit comments

Comments
 (0)