0
0
Blockchain / Solidityprogramming~30 mins

Enums in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Enums in Blockchain Smart Contracts
📖 Scenario: You are building a simple blockchain smart contract to manage the status of a package delivery. The package can be in one of several states: Pending, Shipped, Delivered, or Returned.Using an enum helps keep track of these states clearly and safely.
🎯 Goal: Create a Solidity smart contract that uses an enum to represent package delivery status. You will set the initial status, update it, and then display the current status.
📋 What You'll Learn
Create an enum called Status with values Pending, Shipped, Delivered, and Returned.
Create a state variable called currentStatus of type Status.
Set the initial currentStatus to Pending.
Write a function to update currentStatus to Shipped.
Write a function to get the current status as a string.
Print the current status after updating it.
💡 Why This Matters
🌍 Real World
Enums help track fixed states like package delivery status, voting phases, or contract stages in blockchain smart contracts.
💼 Career
Understanding enums is essential for blockchain developers to write clear, safe, and maintainable smart contracts.
Progress0 / 4 steps
1
Create the Enum and State Variable
Create an enum called Status with values Pending, Shipped, Delivered, and Returned. Then create a public state variable called currentStatus of type Status and set it to Status.Pending.
Blockchain / Solidity
Need a hint?

Use enum Status { Pending, Shipped, Delivered, Returned } to define the states. Then declare Status public currentStatus = Status.Pending;.

2
Add a Function to Update Status
Add a public function called shipPackage that sets currentStatus to Status.Shipped.
Blockchain / Solidity
Need a hint?

Define function shipPackage() public and inside set currentStatus = Status.Shipped;.

3
Add a Function to Get Status as String
Add a public view function called getStatus that returns a string memory representing the current status. Use if or else if statements to check currentStatus and return the matching string: "Pending", "Shipped", "Delivered", or "Returned".
Blockchain / Solidity
Need a hint?

Use if statements to compare currentStatus with each Status value and return the corresponding string.

4
Print the Current Status
Write a public view function called printStatus that returns the current status string by calling getStatus(). This simulates printing the status.
Blockchain / Solidity
Need a hint?

Create function printStatus() public view returns (string memory) and return the result of getStatus().