D:\CurrentProjects\SearsInterviewCode\src\java\binary_tree_reflection\SetFields.java
  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 
  6 package binary_tree_reflection;
  7 
  8 import java.text.*;
  9 import java.lang.*;
 10 import java.util.*;
 11 import java.lang.reflect.*;
 12 
 13 /**
 14  *
 15  * @author sameldin
 16  */
 17 public class SetFields
 18 {
 19     final static int    GREATER     = 2;
 20     final static int    LESS_THAN   = 1;
 21     final static int    EQUAL       = 0;
 22     final static int    ERROR       = -1;
 23 
 24     /** Creates a new instance of SetFields */
 25     public SetFields() {
 26     }
 27 
 28     public int checkFieldValue(Product passedProduct, int value)
 29     {
 30 
 31        Class   classHandle = passedProduct.getClass();
 32        if(classHandle.isInterface())
 33           return ERROR;
 34        Field []  localClassFields = classHandle.getDeclaredFields();
 35 //       System.out.println("There are " + localClassFields.length + " Fields");
 36        String filedName = localClassFields[0].getName();
 37        try
 38        {
 39             localClassFields[0].setAccessible(true);
 40             int privateIntFiled = localClassFields[0].getInt(passedProduct);
 41 //            System.out.println("privateIntFiled =  " + privateIntFiled);
 42             if(privateIntFiled == value)
 43                 return(EQUAL);
 44             else
 45                 if(privateIntFiled < value)
 46                     return GREATER;
 47                 else
 48                     return LESS_THAN;
 49        }
 50        catch (Throwable e)
 51        {
 52             System.err.println(e);
 53                 return ERROR;
 54        }
 55     }
 56 
 57     public void insert(Node node, int value)
 58     {
 59 //        if (value < node.value)
 60           if(LESS_THAN == checkFieldValue(node.nodeProduct, value))
 61           {
 62                 if (node.left != null)
 63                 {
 64                         insert(node.left, value);
 65                        System.out.println("LESS_THAN - node.left added value = " + value);
 66                 }
 67                 else
 68                 {
 69                      node.left = new Node(value);
 70                        System.out.println("LESS_THAN - add node.left added value = " + value);
 71                 }
 72         }
 73 //        else if (value > node.value)
 74         else if(GREATER == checkFieldValue(node.nodeProduct, value))
 75         {
 76             if (node.right != null)
 77             {
 78                 insert(node.right, value);
 79                 System.out.println("GREATER - node.right added value = " + value);
 80             }
 81             else
 82             {
 83                 node.right = new Node(value);
 84                 System.out.println("GREATER - add node.left added value = " + value);
 85             }
 86         }
 87   }
 88 
 89   public Node findNode(Node node, int value)
 90 {
 91   Node tempNode = null;
 92 
 93     if (node != null)
 94     {
 95           if(EQUAL == checkFieldValue(node.nodeProduct, value))
 96             return node;
 97           else
 98             if( (tempNode = findNode(node.left, value)) != null)
 99                 return tempNode;
100             else
101                 if( (tempNode = findNode(node.right, value)) != null)
102                     return tempNode;
103     }
104     return null;
105 }
106 
107 public static String getPipedEven(int [] allValues)
108 {
109     String resultString = "";
110     if(     (null == allValues)
111         && (allValues.length <= 0)
112       )
113        return("Error");
114     for(int index = 0; index < allValues.length; index++)
115     {
116         int myTempInt = allValues[index];
117         if((myTempInt % 2) == 0)
118             resultString +=  myTempInt + "|";
119     }
120     resultString = resultString.substring(0, resultString.lastIndexOf('|'));
121     return resultString;
122 }
123 
124 
125     /**
126      *
127      */
128      public static void main (String[] args)
129     {
130       Node  START =  new Node(0);
131       Node  tempNode = null;
132 
133       SetFields setFieldsHandle = new SetFields();
134       setFieldsHandle.insert(START, 6);
135       setFieldsHandle.insert(START, 5);
136       setFieldsHandle.insert(START, 9);
137 
138       Node myNode = setFieldsHandle.findNode(START, 6);
139       if(null == myNode)
140          System.out.println("myNode = null");
141       else
142          System.out.println("myNode is found");
143 
144       int [] intArray =
145       {
146           1,2,4,5,6,7,8,9,10
147       };
148 
149          System.out.println("pipe \"|\" separated String  = " + getPipedEven(intArray));
150 
151     }
152 
153 }
154 
155