Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Tugas P15.
- * NPM 202043500047
- * Kelas S3A
- * By Ahmad Al Desrahim (original author)
- *
- * @author Ahmad Al Desrahim
- * @version 1.0.0
- */
- import java.sql.*;
- import java.util.Scanner;
- public class Barang {
- Scanner scanner = new Scanner(System.in);
- Connection connection = null;
- PreparedStatement preparedStatement = null;
- ResultSet rs = null;
- String dotDB = "D:/Private/Kuliah/Pemgoraman Sqlite/db_barang.db";
- String leftAlignFormat = "| %-6s | %-13s | %-4s | %-11s |%n";
- public static void main(String[] args) {
- Barang v = new Barang();
- v.displayMenu();
- }
- public Connection getConnection() throws SQLException {
- // mencegah terjadinya LOCK TABLES SQLITE
- return this.connection != null ? this.connection :DriverManager.getConnection("jdbc:sqlite:" + this.dotDB);
- }
- public Statement getStatement() throws SQLException {
- Statement statement;
- this.connection = getConnection();
- statement = this.connection.createStatement();
- statement.setQueryTimeout(30);
- return statement;
- }
- /**
- * Menu utama
- */
- public void displayMenu() {
- this.breakLines(50);
- System.out.println("[1] Lihat data barang");
- System.out.println("[2] Tambah data barang");
- System.out.println("[3] Ubah data barang");
- System.out.println("[4] Hapus data barang");
- System.out.println("[5] Keluar");
- System.out.print(">> ");
- showForm(scanner.nextLine());
- }
- /**
- * Menu selection
- * @param menu
- */
- public void showForm(String menu) {
- this.breakLines(50);
- switch (menu) {
- case "1":
- this.formShow();
- break;
- case "2":
- this.formAdd();
- break;
- case "3":
- this.formUpdate();
- break;
- case "4":
- this.formDelete();
- break;
- case "5":
- return;
- default:
- this.displayMenu();
- return;
- }
- this.goToMenu();
- }
- /**
- * Fungsi untuk mengambil data barang dari DB
- * @param code kode barang yang ingin dicari, biarkan kosong jika ingin mengambil semua data
- * @return boolean
- * @throws SQLException
- */
- public boolean findBarang(String code) throws SQLException {
- String sql = "SELECT * FROM barang";
- if (code != "") {
- sql += " WHERE kode_barang = '"+ code +"'";
- }
- this.rs = this.getStatement().executeQuery(sql);
- return !this.isRSEmpty();
- }
- /**
- * Form Menampilkan barang
- */
- public void formShow() {
- try {
- System.out.println("+--------+---------------+------+-------------+");
- System.out.println("| kode | nama | stok | harga |");
- System.out.println("+--------+---------------+------+-------------+");
- if (this.findBarang("")) while(this.rs.next()) {
- System.out.format(
- this.leftAlignFormat,
- this.rs.getString("kode_barang"),
- this.rs.getString("nama_barang"),
- this.rs.getString("stok"),
- this.rs.getString("harga"));
- } else System.out.format("| %-43s |%n", "Data barang kosong");
- System.out.println("+--------+---------------+------+-------------+");
- } catch (Exception e) {
- this.exceptionToMenu(e);
- } finally {
- this.rs = null;
- }
- }
- /**
- * Form Tambah barang
- */
- public void formAdd() {
- String code = "", name = "", stock = "", price ="";
- boolean codeExists = true,
- isInputEmpty = true;
- try {
- this.connection = this.getConnection();
- this.preparedStatement = this.connection.prepareStatement("INSERT INTO barang(kode_barang,nama_barang,stok,harga) VALUES (?,?,?,?)");
- this.formShow();
- System.out.println("\nTambah data baru: ");
- while (isInputEmpty) {
- System.out.print("Kode: ");
- code = scanner.nextLine();
- isInputEmpty = code.isEmpty();
- codeExists = this.findBarang(code);
- if (isInputEmpty || codeExists) {
- System.out.println(isInputEmpty ? "Kode tidak boleh kosong." : codeExists ? "Kode barang sudah ada." : "");
- isInputEmpty = true;
- }
- }
- this.preparedStatement.setString(1, code);
- isInputEmpty = true;
- while (isInputEmpty) {
- System.out.print("Nama: "); name = scanner.nextLine();
- isInputEmpty = name.isEmpty();
- if (isInputEmpty) System.out.println("Nama tidak boleh kosong.");
- }
- this.preparedStatement.setString(2, name);
- isInputEmpty = true;
- while (isInputEmpty) {
- System.out.print("Stok: ");
- stock = scanner.nextLine();
- isInputEmpty = stock.isEmpty();
- if (isInputEmpty) System.out.println("Stok tidak boleh kosong.");
- }
- this.preparedStatement.setInt(3, Integer.parseInt(stock));
- isInputEmpty = true;
- while (isInputEmpty) {
- System.out.print("Harga: ");
- price = scanner.nextLine();
- isInputEmpty = price.isEmpty();
- if (isInputEmpty) System.out.println("Harga tidak boleh kosong.");
- }
- this.preparedStatement.setDouble(4, Double.parseDouble(price));
- this.preparedStatement.executeUpdate();
- this.preparedStatement.close();
- System.out.println("Barang " + name + " berhasil ditambah.");
- } catch (Exception e) {
- this.exceptionToMenu(e);
- } finally {
- this.preparedStatement = null;
- this.rs = null;
- }
- }
- /**
- * Form Update barang
- */
- public void formUpdate() {
- String code = "", name = "", stock = "", price ="", nameInput = "", stockInput = "", priceInput ="";
- boolean notFound = true;
- try {
- this.connection = this.getConnection();
- this.preparedStatement = this.connection.prepareStatement("UPDATE barang SET nama_barang = ?,stok = ?,harga = ? WHERE kode_barang = ?");
- this.formShow();
- while (notFound) {
- System.out.print("\nUbah data [masukkan kode barang] >> ");
- code = scanner.nextLine();
- notFound = !this.findBarang(code);
- if (!notFound && this.rs.next()) {
- System.out.println("Anda akan mengubah data berikut ini: ");
- code = this.rs.getString("kode_barang");
- System.out.println("Kode: " + code);
- name = this.rs.getString("nama_barang");
- System.out.println("Nama: " + name);
- stock = this.rs.getString("stok");
- System.out.println("Stok: " + stock);
- price = this.rs.getString("harga");
- System.out.println("Harga: " + price);
- System.out.print("Apakah anda yakin ? [Y/n] ");
- notFound = scanner.nextLine().trim().equalsIgnoreCase("n");
- } else {
- System.out.println("Barang dengan kode " + code + " tidak dapat ditemukan.");
- }
- }
- System.out.println("\nLangsung enter jika tidak ingin mengubah data.");
- System.out.println("Ketik - jika ingin mengosongkan data.");
- System.out.println("Kode barang tidak bisa diubah.\n");
- System.out.print("Nama ["+ name +"]: "); nameInput = scanner.nextLine();
- this.preparedStatement.setString(1, nameInput.isEmpty() ? name : nameInput);
- System.out.print("Stok ["+ stock +"]: "); stockInput = scanner.nextLine();
- this.preparedStatement.setInt(2, Integer.parseInt(stockInput.isEmpty() ? stock : stockInput));
- System.out.print("Harga ["+ price +"]: "); priceInput = scanner.nextLine();
- this.preparedStatement.setDouble(3, Double.parseDouble(priceInput.isEmpty() ? price : priceInput));
- this.preparedStatement.setString(4, code);
- this.preparedStatement.executeUpdate();
- this.preparedStatement.close();
- System.out.println("Barang dengan kode " + code + " berhasil diubah.");
- } catch (Exception e) {
- this.exceptionToMenu(e);
- } finally {
- this.preparedStatement = null;
- this.rs = null;
- }
- }
- /**
- * Form Hapus barang
- */
- public void formDelete() {
- String code = "", name = "", stock = "", price ="";
- boolean notFound = true;
- try {
- this.connection = this.getConnection();
- this.preparedStatement = this.connection.prepareStatement("DELETE FROM barang WHERE kode_barang = ?");
- this.formShow();
- while (notFound) {
- System.out.print("\nHapus data [masukkan kode barang] >> ");
- code = scanner.nextLine();
- notFound = !this.findBarang(code);
- if (!notFound && this.rs.next()) {
- System.out.println("Anda akan menghapus data berikut ini: ");
- code = this.rs.getString("kode_barang");
- System.out.println("Kode: " + code);
- name = this.rs.getString("nama_barang");
- System.out.println("Nama: " + name);
- stock = this.rs.getString("stok");
- System.out.println("Stok: " + stock);
- price = this.rs.getString("harga");
- System.out.println("Harga: " + price);
- System.out.print("Apakah anda yakin ? [Y/n] ");
- notFound = scanner.nextLine().trim().equalsIgnoreCase("n");
- } else {
- System.out.println("Barang dengan kode " + code + " tidak dapat ditemukan.");
- }
- }
- this.preparedStatement.setString(1, code);
- this.preparedStatement.executeUpdate();
- this.preparedStatement.close();
- System.out.println("Barang dengan kode " + code + " berhasil dihapus.");
- } catch (Exception e) {
- this.exceptionToMenu(e);
- } finally {
- this.preparedStatement = null;
- this.rs = null;
- }
- }
- /**
- * Prompt untuk kembali ke menu utama, agar user tidak langsung di arahkan ke menu utama
- */
- public void goToMenu() {
- System.out.print("Tekan enter untuk kembali ke menu...\n");
- scanner.nextLine();
- showForm("0");
- }
- /**
- * Untuk handle exception agar aplikasi tidak force close
- * @param e
- */
- public void exceptionToMenu(Exception e) {
- System.err.println("Terjadi kesalahan. error: " + e.getMessage());
- this.goToMenu();
- }
- /**
- * Cek apakah ResultSet kosong
- * @return boolean
- * @throws SQLException
- */
- public boolean isRSEmpty() throws SQLException {
- return (!this.rs.isBeforeFirst() && this.rs.getRow() == 0) || this.rs == null;
- }
- /**
- * Java console ribet buat bikin fungsi clear screen, jadi ini alternatif nya
- * @param howMuch berapa banyak baris yang ingin disisipkan
- */
- public void breakLines(int howMuch) {
- if (howMuch < 1) return;
- for (int i = 0; i < howMuch; i++) System.out.println("\n");
- }
- }
Add Comment
Please, Sign In to add comment