evgeniyosipov

CalculatorEngine.java

Mar 30th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. import java.awt.event.ActionListener;
  2. import java.awt.event.ActionEvent;
  3. import javax.swing.JButton;
  4. import javax.swing.JOptionPane;
  5.  
  6. public class  CalculatorEngine implements ActionListener {
  7. Calculator parent;
  8. char selectedAction = ' '; // +, -, /, или *
  9. double currentResult =0;
  10.  
  11. // Конструктор калькулятора
  12.  
  13. CalculatorEngine(Calculator parent){
  14. this.parent = parent;
  15. }
  16.  
  17. public void actionPerformed(ActionEvent e){
  18. // Получить источник действия
  19. JButton clickedButton = (JButton) e.getSource();
  20. String dispFieldText=parent.displayField.getText();
  21. double displayValue=0;
  22.  
  23. int dot = 0;
  24. for(int i=0; i != dispFieldText.length(); i++){
  25.  
  26.     if (dispFieldText.charAt(i) == '.'){
  27.         ++dot;
  28.  
  29.         if (dot>=2){
  30.             JOptionPane.showConfirmDialog(null,
  31.                     "Точка встречается 2 раза", "Ошибка!",
  32.                     JOptionPane.PLAIN_MESSAGE,JOptionPane.PLAIN_MESSAGE );
  33.         }
  34.     }  
  35.  
  36. }
  37. // Получить число из дисплея калькулятора, если он не пустой.
  38.  
  39. if (!"".equals(dispFieldText)){
  40.  
  41. try {
  42.     displayValue= Double.parseDouble(dispFieldText);
  43. } catch (NumberFormatException e1) {
  44.     javax.swing.JOptionPane.showConfirmDialog(null,
  45.             "Пожалуйста, введите число", "Неправильный ввод",
  46.             javax.swing.JOptionPane.PLAIN_MESSAGE);
  47.             return;
  48. }
  49. }
  50.  
  51. Object src = e.getSource();
  52.  
  53. if (src == parent.buttonPlus){
  54. selectedAction = '+';
  55. currentResult=displayValue;
  56. parent.displayField.setText("");
  57.  
  58. } else if (src == parent.buttonMinus){
  59. selectedAction = '-';
  60. currentResult=displayValue;
  61. parent.displayField.setText("");
  62.  
  63. }else if (src == parent.buttonDivide){
  64. selectedAction = '/';
  65. currentResult=displayValue;
  66. parent.displayField.setText("");
  67.  
  68. } else if (src == parent.buttonMultiply){
  69. selectedAction = '*';
  70. currentResult=displayValue;
  71. parent.displayField.setText("");
  72.  
  73. } else if (src == parent.buttonEqual){
  74. // Совершить арифметическое действие, в зависимости
  75. // от selectedAction, обновить переменную currentResult
  76.  
  77. if (selectedAction=='+'){
  78. currentResult+=displayValue;
  79. // Сконвертировать результат в строку, добавляя его
  80. // к пустой строке
  81. parent.displayField.setText(""+currentResult);
  82.  
  83. }else if (selectedAction=='-'){
  84. currentResult -=displayValue;
  85. parent.displayField.setText(""+currentResult);
  86.  
  87. }else if (selectedAction=='/'){
  88.  
  89.     if (displayValue==0){
  90.         JOptionPane.showConfirmDialog(null,
  91.                 "Деление на ноль или на пустое знеачение не допускается", "Ошибка!",
  92.                 JOptionPane.PLAIN_MESSAGE);
  93.         parent.displayField.setText("");
  94.         }
  95.     else {
  96.         currentResult /=displayValue;
  97.         parent.displayField.setText(""+currentResult);}
  98.  
  99. }else if (selectedAction=='*'){
  100. currentResult*=displayValue;
  101. parent.displayField.setText(""+currentResult);
  102. }
  103. } else{
  104. // Для всех цифровых кнопок присоединить надпись на
  105. // кнопке к надписи в дисплее
  106. String clickedButtonLabel= clickedButton.getText();
  107. parent.displayField.setText(dispFieldText +
  108. clickedButtonLabel);
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment