mercredi 18 mai 2016

How to return the value of a method where the method name is given by a string?

I need to return the value of a method and also I need to print the name of the method including the object by which it is called. For example:

public class FindMethod {    
public void accessor(String m){
            String amount = "getamount()" ;
            String str="";
            if(m.equals("Receive(int)"))
                str+= "LS."+amount;
            System.out.println(str);
        }

         public static void main(String[] args)
         {

             FindMethod fm = new FindMethod();
             fm.accessor("Receive(int)");

         }
}

Result: LS.getamount()

The above program is printing the method name as a string including the object where LS is the object and getamount() is the method of another class LoanApprovalSystem().

But I need to print the integer value that will be returned by the result LS.getamount(). But I have returned LS.getamount() as a string. I am not sure how to return the actual value of LS.getamount() from the string.

Can any one give me some idea that, how can I return the value of the method getamount() which is given by a string?? I mean can I use the string LS.getamount() as a reference to call the method getamount() from the class LoanApprovalSystem()??

The class LoanApprovalSystem() is given below:

public class LoanApprovalSystem {

    private static int amount;
    private static String risklevel ;
    private static boolean approve;
    private static boolean message;
    private static String result ;

    public LoanApprovalSystem(){

    }

    void initialize(){
        amount=0;
        risklevel=null;
        approve=false;
        message=false;      
    }

    public void Receive(int req){   
    amount = req;
    }

    public void Asses(int req){

           if (req > 1000 && req <= 5000)
           {
               risklevel = "low";
               approve = true;

           }
           else if (req > 5000 && req <= 10000)
           {
               risklevel = "high";

           }
           else 
               risklevel = " ";
    }

    public void Approval(int req){
        if ((req > 10000) || ((req <= 10000) & getrisklevel() =="high"))
        {
            approve = false;
        }

        else if (amount <= 5000 && getrisklevel() == "low")
           {
            approve = true;
           }
    }

    public void Sendmessage(String risklevel){      
        if(risklevel == "low")
        {
            message=true;
            //System.out.println(message);
            //System.out.println("Loan approved");
        }
        else
            message=false;
    }

    public void Reply(boolean message, boolean approve){
        if(message == true || approve == true)
        {
            result = ("Loan Approved");
            //System.out.println("Loan Approved");
        }
        else
        {
            result = ("Loan Rejected");
            //System.out.println("Loan Rejected");
        }
    }

    public int getamount(){
        return (amount);
    }

    public String getrisklevel(){
        return(risklevel);
    }

    public boolean getapprove(){
        return (approve);
    }

    public boolean getmessage(){
        return(message);
    }

    public String getresult(){
        return (result);
    }


    public String toString(){
        String str = "";
        str += "(" +result+ ").";
        return str;

    }


    public static void main(String[] args){
        LoanApprovalSystem LS = new LoanApprovalSystem();
        TestdataGeneration testdata = new TestdataGeneration();
        LS.initialize();
        //for(int data:testdata.Testdata())
        {
        LS.Receive(testdata.thirddata());
        LS.Asses(LS.getamount());
        LS.Approval(LS.getamount());
        LS.Sendmessage(LS.getrisklevel());
        LS.Reply(LS.getmessage(), LS.getapprove());
        System.out.println("Final state: "+LS);
        }
    }

}





Aucun commentaire:

Enregistrer un commentaire