1
\$\begingroup\$

I am new to OOD and trying to implement a notation calculator in java with OOD concept, attached is my code. It works but I have feeling it's not good especially the factory method is not good. I will really grateful if you have any suggestion about revise the structure of code:

public class PolishNotationCalculator {
    public interface Token{
        public void func(Stack<Token> st);
    }

    class Operand implements  Token{
        private  int val;
        public Operand(String v){
            this.val = Integer.parseInt(v);  //should catch exception?
        }

        public int getVal(){
            return val;
        }

        public void func(Stack<Token> st){
            st.push(this);
        }
    }

    class Operator implements Token{
        public void func(Stack<Token> st){
            return;
        }
    }

    class Add extends Operator{
        public void func(Stack<Token> st){
            Operand o1 = (Operand)st.pop();
            Operand o2 = (Operand)st.pop();
            st.push(new Operand(""+(o1.getVal()+o2.getVal())));
        }
    }

    class Sub extends Operator{
        public void func(Stack<Token> st){
            Operand o1 = (Operand)st.pop();
            Operand o2 = (Operand)st.pop();
            st.push(new Operand(""+(o1.getVal()-o2.getVal())));
        }
    }

    class Mul extends Operator{
        public void func(Stack<Token> st){
            Operand o1 = (Operand)st.pop();
            Operand o2 = (Operand)st.pop();
            st.push(new Operand(""+(o1.getVal()*o2.getVal())));
        }
    }

    class Div extends Operator{
        public void func(Stack<Token> st){
            Operand o1 = (Operand)st.pop();
            Operand o2 = (Operand)st.pop();
            st.push(new Operand(""+o1.getVal()/o2.getVal()));
        }
    }

    class Factory{
        private Map<String, Operator> hm = new HashMap<>();
        public Factory (){
            hm.put("+", new Add());
            hm.put("-", new Sub());
            hm.put("*", new Mul());
            hm.put("/", new Div());
        }
        public Operand create(String s){
            return new Operand((s));
        }
    }

    Stack<Token> st = new Stack();
    public static void main(String[] args){
        PolishNotationCalculator pc = new PolishNotationCalculator();
        //["2", "1", "+", "3", "*"]
        String [] in = new String[5];
        in[0]="2"; in[1]="1"; in[2]="+"; in[3]="3"; in[4]="*";
        System.out.println(pc.RPN(in));
    }


    public int RPN(String[] rpn){
        Factory factory = new Factory();
        for(String s: rpn){
            if(factory.hm.containsKey(s))
                factory.hm.get(s).func(st);
            else factory.create(s).func(st);
     //super cast to child is impossible,call the empty func...how to fix?
        }
        return ((Operand)st.pop()).getVal();
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.