public class CalculatorModel { int operator=0; double operand1=0; double operand2=0; double result=0; public void setOperand(String opr) { if(!opr.equals("")){ if(operator==0){ operand1=Double.valueOf(opr); }else{ operand2=Double.valueOf(opr); } } } public void setOperator(int operator) { this.operator = operator; } public double getResult() { return result; } public void setResult(double hasil) { this.result = hasil; } public void process(){ switch (operator){ case 1: result = operand1 + operand2; break; case 2: result = operand1 - operand2; break; case 3: result = operand1 * operand2; break; case 4: result = operand1 / operand2; break; case 5: result = operand1 % operand2; break; case 6: result = 1/operand1; break; default: result = operand1; } operand1=result; } public void setOperand1(double operand1) { this.operand1 = operand1; } public void setOperand2(double operand2) { this.operand2 = operand2; } public void clear(){ setOperand1(0); setOperand2(0); setResult(0); setOperator(0); } }
package view; import java.text.DecimalFormat; import javax.swing.JOptionPane; import model.CalculatorModel; public class CalculatorView extends javax.swing.JFrame { /** Creates new form CalculatorView */ public CalculatorView() { initComponents(); } CalculatorModel model = new CalculatorModel(); String operand=""; public void getOperand(javax.swing.JButton button){ operand+=button.getText(); resultLabel.setText(operand); } private void getOperator(int opt){ model.setOperand(operand); model.setOperator(opt); if(opt == 1 || opt == 2){ model.process(); resultLabel.setText(model.getResult()+""); }else{ resultLabel.setText(operand); } operand = ""; } private void process(){ DecimalFormat df = new DecimalFormat("#,###.########"); model.setOperand(operand); model.process(); operand = ""; resultLabel.setText(df.format(model.getResult())+""); } private void button1ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button1); } private void button2ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button2); } private void button3ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button3); } private void button4ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button4); } private void button5ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button5); } private void button6ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button6); } private void button7ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button7); } private void button8ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button8); } private void button9ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button9); } private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt) { getOperator(1); } private void buttonKurangActionPerformed(java.awt.event.ActionEvent evt) { getOperator(2); } private void buttonKaliActionPerformed(java.awt.event.ActionEvent evt) { getOperator(3); } private void buttonBagiActionPerformed(java.awt.event.ActionEvent evt) { getOperator(4); } private void buttonModulusActionPerformed(java.awt.event.ActionEvent evt) { getOperator(5); } private void buttonSeperActionPerformed(java.awt.event.ActionEvent evt) { getOperator(6); } private void buttonSamaDenganActionPerformed(java.awt.event.ActionEvent evt) { process(); } private void button11ActionPerformed(java.awt.event.ActionEvent evt) { getOperand(button11); } private void buttonKomaActionPerformed(java.awt.event.ActionEvent evt) { getOperand(buttonKoma); } private void button12ActionPerformed(java.awt.event.ActionEvent evt) { if(operand.length()>1){ operand = operand.substring(0, operand.length()-1); model.setOperand(operand); resultLabel.setText(operand); }else{ operand = ""; model.setOperand(operand); resultLabel.setText("0"); } } private void buttonACActionPerformed(java.awt.event.ActionEvent evt) { operand = ""; model.clear(); resultLabel.setText("0"); } private void menuExitActionPerformed(java.awt.event.ActionEvent evt) { if(JOptionPane.showConfirmDialog(rootPane, "Are you sure you will exit this application?","Exit Application",1)==0){ System.exit(0); } }
import view.CalculatorView; public class Main { public static void main(String[] args) { new CalculatorView().setVisible(true); } }
Posted by 07.17 and have
0
komentar
, Published at
Tidak ada komentar:
Posting Komentar