Main /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package CallCalc; import InterfaceGrafica.CalculadoraUI; /** * * @author Diego */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { CalculadoraUI calcUI = new CalculadoraUI(); calcUI.setVisible(true); } } Calculadora.java /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Operacional; /** * * @author Diego Doná * @version 2 */ public class Calculadora { //controla a "primeira troca de operação" da calculadora. //Na primeira troca, o resultado é o número que estamos lidando. //Nas seguintes, o resultado é a execução de uma operação private boolean primeiraOperacao; //guarda o resultado final das operações private double resultado; //essa variável contem o número atual com que estamos lidando private double numeroAtual; //guarda com a operação que estamos realizando private EnumOperacoes operacao; //verifica se é a primeira finalização (5 / 2 *CALCULAR*). //se for a primeira, executamos a operação e guardamos o número //se não for, executa a operação com o número guardado private boolean primeiraFinalizacao; public Calculadora() { primeiraOperacao = true; operacao = null; resultado = 0; primeiraFinalizacao = true; } //PARA EXPOR O RESULTADO AO RESTO DOS OBJETOS public double getResultado() { return this.resultado; } //A ÚNICA INTERAÇÃO COM A CLASSE É NA TROCA DE OPERAÇÕES. //DEVE-SE FORNECER A OPERACAO DESEJADA E O NÚMERO public void TrocarDeOperacao(EnumOperacoes pOperacao, double pNumero) { this.primeiraFinalizacao = true; this.numeroAtual = pNumero; //se é a primeira vez, o resultado é o número escolhido if (this.primeiraOperacao) { this.resultado = this.numeroAtual; this.primeiraOperacao = false; } //se não for a primeira vez, //devo calcular o resultado antes de trocar de operador else { this.CalcularOperacaoInterna(); } //guardo a operação this.operacao = pOperacao; } //CALCULO O RESULTADO DE ACORDO COM A OPERAÇÃO GUARDADA private void CalcularOperacaoInterna() { switch(this.operacao) { case DIVISAO: //não pode dividir por 0 if(this.numeroAtual != 0) this.resultado /= this.numeroAtual; break; case MULTIPLICACAO: this.resultado *= this.numeroAtual; break; case SOMA: this.resultado += this.numeroAtual; break; case SUBTRACAO: this.resultado -= this.numeroAtual; break; } } //EXECUTADO EXTERNAMENTE A CLASSE, ATRAVÉS DO BOTÃO DE CALCULAR //DEVO PASSAR O NÚMERO QUE ESTÁ NO VISOR. SE FOR A PRIMEIRA VEZ, //UTILIZO O NÚMERO DO VISOR. CASO CONTRÁRIO CALCULO O VALOR //COM O PENÚLTIMO NÚMERO QUE USEI. //EX: 10 / 5 . Ao clicar em calcular, faço 10 / 5. Se eu clicar em //calcular novamente, farei 2 / 5. public double CalcularOperacaoFinal(double pNumero) { if(this.primeiraFinalizacao) { this.numeroAtual = pNumero; this.primeiraFinalizacao = false; } this.primeiraOperacao = true; this.CalcularOperacaoInterna(); return this.resultado; } //PARA O BOTÃO DE CLEAR public void Limpar() { primeiraOperacao = true; operacao = null; resultado = 0; primeiraFinalizacao = true; } } EnumOperacoes /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Operacional; /** * * @author Diego */ public enum EnumOperacoes { DIVISAO, MULTIPLICACAO, SOMA, SUBTRACAO } CalculadoraUI /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * CalculadoraUI.java * * Created on 15/08/2010, 16:44:34 */ package InterfaceGrafica; import Operacional.Calculadora; import Operacional.EnumOperacoes; import javax.swing.JButton; import javax.swing.JOptionPane; /** * * @author Diego */ public class CalculadoraUI extends javax.swing.JFrame { private static Calculadora calc = new Calculadora(); /** Creates new form CalculadoraUI */ public CalculadoraUI() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); btnDiv = new javax.swing.JButton(); btnMinus = new javax.swing.JButton(); btnMult = new javax.swing.JButton(); btnMais = new javax.swing.JButton(); txtVisor = new javax.swing.JTextField(); btnCalcular = new javax.swing.JButton(); jPanel2 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jButton5 = new javax.swing.JButton(); jButton6 = new javax.swing.JButton(); jButton7 = new javax.swing.JButton(); jButton8 = new javax.swing.JButton(); jButton9 = new javax.swing.JButton(); jButton10 = new javax.swing.JButton(); jButton11 = new javax.swing.JButton(); jButton12 = new javax.swing.JButton(); jSeparator1 = new javax.swing.JSeparator(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Calculadora V2.0"); setFont(new java.awt.Font("Arial", 0, 10)); // NOI18N setResizable(false); jPanel1.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background")); btnDiv.setBackground(java.awt.Color.darkGray); btnDiv.setForeground(new java.awt.Color(255, 255, 255)); btnDiv.setText("/"); btnDiv.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { TrocarOperacao(evt); } }); btnMinus.setBackground(java.awt.Color.darkGray); btnMinus.setForeground(new java.awt.Color(255, 255, 255)); btnMinus.setText("-"); btnMinus.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { TrocarOperacao(evt); } }); btnMult.setBackground(java.awt.Color.darkGray); btnMult.setForeground(new java.awt.Color(255, 255, 255)); btnMult.setText("*"); btnMult.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { TrocarOperacao(evt); } }); btnMais.setBackground(java.awt.Color.darkGray); btnMais.setForeground(new java.awt.Color(255, 255, 255)); btnMais.setText("+"); btnMais.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { TrocarOperacao(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btnMais, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnMinus, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnMult, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnDiv, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(btnDiv, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnMult, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnMais, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); txtVisor.setBackground(java.awt.SystemColor.activeCaptionText); txtVisor.setEditable(false); btnCalcular.setText("CALCULAR"); btnCalcular.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCalcularActionPerformed(evt); } }); jPanel2.setBackground(javax.swing.UIManager.getDefaults().getColor("ToolTip.background")); jButton1.setText("7"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MostrarNumero(evt); } }); jButton2.setText("9"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MostrarNumero(evt); } }); jButton3.setText("8"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MostrarNumero(evt); } }); jButton4.setText("4"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MostrarNumero(evt); } }); jButton5.setText("5"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MostrarNumero(evt); } }); jButton6.setText("6"); jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MostrarNumero(evt); } }); jButton7.setText("1"); jButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MostrarNumero(evt); } }); jButton8.setText("2"); jButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MostrarNumero(evt); } }); jButton9.setText("3"); jButton9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MostrarNumero(evt); } }); jButton10.setText("0"); jButton10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { MostrarNumero(evt); } }); jButton11.setBackground(new java.awt.Color(241, 240, 239)); jButton11.setText("."); jButton11.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton11ActionPerformed(evt); } }); jButton12.setBackground(new java.awt.Color(153, 153, 153)); jButton12.setText("CE"); jButton12.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton12ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(131, 131, 131)) ); jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton4, jButton5, jButton6}); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)) .addGap(9, 9, 9) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton5) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton2, jButton3}); jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton4, jButton5, jButton6}); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtVisor, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(btnCalcular, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE) .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(txtVisor, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(18, 18, 18) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(17, 17, 17) .addComponent(btnCalcular, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); pack(); }// </editor-fold> //mostra numero no txtVisor private void MostrarNumero(java.awt.event.ActionEvent evt) { JButton genBtn = (JButton)evt.getSource(); txtVisor.setText( txtVisor.getText().concat(genBtn.getText())); } //inserir ponto no txtVisor private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) { JButton genBtn = (JButton)evt.getSource(); //se já não tiver um ponto... if(!txtVisor.getText().contains(".")) { txtVisor.setText(txtVisor.getText().concat(genBtn.getText())); } } //TROCAR DE OPERACAO private void TrocarOperacao(java.awt.event.ActionEvent evt) { JButton genBtn = (JButton)evt.getSource(); EnumOperacoes operacao = null; double numero = Double.parseDouble(txtVisor.getText()); if(genBtn.getText().compareTo("/") == 0) operacao = EnumOperacoes.DIVISAO; else if(genBtn.getText().compareTo("*") == 0) operacao = EnumOperacoes.MULTIPLICACAO; else if(genBtn.getText().compareTo("+") == 0) operacao = EnumOperacoes.SOMA; else operacao = EnumOperacoes.SUBTRACAO; calc.TrocarDeOperacao(operacao, numero); txtVisor.setText(""); } private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) { try { double numero = Double.parseDouble(txtVisor.getText()); txtVisor.setText(String.valueOf(calc.CalcularOperacaoFinal(numero))); } catch(Exception e) { JOptionPane.showMessageDialog(this, "Erro ao calcular! Calculadora re iniciada." + "\n'" + e.getMessage() + "'"); calc.Limpar(); } } //CE private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) { calc.Limpar(); txtVisor.setText(""); } // /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new CalculadoraUI().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnCalcular; private javax.swing.JButton btnDiv; private javax.swing.JButton btnMais; private javax.swing.JButton btnMinus; private javax.swing.JButton btnMult; private javax.swing.JButton jButton1; private javax.swing.JButton jButton10; private javax.swing.JButton jButton11; private javax.swing.JButton jButton12; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JButton jButton6; private javax.swing.JButton jButton7; private javax.swing.JButton jButton8; private javax.swing.JButton jButton9; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JSeparator jSeparator1; private javax.swing.JTextField txtVisor; // End of variables declaration }