"Phone Book Contract" Example Version 2

In this version of the phone book, i am making use of a function to verify if the name exists before adding it to the array

what ive learned from this is:
1. Throw vs Revert
2. View vs Pure


pragma solidity ^0.4.0;

contract PhoneBookAresh{
    //Declare a struct similar to C++ 
    struct Person{
        string fullName;
        string number;
    }
    
    //An Array of structs similar to C++ (flexible storage) and index start at 0
    Person[] MyPhoneBook;
    //whos phonebook it is. The Value is the Hex value of the account
    address creator; 
    
    //this is a constructor, notice it has the same name as the Contract name
    function PhoneBookAresh() public {
        creator = msg.sender;
    }
    
    //Adds an item to the Phonebook using PUSH
    function AddPhone(string _fullName, string _number) public
    {
        //if name doesnt exist. still uses GAS to go thru the loop
        if (CheckName(_fullName) == false)
        {
            MyPhoneBook.push(Person(_fullName,_number));
        }
        else
        {
            //revert; will return unused gas whereas throw will continue to consume all available gas.
            revert();
        }
    }
    
    function GetPerson(uint _index) public view returns (string)          
    {
        return MyPhoneBook[_index].fullName;
    }
    
    function CheckName(string _fullName) public view returns (bool)
    {
        uint PhoneBookLength = MyPhoneBook.length;
         //Must be uint and not int
        for (uint i=0; i< PhoneBookLength; i++)
        {
            //As in Java, the == operator does not compare the literals of two strings.
            if (stringsEqual(MyPhoneBook[i].fullName,_fullName))
            {
                return true;
            }
        }
        return false;
    }
    function GetPhoneNumber(string _fullName) public view returns (string)
    {
        uint PhoneBookLength = MyPhoneBook.length;
    
        //Must be uint and not int
        for (uint i=0; i< PhoneBookLength; i++)
        {
            //As in Java, the == operator does not compare the literals of two strings.
            if (stringsEqual(MyPhoneBook[i].fullName,_fullName))
            {
                return MyPhoneBook[i].number;
            }
        }
    }
    
    //Manually comparing strings function used as a utility function
    //source https://github.com/ethereum/dapp-bin/blob/master/library/stringUtils.sol
    
    //View This is generally the replacement for constant. It indicates that the function will not alter the storage state in any way.
    //Pure This is even more restrictive, indicating that it won't even read the storage state.
    function stringsEqual(string storage _a, string memory _b)  internal view returns (bool) {
        bytes storage a = bytes(_a);
        bytes memory b = bytes(_b);
        if (a.length != b.length)
            return false;
        // @todo unroll this loop
        for (uint i = 0; i < a.length; i ++)
            if (a[i] != b[i])
                return false;
        return true;
    }
}

Comments

Popular posts from this blog

Crypto-Currency "Coin Contract" Example

Is Making the Crypto Space Legally Compliant Paving the Road to Mass (Blockchain) Adoption?

Solidity Hash of Structs (Testing hash uniqueness)