0

im new to Solidity.

I was wondering if it is possible to do something like this in Solidity using enum.

enum Menu {
Burger,
Pizza,
Fries
}

uint price; // storing different prices of different items here

// Assuming that the var userMenu contains the user's selected value from Menu enum 
if (userMenu == Menu.Burger) {
price = 2;
} else if (userMenu == Menu.Pizza){
price = 4;
} else if (userMenu == Menu.Fries) {
price = 1;
}

I know that the initialization of values into price can be done more efficiently. I just want to know if there are any methods to do something like this. Thanks!

1 Answer 1

0

You might try like that:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;

contract Test1 {
    Menu userMenu;

    enum Menu {
        Burger,
        Pizza,
        Fries
    }

    uint price; // storing different prices of different items here

    function setUseMenu(Menu _enumIndex) external {
        userMenu = _enumIndex;
    }

    function setPrice() public {
        // Assuming that the var userMenu contains the user's selected value from Menu enum 
        if (userMenu == Menu.Burger) {
            price = 2;
        } else if (userMenu == Menu.Pizza){
            price = 4;
        } else if (userMenu == Menu.Fries) {
            price = 1;
        }
    }

    function getPrice() external view returns(uint) {
        return price;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.