diff --git a/src/main/java/dev/sillyangel/calc/BaseConverter.java b/src/main/java/dev/sillyangel/calc/BaseConverter.java new file mode 100644 index 0000000..8c19789 --- /dev/null +++ b/src/main/java/dev/sillyangel/calc/BaseConverter.java @@ -0,0 +1,110 @@ +package dev.sillyangel.calc; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.border.EmptyBorder; + +public class BaseConverter { + private Calculator ci; + private JDialog BaseConverterDialog; + private JTextField inputField; + private JComboBox fromBaseComboBox; + private JComboBox toBaseComboBox; + private JLabel resultLabel; + + public BaseConverter(Calculator ci) { + System.out.println("Initialized BaseConverter"); + this.ci = ci; + } + + public void BaseConverterGUI() { + BaseConverterDialog = new JDialog(this.ci, "Base System Converter"); + BaseConverterDialog.setSize(400, 250); + BaseConverterDialog.setLocationRelativeTo(this.ci); + BaseConverterDialog.setLayout(new BorderLayout(10, 10)); + + + JPanel inputPanel = new JPanel(); + inputPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); + inputPanel.setLayout(new GridLayout(0, 2, 10, 10)); + + inputPanel.add(new JLabel("Input Number:")); + inputField = new JTextField(20); + inputPanel.add(inputField); + + String[] bases = {"Binary (2)", "Octal (8)", "Decimal (10)", "Hexadecimal (16)"}; + inputPanel.add(new JLabel("From Base:")); + fromBaseComboBox = new JComboBox<>(bases); + inputPanel.add(fromBaseComboBox); + + inputPanel.add(new JLabel("To Base:")); + toBaseComboBox = new JComboBox<>(bases); + toBaseComboBox.setSelectedIndex(2); + inputPanel.add(toBaseComboBox); + + BaseConverterDialog.add(inputPanel, BorderLayout.CENTER); + + + JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); + JButton convertButton = new JButton("Convert"); + convertButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + convert(); + } + }); + buttonPanel.add(convertButton); + BaseConverterDialog.add(buttonPanel, BorderLayout.SOUTH); + + // --- Result Panel --- + JPanel resultPanel = new JPanel(); + resultPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); + resultPanel.add(new JLabel("Result:")); + resultLabel = new JLabel("0"); + resultPanel.add(resultLabel); + BaseConverterDialog.add(resultPanel, BorderLayout.NORTH); + BaseConverterDialog.setVisible(true); + } + + private void convert() { + String input = inputField.getText().trim(); + if (input.isEmpty()) { + JOptionPane.showMessageDialog(BaseConverterDialog, "Please enter a number.", "Error", JOptionPane.ERROR_MESSAGE); + return; + } + + int fromBase = getBaseFromComboBox(fromBaseComboBox); + int toBase = getBaseFromComboBox(toBaseComboBox); + + try { + // 1. Convert from source base string to a standard long + long decimalValue = Long.parseLong(input, fromBase); + + // 2. Convert from the standard long to the target base string + String result = Long.toString(decimalValue, toBase).toUpperCase(); + + resultLabel.setText(result); + + } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(BaseConverterDialog, "Invalid input for the selected 'From Base'. Please check your number.", "Error", JOptionPane.ERROR_MESSAGE); + } catch (Exception e) { + JOptionPane.showMessageDialog(BaseConverterDialog, "An unexpected error occurred: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); + } + } + + private int getBaseFromComboBox(JComboBox comboBox) { + String selected = (String) comboBox.getSelectedItem(); + // Extract the number from the string e.g., "Binary (2)" -> 2 + return Integer.parseInt(selected.replaceAll("[^0-9]", "")); + } +} \ No newline at end of file