Skip to main content
3 of 4
added 1298 characters in body

Pros:

  • basic separation of concerns:

    • you have separate Listeners for Number buttons and operator buttons
    • you created small methods with limited concerns.
    • you created two arrays to hold buttons of same type but different business roles.
  • Naming: you follow the Java naming conventions and your identifier have useful names.

  • comments: you have a comment which tells why the code is at that line. You have no other comments.

cons:

inheritance misconception:

Your class Calculator extends JFrame, but it does not add new functionality to it, it just configures the frame.

if/else cascade instead of polymorphism

The method actionPerformed() Your class MyOperationButtonListener contains a long if/else cascade. This is because you have to know which button caused the event.

This could be avoided if you had a separate Listener instance for each button. This does not mean that a separate Listener class is needed for each button. The listeners should have been created as anonymous inner classes of the ActionListener interface directly.

The downside of it that you cannot create the operator buttons in a loop anymore, but this is ok because each button ha a different behavior:

private void createOperationButtons() {
    operationButtons = new JButton[18];
        operationButtons[0] = new JButton("ME"); // the array operationButtonStrings is not needed.
        operationButtons[0].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e) {
              memoryStore.resetStoredValue();
           }
        });    
        operationButtons[0].setFocusable(false);

        operationButtons[1] = new JButton("MR"); // the array operationButtonStrings is not needed.
        operationButtons[1].addActionListener((e) -> { // java8 lambda version
             display = screen.getText();
             screen.setText(display + "" + memoryStore.getStoredValue());
        });    
        operationButtons[1].setFocusable(false);
       // ...
    }
}

To reduce the code duplication youcould extract the actual buton cration and configuration in an extra method:

private JButton createButton(String text, ActionListener action){
    JButton button = new JButton(text);
    button.addActionListener(action);
    button.setFocusable(false);
    return button;
}

private void createOperationButtons() {
    operationButtons = new JButton[18];
        operationButtons[0] = createButton("ME",(e)->memoryStore.resetStoredValue());
        operationButtons[1] = createButton("MR",(e)->{
             display = screen.getText();
             screen.setText(display + "" + memoryStore.getStoredValue());
        });
    // ....
}

As you can see this brings the "name" and the "function" closer together and does not need any if/else cascade or switch...


I have to do it 18 times in one method it makes the method very big and ugly. There has to be a way of making it nice and clean. – Alex. M

You cannot avoid having to specify the different behavior for the operator buttons.

In your first solution the ugliness was the if/else cascade in your actionPerformed() method.

With my suggestion you could at least ease the readers pain by having an additional method for each operator button:

private JButton createButton(String text, ActionListener action){
    JButton button = new JButton(text);
    button.addActionListener(action);
    button.setFocusable(false);
    return button;
}

private JButton createMemoryEraseButton(){
    return createButton("ME",(e)->memoryStore.resetStoredValue());
}

private JButton createMemoryRecallButton(){
    return createButton("MR",(e)->{
             display = screen.getText();
             screen.setText(display + "" + memoryStore.getStoredValue());
        });
}
// ....

private void createOperationButtons() {
    operationButtons = new JButton[] {
       createMemoryEraseButton(),
       createMemoryRecallButton(),
       // ....
    };
}

You could even go one step further and move the method createOperationButtons() and all the create??Button() methods in to a separate class.

Off cause, all the objects these methods work on you would have to pass in as prarameters either to the constructor of the new class or the createOperationButtons() method and subsequently to the create??Button() methods as needed.