Percabangan dan Loop Menggunakan Java Netbeans



Berikut Source kodenya :
 

package j1f115017_sitimuliasari_prakpbo2;

import java.awt.event.KeyEvent;
import java.text.NumberFormat;
import javax.swing.table.TableColumn;

/**
 *
 * @author user
 */
public class UIPenjualan extends javax.swing.JFrame {
    ModelBarang barang;
    ModelPenjualan penjualan=new ModelPenjualan();
   
    /**
     * Creates new form UIPenjualan
     */
    public UIPenjualan() {
        initComponents();
        fillComboBarang();
        tblBarang.setModel(penjualan.getTabel());
       
    }
   
    private void fillComboBarang(){ //memasukkan nama nama barang pada combo box
        ModelBarang barang1 = new ModelBarang("Susu", "Kaleng", 11000); //menuliskan nama barang susu yang di jual perkaleng dengan harga 11000
        ModelBarang barang2 = new ModelBarang("Gula", "Bungkus", 16000); //menuliskan nama barang gula yang di jual perbungkus dengan harga 16000
        ModelBarang barang3 = new ModelBarang("Snack", "Bungkus", 6500); //menuliskan nama barang snack yang di jual perbungkus dengan harga 6500
        ModelBarang barang4 = new ModelBarang("Minyak", "Bungkus", 21000); //menuliskan nama barang minyak yang di jual perkaleng dengan harga 21000       
        cboBarang.addItem(barang1);
        cboBarang.addItem(barang2);
        cboBarang.addItem(barang3);
        cboBarang.addItem(barang4);
    }
private void txtQuantityKeyPressed(java.awt.event.KeyEvent evt) {                                      
        if(evt.getKeyCode()==KeyEvent.VK_ENTER)
            btnSimpan.requestFocus();
    }                                     
    private void cboBarangActionPerformed(java.awt.event.ActionEvent evt) {                                         
        barang = (ModelBarang)cboBarang.getSelectedItem();
        lblHarga.setText(NumberFormat.getNumberInstance().format(barang.getHarga()));
        lblSatuan.setText(barang.getSatuan());
    }                                        

    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) //fungsi pada button simpan
 {                                         
        String[] data = new String[5];
        double harga, jumlah=0;
        int qty=0;
        data[0]=barang.getNamaBarang();
        harga=barang.getHarga();
        data[1]=String.valueOf(barang.getHarga());
        qty=Integer.parseInt(txtQuantity.getText());
        data[2]=txtQuantity.getText();
        data[3]=barang.getSatuan();
        jumlah=harga*qty; //rumus jumlah = harga*qty
        data[4]=String.valueOf(jumlah);
        penjualan.getTabel().addRow(data); lblSubtotal.setText(NumberFormat.getNumberInstance().format(penjualan.countSubtotal()));
        chkPPNActionPerformed(null);
        cboBarang.requestFocus();
    }                                         
    private void chkPPNActionPerformed(java.awt.event.ActionEvent evt) {                                      
        if (chkPPN.isSelected())//combo box pada ppn        lblPPN.setText(NumberFormat.getNumberInstance().format(penjualan.countPPN()));
        else{
            lblPPN.setText("0");
            penjualan.setPpn(0);
       }    lblTotal.setText(NumberFormat.getNumberInstance().format(penjualan.countTotal()));
    }                                     
    private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) //fungsi pada button hapus
{                                                 penjualan.getTabel().removeRow(tblBarang.getSelectedRow());
        lblSubtotal.setText(NumberFormat.getNumberInstance().format(penjualan.countSubtotal()));
        chkPPNActionPerformed(null);
    }                                       

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UIPenjualan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UIPenjualan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UIPenjualan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UIPenjualan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new UIPenjualan().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify   //pendeklarasian semua variabel yang dipakai                 
    private javax.swing.JButton btnHapus;
    private javax.swing.JButton btnSimpan;
    private javax.swing.JComboBox cboBarang;
    private javax.swing.JCheckBox chkPPN;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblHarga;
    private javax.swing.JLabel lblPPN;
    private javax.swing.JLabel lblSatuan;
    private javax.swing.JLabel lblSubtotal;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JTable tblBarang;
    private javax.swing.JTextField txtQuantity;
    // End of variables declaration                  
}
Previous
Next Post »
Thanks for your comment