You are on page 1of 12

import java.awt.

*;
import java.awt.event.*;
import java.sql.*;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Date;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.table.DefaultTableModel;
public class Kasir extends JFrame {
JTextField kode = new JTextField();
JTextField item = new JTextField();
JTextField harga = new JTextField();
JTextField nama = new JTextField();
JButton bOk = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JLabel total = new JLabel();
JTable tabel = new JTable();
DefaultTableModel tabMode;
JLabel tanggal = new JLabel();
JLabel Njam = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JTextField bayar_2 = new JTextField();
JTextField kembalian = new JTextField();
JTextField bayar = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JButton bCetak = new JButton();
Connection con;
JLabel jLabel3 = new JLabel();
JLabel no_tran = new JLabel();
public Kasir() {

this.getContentPane().setLayout(null);
kode.setBounds(new Rectangle(8, 71, 120, 20));
item.setBounds(new Rectangle(144, 72, 47, 20));
harga.setEnabled(true);
harga.setDisabledTextColor(Color.black);
harga.setHorizontalAlignment(SwingConstants.RIGHT);
harga.setBounds(new Rectangle(201, 71, 109, 21));
nama.setEnabled(true);
nama.setDisabledTextColor(Color.black);
nama.setEditable(true);
nama.setBounds(new Rectangle(9, 98, 217, 21));
bOk.setBackground(SystemColor.controlHighlight);
bOk.setBounds(new Rectangle(243, 99, 71, 23));
bOk.setEnabled(true);
bOk.setText(O K);
jScrollPane1.setViewportBorder(null);
jScrollPane1.setBorder(null);
jScrollPane1.setBounds(new Rectangle(8, 181, 400, 210));
total.setFont(new java.awt.Font(Dialog, Font.BOLD, 30));
total.setHorizontalAlignment(SwingConstants.LEFT);
total.setText(0);
total.setBounds(new Rectangle(211, 127, 169, 35));
Object[] row = {Nama Barang, Harga Satuan, Item, Jumlah Harga};
tabMode = new DefaultTableModel(null, row);
tabel.setBorder(null);
tabel.setGridColor(UIManager.getColor(Button.background));
tabel.setModel(tabMode);
tanggal.setFont(new java.awt.Font(Dialog, Font.BOLD, 14));
tanggal.setText(DD MM YYYY);
tanggal.setBounds(new Rectangle(11, 8, 98, 24));
Njam.setFont(new java.awt.Font(Dialog, Font.BOLD, 14));
Njam.setText(HH : MM : SS);
Njam.setBounds(new Rectangle(122, 7, 106, 26));
jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
jLabel4.setText(Kode Barang);

jLabel4.setBounds(new Rectangle(8, 50, 120, 19));


jLabel5.setHorizontalAlignment(SwingConstants.CENTER);
jLabel5.setText(Item);
jLabel5.setBounds(new Rectangle(144, 51, 47, 22));
jLabel6.setHorizontalAlignment(SwingConstants.CENTER);
jLabel6.setText(Harga Satuan);
jLabel6.setBounds(new Rectangle(203, 51, 107, 20));
jLabel7.setFont(new java.awt.Font(Dialog, Font.BOLD, 30));
jLabel7.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel7.setText(TOTAL : Rp.);
jLabel7.setBounds(new Rectangle(8, 120, 191, 44));
bayar_2.setFont(new java.awt.Font(Dialog, Font.BOLD, 20));
bayar_2.setEditable(false);
bayar_2.setText(0);
bayar_2.setHorizontalAlignment(SwingConstants.RIGHT);
bayar_2.setBounds(new Rectangle(276, 403, 134, 29));
kembalian.setFont(new java.awt.Font(Dialog, Font.BOLD, 20));
kembalian.setEditable(false);
kembalian.setText(0);
kembalian.setHorizontalAlignment(SwingConstants.RIGHT);
kembalian.setBounds(new Rectangle(277, 439, 133, 31));
bayar.setFont(new java.awt.Font(Dialog, Font.BOLD, 20));
bayar.setText();
bayar.setHorizontalAlignment(SwingConstants.RIGHT);
bayar.setBounds(new Rectangle(129, 403, 134, 30));
jLabel1.setFont(new java.awt.Font(Dialog, Font.BOLD, 20));
jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel1.setText(Bayar :);
jLabel1.setBounds(new Rectangle(10, 402, 110, 29));
jLabel2.setFont(new java.awt.Font(Dialog, Font.BOLD, 20));
jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel2.setText(Kembali :);
jLabel2.setBounds(new Rectangle(129, 440, 135, 30));
bCetak.setBackground(Color.green);
bCetak.setBounds(new Rectangle(308, 475, 101, 31));

bCetak.setEnabled(false);
bCetak.setFont(new java.awt.Font(Dialog, Font.BOLD, 14));
bCetak.setToolTipText();
bCetak.setText(C E T A K);
jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel3.setText(Nomor Transaksi :);
jLabel3.setBounds(new Rectangle(204, 8, 114, 24));
no_tran.setFont(new java.awt.Font(Dialog, Font.BOLD, 11));
no_tran.setText(00000);
no_tran.setBounds(new Rectangle(324, 10, 91, 20));
this.getContentPane().setBackground(SystemColor.info);
this.getContentPane().add(kode);
this.getContentPane().add(nama);
this.getContentPane().add(bOk);
this.getContentPane().add(harga);
this.getContentPane().add(item);
this.getContentPane().add(jScrollPane1);
this.getContentPane().add(tanggal);
this.getContentPane().add(jLabel4);
this.getContentPane().add(jLabel5);
this.getContentPane().add(jLabel6);
this.getContentPane().add(jLabel7);
this.getContentPane().add(total);
this.getContentPane().add(jLabel3);
this.getContentPane().add(no_tran);
this.getContentPane().add(bayar_2);
this.getContentPane().add(kembalian);
this.getContentPane().add(jLabel2);
this.getContentPane().add(bCetak);
this.getContentPane().add(jLabel1);
this.getContentPane().add(bayar);
this.getContentPane().add(Njam);
jScrollPane1.getViewport().add(tabel);
Jam();
koneksiDatabase();

tambahListener();
ambilNoTran();
}
/**
* main
*
* @param args String
*/
public static void main(String[] args) {
Kasir u = new Kasir();
u.setTitle(:: Kasir ::);
u.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
u.setSize(440, 560);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int lebar = (screen.width u.getSize().width) / 2;
int tinggi = (screen.height u.getSize().height) / 2;
u.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
u.setLocation(lebar, tinggi);
u.show();
}
/**
* tambahKeyListener
*/
public void tambahListener() {
kode.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
kode_keyPressed(e);
}
});
item.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
item_keyPressed(e);
}

});
bOk.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
bOk_keyPressed(e);
}
});
bOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bOk_actionPerformed(e);
}
});
bayar.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
bayar_keyPressed(e);
}
});
bayar.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
bayar_caretUpdate(e);
}
});
bCetak.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
bCetak_keyPressed(e);
}
});
bCetak.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bCetak_actionPerformed(e);
}
});
}

/**
* hapusTabel
*/
public void hapusTabel() {
int row = tabMode.getRowCount();
for (int i = 0; i < row; i++) {
tabMode.removeRow(0);
}
}
/**
* ambilNoTran
*/
public void ambilNoTran() {
try {
String sql = "Select Max(no_transaksi) as no from transaksi";
Statement stat = con.createStatement();
ResultSet set = stat.executeQuery(sql);
while (set.next()) {
String nomor = set.getString("no");
int no_t = Integer.parseInt(nomor) + 1;
no_tran.setText(Integer.toString(no_t));
}
}
catch (Exception e) {
}
}
/**
* Jam
*/
public void Jam() {
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {

String nol_bulan = "";


String nol_hari = "";
String nol_jam = "";
String nol_menit = "";
String nol_detik = "";
// Membuat Date
Date dt = new Date();
// Mengambil nilai JAM, MENIT, dan DETIK Sekarang
int nilai_tahun = dt.getYear() + 1900;
int nilai_bulan = dt.getMonth() + 1;
int nilai_hari = dt.getDate();
int nilai_jam = dt.getHours();
int nilai_menit = dt.getMinutes();
int nilai_detik = dt.getSeconds();
// Jika nilai JAM lebih kecil dari 10 (hanya 1 digit)
if (nilai_bulan <= 9) {
// Tambahkan "0" didepannya
nol_bulan = "0";
}
if (nilai_hari <= 9) {
// Tambahkan "0" didepannya
nol_hari = "0";
}
if (nilai_jam <= 9) {
// Tambahkan "0" didepannya
nol_jam = "0";
}
// Jika nilai MENIT lebih kecil dari 10 (hanya 1 digit)
if (nilai_menit <= 9) {
// Tambahkan "0" didepannya
nol_menit = "0";
}
// Jika nilai DETIK lebih kecil dari 10 (hanya 1 digit)
if (nilai_detik 0) {
JOptionPane.showMessageDialog(null,Uang Yang Dibayarkan Kurang !!!!!);

kembalian.setText(0);
bayar.requestFocus();
}
else {
String format = NumberFormat.getNumberInstance(Locale.
ENGLISH).
format(hasil_kembalian);
StringTokenizer token = new StringTokenizer(format, .);
format = token.nextToken();
format = format.replace(,, .);
kembalian.setText(format);
bCetak.requestFocus();
}
}
}
}
public void bayar_caretUpdate(CaretEvent e) {
if (bayar.getText().equals()) {
bayar_2.setText(0);
}
else {
double money = Double.parseDouble(bayar.getText());
String format = NumberFormat.getNumberInstance(Locale.ENGLISH).
format(
money);
StringTokenizer token = new StringTokenizer(format, .);
format = token.nextToken();
format = format.replace(,, .);
bayar_2.setText(format);
bCetak.setEnabled(true);
}
}

public void bCetak_actionPerformed(ActionEvent e) {


cetak_hasil();
updateDataSudahBayar();
tampilDataKeTabel();
ambilNoTran();
}
public void bCetak_keyPressed(KeyEvent e) {
int ascii = e.getKeyCode();
if (ascii == 10) {
cetak_hasil();
updateDataSudahBayar();
tampilDataKeTabel();
ambilNoTran();
}
}
/**
* updateDataSudahBayar
*/
public void updateDataSudahBayar() {
try {
String sql = Update transaksi set status = Bayar where status = Belum Bayar';
Statement stat = con.createStatement();
stat.executeUpdate(sql);
}
catch (Exception e) {}
}
public void cetak_hasil() {
int y = 0;
Frame fr = new Frame();
PrintJob print = fr.getToolkit().getPrintJob(fr, Prinnting, null, null);
if (print != null) {
Graphics g = print.getGraphics();

if (g != null) {
g.setFont(new Font(Dialog, 1, 11));
g.drawString(AMIK WAHANA MANDIRI, 30, 40);
g.drawString(Jl. Cabe Raya, Pamulang, 30, 50);
g.drawString(0815-14320388, 30, 60);
g.setFont(new Font(Dialog, 0, 8));
g.drawString(tanggal.getText() + / + Njam.getText() + / + no_tran.getText(),
30, 70);
String nam_brg = tabel.getColumnName(0);
String harga_brg = tabel.getColumnName(1);
String item_brg = tabel.getColumnName(2);
String sub_total = tabel.getColumnName(3);
g.setFont(new Font(Dialog, 1, 8));
g.drawString(nam_brg, 30, 100);
g.drawString(harga_brg, 110, 100);
g.drawString(item_brg, 170, 100);
g.drawString(sub_total, 200, 100);
g.drawLine(30, 103, 251, 103);
int n = tabMode.getRowCount();
for (int i = 0; i < n; i++) {
int k = i + 1;
int j = 10 * k;
y = 103 + j;
g.setFont(new Font("Dialog", 0, 8));
String data_nam_brg = tabMode.getValueAt(i, 0).toString();
String data_harga_brg = tabMode.getValueAt(i, 1).toString();
String data_item_brg = tabMode.getValueAt(i, 2).toString();
String data_sub_total = tabMode.getValueAt(i, 3).toString();
g.drawString(data_nam_brg, 30, y);
g.drawString(data_harga_brg, 120, y);
g.drawString(data_item_brg, 175, y);
g.drawString(data_sub_total, 210, y);
}
g.setFont(new Font("Dialog", 1, 10));
g.drawLine(30, y + 5, 251, y + 5);

g.drawString("Total", 150, y + 15);


g.drawString(":", 190, y + 15);
g.drawString(total.getText(), 210, y + 15);
g.drawString("Bayar", 150, y + 25);
g.drawString(":", 190, y + 25);
g.drawString(bayar_2.getText(), 210, y + 25);
g.drawString("Kembali", 150, y + 35);
g.drawString(":", 190, y + 35);
g.drawString(kembalian.getText(), 210, y + 35);
}
print.end();
print.end();
}
kode.requestFocus();
bCetak.setEnabled(true);
bayar.setText("");
bayar_2.setText("0");
kembalian.setText("0");
}
}

Related Interests