Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.53 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.calculator.cs41;
  2.  
  3. import java.awt.Color;
  4. import java.awt.EventQueue;
  5. import java.awt.Font;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.util.ArrayList;
  9. import javax.swing.AbstractAction;
  10. import javax.swing.AbstractButton;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JTextField;
  14. import javax.swing.SwingConstants;
  15.  
  16.  
  17. public class calculators {
  18.  
  19. protected static final Calculate essam = null;
  20. public JFrame frame;
  21. static JTextField text;
  22. static double FirstNum ;
  23. static double SecondNum ;
  24. static char Operation ;
  25. double Result ;
  26. String Final ;
  27. String Savee ;
  28. static int g = 0;
  29. static int p = 1 ;
  30. static ArrayList<String> Saved = new ArrayList<String>(5) ;
  31. /**
  32. * Launch the application.
  33. */
  34. public static void main(String[] args) {
  35. EventQueue.invokeLater(new Runnable() {
  36. public void run() {
  37. try {
  38. calculators window = new calculators();
  39. window.frame.setVisible(true);
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. });
  45. }
  46.  
  47. /**
  48. * Create the application.
  49. */
  50. public calculators() {
  51. initialize();
  52. }
  53.  
  54. /**
  55. * Initialize the contents of the frame.
  56. */
  57. public void initialize() {
  58. frame = new JFrame();
  59. frame.setBounds(100, 100, 450, 300);
  60. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  61. frame = new JFrame();
  62. frame.getContentPane().setBackground(Color.GRAY);
  63. frame.getContentPane().setForeground(new Color(0, 0, 0));
  64. frame.setBounds(100, 100, 433, 482);
  65. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66. frame.getContentPane().setLayout(null);
  67.  
  68. text = new JTextField();
  69. text.setFont(new Font("Yu Gothic UI", Font.BOLD, 20));
  70. text.setHorizontalAlignment(SwingConstants.RIGHT);
  71. text.setBackground(Color.LIGHT_GRAY);
  72. text.setBounds(10, 11, 397, 84);
  73. frame.getContentPane().add(text);
  74. text.setColumns(10);
  75.  
  76. JButton btn0 = new JButton("0");
  77. btn0.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  78. btn0.addActionListener(new ActionListener() {
  79. public void actionPerformed(ActionEvent e) {
  80. String EnterNumber = text.getText() + btn0.getText() ;
  81. text.setText(EnterNumber);
  82. }
  83. });
  84. btn0.setBackground(Color.WHITE);
  85. btn0.setBounds(10, 369, 72, 63);
  86. btn0.setForeground(new Color(255, 69, 0));
  87. frame.getContentPane().add(btn0);
  88.  
  89. JButton btn8 = new JButton("8");
  90. btn8.addActionListener(new ActionListener() {
  91. public void actionPerformed(ActionEvent e) {
  92. String EnterNumber = text.getText() +btn8.getText() ;
  93. text.setText(EnterNumber);
  94. }
  95. });
  96. btn8.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  97. btn8.setBackground(Color.WHITE);
  98. btn8.setBounds(95, 117, 72, 63);
  99. btn8.setForeground(new Color(255, 69, 0));
  100. frame.getContentPane().add(btn8);
  101.  
  102. JButton btn5 = new JButton("5");
  103. btn5.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  104. btn5.setBackground(Color.WHITE);
  105. btn5.setBounds(95, 201, 72, 63);
  106. btn5.addActionListener(new ActionListener() {
  107. public void actionPerformed(ActionEvent e) {
  108. String EnterNumber = text.getText() + btn5.getText() ;
  109. text.setText(EnterNumber);
  110. }
  111. });
  112. btn5.setForeground(new Color(255, 69, 0));
  113. frame.getContentPane().add(btn5);
  114.  
  115. JButton btn2 = new JButton("2");
  116. btn2.addActionListener(new ActionListener() {
  117. public void actionPerformed(ActionEvent e) {
  118. String EnterNumber = text.getText() + btn2.getText() ;
  119. text.setText(EnterNumber);
  120. }
  121. });
  122. btn2.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  123. btn2.setBackground(Color.WHITE);
  124. btn2.setBounds(95, 284, 72, 63);
  125. btn2.setForeground(new Color(255, 69, 0));
  126. frame.getContentPane().add(btn2);
  127.  
  128. JButton btnDot = new JButton(".");
  129. btnDot.addActionListener(new ActionListener() {
  130. public void actionPerformed(ActionEvent e) {
  131. String Dot = text.getText() + "." ;
  132. text.setText(Dot);
  133. }
  134. });
  135. btnDot.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  136. btnDot.setBackground(Color.WHITE);
  137. btnDot.setBounds(92, 369, 72, 63);
  138. btnDot.setForeground(new Color(255, 69, 0));
  139. frame.getContentPane().add(btnDot);
  140.  
  141. JButton btn4 = new JButton("4");
  142. btn4.addActionListener(new ActionListener() {
  143. public void actionPerformed(ActionEvent e) {
  144. String EnterNumber =text.getText() + btn4.getText() ;
  145. text.setText(EnterNumber);
  146. }
  147. });
  148. btn4.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  149. btn4.setBackground(Color.WHITE);
  150. btn4.setBounds(10, 201, 72, 63);
  151. btn4.setForeground(new Color(255, 69, 0));
  152. frame.getContentPane().add(btn4);
  153.  
  154. JButton btn1 = new JButton("1");
  155. btn1.addActionListener(new ActionListener() {
  156. public void actionPerformed(ActionEvent e) {
  157. String EnterNumber =text.getText() + btn1.getText() ;
  158. text.setText(EnterNumber);
  159. }
  160. });
  161. btn1.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  162. btn1.setBackground(Color.WHITE);
  163. btn1.setBounds(10, 284, 72, 63);
  164. btn1.setForeground(new Color(255, 69, 0));
  165. frame.getContentPane().add(btn1);
  166.  
  167. JButton btn9 = new JButton("9");
  168. btn9.addActionListener(new ActionListener() {
  169. public void actionPerformed(ActionEvent e) {
  170. String EnterNumber = text.getText() +btn9.getText() ;
  171. text.setText(EnterNumber);
  172. }
  173. });
  174. btn9.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  175. btn9.setBackground(Color.WHITE);
  176. btn9.setBounds(177, 117, 72, 63);
  177. btn9.setForeground(new Color(255, 69, 0));
  178. frame.getContentPane().add(btn9);
  179.  
  180. JButton btn3 = new JButton("3");
  181. btn3.addActionListener(new ActionListener() {
  182. public void actionPerformed(ActionEvent e) {
  183. String EnterNumber = text.getText() +btn3.getText() ;
  184. text.setText(EnterNumber);
  185. }
  186. });
  187. btn3.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  188. btn3.setBackground(Color.WHITE);
  189. btn3.setBounds(177, 284, 72, 63);
  190. btn3.setForeground(new Color(255, 69, 0));
  191. frame.getContentPane().add(btn3);
  192.  
  193. JButton btnEqule = new JButton("=");
  194. btnEqule.addActionListener(new ActionListener() {
  195. public void actionPerformed(ActionEvent e) {
  196. String l = text.getText() ;
  197. essam.input(l);
  198. essam.getResult() ;
  199. text.setText(Double.toString(Calculate.Answer));
  200. }}
  201. );
  202. btnEqule.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  203. btnEqule.setBackground(Color.WHITE);
  204. btnEqule.setBounds(259, 358, 50, 74);
  205. btnEqule.setForeground(new Color(255, 69, 0));
  206. frame.getContentPane().add(btnEqule);
  207.  
  208. JButton btnDiv = new JButton("/");
  209. btnDiv.addActionListener(new ActionListener() {
  210. public void actionPerformed(ActionEvent e) {
  211. if(essam.CheckOperationErorr()) {
  212. FirstNum = Double.parseDouble(text.getText());
  213. text.setText("");
  214. Operation = '/' ;
  215. g = 1 ;}
  216. else {
  217. text.setText(" Wrong Syntaix Clear and try again ");
  218. g = 0 ;
  219. }
  220. }
  221. });
  222. btnDiv.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  223. btnDiv.setBackground(Color.WHITE);
  224. btnDiv.setBounds(259, 177, 50, 50);
  225. btnDiv.setForeground(new Color(255, 69, 0));
  226. frame.getContentPane().add(btnDiv);
  227.  
  228. JButton btnPlus = new JButton("+");
  229. btnPlus.addActionListener(new ActionListener() {
  230. public void actionPerformed(ActionEvent e) {
  231. if(essam.CheckOperationErorr()) {
  232. FirstNum = Double.parseDouble(text.getText());
  233. text.setText("");
  234. Operation = '+' ;
  235. g = 1 ;}
  236. else {
  237. text.setText(" Wrong Syntaix Clear and try again ");
  238. g = 0 ;
  239. }
  240. }
  241. });
  242. btnPlus.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  243. btnPlus.setBackground(Color.WHITE);
  244. btnPlus.setBounds(259, 236, 50, 50);
  245. btnPlus.setForeground(new Color(255, 69, 0));
  246. frame.getContentPane().add(btnPlus);
  247.  
  248. JButton btnMinze = new JButton("-");
  249. btnMinze.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  250. btnMinze.setBackground(Color.WHITE);
  251. btnMinze.setBounds(259, 297, 50, 50);
  252. btnMinze.addActionListener(new ActionListener() {
  253. public void actionPerformed(ActionEvent e) {
  254. if(essam.CheckOperationErorr()) {
  255. FirstNum = Double.parseDouble(text.getText());
  256. text.setText("");
  257. Operation = '-' ;
  258. g = 1 ;}
  259. else {
  260. text.setText(" Wrong Syntaix Clear and try again ");
  261. g = 0 ;
  262. }
  263. }
  264. });
  265. btnMinze.setForeground(new Color(255, 69, 0));
  266. frame.getContentPane().add(btnMinze);
  267.  
  268. JButton btn6 = new JButton("6");
  269. btn6.addActionListener(new ActionListener() {
  270. public void actionPerformed(ActionEvent e) {
  271. String EnterNumber = text.getText() +btn6.getText() ;
  272. text.setText(EnterNumber);
  273. }
  274. });
  275. btn6.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  276. btn6.setBackground(Color.WHITE);
  277. btn6.setBounds(177, 201, 72, 63);
  278. btn6.setForeground(new Color(255, 69, 0));
  279. frame.getContentPane().add(btn6);
  280.  
  281. JButton btnMulti = new JButton("*");
  282. btnMulti.addActionListener(new ActionListener() {
  283. public void actionPerformed(ActionEvent e) {
  284. //if(essam.CheckOperationErorr()) {
  285. FirstNum = Double.parseDouble(text.getText());
  286. text.setText("");
  287. Operation = '*' ;
  288. g = 1 ;
  289. /*}
  290. else {
  291. txtDisplay.setText(" Wrong Syntaix Clear and try again ");
  292. g = 0 ;
  293. }*/
  294. }
  295. });
  296. btnMulti.setForeground(new Color(255, 69, 0));
  297. btnMulti.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  298. btnMulti.setBackground(Color.WHITE);
  299. btnMulti.setBounds(259, 116, 50, 50);
  300. frame.getContentPane().add(btnMulti);
  301.  
  302. JButton btnNigtive = new JButton("+/-");
  303. btnNigtive.addActionListener(new ActionListener() {
  304. public void actionPerformed(ActionEvent e) {
  305. double ops = Double.parseDouble(String.valueOf(text.getText()));
  306. ops = ops * (-1) ;
  307. text.setText(String.valueOf(ops));
  308. }
  309. });
  310. btnNigtive.setForeground(new Color(255, 69, 0));
  311. btnNigtive.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  312. btnNigtive.setBackground(Color.WHITE);
  313. btnNigtive.setBounds(177, 369, 72, 63);
  314. frame.getContentPane().add(btnNigtive);
  315.  
  316. JButton btn7 = new JButton("7");
  317. btn7.addActionListener(new ActionListener() {
  318. public void actionPerformed(ActionEvent e) {
  319. String EnterNumber = text.getText() + btn7.getText() ;
  320. text.setText(EnterNumber);
  321. }
  322. });
  323. btn7.setForeground(new Color(255, 69, 0));
  324. btn7.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  325. btn7.setBackground(Color.WHITE);
  326. btn7.setBounds(10, 117, 72, 63);
  327. frame.getContentPane().add(btn7);
  328.  
  329. JButton btnC = new JButton("C");
  330. btnC.addActionListener(new ActionListener() {
  331. public void actionPerformed(ActionEvent e) {
  332. text.setText("");
  333. }
  334. });
  335. btnC.setForeground(new Color(255, 69, 0));
  336. btnC.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
  337. btnC.setBackground(Color.WHITE);
  338. btnC.setBounds(319, 117, 88, 122);
  339. frame.getContentPane().add(btnC);
  340.  
  341. JButton btnSave = new JButton("Save");
  342. btnSave.setForeground(new Color(255, 69, 0));
  343. btnSave.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 11));
  344. btnSave.setBackground(Color.WHITE);
  345. btnSave.setBounds(319, 248, 88, 38);
  346. frame.getContentPane().add(btnSave);
  347.  
  348. JButton btnLoad = new JButton("Load");
  349. btnLoad.addActionListener(new ActionListener() {
  350. public void actionPerformed(ActionEvent e) {
  351. }
  352. });
  353. btnLoad.setForeground(new Color(255, 69, 0));
  354. btnLoad.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 11));
  355. btnLoad.setBackground(Color.WHITE);
  356. btnLoad.setBounds(319, 297, 88, 38);
  357. frame.getContentPane().add(btnLoad);
  358.  
  359. JButton btnNext = new JButton("Next");
  360. btnNext.addActionListener(new ActionListener() {
  361. public void actionPerformed(ActionEvent e) {
  362. essam.next();
  363. }
  364. });
  365. btnNext.setForeground(new Color(255, 69, 0));
  366. btnNext.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 11));
  367. btnNext.setBackground(Color.WHITE);
  368. btnNext.setBounds(319, 346, 88, 38);
  369. frame.getContentPane().add(btnNext);
  370.  
  371. JButton btnPrevious = new JButton("Previous");
  372. btnPrevious.setForeground(new Color(255, 69, 0));
  373. btnPrevious.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 11));
  374. btnPrevious.setBackground(Color.WHITE);
  375. btnPrevious.setBounds(319, 394, 88, 38);
  376. frame.getContentPane().add(btnPrevious);
  377. btnNext.addActionListener(new ActionListener() {
  378. public void actionPerformed(ActionEvent e) {
  379. essam.prev();
  380. }
  381. });
  382.  
  383. }
  384. private class SwingAction extends AbstractAction {
  385. public SwingAction() {
  386. putValue(NAME, "SwingAction");
  387. putValue(SHORT_DESCRIPTION, "Some short description");
  388. }
  389. public void actionPerformed(ActionEvent e) {
  390. }
  391. }
  392. public static class Calculate implements Calculator {
  393. String S = text.getText() ;
  394. static Double Answer ;
  395. int i = 0 ;
  396. int j = 0 ;
  397. int z = 0 ;
  398. int w = 0 ;
  399. public void DisplayNumbers (String EnterNumber) {
  400. text.setText(EnterNumber);
  401. }
  402.  
  403. public boolean CheckOperationErorr() {
  404. if(g == 0) {
  405. return true;}
  406. else {
  407. return false ;
  408. }}
  409. public void Save () {
  410. String Savee = Double.toString(FirstNum) + Operation + Double.toString(SecondNum) ;
  411. if(Saved.size() < 5) {
  412. Saved.add(Savee);
  413. }
  414. else {
  415. Saved.remove(0);
  416. Saved.add(4,Savee) ;
  417. }
  418. AutoDelete();
  419. }
  420. public void AutoDelete () {
  421. FirstNum = 0 ;
  422. SecondNum = 0 ;
  423. g = 0 ;
  424. }
  425.  
  426. @Override
  427. public void input(String s) {
  428. while( i < S.length()) {
  429. if(S.charAt(i) == '+' ||S.charAt(i) == '/' ||S.charAt(i) == '*' ||S.charAt(i) == '-' ) {
  430. j = 1 ;
  431. z = i ;
  432. }
  433. i++ ;
  434. }
  435. if(j != 0 ) {
  436. String first = "" ;
  437. String second = "" ;
  438. Operation = S.charAt(z) ;
  439. for(w =0 ; w < z ; w++) {
  440. first = first + S.charAt(w) ;
  441. }
  442. for(w = z+1 ; w < S.length() ; w++) {
  443. second = second + S.charAt(w) ;
  444. }
  445. FirstNum = Double.parseDouble(first) ;
  446. double l ;
  447. l = Double.parseDouble(second);
  448. SecondNum = l ;
  449. }
  450.  
  451. else {
  452. SecondNum = Double.parseDouble(text.getText()) ;
  453. }}
  454. @Override
  455. public String getResult() {
  456. if( Operation == '+') {
  457. Answer = FirstNum + SecondNum ;
  458. text.setText(String.valueOf(Answer));
  459. Save();
  460. }
  461. else if( Operation == '-') {
  462. Answer = FirstNum - SecondNum ;
  463. text.setText(String.valueOf(Answer));
  464. Save();
  465. }
  466. else if( Operation == '*') {
  467. Answer = FirstNum * SecondNum ;
  468. text.setText(String.valueOf(Answer));
  469. Save();
  470. }
  471. else if( Operation == '/') {
  472. if(SecondNum == 0) {
  473. text.setText("Mathmatical Erorr");
  474. }
  475. else {
  476. Answer = FirstNum / SecondNum ;
  477. text.setText(String.valueOf(Answer));
  478. Save();
  479. }
  480. }
  481.  
  482. return current();
  483. }
  484. @Override
  485. public String current() {
  486. if(Saved.size() == 5) {
  487. return Saved.get(4);}
  488. else if (text.getText() == "Mathmatical Erorr") {
  489. return null ;
  490. }
  491. else {
  492. return Saved.get(Saved.size()) ;
  493. }
  494. }
  495. public String prev() {
  496. if(p < 5 ) {
  497. (p)++;
  498. return Saved.get(5-(p-1));
  499. }
  500. else {
  501. text.setText(" Nothing to show ");
  502. return null ;
  503. }
  504. }
  505. public String next() {
  506. if(p == 1) {
  507. text.setText("Nothing to show");
  508. return null ;
  509. }
  510. else {
  511. p-- ;
  512. return Saved.get(5-p);
  513. }
  514. }
  515. @Override
  516. public void save() {
  517. // TODO Auto-generated method stub
  518.  
  519. }
  520. @Override
  521. public void load() {
  522. // TODO Auto-generated method stub
  523.  
  524. }
  525. }
  526. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement