Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.oop.calculator.cs41;
- import java.awt.Color;
- import java.awt.EventQueue;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import javax.swing.AbstractAction;
- import javax.swing.AbstractButton;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- import javax.swing.SwingConstants;
- public class calculators {
- protected static final Calculate essam = null;
- public JFrame frame;
- static JTextField text;
- static double FirstNum ;
- static double SecondNum ;
- static char Operation ;
- double Result ;
- String Final ;
- String Savee ;
- static int g = 0;
- static int p = 1 ;
- static ArrayList<String> Saved = new ArrayList<String>(5) ;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- calculators window = new calculators();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public calculators() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- public void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 450, 300);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame = new JFrame();
- frame.getContentPane().setBackground(Color.GRAY);
- frame.getContentPane().setForeground(new Color(0, 0, 0));
- frame.setBounds(100, 100, 433, 482);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- text = new JTextField();
- text.setFont(new Font("Yu Gothic UI", Font.BOLD, 20));
- text.setHorizontalAlignment(SwingConstants.RIGHT);
- text.setBackground(Color.LIGHT_GRAY);
- text.setBounds(10, 11, 397, 84);
- frame.getContentPane().add(text);
- text.setColumns(10);
- JButton btn0 = new JButton("0");
- btn0.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btn0.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String EnterNumber = text.getText() + btn0.getText() ;
- text.setText(EnterNumber);
- }
- });
- btn0.setBackground(Color.WHITE);
- btn0.setBounds(10, 369, 72, 63);
- btn0.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btn0);
- JButton btn8 = new JButton("8");
- btn8.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String EnterNumber = text.getText() +btn8.getText() ;
- text.setText(EnterNumber);
- }
- });
- btn8.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btn8.setBackground(Color.WHITE);
- btn8.setBounds(95, 117, 72, 63);
- btn8.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btn8);
- JButton btn5 = new JButton("5");
- btn5.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btn5.setBackground(Color.WHITE);
- btn5.setBounds(95, 201, 72, 63);
- btn5.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String EnterNumber = text.getText() + btn5.getText() ;
- text.setText(EnterNumber);
- }
- });
- btn5.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btn5);
- JButton btn2 = new JButton("2");
- btn2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String EnterNumber = text.getText() + btn2.getText() ;
- text.setText(EnterNumber);
- }
- });
- btn2.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btn2.setBackground(Color.WHITE);
- btn2.setBounds(95, 284, 72, 63);
- btn2.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btn2);
- JButton btnDot = new JButton(".");
- btnDot.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String Dot = text.getText() + "." ;
- text.setText(Dot);
- }
- });
- btnDot.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btnDot.setBackground(Color.WHITE);
- btnDot.setBounds(92, 369, 72, 63);
- btnDot.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btnDot);
- JButton btn4 = new JButton("4");
- btn4.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String EnterNumber =text.getText() + btn4.getText() ;
- text.setText(EnterNumber);
- }
- });
- btn4.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btn4.setBackground(Color.WHITE);
- btn4.setBounds(10, 201, 72, 63);
- btn4.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btn4);
- JButton btn1 = new JButton("1");
- btn1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String EnterNumber =text.getText() + btn1.getText() ;
- text.setText(EnterNumber);
- }
- });
- btn1.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btn1.setBackground(Color.WHITE);
- btn1.setBounds(10, 284, 72, 63);
- btn1.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btn1);
- JButton btn9 = new JButton("9");
- btn9.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String EnterNumber = text.getText() +btn9.getText() ;
- text.setText(EnterNumber);
- }
- });
- btn9.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btn9.setBackground(Color.WHITE);
- btn9.setBounds(177, 117, 72, 63);
- btn9.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btn9);
- JButton btn3 = new JButton("3");
- btn3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String EnterNumber = text.getText() +btn3.getText() ;
- text.setText(EnterNumber);
- }
- });
- btn3.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btn3.setBackground(Color.WHITE);
- btn3.setBounds(177, 284, 72, 63);
- btn3.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btn3);
- JButton btnEqule = new JButton("=");
- btnEqule.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String l = text.getText() ;
- essam.input(l);
- essam.getResult() ;
- text.setText(Double.toString(Calculate.Answer));
- }}
- );
- btnEqule.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btnEqule.setBackground(Color.WHITE);
- btnEqule.setBounds(259, 358, 50, 74);
- btnEqule.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btnEqule);
- JButton btnDiv = new JButton("/");
- btnDiv.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(essam.CheckOperationErorr()) {
- FirstNum = Double.parseDouble(text.getText());
- text.setText("");
- Operation = '/' ;
- g = 1 ;}
- else {
- text.setText(" Wrong Syntaix Clear and try again ");
- g = 0 ;
- }
- }
- });
- btnDiv.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btnDiv.setBackground(Color.WHITE);
- btnDiv.setBounds(259, 177, 50, 50);
- btnDiv.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btnDiv);
- JButton btnPlus = new JButton("+");
- btnPlus.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(essam.CheckOperationErorr()) {
- FirstNum = Double.parseDouble(text.getText());
- text.setText("");
- Operation = '+' ;
- g = 1 ;}
- else {
- text.setText(" Wrong Syntaix Clear and try again ");
- g = 0 ;
- }
- }
- });
- btnPlus.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btnPlus.setBackground(Color.WHITE);
- btnPlus.setBounds(259, 236, 50, 50);
- btnPlus.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btnPlus);
- JButton btnMinze = new JButton("-");
- btnMinze.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btnMinze.setBackground(Color.WHITE);
- btnMinze.setBounds(259, 297, 50, 50);
- btnMinze.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(essam.CheckOperationErorr()) {
- FirstNum = Double.parseDouble(text.getText());
- text.setText("");
- Operation = '-' ;
- g = 1 ;}
- else {
- text.setText(" Wrong Syntaix Clear and try again ");
- g = 0 ;
- }
- }
- });
- btnMinze.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btnMinze);
- JButton btn6 = new JButton("6");
- btn6.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String EnterNumber = text.getText() +btn6.getText() ;
- text.setText(EnterNumber);
- }
- });
- btn6.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btn6.setBackground(Color.WHITE);
- btn6.setBounds(177, 201, 72, 63);
- btn6.setForeground(new Color(255, 69, 0));
- frame.getContentPane().add(btn6);
- JButton btnMulti = new JButton("*");
- btnMulti.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- //if(essam.CheckOperationErorr()) {
- FirstNum = Double.parseDouble(text.getText());
- text.setText("");
- Operation = '*' ;
- g = 1 ;
- /*}
- else {
- txtDisplay.setText(" Wrong Syntaix Clear and try again ");
- g = 0 ;
- }*/
- }
- });
- btnMulti.setForeground(new Color(255, 69, 0));
- btnMulti.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btnMulti.setBackground(Color.WHITE);
- btnMulti.setBounds(259, 116, 50, 50);
- frame.getContentPane().add(btnMulti);
- JButton btnNigtive = new JButton("+/-");
- btnNigtive.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(text.getText()));
- ops = ops * (-1) ;
- text.setText(String.valueOf(ops));
- }
- });
- btnNigtive.setForeground(new Color(255, 69, 0));
- btnNigtive.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btnNigtive.setBackground(Color.WHITE);
- btnNigtive.setBounds(177, 369, 72, 63);
- frame.getContentPane().add(btnNigtive);
- JButton btn7 = new JButton("7");
- btn7.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String EnterNumber = text.getText() + btn7.getText() ;
- text.setText(EnterNumber);
- }
- });
- btn7.setForeground(new Color(255, 69, 0));
- btn7.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btn7.setBackground(Color.WHITE);
- btn7.setBounds(10, 117, 72, 63);
- frame.getContentPane().add(btn7);
- JButton btnC = new JButton("C");
- btnC.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- text.setText("");
- }
- });
- btnC.setForeground(new Color(255, 69, 0));
- btnC.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 20));
- btnC.setBackground(Color.WHITE);
- btnC.setBounds(319, 117, 88, 122);
- frame.getContentPane().add(btnC);
- JButton btnSave = new JButton("Save");
- btnSave.setForeground(new Color(255, 69, 0));
- btnSave.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 11));
- btnSave.setBackground(Color.WHITE);
- btnSave.setBounds(319, 248, 88, 38);
- frame.getContentPane().add(btnSave);
- JButton btnLoad = new JButton("Load");
- btnLoad.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- }
- });
- btnLoad.setForeground(new Color(255, 69, 0));
- btnLoad.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 11));
- btnLoad.setBackground(Color.WHITE);
- btnLoad.setBounds(319, 297, 88, 38);
- frame.getContentPane().add(btnLoad);
- JButton btnNext = new JButton("Next");
- btnNext.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- essam.next();
- }
- });
- btnNext.setForeground(new Color(255, 69, 0));
- btnNext.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 11));
- btnNext.setBackground(Color.WHITE);
- btnNext.setBounds(319, 346, 88, 38);
- frame.getContentPane().add(btnNext);
- JButton btnPrevious = new JButton("Previous");
- btnPrevious.setForeground(new Color(255, 69, 0));
- btnPrevious.setFont(new Font("Yu Gothic UI Semilight", Font.BOLD, 11));
- btnPrevious.setBackground(Color.WHITE);
- btnPrevious.setBounds(319, 394, 88, 38);
- frame.getContentPane().add(btnPrevious);
- btnNext.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- essam.prev();
- }
- });
- }
- private class SwingAction extends AbstractAction {
- public SwingAction() {
- putValue(NAME, "SwingAction");
- putValue(SHORT_DESCRIPTION, "Some short description");
- }
- public void actionPerformed(ActionEvent e) {
- }
- }
- public static class Calculate implements Calculator {
- String S = text.getText() ;
- static Double Answer ;
- int i = 0 ;
- int j = 0 ;
- int z = 0 ;
- int w = 0 ;
- public void DisplayNumbers (String EnterNumber) {
- text.setText(EnterNumber);
- }
- public boolean CheckOperationErorr() {
- if(g == 0) {
- return true;}
- else {
- return false ;
- }}
- public void Save () {
- String Savee = Double.toString(FirstNum) + Operation + Double.toString(SecondNum) ;
- if(Saved.size() < 5) {
- Saved.add(Savee);
- }
- else {
- Saved.remove(0);
- Saved.add(4,Savee) ;
- }
- AutoDelete();
- }
- public void AutoDelete () {
- FirstNum = 0 ;
- SecondNum = 0 ;
- g = 0 ;
- }
- @Override
- public void input(String s) {
- while( i < S.length()) {
- if(S.charAt(i) == '+' ||S.charAt(i) == '/' ||S.charAt(i) == '*' ||S.charAt(i) == '-' ) {
- j = 1 ;
- z = i ;
- }
- i++ ;
- }
- if(j != 0 ) {
- String first = "" ;
- String second = "" ;
- Operation = S.charAt(z) ;
- for(w =0 ; w < z ; w++) {
- first = first + S.charAt(w) ;
- }
- for(w = z+1 ; w < S.length() ; w++) {
- second = second + S.charAt(w) ;
- }
- FirstNum = Double.parseDouble(first) ;
- double l ;
- l = Double.parseDouble(second);
- SecondNum = l ;
- }
- else {
- SecondNum = Double.parseDouble(text.getText()) ;
- }}
- @Override
- public String getResult() {
- if( Operation == '+') {
- Answer = FirstNum + SecondNum ;
- text.setText(String.valueOf(Answer));
- Save();
- }
- else if( Operation == '-') {
- Answer = FirstNum - SecondNum ;
- text.setText(String.valueOf(Answer));
- Save();
- }
- else if( Operation == '*') {
- Answer = FirstNum * SecondNum ;
- text.setText(String.valueOf(Answer));
- Save();
- }
- else if( Operation == '/') {
- if(SecondNum == 0) {
- text.setText("Mathmatical Erorr");
- }
- else {
- Answer = FirstNum / SecondNum ;
- text.setText(String.valueOf(Answer));
- Save();
- }
- }
- return current();
- }
- @Override
- public String current() {
- if(Saved.size() == 5) {
- return Saved.get(4);}
- else if (text.getText() == "Mathmatical Erorr") {
- return null ;
- }
- else {
- return Saved.get(Saved.size()) ;
- }
- }
- public String prev() {
- if(p < 5 ) {
- (p)++;
- return Saved.get(5-(p-1));
- }
- else {
- text.setText(" Nothing to show ");
- return null ;
- }
- }
- public String next() {
- if(p == 1) {
- text.setText("Nothing to show");
- return null ;
- }
- else {
- p-- ;
- return Saved.get(5-p);
- }
- }
- @Override
- public void save() {
- // TODO Auto-generated method stub
- }
- @Override
- public void load() {
- // TODO Auto-generated method stub
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement