0

I have a simple app to design. Two text fields, each for an operand, a compute button to compute the value of an operation applied to these 2 operands and 4 radio buttons, organised into two button groups: first group contains buttons A and B, second group contains buttons C and D

I need to implement a certain operation (ex. addition) for a combination of these 4 buttons.(A + C => operation <- addition) Only one button to be active in each group. I have to avoid IF statements.

This is what I have for now. I have to add new code to addListeners() function.

package calculator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator {
    private JFrame frame;
    private JPanel mainPanel;
    private JTextField firstField;
    private JTextField secondField;
    private JButton computeButton;
    private JTextField resultField;
    private JRadioButton aRadioButton;
    private JRadioButton bRadioButton;
    private JRadioButton cRadioButton;
    private JRadioButton dRadioButton;
    private ButtonGroup firstRadioButtonGroup;
    private ButtonGroup secondRadioButtonGroup;
    private IOperation operation;
    private double firstOperand;
    private double secondOperand;
    private double result;

    public Calculator() {
        initialize();
        adjustWindow();
        applyStyle();
        centerWindow();
        addToFrame();
        addListeners();


    }

    private void addListeners() {

        computeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                firstOperand = Double.parseDouble(firstField.getText());
                secondOperand = Double.parseDouble(secondField.getText());
                result = operation.compute(firstOperand, secondOperand);
                resultField.setText(Double.toString(result));
            }
        });

    }

    private void addToFrame() {
        mainPanel.add(firstField);
        mainPanel.add(secondField);
        mainPanel.add(computeButton);
        mainPanel.add(aRadioButton);
        mainPanel.add(bRadioButton);
        mainPanel.add(cRadioButton);
        mainPanel.add(dRadioButton);        firstRadioButtonGroup.add(aRadioButton);
        firstRadioButtonGroup.add(bRadioButton);
        secondRadioButtonGroup.add(cRadioButton);
        secondRadioButtonGroup.add(dRadioButton);
        mainPanel.add(aRadioButton);
        mainPanel.add(bRadioButton);
        mainPanel.add(cRadioButton);
        mainPanel.add(dRadioButton);
        firstRadioButtonGroup.clearSelection();
        mainPanel.add(resultField);
        frame.add(mainPanel);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void initialize() {
        firstOperand = 0;
        secondOperand = 0;
        frame = new JFrame("Calculator");
        mainPanel = new JPanel();
        firstField = new JTextField();
        secondField = new JTextField();
        computeButton = new JButton("Compute");
        resultField = new JTextField();
        aRadioButton = new JRadioButton("A");
        bRadioButton = new JRadioButton("B");
        cRadioButton = new JRadioButton("C");
        dRadioButton = new JRadioButton("D");
        firstRadioButtonGroup = new ButtonGroup();
        secondRadioButtonGroup = new ButtonGroup();
    }

    private void adjustWindow() {
        frame.setSize(300, 300);
    }

    private void centerWindow() {
        Toolkit tool = Toolkit.getDefaultToolkit();
        Dimension screenSize = tool.getScreenSize();

        double screenHeight = screenSize.height;
        double screenWidth = screenSize.width;

        double currentWindowHeight = frame.getHeight();
        double currentWindowWidth = frame.getWidth();

        int windowHeight = (int) ((screenHeight / 2) - (currentWindowHeight / 2));
        int windowWidth = (int) ((screenWidth / 2) - (currentWindowWidth / 2));

        frame.setLocation(windowWidth, windowHeight);
    }

    private void applyStyle() {
        firstField.setPreferredSize(new Dimension(240, 30));
        secondField.setPreferredSize(new Dimension(240, 30));
        resultField.setPreferredSize(new Dimension(240, 30));
        computeButton.setPreferredSize(new Dimension(240, 30));
    }
}

I would like to hear more about the design. Is it possible to separate the GUI of the calculator from the operation?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
CyberFox
  • 342
  • 3
  • 16
  • The only question above, is *"Is it possible to separate the GUI of the calculator from the operation?"*. It seems quite unrelated to the title and all the talk of radio buttons & calculations. Is that last (only) question what you want to **focus on** in this .. question? – Andrew Thompson Jul 25 '18 at 09:02

2 Answers2

1

You could create a map where the keys are the possible button-text combinations and the value the respective operation

IOperation addition = ...
Map<String, IOperation> operations = ...;
operations.put("AC", addition);

Within the listener you check which buttons are selected and concatenate their text.

String operationKey = selectedButton1Text + selectedButton2Text;

See this answer on how to get the radio buttons' text. Then perform the operation:

operations.get(operationKey).compute(...)
pma
  • 860
  • 1
  • 9
  • 26
  • You would still have to put if statements when checking that string. Thumbs up for the solution. – CyberFox Jul 25 '18 at 09:23
  • @CyberFox You mean when getting the selected buttons text? Use the actionCommand instead: https://stackoverflow.com/a/25246583/2746919 – pma Jul 25 '18 at 09:32
0

I have found a workaround for this. I set up a state machine, starting with addition as default and going to the next state whenever we have a radio button update. Here's the link to a git repo: Code

CyberFox
  • 342
  • 3
  • 16