Seperti yang Kita tau, sebuah bisnis penjualan tentu terdapat transaksi jual beli. Transaksi jual beli dilakukan untuk menjual barang secara kredit maupun tunai. Dalam proses penjualan perlu adanya bukti transaksi yang digunakan untuk merekam semua kegiatan penjualan. Setiap proses transaksi dibutuhkan program untuk mencatat setiap transaksi penjualan sehingga proses lebih cepat. Terinspirasi dari hal tersebut, Kami mencoba membuat program transaksi penjualan menggunakan Java NetBeans. Program ini cukup sederhana dan semoga dapat membantu dalam proses penyimpanan data transaksi penjualan.
1. Buat project baru
Jalankan software Java NetBeans dan membuat sebuah project baru dengan memilih File > New project > buat nama project Penjualan.
Selanjutnya Anda dapat membuat class dengan cara Klik kanan pada package > New > Java class dan buat 3 class dengan nama, seperti: ModelBarang, ModelPenjualan dan Penjualan.
Selanjutnya Anda dapat membuat class dengan cara Klik kanan pada package > New > Java class dan buat 3 class dengan nama, seperti: ModelBarang, ModelPenjualan dan Penjualan.
Buat script pada class ModelBarang seperti berikut ini.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package penjualan;
/**
*
* @author user
*/
public class ModelBarang {
private String namaBarang;
private String satuan;
private double harga;
public ModelBarang(String namaBarang, String satuan, double harga) {
this.namaBarang = namaBarang;
this.satuan = satuan;
this.harga = harga;
}
@Override
public String toString() {
return this.namaBarang;
}
/**
* @return the namaBarang
*/
public String getNamaBarang() {
return namaBarang;
}
/**
* @param namaBarang the namaBarang to set
*/
public void setNamaBarang(String namaBarang) {
this.namaBarang = namaBarang;
}
/**
* @return the satuan
*/
public String getSatuan() {
return satuan;
}
/**
* @param satuan the satuan to set
*/
public void setSatuan(String satuan) {
this.satuan = satuan;
}
/**
* @return the harga
*/
public double getHarga() {
return harga;
}
/**
* @param harga the harga to set
*/
public void setHarga(double harga) {
this.harga = harga;
}
}
Buat script pada class ModelPenjualan seperti berikut ini.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package penjualan;
import javax.swing.table.DefaultTableModel;
/**
*
* @author master
*/
public class ModelPenjualan {
private double subTotal=0;
private double ppn=0;
private double total=0;
private DefaultTableModel tabel = new DefaultTableModel();
public ModelPenjualan(){
getTabel().addColumn("Barang");
getTabel().addColumn("Harga");
getTabel().addColumn("Qty");
getTabel().addColumn("Satuan");
getTabel().addColumn("Jumlah");
}
public double countSubtotal(){
subTotal=0;
for (int i=0;i<tabel.getRowCount();i++){
subTotal=subTotal+Double.parseDouble(tabel.getValueAt(i, 4).toString());
}
return subTotal;
}
public double countPPN(){
ppn=subTotal*0.1;
return ppn;
}
public double countTotal(){
total=subTotal+ppn;
return total;
}
/**
* @return the subTotal
*/
public double getSubTotal() {
return subTotal;
}
/**
* @param subTotal the subTotal to set
*/
public void setSubTotal(double subTotal) {
this.subTotal = subTotal;
}
/**
* @return the ppn
*/
public double getPpn() {
return ppn;
}
/**
* @param ppn the ppn to set
*/
public void setPpn(double ppn) {
this.ppn = ppn;
}
/**
* @return the total
*/
public double getTotal() {
return total;
}
/**
* @param total the total to set
*/
public void setTotal(double total) {
this.total = total;
}
/**
* @return the tabel
*/
public DefaultTableModel getTabel() {
return tabel;
}
/**
* @param tabel the tabel to set
*/
public void setTabel(DefaultTableModel tabel) {
this.tabel = tabel;
}
}
Untuk menghubungkan antara class dan desain frame Anda dapat membuat script pada class Penjualanseperti berikut ini.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package penjualan;
/**
*
* @author user
*/
public class Penjualan {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
UIPenjualan penjualan = new UIPenjualan();
penjualan.setVisible(true);
}
}
Buat desain Frame untuk tampilan program Anda dengan cara Klik kanan pada package > New > JFrame Form > buat nama frame UIPenjualan.
Buat desain frame UIPenjualan.
Buat desain frame UIPenjualan.
Buat script pada desain frame UIPenjualan seperti berikut ini.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package penjualan;
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(){
ModelBarang barang1 = new ModelBarang("Susu", "Kaleng", 11000);
ModelBarang barang2 = new ModelBarang("Rokok", "Bungkus", 16000);
ModelBarang barang3 = new ModelBarang("Snack", "Bungkus", 6500);
ModelBarang barang4 = new ModelBarang("Sabun", "Batang", 2000);
cboBarang.addItem(barang1);
cboBarang.addItem(barang2);
cboBarang.addItem(barang3);
cboBarang.addItem(barang4);
}
/**
* 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() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
cboBarang = new javax.swing.JComboBox();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
lblHarga = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
lblSatuan = new javax.swing.JLabel();
txtQuantity = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
tblBarang = new javax.swing.JTable();
btnSimpan = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
lblSubtotal = new javax.swing.JLabel();
chkPPN = new javax.swing.JCheckBox();
jLabel8 = new javax.swing.JLabel();
lblPPN = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
lblTotal = new javax.swing.JLabel();
btnHapus = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("Program Transaksi Penjualan");
jLabel2.setText("Barang");
cboBarang.setNextFocusableComponent(txtQuantity);
cboBarang.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cboBarangActionPerformed(evt);
}
});
jLabel3.setText("Quantity");
jLabel4.setText("Harga");
lblHarga.setText("00");
jLabel5.setText("Rp.");
lblSatuan.setText("jLabel6");
txtQuantity.setNextFocusableComponent(btnSimpan);
txtQuantity.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
txtQuantityKeyPressed(evt);
}
});
tblBarang.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(tblBarang);
btnSimpan.setText("Simpan");
btnSimpan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSimpanActionPerformed(evt);
}
});
jLabel6.setText("Subtotal :");
jLabel7.setText("Rp.");
lblSubtotal.setText("00");
chkPPN.setText("PPN");
chkPPN.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chkPPNActionPerformed(evt);
}
});
jLabel8.setText("Rp.");
lblPPN.setText("00");
jLabel9.setText("Total");
jLabel10.setText("Rp.");
lblTotal.setText("00");
btnHapus.setText("Hapus");
btnHapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnHapusActionPerformed(evt);
}
});
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)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel4))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblHarga, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(cboBarang, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblSatuan)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSimpan)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnHapus))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 545, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(142, 142, 142)
.addComponent(jLabel1)))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel10)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(chkPPN))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblPPN, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnHapus, btnSimpan});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(cboBarang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(lblHarga)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(lblSatuan)
.addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnHapus)
.addComponent(btnSimpan))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(lblSubtotal)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(chkPPN)
.addComponent(lblPPN)
.addComponent(jLabel8))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel9)
.addComponent(lblTotal)
.addComponent(jLabel10))
.addContainerGap(29, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
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) {
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;
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())
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) {
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
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
}
2. Lihat hasil Program
Anda dapat menjalankan program dengan memilih Run Main Project(F6) atau Pilih Project > Klik kanan > Run. Selanjutnya akan muncul program yang telah Anda buat.
Anda dapat menjalankan program dengan memilih Run Main Project(F6) atau Pilih Project > Klik kanan > Run. Selanjutnya akan muncul program yang telah Anda buat.
Hahaha... Parahh
BalasHapusParah knp ya mas bro
BalasHapusKurang bagus...
BalasHapusMaklum Mas bro masih Belajar alias Nubie heheeheh
BalasHapus