Shorter solution to if,else if,else if

前端 未结 6 1817
深忆病人
深忆病人 2021-01-06 08:58

I\'m looking for a way to shorten this code up and avoid repeating code and if statements. What I\'m doing is creating a calculator that searches strings for operators &quo

6条回答
  •  不思量自难忘°
    2021-01-06 09:29

    You could write an AbstractCalculationOperation class with execute method, with Add, Subtract, etc extending it.

    Then, just parse leftHand, rightHand, and calculationOperation and run calculationOperation.execute( rightHand, leftHand ).

    public interface CalculationOperation {
        double calculate ( double lh, double rh );
        long calculate ( long lh, long rh );
    }
    
    public class Add implements CalculationOperation {
        public static final CalculationOperation INSTANCE = new Add();
        public double calculate ( double rh, double lh ) { return lh + rh; }
        public long calculate ( long rh, long lh ) { return lh + rh; }
    }
    

    And then:

    int lh = exp.substring(0, i);
    int rh = exp.substring(i+1);
    CalculationOperation op;
    switch( exp.charAt(i) ) {
        case '*': op = Multiply.INSTANCE; break;
        case '/': op = Divide.INSTANCE; break;
        case '+': op = Add.INSTANCE; break;
        case '-': op = Subtract.INSTANCE; break;
    }
    newResult = op.calculate( rh, lh );
    primeResult = newResult;
    System.out.println(primeResult);
    

    Alternate enum variant:

    public enum Calculation {
        ADD('+') {
            public int calculate( int lhs, int rhs ) { return lhs + rhs; }
            public long calculate( long lhs, long rhs ) { return lhs + rhs; }
            public float calculate( float lhs, float rhs ) { return lhs + rhs; }
            public double calculate( double lhs, double rhs ) { return lhs + rhs; }
        },
        SUBTRACT('-') {
            public int calculate( int lhs, int rhs ) { return lhs - rhs; }
            public long calculate( long lhs, long rhs ) { return lhs - rhs; }
            public float calculate( float lhs, float rhs ) { return lhs - rhs; }
            public double calculate( double lhs, double rhs ) { return lhs - rhs; }
        },
        MULTIPLY('*') {
            public int calculate( int lhs, int rhs ) { return lhs * rhs; }
            public long calculate( long lhs, long rhs ) { return lhs * rhs; }
            public float calculate( float lhs, float rhs ) { return lhs * rhs; }
            public double calculate( double lhs, double rhs ) { return lhs * rhs; }
        },
        DIVIDE('/') {
            public int calculate( int lhs, int rhs ) { return lhs / rhs; }
            public long calculate( long lhs, long rhs ) { return lhs / rhs; }
            public float calculate( float lhs, float rhs ) { return lhs / rhs; }
            public double calculate( double lhs, double rhs ) { return lhs / rhs; }
        };
    
        private final char textValue;
        Calculation ( char textValue )
        {
            this.textValue = textValue;
        }
    
        public abstract int calculate ( int lht, int rhs );
        public abstract long calculate ( long lht, long rhs );
        public abstract float calculate ( float lht, float rhs );
        public abstract double calculate ( double lht, double rhs );
    
        public static Calculation fromTextValue( char textValue ) {
            for( Calculation op : values() )
                if( op.textValue == textValue )
                    return op;
            throw new IllegalArgumentException( "Unknown operation: " + textValue );
        }
    }
    

    and then:

    int lh = exp.substring(0, i);
    int rh = exp.substring(i+1);
    Calculation op = Calculation.fromTextValue( exp.substring(i,1) );
    newResult = op.calculate( lh, rh );
    primeResult = newResult;
    System.out.println(primeResult);
    

提交回复
热议问题