On National Teacher Day, meet the 2024-25 Kenan Fellows
4SCQ7M1 Last session
1. Riza Muhammad Nurman 4SC
Click to edit Master title style
LAST
SESSIONon
4SC Q7
LAST
SESSIONon
4SC Q7
FACULTYFACULTY
Riza Muhammad NurmanRiza Muhammad Nurman
Enhanced AppsEnhanced Apps
Facebook : https://facebook.com/rizamanFacebook : https://facebook.com/rizaman
Twitter : https://twitter.com/rhyzoneTwitter : https://twitter.com/rhyzone
SlideShare : https://slideshare.net/rizamanSlideShare : https://slideshare.net/rizaman
2. Riza Muhammad Nurman 4SC
Click to edit Master title styleContent
• Bekerja Dengan Gambar
• Membuat Form Transaksi
*Source Code : Twitter RMN 4SC_Q7M1_8
4. Riza Muhammad Nurman 4SC
Click to edit Master title styleDatabase & Class Faculty
public class Faculty {
private String fac_code, fac_name, fac_email, fac_phone, fac_photo;
private Koneksi obj_koneksi = new Koneksi();
….
public String getFac_photo() {
return fac_photo;
}
public void setFac_photo(String fac_photo) {
this.fac_photo = fac_photo;
}
5. Riza Muhammad Nurman 4SC
Click to edit Master title styleClass Faculty – doInsert()
public int doInsert() {
int i = 0;
try
{
obj_koneksi.bukaKoneksi();
File file = new File(fac_photo.trim());
FileInputStream fis = new FileInputStream(file);
int len = (int)file.length();
String str = "INSERT INTO faculty(fac_code, fac_name, fac_email, fac_phone, fac_photo) VALUES(?,?,?,?,?)";
PreparedStatement pr = obj_koneksi.con.prepareStatement(str);
pr.setString(1,fac_code.trim());
pr.setString(2,fac_name.trim());
pr.setString(3,fac_email.trim());
pr.setString(4,fac_phone.trim());
pr.setBinaryStream(5, fis, len);
i = pr.executeUpdate();
}
catch(FileNotFoundException | SQLException ex){
System.out.println(ex.getMessage());
}
return i;
}
6. Riza Muhammad Nurman 4SC
Click to edit Master title styleClass Faculty – doUpdate()
public int doUpdate() {
int i = 0; PreparedStatement pr = null;
try
{
obj_koneksi.bukaKoneksi();
if(fac_photo.trim().equals(""))
{
…..
}
else
{
File file = new File(fac_photo.trim());
FileInputStream fis = new FileInputStream(file);
int len = (int)file.length();
String str = "UPDATE faculty SET fac_name = ?," +
"fac_email = ?," +
"fac_phone = ?, " +
"fac_photo = ? " +
"WHERE fac_code = ?";
pr = obj_koneksi.con.prepareStatement(str);
pr.setString(1,fac_name);
pr.setString(2,fac_email);
pr.setString(3,fac_phone);
pr.setBinaryStream(4, fis, len);
pr.setString(5,fac_code);
}
i = pr.executeUpdate();
}
catch(FileNotFoundException | SQLException ex){ System.out.println(ex.getMessage()); }
return i;
}
7. Riza Muhammad Nurman 4SC
Click to edit Master title styleClass Faculty – getRecord()
public ArrayList getRecord() {
ArrayList data = new ArrayList();
byte[] fileBytes;
try
{
obj_koneksi.bukaKoneksi();
String str = "SELECT * FROM faculty WHERE fac_code = ?";
PreparedStatement pr = obj_koneksi.con.prepareStatement(str);
pr.setString(1,fac_code);
ResultSet rs = pr.executeQuery();
while(rs.next()) {
this.setFac_code(rs.getString(1));
this.setFac_name(rs.getString(2));
this.setFac_email(rs.getString(3));
this.setFac_phone(rs.getString(4));
fileBytes = rs.getBytes(5);
String file = "photo.jpg";
try (OutputStream targetFile = new FileOutputStream(file)) {
targetFile.write(fileBytes);
}
this.setFac_photo(file);
data.add(this.getFac_code()); data.add(this.getFac_name()); data.add(this.getFac_email()); data.add(this.getFac_phone());
data.add(this.getFac_photo());
}
}
catch(IOException | SQLException ex){ System.out.println(ex.getMessage()); }
return data;
}
8. Riza Muhammad Nurman 4SC
Click to edit Master title styleMerancang Faculty Form
PalettePalette
Tambahkan:
•Label bertuliskan photo
•Tombol btn_photo
bertuliskan Get Photo untuk
mengambil gambar
•Label lbl_photo untuk
menampilkan gambar
•TextField txt_photo untuk
menampilkan nama foto
yang diambil
Tambahkan:
•Label bertuliskan photo
•Tombol btn_photo
bertuliskan Get Photo untuk
mengambil gambar
•Label lbl_photo untuk
menampilkan gambar
•TextField txt_photo untuk
menampilkan nama foto
yang diambil
Panel NavigatorPanel Navigator
9. Riza Muhammad Nurman 4SC
Click to edit Master title styleFaculty Form
private void add() {
try {
obj.setFac_code(txt_code.getText());
obj.setFac_name(txt_name.getText());
obj.setFac_email(txt_email.getText());
obj.setFac_phone(txt_phone.getText());
obj.setFac_photo(txt_photo.getText());
int i = obj.doInsert();
if(i > 0) {
JOptionPane.showMessageDialog(null, "SUKSES! Data Berhasil Disimpan");
this.clear();
}
else {
JOptionPane.showMessageDialog(null, "PERINGATAN! Harap Memasukkan Data Yang Sesuai");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error: " + e);
}
}
10. Riza Muhammad Nurman 4SC
Click to edit Master title styleFaculty Form - 2
private void update() {
try {
obj.setFac_code(txt_code.getText().trim());
obj.setFac_name(txt_name.getText().trim());
obj.setFac_email(txt_email.getText().trim());
obj.setFac_phone(txt_phone.getText().trim());
if(!txt_photo.getText().trim().equals("")) {
obj.setFac_photo(txt_photo.getText());
}
int i = obj.doUpdate();
if(i > 0) {
JOptionPane.showMessageDialog(null, "SUKSES! Data Berhasil Diubah");
this.clear();
}
else {
JOptionPane.showMessageDialog(null, "PERINGATAN! Harap Memasukkan Data Yang Sesuai");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error: " + e);
}
}
11. Riza Muhammad Nurman 4SC
Click to edit Master title styleFaculty Form - 3
private void getPhoto() {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
lbl_photo.setIcon(new javax.swing.ImageIcon(file.getAbsolutePath()));
txt_photo.setText(file.getAbsolutePath());
} else {
System.out.println("File access cancelled by user.");
}
}
…
private void btn_photoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.getPhoto();
}
13. Riza Muhammad Nurman 4SC
Click to edit Master title styleFaculty Form - 4
private void getRecord(String id)
{
try{
obj.setFac_code(id);
ArrayList data = obj.getRecord();
txt_code.setText((String)data.get(0));
txt_name.setText((String)data.get(1));
txt_email.setText((String)data.get(2));
txt_phone.setText((String)data.get(3));
txt_photo.setText((String)data.get(4));
lbl_photo.setText("");
BufferedImage bufImg=ImageIO.read(new File((String)data.get(4)));
lbl_photo.setIcon(new javax.swing.ImageIcon(bufImg));
}
catch(Exception ex) {
JOptionPane.showMessageDialog(null, "Data Gagal Ditampilkan");
}
}
14. Riza Muhammad Nurman 4SC
Click to edit Master title styleMembuat Form Transaksi
ADD ITEM : Untuk menambahkan
item ke dalam tabel penjualan
TOTAL :
Perhitungan
total otomatis
dengan
menjumlahkan
Sub Total dari
tabel penjualan
TOTAL :
Perhitungan
total otomatis
dengan
menjumlahkan
Sub Total dari
tabel penjualan
CHANGE : Hasil
pengurangan
PAYMENT -
TOTAL
CHANGE : Hasil
pengurangan
PAYMENT -
TOTAL
Tekan Tombol
OK untuk
memproses
Transaksi
15. Riza Muhammad Nurman 4SC
Click to edit Master title styleHasil Setelah Eksekusi Program
16. Riza Muhammad Nurman 4SC
Click to edit Master title styleForm Transaksi - Tabel
1. create table employee
2. (
3. emp_code char(10) not null primary key,
4. emp_name varchar(30) not null,
5. emp_position varchar(30) not null
6. )
7. create table item
8. (
9. item_code char(10) not null primary key,
10. item_name varchar(30) not null,
11. item_price money not null,
12. item_stock int not null
13. )
14. create table transactions
15. (
16. trans_code char(10) not null primary key,
17. trans_date datetime not null,
18. emp_code char(10) not null references employee(emp_code),
19. total money not null
20. )
21. create table transactions_detail
22. (
23. id int identity(1,1) primary key,
24. trans_code char(10) not null references transactions(trans_code),
25. item_code char(10) not null references item(item_code),
26. quantity int not null,
27. sub_total money not null
28. )
17. Riza Muhammad Nurman 4SC
Click to edit Master title styleForm Transaksi – Trigger Stok
1. alter trigger trgUpdateStock
2. on transactions_detail
3. for insert
4. as
5. declare @qty as int
6. declare @item_code as char(10)
7. declare @trans_code as char(10)
8. declare @subtotal as money
9. declare @stock as int
10. declare @check as int
11. set @trans_code = (select trans_code from inserted)
12. set @item_code = (select item_code from inserted)
13. set @qty = (select quantity from inserted)
14. set @subtotal = (select sub_total from inserted)
15. begin
16. update item
17. set item_stock = item_stock - @qty
18. where item_code = @item_code
19. update transactions
20. set total = total + @subtotal
21. where trans_code = @trans_code
22. end
18. Riza Muhammad Nurman 4SC
Click to edit Master title styleTransaction Class
public class Transaction {
private String trans_code, trans_date, emp_code, item_code;
private int total, quantity, sub_total;
private Koneksi obj_koneksi = new Koneksi();
….
// Getter & Setter Code
19. Riza Muhammad Nurman 4SC
Click to edit Master title styleTransaction Class - 2
public int doTransaction() {
int i = 0;
try
{
obj_koneksi.bukaKoneksi();
String str = "INSERT INTO transactions(trans_code, trans_date, emp_code, total)
VALUES(?,getdate(),?,?)";
PreparedStatement pr = obj_koneksi.con.prepareStatement(str);
pr.setString(1,trans_code.trim());
pr.setString(2,emp_code.trim());
pr.setInt(3,total);
i = pr.executeUpdate();
}
catch(SQLException ex){
System.out.println(ex.getMessage());
}
return i;
}
20. Riza Muhammad Nurman 4SC
Click to edit Master title styleTransaction Class - 3
public int setDetailTransaction() {
int i = 0;
try
{
obj_koneksi.bukaKoneksi();
String str = "INSERT INTO transactions_detail(trans_code, item_code, quantity, sub_total)
VALUES(?,?,?,?)";
PreparedStatement pr = obj_koneksi.con.prepareStatement(str);
pr.setString(1,trans_code.trim());
pr.setString(2,item_code.trim());
pr.setInt(3,quantity);
pr.setInt(4,sub_total);
i = pr.executeUpdate();
}
catch(SQLException ex){
System.out.println(ex.getMessage());
}
return i;
}
21. Riza Muhammad Nurman 4SC
Click to edit Master title styleVTransaction
public class VTransaction extends javax.swing.JFrame {
Transaction obj_trans = new Transaction();
Item obj_item = new Item();
DefaultTableModel model;
int total;
public VTransaction() {
initComponents();
lbl_date.setText(new java.util.Date().toString());
}
private void clear() {
txt_code.setText("");
txt_quantity.setText("0");
txt_change.setText("");
txt_payment.setText("");
txt_total.setText("");
total = 0;
}
22. Riza Muhammad Nurman 4SC
Click to edit Master title styleVTransaction - 2
private void doTransactions() {
try {
obj_trans.setTrans_code(txt_trans.getText());
obj_trans.setEmp_code(lbl_empcode.getText());
obj_trans.setTotal(0);
int i = obj_trans.doTransaction();
if(i > 0) {
JOptionPane.showMessageDialog(null, "SUKSES! Data Berhasil Disimpan");
}
else {
JOptionPane.showMessageDialog(null, "PERINGATAN! Harap Memasukkan Data Yang
Sesuai");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error: " + e);
}
}
23. Riza Muhammad Nurman 4SC
Click to edit Master title styleVTransaction - 3
private void doTransactionDetails() {
model = (DefaultTableModel) table_item.getModel();
int row_count = table_item.getRowCount();
int count = 0;
try {
while(count < row_count) {
String code = model.getValueAt(count,0).toString();
int qty = Integer.parseInt(model.getValueAt(count,3).toString());
int subtotal = Integer.parseInt(model.getValueAt(count,4).toString());
obj_trans.setItem_code(code);
obj_trans.setQuantity(qty);
obj_trans.setSub_total(subtotal);
int i = obj_trans.setDetailTransaction();
count++;
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error Details: " + e);
}
}
24. Riza Muhammad Nurman 4SC
Click to edit Master title styleVTransaction - 4
private void addItem() {
try{
int quantity = Integer.parseInt(txt_quantity.getText());
obj_item.setItem_code(txt_code.getText());
ArrayList data = obj_item.getRecord();
for(int i = 0;i < data.size()-1;i+=4)
{
String item_code = (String)data.get(i);
String item_name = (String)data.get(i+1);
int item_price = (Integer)data.get(i+2);
int subtotal = item_price * quantity;
String[] data_field =
{item_code.trim(),item_name.trim(),String.valueOf(item_price),String.valueOf(quantity), String.valueOf(subtotal) };
model = (DefaultTableModel)table_item.getModel();
model.addRow(data_field);
total = total + subtotal;
obj_trans.setTotal(total);
txt_total.setText(String.valueOf(obj_trans.getTotal()));
}
}
catch(Exception ex) {
JOptionPane.showMessageDialog(null, "Data Gagal Ditampilkan" + ex.getMessage());
}
}
25. Riza Muhammad Nurman 4SC
Click to edit Master title styleVTransaction - 5
private void clearTable() {
model = (DefaultTableModel) table_item.getModel();
int baris = model.getRowCount();
for (int i = 0; i< baris; i++) {
model.removeRow(0);
}
}
26. Riza Muhammad Nurman 4SC
Click to edit Master title styleVTransaction - Event
private void btn_itemActionPerformed(java.awt.event.ActionEvent evt) {
this.addItem();
}
private void txt_changeMouseClicked(java.awt.event.MouseEvent evt) {
int payment = Integer.parseInt(txt_payment.getText());
int change = payment - total;
txt_change.setText(String.valueOf(change));
}
private void btn_clearActionPerformed(java.awt.event.ActionEvent evt) {
this.clearTable();
this.clear();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.doTransactions();
this.doTransactionDetails();
txt_trans.setText("TRANS NUMBER");
this.clearTable();
this.clear();
}