Jumat, 06 Maret 2015

Billing Tiket Bioskop menggunakan Database Ms. Access

Aplikasi Billing Tiket Bioskop menggunakan Database Ms.Access
Ditutorial sebelumnya kita belajar mengenai aplikasi Billing Tiket Bioskop sederhana. Kali ini kita akan melanjutkan aplikasi tersebut dengan menggunakan database.
Database yang digunakan menggunakan Microsoft Access. Berikut form tampilan dan script codingnya masih menggunakan VB.Net.

Berikut tampilan Form masih sama dengan yang sebelumnya hanya saja ditambah komponen DataGridView.


Buat Data Base Dengan Ms.Access dengan nama dbBilling.accdb, buat field sesuai entrain form diatas, lalu simpan di D:\ato\KAMPUS\Semester 5\Pemrograman Visual\UAS\BillingTiketBioskop\dbBilling.accdb"
Buat satu buah class dengan nama hitung.vb. Class tersebut digunakan untuk proses perhitungan uang kembalian seperti dijelaskan pada tutorial sebelumnya. Berikut script untuk class hitung.vb
Public Class hitung
    Public Function totalHarga(ByVal harga As Double, ByVal jumlah As Double) As Double
        Dim hasil As Double
        hasil = harga * jumlah
        totalHarga = hasil
    End Function

    Public Function kembalian(ByVal total As Double, ByVal bayar As Double) As Double
        Dim hasil As Double
        hasil = bayar - total
        kembalian = hasil
    End Function
End Class


Masukan script berikut pada editor code form diatas.
Untuk koneksi ke database Ms.acces
Imports System.Data.OleDb
Public Class Form1
    Dim koneksi As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\ato\KAMPUS\Semester 5\Pemrograman Visual\UAS\BillingTiketBioskop\dbBilling.accdb")


Untuk menampilkan data ke DataGridView.

    Sub tampilGrid()
        koneksi.Close()
        koneksi.Open()

        Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM TBL_BILLING", koneksi)
        Dim ds As New DataSet

        da.Fill(ds, "TBL_BILLING")
        DataGridView1.DataSource = ds.Tables("TBL_BILLING")

    End Sub

Untuk mencari data.

    Function cariData(ByVal txt As String)
        Dim cmdCari As New OleDbCommand
        cmdCari.Connection = koneksi
        cmdCari.CommandType = CommandType.Text
        cmdCari.CommandText = "SELECT * FROM TBL_BILLING WHERE TBKODE= " & txt & ""
        cariData = cmdCari
    End Function

Untuk load form 1 dimana fungsi-fungsi untuk tampilan awal dijalankan.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tampilGrid()
        cmdDelete.Enabled = False
        cmdUpdate.Enabled = False
        For i As Integer = 1 To 6
            cboStudio.Items.Add("Studio " & i)
        Next

        With cboHari
            .Items.Add("Senin")
            .Items.Add("Selasa")
            .Items.Add("Rabu")
            .Items.Add("Kamis")
            .Items.Add("Jumat")
            .Items.Add("Sabtu")
            .Items.Add("Minggu")
        End With

        Dim a As Integer = 11
        Do While a < 24
            cboJam.Items.Add(a & ":00")
            a = a + 1
        Loop

        txtBayar.Enabled = False
        txtKembali.Enabled = False
        txtFilm.Enabled = False
        txtHarga.Enabled = False
    End Sub

Berikur fungsi-fungsi untuk kebutuhan seperti isi-isi combobox

    Private Sub cboStudio_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboStudio.SelectedIndexChanged
        Select Case cboStudio.Text
            Case Is = "Studio 1"
                txtFilm.Text = "The Hunger Games : Mokingjay"
            Case Is = "Studio 2"
                txtFilm.Text = "Big Hero 6"
            Case Is = "Studio 3"
                txtFilm.Text = "Interstellar"
            Case Is = "Studio 4"
                txtFilm.Text = "The Avenger 2"
            Case Is = "Studio 5"
                txtFilm.Text = "Automata"
            Case Is = "Studio 6"
                txtFilm.Text = "Penguin Of Madagascar"
        End Select
    End Sub
    Private Sub cboHari_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboHari.SelectedIndexChanged
        If cboHari.Text = "Senin" Or cboHari.Text = "Selasa" Or cboHari.Text = "Rabu" Or cboHari.Text = "Kamis" Then
            txtHarga.Text = "35000"
        ElseIf cboHari.Text = "Jumat" Then
            txtHarga.Text = "40000"
        Else
            txtHarga.Text = "50000"
        End If
    End Sub
    Private Sub txtJumlah_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtJumlah.KeyUp
        Dim hitung As hitung
        hitung = New hitung
        txtBayar.Text = hitung.totalHarga(Val(txtHarga.Text), Val(txtJumlah.Text))
    End Sub
    Private Sub cmdProses_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProses.Click
        Dim hitung As hitung
        hitung = New hitung
        txtKembali.Text = hitung.kembalian(Val(txtBayar.Text), Val(txtDibayar.Text))
    End Sub
    Private Sub cmdBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBatal.Click
        kosongkan()
    End Sub
    Private Sub cmdKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdKeluar.Click
        End
    End Sub
    Sub kosongkan()
        txtKode.Text = ""
        cboStudio.Text = ""
        txtFilm.Text = ""
        cboHari.Text = ""
        txtHarga.Text = ""
        txtJumlah.Text = ""
        txtBayar.Text = ""
        txtDibayar.Text = ""
        txtKembali.Text = ""
    End Sub

    Function validasiForm()
        If cboStudio.Text = "" Or txtFilm.Text = "" Or cboJam.Text = "" Or cboHari.Text = "" Or txtHarga.Text = "" Or txtJumlah.Text = "" Or txtBayar.Text = "" Or txtDibayar.Text = "" Or txtKembali.Text = "" Then
            validasiForm = "tidak valid"
        Else
            validasiForm = "valid"
        End If
    End Function

Untuk menyimpan data yang telah di entri.

    Private Sub cmdSave_Click(sender As System.Object, e As System.EventArgs) Handles cmdSave.Click
        Dim cek = validasiForm()

        koneksi.Close()
        koneksi.Open()

        Dim x = MsgBox("Simpan Data?", vbYesNo + vbQuestion, "Konfirmasi")
        If x = vbYes And cek = "valid" Then
            Dim cmdSave As New OleDbCommand
            cmdSave.Connection = koneksi
            cmdSave.CommandType = CommandType.Text
            cmdSave.CommandText = "INSERT INTO TBL_BILLING VALUES('" & txtKode.Text & "','" & cboStudio.Text & "', '" & txtFilm.Text & "', '" & cboJam.Text & "', '" & cboHari.Text & "', '" & txtHarga.Text & "', '" & txtJumlah.Text & "', '" & txtBayar.Text & "','" & txtDibayar.Text & "','" & txtKembali.Text & "')"
            cmdSave.ExecuteNonQuery()
            MsgBox("Data billing tiket berhasil disimpan")

            Call tampilGrid()
            Call kosongkan()
        ElseIf cek = "tidak valid" Then
            MsgBox("Data tidak valid, silahkan coba kembali...")
            cboStudio.Focus()
        Else
            cboStudio.Focus()
        End If
    End Sub

Untuk update data setela diedit.

    Private Sub cmdUpdate_Click(sender As System.Object, e As System.EventArgs) Handles cmdUpdate.Click
        Dim cek = validasiForm()
        If cek = "valid" Then
            koneksi.Close()
            koneksi.Open()

            Dim cmdUpdate As New OleDbCommand
            cmdUpdate.Connection = koneksi
            cmdUpdate.CommandType = CommandType.Text
            cmdUpdate.CommandText = "UPDATE TBL_BILLING SET TBSTUDIO='" & cboStudio.Text & "', TBFILM='" & txtFilm.Text & "', TBJAM='" & cboJam.Text & "', TBHARI='" & cboHari.Text & "', TBHARGA='" & txtHarga.Text & "', TBJMLTIKET='" & txtJumlah.Text & "', TBLTOTAL='" & txtBayar.Text & "', TBDIBAYAR='" & txtDibayar.Text & "', TBKEMBALI='" & txtKembali.Text & "' WHERE TBKODE='" & txtKode.Text & "'"
            cmdUpdate.ExecuteNonQuery()
            MsgBox("Data berhasil diperbaharui")

            Call tampilGrid()
            Call kosongkan()
        Else
            MsgBox("Data tidak valid, silahkan coba kembali...")
            cboStudio.Focus()
        End If
    End Sub

Untuk Menghapus Data.

    Private Sub cmdDelete_Click(sender As System.Object, e As System.EventArgs) Handles cmdDelete.Click
        Dim cmdCari = cariData(txtCari.Text)

        Dim dr As OleDbDataReader
        dr = cmdCari.ExecuteReader

        If dr.HasRows = True Then
            koneksi.Close()
            koneksi.Open()

            Dim cmdDelete As New OleDbCommand
            cmdDelete.Connection = koneksi
            cmdDelete.CommandType = CommandType.Text
            cmdDelete.CommandText = "DELETE FROM TBL_BILLING WHERE TBKODE= '" & txtKode.Text & "'"
            cmdDelete.ExecuteNonQuery()
            MsgBox("Data berhasil dihapus")

            Call tampilGrid()
            Call kosongkan()
        Else
            MsgBox("Data " & txtKode.Text & " tidak ditemukan, data gagal dihapus", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
        End If

    End Sub

    Private Sub cmdCari_Click(sender As System.Object, e As System.EventArgs) Handles cmdCari.Click
        koneksi.Close()
        koneksi.Open()

        Dim teangan = cariData(txtCari.Text)

        Dim dr As OleDbDataReader
        dr = teangan.ExecuteReader

        If dr.HasRows = True Then
            dr.Read()
            txtKode.Text = dr("TBKODE")
            cboStudio.Text = dr("TBSTUDIO")
            txtFilm.Text = dr("TBFILM")
            cboJam.Text = dr("TBJAM")
            cboHari.Text = dr("TBHARI")
            txtHarga.Text = dr("TBHARGA")
            txtJumlah.Text = dr("TBJMLTIKET")
            txtBayar.Text = dr("TBTOTAL")
            txtDibayar.Text = dr("TBDIBAYAR")
            txtKembali.Text = dr("TBKEMBALI")

            cmdUpdate.Enabled = True
            cmdDelete.Enabled = True
        Else
            MsgBox("Data " & TxtCari.Text & " tidak ditemukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
        End If
        txtKode.Enabled = False
    End Sub
End Class


Sekian tutorial dari saya, samapi jumpa ditutorial selanjutnya.
Selamat mencoba.

Senin, 24 November 2014

Membuat Billing Tiket Bioskop Sederhana

Membuat billing tiket bioskop sederhana dan sangat mudah sekali . Program ini tidak perlu menggunakan database karena masih sangat sederhana.
Disamping kesederhanaannya program ini sangat tepat untuk belajar dasar vb.net karena sudah terdapat
contoh fungsi aritmatika, condition if, elseif, for while Case dan Class.
Langsung saja berikut sedikit screen shoot aplikasi beserta codingannya.




Script Form1.vb

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 1 To 6
            cboStudio.Items.Add("Studio " & i)
        Next

        With cboHari
            .Items.Add("Senin")
            .Items.Add("Selasa")
            .Items.Add("Rabu")
            .Items.Add("Kamis")
            .Items.Add("Jumat")
            .Items.Add("Sabtu")
            .Items.Add("Minggu")
        End With

        Dim a As Integer = 11
        Do While a < 24
            cboJam.Items.Add(a & ":00")
            a = a + 1
        Loop

        txtBayar.Enabled = False
        txtKembali.Enabled = False
        txtFilm.Enabled = False
        txtHarga.Enabled = False
    End Sub

    Private Sub cboStudio_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboStudio.SelectedIndexChanged
        Select Case cboStudio.Text
            Case Is = "Studio 1"
                txtFilm.Text = "The Hunger Games : Mokingjay"
            Case Is = "Studio 2"
                txtFilm.Text = "Big Hero 6"
            Case Is = "Studio 3"
                txtFilm.Text = "Interstellar"
            Case Is = "Studio 4"
                txtFilm.Text = "Salah Body"
            Case Is = "Studio 5"
                txtFilm.Text = "Automata"
            Case Is = "Studio 6"
                txtFilm.Text = "Penguin Of Madagascar"
        End Select
    End Sub
    Private Sub cboHari_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboHari.SelectedIndexChanged
        If cboHari.Text = "Senin" Or cboHari.Text = "Selasa" Or cboHari.Text = "Rabu" Or cboHari.Text = "Kamis" Then
            txtHarga.Text = "35000"
        ElseIf cboHari.Text = "Jumat" Then
            txtHarga.Text = "40000"
        Else
            txtHarga.Text = "50000"
        End If
    End Sub
    Private Sub txtJumlah_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtJumlah.KeyUp
        Dim hitung As hitung
        hitung = New hitung
        txtBayar.Text = hitung.totalHarga(Val(txtHarga.Text), Val(txtJumlah.Text))
    End Sub
    Private Sub cmdProses_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProses.Click
        Dim hitung As hitung
        hitung = New hitung
        txtKembali.Text = hitung.kembalian(Val(txtBayar.Text), Val(txtDibayar.Text))
    End Sub
    Private Sub cmdBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBatal.Click
        cboStudio.Text = ""
        txtFilm.Text = ""
        cboHari.Text = ""
        txtHarga.Text = ""
        txtJumlah.Text = ""
        txtBayar.Text = ""
        txtDibayar.Text = ""
        txtKembali.Text = ""
    End Sub
    Private Sub cmdKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdKeluar.Click
        End
    End Sub
End Class


Script hitung.class

Public Class hitung
    Public Function totalHarga(ByVal harga As Double, ByVal jumlah As Double) As Double
        Dim hasil As Double
        hasil = harga * jumlah
        totalHarga = hasil
    End Function

    Public Function kembalian(ByVal total As Double, ByVal bayar As Double) As Double
        Dim hasil As Double
        hasil = bayar - total
        kembalian = hasil
    End Function
End Class