Thursday, February 26, 2015

Project Sederhana Penggajian dengan Java


Pengaturan Variable Name:
Klik Kanan > Change Variable Name
- JTextField  (txtnip, txtnama, xtgapok, txtistri, txtjumlah, txtanak, txtgator, txtpajak, txtgaber)
- JComboBox (cmbjabatan)
- JRadioButton (opsi1,opsi2)
- JButton (btnbersih, btnhitung)

Ketikkan kode program dibawah baris public class jfrmGaji extends javax.swing.JFrame {
  1. int gapok;
  2. int gator;
  3. int pajak;
  4. int gaber;
  5. int t_istri;
  6. int t_anak;
  7. int j_anak;
  8. String t_istri_1;
  9. String t_anak_1;
  10. String gator_1;
  11. String pajak_1;
  12. String gaber_1;

Pastikan atur properties variabel name pada jframe sebelum mengetik kode program dibawah ini:
  1.      public jfrmGaji() {
  2.         setTitle("Aplikasi Perhitungan Gaji");
  3.         initComponents();
  4.      }
  5.    
  6.      private void gaji(){
  7.         int pilihan=cmbjabatan.getSelectedIndex();
  8.         switch(pilihan){
  9.             case 0:
  10.                 txtgapok.setText("7500000");
  11.             break;    
  12.             case 1:
  13.                 txtgapok.setText("5000000");
  14.             break;
  15.             case 2:
  16.                 txtgapok.setText("3000000");
  17.             break;
  18.             case 3:
  19.                 txtgapok.setText("2000000");
  20.             break;
  21.             case 4:
  22.                 txtgapok.setText("1200000");
  23.             break;
  24.             case 5:
  25.                 txtgapok.setText("750000");
  26.             break;        
  27.         }
  28.     }
  29.    
  30.     private void kawin(){
  31.         txtistri.setEnabled(true);
  32.         txtanak.setEnabled(true);
  33.         txtjumlah.setEnabled(true);
  34.         gapok=Integer.valueOf(txtgapok.getText());
  35.         t_istri=(20*gapok)/100;
  36.         t_istri_1=String.valueOf(t_istri);
  37.         txtistri.setText(t_istri_1);
  38.         if(txtjumlah.getText()!=""||txtjumlah.getText()!="0"){
  39.             j_anak=Integer.valueOf(txtjumlah.getText());
  40.             if(j_anak<=2&&j_anak>=1){
  41.                 t_anak=((15*gapok)/100)*j_anak;
  42.             }else if(j_anak>2){
  43.                 t_anak=((15*gapok)/100)*2;
  44.             }else{
  45.                 t_anak=0;
  46.             }
  47.             t_anak_1=String.valueOf(t_anak);
  48.             txtanak.setText(t_anak_1);
  49.         }      
  50.     }
  51.     private void tidakkawin(){
  52.          t_istri=0;
  53.          t_anak=0;
  54.          j_anak=0;
  55.         txtistri.setText("0");
  56.         txtanak.setText("0");
  57.         txtjumlah.setText("0");
  58.         txtistri.setEnabled(false);
  59.         txtanak.setEnabled(false);
  60.         txtjumlah.setEnabled(false);
  61.     }
  62.     private void total(){
  63.         gapok=Integer.valueOf(txtgapok.getText());
  64.         if(opsi1.isSelected()==true){
  65.          gator=gapok+t_anak+t_istri;  
  66.         }else{
  67.             gator=gapok;
  68.         }
  69.         pajak=(gator*10)/100;
  70.         gaber=gator-pajak;
  71.         gator_1=String.valueOf(gator);
  72.         pajak_1=String.valueOf(pajak);
  73.         gaber_1=String.valueOf(gaber);
  74.         txtgator.setText(gator_1);
  75.         txtpajak.setText(pajak_1);
  76.         txtgaber.setText(gaber_1);
  77.     }
  78.     private void bersih(){
  79.          gapok=0;
  80.          gator=0;
  81.          pajak=0;
  82.          gaber=0;
  83.          t_istri=0;
  84.          t_anak=0;
  85.          j_anak=0;
  86.         txtgapok.setText("0");
  87.         txtistri.setText("0");
  88.         txtjumlah.setText("0");
  89.         txtanak.setText("0");
  90.         txtgator.setText("0");
  91.         txtpajak.setText("0");
  92.         txtgaber.setText("0");
  93.     }

Ketik kode program dibawah @SuppressWarnings("unchecked"):
  1. private void btnhitungMouseClicked(java.awt.event.MouseEvent evt) {                                      
  2.         // TODO add your handling code here:
  3.         total();
  4.     }                                      
  5.     private void btnbersihMouseClicked(java.awt.event.MouseEvent evt) {                                      
  6.         // TODO add your handling code here:
  7.         bersih();
  8.     }                                      
  9.     private void cmbjabatanItemStateChanged(java.awt.event.ItemEvent evt) {                                            
  10.         // TODO add your handling code here:
  11.         bersih();
  12.     }                                          
  13.     private void cmbjabatanActionPerformed(java.awt.event.ActionEvent evt) {                                          
  14.         // TODO add your handling code here:
  15.         gaji();
  16.         if(opsi1.isSelected()==true){
  17.             kawin();
  18.         }else{
  19.             tidakkawin();
  20.         }
  21.     }                                          
  22.     private void opsi1ActionPerformed(java.awt.event.ActionEvent evt) {                                      
  23.         // TODO add your handling code here:
  24.         kawin();
  25.     }                                    
  26.     private void opsi2ActionPerformed(java.awt.event.ActionEvent evt) {                                      
  27.         // TODO add your handling code here:
  28.         tidakkawin();
  29.     }                                    
  30.     private void txtjumlahKeyReleased(java.awt.event.KeyEvent evt) {                                      
  31.         // TODO add your handling code here:
  32.         kawin();
  33.     }

Selanjutnya, untuk melihat hasil programnya klik kanan Name Project > Run


Semoga berhasil :)

No comments:

Post a Comment