1717package simplejavacalculator ;
1818
1919import java .awt .FlowLayout ;
20- import javax .swing .*;
21- import java .awt .event .ActionListener ;
2220import java .awt .event .ActionEvent ;
21+ import java .awt .event .ActionListener ;
22+
23+ import javax .swing .JButton ;
24+ import javax .swing .JFrame ;
25+ import javax .swing .JPanel ;
26+ import javax .swing .JTextArea ;
2327
2428public class UI implements ActionListener {
29+ private final JFrame frame ;
30+ private final JPanel panel ;
31+ private final JTextArea text ;
32+ private final JButton but [], butAdd , butMinus , butMultiply , butDivide ,
33+ butEqual , butCancel , butSquareRoot , butSquare , butOneDevidedBy ,
34+ butCos , butSin , butTan ;
35+ private final Calculator calc ;
2536
26- private JFrame frame ;
27- private JPanel panel ;
28- private JTextArea text ;
29- private JButton but [], butAdd , butMinus , butMultiply , butDivide , butEqual , butCancel , butSquareRoot , butSquare , butOneDevidedBy , butCos , butSin , butTan ;
30- private Double num1 , num2 , result ;
31- private int add = 0 , minus = 0 , multiply = 0 , divide = 0 ;
32- String [] buttonValue = {"0" ,"1" ,"2" ,"3" ,"4" ,"5" ,"6" ,"7" ,"8" ,"9" };
37+ private final String [] buttonValue = { "0" , "1" , "2" , "3" , "4" , "5" , "6" ,
38+ "7" , "8" , "9" };
3339
3440 public UI () {
3541 frame = new JFrame ("Calculator PH" );
3642 frame .setResizable (false );
3743 panel = new JPanel (new FlowLayout ());
38-
39- text = new JTextArea (2 ,25 );
44+
45+ text = new JTextArea (2 , 25 );
4046 but = new JButton [10 ];
41- for (int i = 0 ; i < 10 ; i ++) {
47+ for (int i = 0 ; i < 10 ; i ++) {
4248 but [i ] = new JButton (String .valueOf (i ));
4349 }
44-
50+
4551 butAdd = new JButton ("+" );
4652 butMinus = new JButton ("-" );
4753 butMultiply = new JButton ("*" );
@@ -53,18 +59,20 @@ public UI() {
5359 butCos = new JButton ("Cos" );
5460 butSin = new JButton ("Sin" );
5561 butTan = new JButton ("Tan" );
56-
62+
5763 butCancel = new JButton ("C" );
64+
65+ calc = new Calculator ();
5866 }
59-
67+
6068 public void init () {
6169 frame .setVisible (true );
62- frame .setSize (350 ,280 );
70+ frame .setSize (350 , 280 );
6371 frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
6472 frame .add (panel );
65-
73+
6674 panel .add (text );
67- for (int i = 0 ; i < 10 ; i ++) {
75+ for (int i = 0 ; i < 10 ; i ++) {
6876 panel .add (but [i ]);
6977 but [i ].addActionListener (this );
7078 }
@@ -82,7 +90,7 @@ public void init() {
8290
8391 panel .add (butEqual );
8492 panel .add (butCancel );
85-
93+
8694 butAdd .addActionListener (this );
8795 butMinus .addActionListener (this );
8896 butMultiply .addActionListener (this );
@@ -93,139 +101,87 @@ public void init() {
93101 butCos .addActionListener (this );
94102 butSin .addActionListener (this );
95103 butTan .addActionListener (this );
96-
104+
97105 butEqual .addActionListener (this );
98106 butCancel .addActionListener (this );
99107 }
100108
101109 @ Override
102110 public void actionPerformed (ActionEvent e ) {
103- Object source = e .getSource ();
111+ final Object source = e .getSource ();
104112
105- for (int i = 0 ; i < 10 ; i ++) {
106- if (source == but [i ]) {
107- text .append (buttonValue [i ]);
113+ for (int i = 0 ; i < 10 ; i ++) {
114+ if (source == but [i ]) {
115+ text .append (buttonValue [i ]);
108116 }
109117 }
110-
111- if (source == butAdd ) {
112- num1 = reader ();
113- text .setText ("" );
114- add = 1 ;
115- minus = 0 ;
116- multiply = 0 ;
117- divide = 0 ;
118- }
119-
120- if (source == butAdd ) {
121- num1 = reader ();
118+
119+ if (source == butAdd ) {
120+ calc .setValue (Calculator .BiOperatorModes .add , reader ());
122121 text .setText ("" );
123- add = 1 ;
124- minus = 0 ;
125- multiply = 0 ;
126- divide = 0 ;
127122 }
128-
129- if (source == butMinus ) {
130- num1 = reader ();
123+
124+ if (source == butMinus ) {
125+ calc . setValue ( Calculator . BiOperatorModes . minus , reader () );
131126 text .setText ("" );
132- add = 0 ;
133- minus = 1 ;
134- multiply = 0 ;
135- divide = 0 ;
136127 }
137-
138- if (source == butMultiply ) {
139- num1 = reader ();
128+
129+ if (source == butMultiply ) {
130+ calc . setValue ( Calculator . BiOperatorModes . multiply , reader () );
140131 text .setText ("" );
141- add = 0 ;
142- minus = 0 ;
143- multiply = 1 ;
144- divide = 0 ;
145132 }
146-
147- if (source == butDivide ) {
148- num1 = reader ();
133+
134+ if (source == butDivide ) {
135+ calc . setValue ( Calculator . BiOperatorModes . divide , reader () );
149136 text .setText ("" );
150- add = 1 ;
151- minus = 0 ;
152- multiply = 0 ;
153- divide = 1 ;
154137 }
155138
156- if (source == butSquare ) {
157- num1 = reader ();
158- result = num1 *num1 ;
159- text .setText (Double .toString (result ));
139+ if (source == butSquare ) {
140+ text .setText (Double .toString (calc .calculateMono (
141+ Calculator .MonoOperatorModes .square , reader ())));
160142 }
161143
162- if (source == butSquareRoot ) {
163- num1 = reader ();
164- result = Math .sqrt (num1 );
165- text .setText (Double .toString (result ));
144+ if (source == butSquareRoot ) {
145+ text .setText (Double .toString (calc .calculateMono (
146+ Calculator .MonoOperatorModes .squareRoot , reader ())));
166147 }
167148
168- if (source == butOneDevidedBy ) {
169- num1 = reader ();
170- result = 1 /num1 ;
171- text .setText (Double .toString (result ));
149+ if (source == butOneDevidedBy ) {
150+ text .setText (Double .toString (calc .calculateMono (
151+ Calculator .MonoOperatorModes .oneDevidedBy , reader ())));
172152 }
173153
174- if (source == butCos ) {
175- num1 = reader ();
176- result = Math .cos (num1 );
177- text .setText (Double .toString (result ));
154+ if (source == butCos ) {
155+ text .setText (Double .toString (calc .calculateMono (
156+ Calculator .MonoOperatorModes .cos , reader ())));
178157 }
179158
180- if (source == butSin ) {
181- num1 = reader ();
182- result = Math .sin (num1 );
183- text .setText (Double .toString (result ));
159+ if (source == butSin ) {
160+ text .setText (Double .toString (calc .calculateMono (
161+ Calculator .MonoOperatorModes .sin , reader ())));
184162 }
185163
186- if (source == butTan ) {
187- num1 = reader ();
188- result = Math .tan (num1 );
189- text .setText (Double .toString (result ));
164+ if (source == butTan ) {
165+ text .setText (Double .toString (calc .calculateMono (
166+ Calculator .MonoOperatorModes .tan , reader ())));
190167 }
191-
192- if (source == butEqual ) {
193- num2 = reader ();
194-
195- if (add > 0 ) {
196- result = num1 +num2 ;
197- text .setText (Double .toString (result ));
198- }
199-
200- if (minus > 0 ) {
201- result = num1 -num2 ;
202- text .setText (Double .toString (result ));
203- }
204-
205- if (multiply > 0 ) {
206- result = num1 *num2 ;
207- text .setText (Double .toString (result ));
208- }
209-
210- if (divide > 0 ) {
211- result = num1 /num2 ;
212- text .setText (Double .toString (result ));
213- }
168+
169+ if (source == butEqual ) {
170+ text .setText (Double .toString (calc .calculateBi (reader ())));
214171 }
215-
216- if (source == butCancel ) {
217- num1 = 0.0 ;
218- num2 = 0.0 ;
172+
173+ if (source == butCancel ) {
174+ calc .setValue (Calculator .BiOperatorModes .normal , 0.0 ); // reset
219175 text .setText ("" );
220176 }
221177 }
222-
178+
223179 public double reader () {
224180 Double num ;
225181 String str ;
226182 str = text .getText ();
227183 num = Double .valueOf (str );
228-
229- return num ;
184+
185+ return num ;
230186 }
231187}
0 commit comments