/*
 * [TestHashMapModify.java]
 *
 * Summary: example use of java.util.HashMap to modify keys and values.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2009-05-15 initial version
 */
package com.mindprod.example;

import java.util.HashMap;
import java.util.Map;

import static java.lang.System.*;

/**
 * example use of java.util.HashMap to modify keys and values.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-05-15 initial version
 * @since 2009-05-15
 */
@SuppressWarnings( { "UnusedDeclaration" } )
public final class TestHashMapModify
    {
    /**
     * Sample code to TEST HashMap Modifying
     *
     * @param args not used
     */
    public static void main( String[] args )
        {
        // create a new HashMap
        final Map<String, Food> h = new HashMap<>( 149 /* capacity */, 0.75f /* loadfactor */ );
        {
        // add some Food objects to the HashMap
        // see http://www.calorie-charts.net  for calories/gram
        h.put( "sugar", new Food( "sugar", "white", 4.5f ) );
        h.put( "alchol", new Food( "alcohol", "clear", 7.0f ) );
        h.put( "cheddar", new Food( "cheddar", "orange", 4.03f ) );
        h.put( "peas", new Food( "peas", "green", .81f ) );
        h.put( "salmon", new Food( "salmon", "pink", 2.16f ) );
        }
        // (1) modify the alcohol key to fix the spelling error in the key.
        Food alc = h.get( "alchol" ); // sic
        h.put( "alcohol", alc );
        h.remove( "alchol" );
        // (2) modify the value object for sugar key.
        Food sug = h.get( "sugar" );
        sug.setColour( "brown" );
        // do not need to put.
        // (3) replace the value object for the cheddar key
        // don't need to get the old value first.
        h.put( "cheddar", new Food( "cheddar", "white", 4.02f ) );
        // (4) replace the value object for the peas key with object based on previous
        Food peas = h.get( "peas" );
        h.put( "peas", new Food( peas.getName(), peas.getColour(), peas.getCaloriesPerGram() * 1.05f ) );
        // enumerate all the keys in the HashMap in random order
        for ( String key : h.keySet() )
            {
            out.println( key + " = " + h.get( key ).toString() );
            }
        } // end main

    static class Food
        {
        String colour;

        String name;

        float caloriesPerGram;

        Food( final String name, final String colour, final float caloriesPerGram )
            {
            this.name = name;
            this.colour = colour;
            this.caloriesPerGram = caloriesPerGram;
            }

        public float getCaloriesPerGram()
            {
            return caloriesPerGram;
            }

        public void setCaloriesPerGram( final float caloriesPerGram )
            {
            this.caloriesPerGram = caloriesPerGram;
            }

        public String getColour()
            {
            return colour;
            }

        public void setColour( final String colour )
            {
            this.colour = colour;
            }

        public String getName()
            {
            return name;
            }

        public void setName( final String name )
            {
            this.name = name;
            }

        public String toString()
            {
            return name + " : " + colour + " : " + caloriesPerGram;
            }
        }
    }