简体   繁体   中英

Convert This String to return Double

public static String formulaCalcularRenda = "(Loja.getRendaFixa()*(1+super.getArea()/100)+super.getReceita()/100)";

@Override
    public double calcularRenda(){
        return (Loja.getRendaFixa()*(1+super.getArea()/100)+super.getReceita()/100);
    }

My main objective is to reuse the code, while being the formula altered by order of the user itself. I've tried something similar before, but I was uncapable to succeed, because the string has letters.

@Override
        public double calcularRenda(){
            return Double.parseDouble(Loja.formulaCalcularRenda);
        }

Can someone help me?

As others are commenting, this is not an easy or generally wise thing to do. But if you are committed to this task, a scripting language is a much better choice for the expression evaluation. Scripting language support is built into Java and is relatively easy to use.

The Java javascript programmer guide would be a good place to start. It has examples that you should be able to adapt to your needs.

If you think about it, it doen't make too much sense for the user to write the code. So, in your opinion, what would be the best way for me, not to repeat the same code, over and over again?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM