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(),
// ....
};
}