Skip to content

Instantly share code, notes, and snippets.

@sakti
Created November 26, 2010 01:52
Show Gist options
  • Save sakti/716178 to your computer and use it in GitHub Desktop.
Save sakti/716178 to your computer and use it in GitHub Desktop.
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;
public class CalculatorClient extends javax.swing.JFrame {
private String endPoint,operasi;
private Service service;
private double hasil, tmp;
private boolean berubah;
public CalculatorClient() {
initComponents();
endPoint = "http://localhost:8080/axis/Hitung.jws";
service = new Service();
hasil = tmp = 0.0;
berubah = false;
operasi = "";
}
private double request(String op, double a, double b) {
Double nilai = 0.0;
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endPoint));
call.setOperationName(op);
call.addParameter("op1", XMLType.XSD_DOUBLE, ParameterMode.IN);
call.addParameter("op2", XMLType.XSD_DOUBLE, ParameterMode.IN);
call.setReturnType(XMLType.XSD_DOUBLE);
nilai = (Double) call.invoke(new Object[]{a, b});
} catch (Exception ex) {
ex.printStackTrace();
}
return nilai;
}
private void klikTombol(java.awt.event.ActionEvent evt){
String no = evt.getActionCommand();
if (tmp == 0) {
txtHasil.setText(no);
} else {
txtHasil.setText(txtHasil.getText() + no);
}
tmp = Double.parseDouble(txtHasil.getText());
berubah = true;
}
private void klikOperator(java.awt.event.ActionEvent evt){
String op = evt.getActionCommand();
if (!txtKeterangan.getText().endsWith(op+" ") && txtKeterangan.getText().length() > 2 && !berubah) {
txtKeterangan.setText(txtKeterangan.getText().substring(0, txtKeterangan.getText().length() - 3) + " "+op+" ");
} else if (berubah) {
if (txtKeterangan.getText().trim().length() == 0) {
hasil = tmp;
} else {
hasil = request(operasi, hasil, tmp);
txtHasil.setText(hasil + "");
}
txtKeterangan.setText(txtKeterangan.getText() + String.format("%.0f", tmp) + " "+op+" ");
tmp = 0;
}
if(op.equals("+")){
operasi = "tambah";
}else if(op.equals("-")){
operasi = "kurang";
}else if(op.equals("*")){
operasi = "kali";
}else{
operasi = "bagi";
}
berubah = false;
}
//code untuk event click tombol digit input
private void tombol1ActionPerformed(java.awt.event.ActionEvent evt) {
klikTombol(evt);
}
private void tombol2ActionPerformed(java.awt.event.ActionEvent evt) {
klikTombol(evt);
}
private void tombol3ActionPerformed(java.awt.event.ActionEvent evt) {
klikTombol(evt);
}
//lengkapi untuk tombol 4 sampai tombol 0
//tombol C, untuk menghapus
private void tombolCActionPerformed(java.awt.event.ActionEvent evt) {
tmp = 0;
hasil = 0;
txtHasil.setText("0");
txtKeterangan.setText("");
}
//tombol operasi tambah
private void tombolTambahActionPerformed(java.awt.event.ActionEvent evt) {
klikOperator(evt);
}
//tombol operasi kurang
private void tombolKurangActionPerformed(java.awt.event.ActionEvent evt) {
klikOperator(evt);
}
//lengkapi untuk operasi kali dan bagi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment