Sabtu, 07 Maret 2015

Sistem Aplikasi Gudang Buku - UAS Pemograman Visual (VB.Net dan Microsoft Access)


Program sederhana ini hanya menggunakan Toolbox: TextBox, Label, Button, ComboBox, DataGridView…
Buatkan dulu databasenya di Ms. Access dengan nama database UAS2015 .
Tabel dengan nama tbl_buku [ID,Judul,Penerbit,Penulis,Tebal,Harga,Stock]


Ini source codenya :

Imports System.Data.OleDb
Public Class Form1
    ' Added By Redza, 07 Maret 2015
    ' iniTehKoneksi ke DB
    Dim iniTehKoneksi As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\MR - College\uas\UAS2015.accdb")
    Dim alertBerhasilSimpan As String = "Data Berhasil Tersimpan"
    Dim alertGagalSimpan As String = "Data Gagal Tersimpan"
    Dim alertBerhasilEdit As String = "Data Berhasil Diperbaharui"
    Dim alertGagalEdit As String = "Data Gagal Diperbaharui"
    Dim alertBerhasilHapus As String = "Data Berhasil Dihapus"
    Dim alertGagalHapus As String = "Data Gagal Dihapus"
    Dim alertFieldKosong As String = "Field masih ada yang kosong"

    Sub tampilkanGrid()
        iniTehKoneksi.Close()
        iniTehKoneksi.Open()

        Me.Tbl_bukuTableAdapter.Fill(Me.UAS2015DataSet.tbl_buku)
        Dim da As New OleDb.OleDbDataAdapter("Select * From tbl_buku", iniTehKoneksi)
        Dim ds As New DataSet
        da.Fill(ds, "tbl_buku")
        Tbl_bukuDataGridView.DataSource = ds.Tables("tbl_buku")
        Tbl_bukuBindingSource.DataSource = ds.Tables("tbl_buku")


    End Sub

    Sub kosongkanPerlengkapanTempur()
        IDTextBox.Text = String.Empty
        JudulTextBox.Text = String.Empty
        PenerbitTextBox.Text = String.Empty
        PenulisTextBox.Text = String.Empty
        TebalTextBox.Text = String.Empty
        HargaTextBox.Text = String.Empty
        StockTextBox.Text = String.Empty
        CariTextBox.Text = String.Empty
        ComboBox1.Text = String.Empty
    End Sub

    Sub matiHidupkanPerlengkapanTempur(Optional ByVal onoff As Boolean = True)
        IDTextBox.Enabled = onoff
        JudulTextBox.Enabled = onoff
        PenerbitTextBox.Enabled = onoff
        PenulisTextBox.Enabled = onoff
        TebalTextBox.Enabled = onoff
        HargaTextBox.Enabled = onoff
        StockTextBox.Enabled = onoff
    End Sub

    Sub matiHidupkanBtn(ByVal a As Boolean, ByVal b As Boolean, ByVal c As Boolean, ByVal d As Boolean)
        btnTambah.Enabled = a
        btnSimpan.Enabled = b
        btnEdit.Enabled = c
        btnHapus.Enabled = d
    End Sub

    Sub combo()
        ComboBox1.Items.Clear()
        ComboBox1.Items.Add("Judul")
        ComboBox1.Items.Add("Penerbit")
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            'TODO: This line of code loads data into the 'DBInventoryDataSet1.TblBarang’ table. You can move, or remove it, as needed.
            Call tampilkanGrid()
            Call matiHidupkanPerlengkapanTempur(False)
            Call matiHidupkanBtn(True, False, True, True)
            Call combo()

        Catch ex As Exception
            MsgBox(ex.Message.ToString())
        End Try
    End Sub

    Private Sub btnTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTambah.Click
        If btnTambah.Text.ToLower = "tambah" Then
            Call kondisiForm()
            Call matiHidupkanBtn(True, True, False, False)
        ElseIf btnTambah.Text.ToLower = "batal" Then
            Call kondisiForm(False)
            Call matiHidupkanBtn(True, False, True, True)
            btnEdit.Text = "Edit"
        End If
    End Sub

    Sub kondisiForm(Optional ByVal tambah As Boolean = True, Optional ByVal kosongkan As Boolean = True)

        If tambah Then
            Call matiHidupkanPerlengkapanTempur()
            If kosongkan Then
                Call kosongkanPerlengkapanTempur()
            End If
            IDTextBox.Focus()
            btnTambah.Text = "Batal"
        Else
            If kosongkan Then
                Call kosongkanPerlengkapanTempur()
            End If
            IDTextBox.Focus()
            btnTambah.Text = "Tambah"
            Call matiHidupkanPerlengkapanTempur(False)
            Call tampilkanGrid()
        End If

    End Sub

    Sub cekKosongDimana()
        If IDTextBox.Text = String.Empty Then
            IDTextBox.Focus()
            Exit Sub
        End If
        If JudulTextBox.Text = String.Empty Then
            JudulTextBox.Focus()
            Exit Sub
        End If
        If PenerbitTextBox.Text = String.Empty Then
            PenerbitTextBox.Focus()
            Exit Sub
        End If
        If PenulisTextBox.Text = String.Empty Then
            PenulisTextBox.Focus()
            Exit Sub
        End If
        If TebalTextBox.Text = String.Empty Then
            TebalTextBox.Focus()
            Exit Sub
        End If
        If HargaTextBox.Text = String.Empty Then
            HargaTextBox.Focus()
            Exit Sub
        End If
        If StockTextBox.Text = String.Empty Then
            StockTextBox.Focus()
            Exit Sub
        End If
    End Sub

    Sub simpanData()
        Dim simpan As New OleDbCommand
        iniTehKoneksi.Close()
        iniTehKoneksi.Open()
        simpan.Connection = iniTehKoneksi
        simpan.CommandType = CommandType.Text
        simpan.CommandText = "INSERT INTO tbl_buku VALUES ('" & IDTextBox.Text & "', '" & JudulTextBox.Text & "', '" & PenerbitTextBox.Text & "','" & PenulisTextBox.Text & "','" & TebalTextBox.Text & "','" & HargaTextBox.Text & "','" & StockTextBox.Text & "')"
        simpan.ExecuteNonQuery()
    End Sub

    Sub updateData()
        iniTehKoneksi.Close()
        iniTehKoneksi.Open()
        Dim edit As New OleDbCommand
        edit.Connection = iniTehKoneksi
        edit.CommandType = CommandType.Text
        edit.CommandText = "UPDATE tbl_buku set Judul = '" & JudulTextBox.Text & "',Penerbit = '" & PenerbitTextBox.Text & "', Penulis = '" & PenulisTextBox.Text & "', Tebal = '" & TebalTextBox.Text & "', Harga = '" & HargaTextBox.Text & "', Stock = '" & StockTextBox.Text & "' WHERE ID = '" & IDTextBox.Text & "'"
        edit.ExecuteNonQuery()
    End Sub

    Sub hapusData(ByVal id As String)
        iniTehKoneksi.Close()
        iniTehKoneksi.Open()
        Dim hapus As New OleDbCommand
        hapus.Connection = iniTehKoneksi
        hapus.CommandType = CommandType.Text
        hapus.CommandText = "DELETE FROM tbl_buku WHERE ID = '" & id & "'"

        hapus.ExecuteNonQuery()
    End Sub

    Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
        Try
            If IDTextBox.Text <> String.Empty And JudulTextBox.Text <> String.Empty And PenerbitTextBox.Text <> String.Empty And PenulisTextBox.Text <> String.Empty And TebalTextBox.Text <> String.Empty And HargaTextBox.Text <> String.Empty And StockTextBox.Text <> String.Empty Then
                Try
                    Call simpanData()
                    MsgBox(alertBerhasilSimpan)
                    Call kondisiForm(False)
                    Call matiHidupkanBtn(True, False, True, True)
                Catch ex As Exception
                    MsgBox(alertGagalSimpan)
                End Try
            Else
                MsgBox(alertFieldKosong)
                Call cekKosongDimana()
                Call matiHidupkanBtn(True, True, False, False)
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString())
        End Try

    End Sub

    Private Sub btnCari_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCari.Click
        Try
            If ComboBox1.Text <> String.Empty And CariTextBox.Text <> String.Empty Then
                iniTehKoneksi.Close()
                iniTehKoneksi.Open()
                If ComboBox1.Text.ToLower = "judul" Then
                    iniTehKoneksi.Close()
                    iniTehKoneksi.Open()
                    'tampilkan ke grid
                    Dim ca As New OleDb.OleDbDataAdapter("Select * From tbl_buku WHERE Judul LIKE '%" & CariTextBox.Text & "%'", iniTehKoneksi)
                    Dim cs As New DataSet
                    ca.Fill(cs, "tbl_buku")
                    Tbl_bukuDataGridView.DataSource = cs.Tables("tbl_buku")

                ElseIf ComboBox1.Text.ToLower = "penerbit" Then
                    iniTehKoneksi.Close()
                    iniTehKoneksi.Open()
                    Dim cari As New OleDbCommand
                    cari.Connection = iniTehKoneksi
                    cari.CommandType = CommandType.Text
                    cari.CommandText = "SELECT * FROM tbl_buku WHERE Penerbit LIKE '%" & CariTextBox.Text & "%'"
                    Dim dr As OleDbDataReader
                    dr = cari.ExecuteReader
                    If dr.HasRows = True Then
                        dr.Read()
                        IDTextBox.Text = dr("ID")
                        JudulTextBox.Text = dr("Judul")
                        PenerbitTextBox.Text = dr("Penerbit")
                        PenulisTextBox.Text = dr("Penulis")
                        TebalTextBox.Text = dr("Tebal")
                        HargaTextBox.Text = dr("Harga")
                        StockTextBox.Text = dr("Stock")

                        Call kondisiForm(True, False)
                        matiHidupkanBtn(True, False, True, True)
                    End If

                    'tampilkan ke grid
                    Dim ca As New OleDb.OleDbDataAdapter("Select * From tbl_buku WHERE Penerbit LIKE '%" & CariTextBox.Text & "%'", iniTehKoneksi)
                    Dim cs As New DataSet
                    ca.Fill(cs, "tbl_buku")
                    Tbl_bukuDataGridView.DataSource = cs.Tables("tbl_buku")
                Else
                    MsgBox("Filter By '" & ComboBox1.Text & "' Tidak Tersedia")
                End If
            Else
                MsgBox("Silahkan isi field 'Filter By' atau teks pencariannya")
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString())
        End Try
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Try
            If btnEdit.Text.ToLower = "edit" Then
                Dim baris As Integer
                With Tbl_bukuDataGridView
                    baris = .CurrentRow.Index
                    IDTextBox.Text = .Item(0, baris).Value
                    JudulTextBox.Text = .Item(1, baris).Value
                    PenerbitTextBox.Text = .Item(2, baris).Value
                    PenulisTextBox.Text = .Item(3, baris).Value
                    TebalTextBox.Text = .Item(4, baris).Value
                    HargaTextBox.Text = .Item(5, baris).Value
                    StockTextBox.Text = .Item(6, baris).Value
                End With
                matiHidupkanBtn(True, False, True, False)
                btnEdit.Text = "Update"
                btnTambah.Text = "Batal"
                Call matiHidupkanPerlengkapanTempur()
                IDTextBox.Enabled = False
                JudulTextBox.Focus()
            ElseIf btnEdit.Text.ToLower = "update" Then
                If IDTextBox.Text <> String.Empty And JudulTextBox.Text <> String.Empty And PenerbitTextBox.Text <> String.Empty And PenulisTextBox.Text <> String.Empty And TebalTextBox.Text <> String.Empty And HargaTextBox.Text <> String.Empty And StockTextBox.Text <> String.Empty Then
                    Try
                        Call updateData()
                        MsgBox(alertBerhasilEdit)
                        Call kondisiForm(False)
                    Catch ex As Exception
                        MsgBox(alertGagalSimpan)
                    Finally
                        btnEdit.Text = "Edit"
                        Call matiHidupkanPerlengkapanTempur(False)
                        Call kosongkanPerlengkapanTempur()
                        Call matiHidupkanBtn(True, False, True, True)
                    End Try
                Else
                    MsgBox(alertFieldKosong)
                    Call cekKosongDimana()
                    Call matiHidupkanBtn(True, False, True, False)
                End If

            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString())
        End Try
    End Sub

    Private Sub btnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHapus.Click
        Try
            Try
                Dim baris As Integer
                Dim ID As String
                With Tbl_bukuDataGridView
                    baris = .CurrentRow.Index
                    ID = .Item(0, baris).Value
                End With

                Dim q = MsgBox("Data akan dihapus?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Konfirmasi")
                If q = vbYes Then
                    hapusData(ID)
                End If

                Call kondisiForm(False)
                MsgBox(alertBerhasilHapus)
            Catch ex As Exception
                MsgBox(alertGagalHapus)
            Finally
                matiHidupkanPerlengkapanTempur(False)
                kosongkanPerlengkapanTempur()
                Call matiHidupkanBtn(True, False, True, True)
            End Try

        Catch ex As Exception
            MsgBox(ex.Message.ToString())
        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'TODO: This line of code loads data into the 'DBInventoryDataSet1.TblBarang’ table. You can move, or remove it, as needed.
        Call tampilkanGrid()
        Call kosongkanPerlengkapanTempur()
        Call matiHidupkanPerlengkapanTempur(False)
        Call matiHidupkanBtn(True, False, True, True)
        Call combo()
    End Sub
End Class

Senin, 24 November 2014

UTS Mata kuliah Pemrograman Visual Eresha Wikrama Kelas B

Program Terbilang


Program ini dibuat untuk menkonversikan nilai / angka ke bentuk huruf, berikut screenshootnya :



Dan ini source code dari program ini :


1:  Public Class Form1  
2:    
3:    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProses.Click  
4:      Dim newClass As classTerbilang = New classTerbilang()  
5:      If txtAngka.Text <> "" Then  
6:        lblHasil.Text = newClass.funcCalculate(Val(txtAngka.Text))  
7:      Else  
8:        MsgBox("Silahkan isi field angka", MsgBoxStyle.Information, "Validation")  
9:      End If  
10:      txtAngka.Focus()  
11:    End Sub  
12:    
13:    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
14:      Me.Text = "mredzase.blogspot.com"  
15:    End Sub  
16:    
17:    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAngka.KeyPress  
18:      If (e.KeyChar = Chr(13)) Then  
19:        Button1_Click(New Object(), New EventArgs())  
20:      End If  
21:    
22:      If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then  
23:        e.Handled = True  
24:      End If  
25:    
26:    End Sub  
27:    
28:  End Class  
29:    
30:  Option Explicit On  
31:  Public Class classTerbilang  
32:    Public Function funcCalculate(ByVal x As Double) As String  
33:      Dim tampung As Double  
34:      Dim teks As String = String.Empty  
35:      Dim bagian As String = String.Empty  
36:      Dim i As Integer  
37:      Dim tanda As Boolean  
38:      Dim terbilang As String = String.Empty  
39:    
40:      Dim letak(5)  
41:      letak(1) = "RIBU "  
42:      letak(2) = "JUTA "  
43:      letak(3) = "MILYAR "  
44:      letak(4) = "TRILYUN "  
45:    
46:      If (x < 0) Then  
47:        terbilang = ""  
48:        GoTo keluar  
49:      End If  
50:    
51:      If (x = 0) Then  
52:        terbilang = "NOL"  
53:        GoTo keluar  
54:      End If  
55:    
56:      If (x < 2000) Then  
57:        tanda = True  
58:      End If  
59:      teks = ""  
60:    
61:      If (x >= 1.0E+15) Then  
62:        terbilang = "NILAI TERLALU BESAR"  
63:        GoTo keluar  
64:      End If  
65:    
66:      For i = 4 To 1 Step -1  
67:        tampung = Int(x / (10 ^ (3 * i)))  
68:        If (tampung > 0) Then  
69:          bagian = funcRatusan(tampung, tanda)  
70:          teks = teks & bagian & letak(i)  
71:        End If  
72:        x = x - tampung * (10 ^ (3 * i))  
73:      Next  
74:      teks = teks & funcRatusan(x, False)  
75:      terbilang = teks  
76:  keluar:  
77:      Return terbilang  
78:    End Function  
79:    
80:    Function funcRatusan(ByVal y As Double, ByVal flag As Boolean) As String  
81:      Dim tmp As Double  
82:      Dim bilang As String = String.Empty  
83:      Dim bag As String = String.Empty  
84:      Dim j As Integer  
85:      Dim ratusan As String = String.Empty  
86:    
87:      Dim angka(9)  
88:      angka(1) = "SE"  
89:      angka(2) = "DUA "  
90:      angka(3) = "TIGA "  
91:      angka(4) = "EMPAT "  
92:      angka(5) = "LIMA "  
93:      angka(6) = "ENAM "  
94:      angka(7) = "TUJUH "  
95:      angka(8) = "DELAPAN "  
96:      angka(9) = "SEMBILAN "  
97:    
98:      Dim posisi(2)  
99:      posisi(1) = "PULUH "  
100:      posisi(2) = "RATUS "  
101:    
102:      bilang = ""  
103:      For j = 2 To 1 Step -1  
104:        tmp = Int(y / (10 ^ j))  
105:        If (tmp > 0) Then  
106:          bag = angka(tmp)  
107:          If (j = 1 And tmp = 1) Then  
108:            y = y - tmp * 10 ^ j  
109:            If (y >= 1) Then  
110:              posisi(j) = "BELAS "  
111:            Else  
112:              angka(y) = "SE"  
113:            End If  
114:            bilang = bilang & angka(y) & posisi(j)  
115:            ratusan = bilang  
116:          Else  
117:            bilang = bilang & bag & posisi(j)  
118:          End If  
119:        End If  
120:        y = y - tmp * 10 ^ j  
121:      Next  
122:    
123:      If (flag = False) Then  
124:        angka(1) = "SATU "  
125:      End If  
126:      bilang = bilang & angka(IIf(y < 0, Nothing, y))  
127:      ratusan = bilang  
128:    
129:      Return ratusan  
130:    End Function  
131:  End Class  
132:    

Rabu, 13 November 2013

Polimorfisme (Polymorphism)

1. Penjelasan singkat tentang Polymorphism

Polymorphism merupakan suatu object yang dapat memiliki berbagai bentuk, sebagai object dari classnya sendiri atau object dari superclassnya. Kata "polimorfisme" berarti "banyak bentuk". Ini berasal dari kata Yunani "poli" (berarti banyak) dan "MorphOS" (bentuk berarti).

2. Perbedaan Class dan Object
Kita langsung ke contoh aja ya, supaya lebih jelas :D
Contoh Class adalah Motor, contoh Object adalah mobilnya Redza, mobilku. 
Jadi class itu merupakan parent dari suatu object, sedangkan object adalah sub dari parent itu sendiri atau semua bagian dari class.

Ada 2 macam cara untuk menggunakan Polymorphism : 
1. Overloading: Penggunaan satu nama untuk beberapa method yang berbeda (beda parameter)
2. Overriding: Terjadi ketika deklarasi method subclass dengan nama dan parameter yang sama  dengan method dari superclassnya

Berikut adalah contoh dari Polymorphism pada java:

1. Kita buat dulu class Motor, seperti gambar dibawah ini : 




2. Lalu kita buat class subMotornya, seperti gambar dibawah ini :