Examples of Nested Mapping in Solidity

Nested Mapping 

1.  Manage Ownership Information of Housing Society


Description :

The "NestedMapping" smart contract is a Solidity example that demonstrates the use of nested mappings to manage ownership information of houses within a housing society. The contract allows users to record the ownership details of different apartments within the society, associating each apartment number with the name of its respective owner.

The main components of the contract are as follows:

1. Nested Mappings:

   - "OwnerName" is a nested mapping that uses the address of the housing society as the first key and the uint256 representing apartment numbers as the second key. The mapping stores the name of the owner associated with each apartment number. The structure can be visualized as `mapping (address => mapping (uint256 => string)) public OwnerName`.

2. Dynamic Array:

   - "Society" is a dynamic array that stores the list of apartment numbers (uint256) within the housing society for a given address. It is used to keep track of all the apartments within the society associated with a particular address.

3. Function - "Buy_House":

   - This function allows users to record ownership details when someone buys a house             (apartment) within the society. The function takes two parameters: `_Flat_No` (uint256)       representing the apartment number and `Person_Name` (string) representing the name of the new owner.

   - The function updates the "OwnerName" mapping with the new owner's name for the given apartment number and housing society address.

   - If the apartment number is not already present in the "Society" array associated with the given address, it adds the new apartment number to the array.

4. Function - "getOwnerName":

   - This function allows users to query and retrieve the name of the owner associated with a       specific apartment number in a given housing society. It takes two parameters:                     `Society_adr` (address) representing the address of the housing society and `_flat_no`         (uint256) representing the apartment number.

   - The function returns the name of the owner associated with the provided apartment number and society address.

5. Function - "getNameFromAddress":

   - This function allows users to retrieve a list of owner names associated with all the apartments within a given housing society (address). It takes one parameter:                         `_society_adr` (address) representing the address of the housing society.

   - The function first fetches the list of apartment numbers from the "Society" array associated with the provided housing society address.

   - It then iterates through the apartment numbers, retrieves the corresponding owner names from the "OwnerName" mapping, and stores them in a string array.

   - The function finally returns the array containing all the owner names associated with the apartments in the given society.

6. Function - "contains":

   - This private helper function checks whether a given apartment number exists within a          list of apartment numbers. It takes two parameters: `list` (uint256[]) representing the list of apartment numbers and `_Flat_No` (uint256) representing the apartment number to check.

   - The function iterates through the list and returns true if it finds a match; otherwise, it returns false.


Overall, this smart contract demonstrates a simple yet effective way to manage ownership information within a housing society using nested mappings and dynamic arrays, providing easy access to owner details based on the apartment number and society address.


2. Manage Ownership Information of a Housing Society



The "NestedMapping2" smart contract is a Solidity example that demonstrates the use of nested mappings to manage student information in an educational institute. The contract allows the storage and retrieval of unique student IDs based on their first names and last names. The contract also maintains separate lists of last names and first names for each unique entry, providing easy access to all students sharing the same first or last name.


The main components of the contract are as follows:

1. Nested Mappings:

   - "student_id" is a nested mapping that uses the first name (string) as the first key and the last name (string) as the second key. The mapping stores a uint256 value representing the unique student ID for each student.

   - The structure can be visualized as `mapping (string => mapping (string => uint256))          public student_id`.


2. Dynamic Arrays:

   - "last_names" is a dynamic array mapping that stores the list of last names (string)                 associated with each unique first name. This mapping is used to keep track of all the last names for a given first name.

   - "first_names" is another dynamic array mapping that stores the list of first names                 (string) associated with each unique last name. This mapping is used to keep track of all the first names for a given last name.


3. Student ID Assignment:

   - The contract has an integer variable "id" that is initialized to 1. This variable is used to assign unique student IDs to each new student added to the "student_id" mapping.


4. Function - "getStudent_id":

   - This function allows users to query and retrieve the unique student ID associated with a      particular student based on their first name and last name. It takes two parameters:                 `first_name` (string) and `last_name` (string).

   - The function returns the student ID for the provided first name and last name from the         "student_id" mapping.

5. Function - "setStudent":

   - This function allows users to add new students to the "student_id" mapping. It takes two parameters: `first_name` (string) and `last_name` (string) representing the student's first name and last name, respectively.

   - The function assigns a unique student ID to the new student and updates the                           "student_id" mapping accordingly. It also increments the "id" variable to ensure each student receives a unique ID.

   - Additionally, the function checks if the provided first name is already present in the             "last_names" mapping. If not, it adds the first name to the corresponding list of last names. Similarly, it checks if the provided last name is already present in the                         "first_names" mapping and adds it if not.

6. Functions - "getLastNames" and "getFirstNames":

   - These functions allow users to retrieve lists of last names and first names associated with a given first name or last name, respectively. Users can use these functions to query all students sharing the same first or last name.

   - The functions take one parameter: `first_name` or `last_name`, respectively, and return the corresponding dynamic array containing the associated names.

7. Function - "contains":

   - This private helper function checks whether a given element (string) exists within a list         (string[]) of elements. It takes two parameters: `list` (string[]) representing the list of elements and `element` (string) to check.

   - The function iterates through the list and returns true if it finds a match using the                 keccak256 hash comparison; otherwise, it returns false.


In summary, this smart contract demonstrates a practical use case of nested mappings to efficiently manage student information based on their first and last names in an educational institute. By leveraging nested mappings and dynamic arrays, the contract allows easy retrieval of student IDs and the ability to query all students sharing the same first or last name.

Comments