Skip to content

Instantly share code, notes, and snippets.

@calvinalx
Last active May 20, 2023 10:00
Show Gist options
  • Select an option

  • Save calvinalx/eb9757911eb88ca9d43e70b6e744644d to your computer and use it in GitHub Desktop.

Select an option

Save calvinalx/eb9757911eb88ca9d43e70b6e744644d to your computer and use it in GitHub Desktop.
package com.mycompany.mavenproject1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.DefaultTableModel;
public class NewJFrame extends javax.swing.JFrame {
int LEASE_PRICE = 10000;
class Book {
String title;
String author;
int stock;
public Book(String title1, String author1, int stock1) {
title = title1;
author = author1;
stock = stock1;
}
}
Book book1 = new Book("Harry Potter", "Joni", 2);
Book book2 = new Book("Doraemon", "Toni", 1);
Book book3 = new Book("Twilight", "Brian", 0);
Book book4 = new Book("Conan", "Brian", 5);
List<Book> books = new ArrayList<>(
Arrays.asList(
book1, book2, book3, book4
)
);
DefaultTableModel DftTblModel_book;
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
jTable1.getSelectionModel().addListSelectionListener((ListSelectionEvent event) -> {
jButton2.setEnabled(false);
});
DftTblModel_book = (DefaultTableModel) jTable1.getModel();
DftTblModel_book.addColumn("TITLE");
DftTblModel_book.addColumn("AUTHOR");
DftTblModel_book.addColumn("STOCK");
for (int i = 0; i < 4; i++) {
DftTblModel_book.addRow(new Object[]{
books.get(i).title,
books.get(i).author,
books.get(i).stock
});
}
}
/**
* 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() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
jScrollPane1.setViewportView(jTable1);
jButton1.setText("Cek");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setColumns(2);
jTextField1.setText("1");
jTextField1.setToolTipText("Qty");
jLabel1.setText("Total Rp");
jLabel2.setText("0");
jButton2.setText("Sewa");
jButton2.setEnabled(false);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel3.setText("Qty");
jLabel4.setText("Sewa Komik");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(48, 48, 48)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(62, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addComponent(jButton2)
.addContainerGap(33, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int qtyInput = Integer.parseInt(jTextField1.getText());
int totalPrice = qtyInput * LEASE_PRICE;
int row = jTable1.getSelectedRow();
if (row < 0) {
JOptionPane.showMessageDialog(null, "You havent selected an item.");
jLabel2.setText("Please select an item");
return;
}
String stock = jTable1.getModel().getValueAt(row, 2).toString();
int _stock = Integer.parseInt(stock);
if (qtyInput > _stock) {
JOptionPane.showMessageDialog(null, "Not enough stock");
jLabel2.setText("Sold out");
return;
}
jButton2.setEnabled(true);
jLabel2.setText(String.valueOf(totalPrice));
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int qtyInput = Integer.parseInt(jTextField1.getText());
int row = jTable1.getSelectedRow();
if (row < 0) {
JOptionPane.showMessageDialog(null, "You havent selected an item.");
return;
}
String stock = jTable1.getModel().getValueAt(row, 2).toString();
int _stock = Integer.parseInt(stock);
if (qtyInput > _stock) {
JOptionPane.showMessageDialog(null, "Not enough stock");
return;
}
int finalStock = _stock - qtyInput;
DftTblModel_book.setValueAt(finalStock, row, 2);
JOptionPane.showMessageDialog(null, "Rent successful.");
}
/**
* @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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new NewJFrame().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
public javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment