Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.JButton;
- import javax.swing.JOptionPane;
- public class CalculatorEngine implements ActionListener {
- Calculator parent;
- char selectedAction = ' '; // +, -, /, или *
- double currentResult =0;
- // Конструктор калькулятора
- CalculatorEngine(Calculator parent){
- this.parent = parent;
- }
- public void actionPerformed(ActionEvent e){
- // Получить источник действия
- JButton clickedButton = (JButton) e.getSource();
- String dispFieldText=parent.displayField.getText();
- double displayValue=0;
- int dot = 0;
- for(int i=0; i != dispFieldText.length(); i++){
- if (dispFieldText.charAt(i) == '.'){
- ++dot;
- if (dot>=2){
- JOptionPane.showConfirmDialog(null,
- "Точка встречается 2 раза", "Ошибка!",
- JOptionPane.PLAIN_MESSAGE,JOptionPane.PLAIN_MESSAGE );
- }
- }
- }
- // Получить число из дисплея калькулятора, если он не пустой.
- if (!"".equals(dispFieldText)){
- try {
- displayValue= Double.parseDouble(dispFieldText);
- } catch (NumberFormatException e1) {
- javax.swing.JOptionPane.showConfirmDialog(null,
- "Пожалуйста, введите число", "Неправильный ввод",
- javax.swing.JOptionPane.PLAIN_MESSAGE);
- return;
- }
- }
- Object src = e.getSource();
- if (src == parent.buttonPlus){
- selectedAction = '+';
- currentResult=displayValue;
- parent.displayField.setText("");
- } else if (src == parent.buttonMinus){
- selectedAction = '-';
- currentResult=displayValue;
- parent.displayField.setText("");
- }else if (src == parent.buttonDivide){
- selectedAction = '/';
- currentResult=displayValue;
- parent.displayField.setText("");
- } else if (src == parent.buttonMultiply){
- selectedAction = '*';
- currentResult=displayValue;
- parent.displayField.setText("");
- } else if (src == parent.buttonEqual){
- // Совершить арифметическое действие, в зависимости
- // от selectedAction, обновить переменную currentResult
- if (selectedAction=='+'){
- currentResult+=displayValue;
- // Сконвертировать результат в строку, добавляя его
- // к пустой строке
- parent.displayField.setText(""+currentResult);
- }else if (selectedAction=='-'){
- currentResult -=displayValue;
- parent.displayField.setText(""+currentResult);
- }else if (selectedAction=='/'){
- if (displayValue==0){
- JOptionPane.showConfirmDialog(null,
- "Деление на ноль или на пустое знеачение не допускается", "Ошибка!",
- JOptionPane.PLAIN_MESSAGE);
- parent.displayField.setText("");
- }
- else {
- currentResult /=displayValue;
- parent.displayField.setText(""+currentResult);}
- }else if (selectedAction=='*'){
- currentResult*=displayValue;
- parent.displayField.setText(""+currentResult);
- }
- } else{
- // Для всех цифровых кнопок присоединить надпись на
- // кнопке к надписи в дисплее
- String clickedButtonLabel= clickedButton.getText();
- parent.displayField.setText(dispFieldText +
- clickedButtonLabel);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment