/**
 * This function takes a list of farmers and calculates the total profit earned by each farmer.
 * It uses nested try-catch statements to handle potential errors and return the total profit.
 *
 * @param farmerList A list containing dictionaries of farmers' information.
 * @return The total profit earned by all the farmers.
 */
fun calculateTotalProfit(farmerList: List<Map<String, Any>>): Int {

    var totalProfit = 0

    try {
        for (farmer in farmerList) {
            try {
                totalProfit += farmer["profit"] as Int
            } catch (e: ClassCastException) {
                throw ClassCastException("Profit value must be a number.")
            } catch (e: NoSuchElementException) {
                throw NoSuchElementException("Profit key missing for a farmer.")
            }
        }
    } catch (e: ClassCastException) {
        throw ClassCastException("Farmers must be a list of dictionaries.")
    }

    return totalProfit
}
/**
 * This function takes a list of farmers and calculates the total profit earned by each farmer.
 * It uses nested try-catch statements to handle potential errors and return the total profit.
 *
 * @param farmerList A list containing dictionaries of farmers' information.
 * @return The total profit earned by all the farmers.
 */
fun calculateTotalProfit(farmerList: List<Map<String, Any>>): Int {
/**
 * This function takes a list of farmers and calculates the total profit earned by each farmer.
 * It uses nested try-catch statements to handle potential errors and return the total profit.
 *
 * @param farmerList A list containing dictionaries of farmers' information.
 * @return The total profit earned by all the farmers.
 */
fun calculateTotalProfit(farmerList: List<Map<String, Any>>): Int {

    var totalProfit = 0

    try {
        for (farmer in farmerList) {
            try {
                totalProfit += farmer["profit"] as Int
            } catch (e: ClassCastException) {
                throw ClassCastException("Profit value must be a number.")
            } catch (e: NoSuchElementException) {
                throw NoSuchElementException("Profit key missing for a farmer.")
            }
        }
    } catch (e: ClassCastException) {
        throw ClassCastException("Farmers must be a list of dictionaries.")
    }

    return totalProfit
}