Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

114

Lampiran 2 Listing Program


1). Class Database
Imports System.Data.SqlClient
Module database
Private xkoneksi As New SqlConnection("server=
(local);database=pengadaan;integrated security=true")
Public xcommand As New SqlCommand("", xkoneksi)
Public xrdr As SqlDataReader
Public ds As DataSet
Public da As SqlDataAdapter
Public dr As SqlDataReader
Public tahun As Integer = Format(Now.Date, "yyyy")
Public semester As Integer = 1
Private Sub konek()
Try
closeCon()
xkoneksi.Open()
'If xkoneksi.State = ConnectionState.Closed Then
xkoneksi.Open()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Public Sub closeCon()
Try
If xkoneksi.State = ConnectionState.Open Then
xkoneksi.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub isicombo(ByVal query As String, ByVal cmb As ComboBox)
konek()
xcommand.CommandText = query
xrdr = xcommand.ExecuteReader
Dim bol As Boolean = False
If xrdr.HasRows Then
bol = True
While xrdr.Read
cmb.Items.Add(xrdr.Item(0))
End While
End If
xrdr.Close()
'xkoneksi.Close()
If bol Then cmb.SelectedIndex = 0
End Sub
Public Function cekpass(ByVal query As String)
konek()
Try
xcommand.CommandText = query
Return xcommand.ExecuteScalar
Catch ex As Exception
End Try
End Function
Public Sub getDataset(ByVal sql As String, ByVal table As String)
115

Try
closeCon()
ds = New DataSet
da = New SqlDataAdapter(sql, xkoneksi)
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
da.Fill(ds, table)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub DML(ByVal query As String, ByVal jns As String)
konek()
xcommand.CommandText = query
Try
xcommand.ExecuteNonQuery()
MsgBox(jns & "success", MsgBoxStyle.Information)
Catch ex As SqlException
MsgBox(ex.Message)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub DML(ByVal query As String)
konek()
xcommand.CommandText = query
Try
xcommand.ExecuteNonQuery()
Catch ex As SqlException
MsgBox(ex.Message)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub getRead(ByVal query As String)
konek()
xcommand.CommandText = query
Try
dr = xcommand.ExecuteReader
Catch ex As SqlException
MsgBox(ex.Message)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public id_sp As String
Public nm_sp As String
Public id_bp As String
Public nm_bp As String
Public jml As String
Public satuan As String
Public harga As Integer
Public statuslogin As Char
Public userlogin As String
Public bollogin As Boolean

End Module
116

2). Class BAHP

Public Class BAHP


Public bol As Boolean
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Close()
End Sub

Private Sub BAHP_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
gdata("select * from BAHP")
If bol = True Then
Label2.Text = "*) Klik 2 kali untuk memilih data "
End If
End Sub

Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object,


ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles
DataGridView1.CellDoubleClick

Dim bp As New DML_BAHP

If bol = True Then


database.id_bp = DataGridView1.Item(0,
DataGridView1.CurrentRow.Index).Value
database.nm_bp = DataGridView1.Item(1,
DataGridView1.CurrentRow.Index).Value
bol = False
Close()
Else
bp.boldml = True
bp.id = DataGridView1.CurrentCell.Value
bp.ShowDialog()
gdata("select * from BAHP")
End If

End Sub
Sub gdata(ByVal txt)
database.getDataset(txt, "Bahp")
DataGridView1.DataSource = ds.Tables("Bahp").DefaultView
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles TextBox1.TextChanged
gdata("select * from BAHP where nama_bahp like '%" &
TextBox1.Text & "%'")
End Sub

End Class
117

3). Class DML_BAHP

Public Class DML_BAHP


Public boldml As Boolean
Public id As String

Private Sub DML_Satuan_Load(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles MyBase.Load
isicombo()
cmbsatuan.SelectedValue = 0

If boldml = True Then


database.getRead("SELECT id_Bahp
,nama_Bahp,stok_limit,satuan from bahp where id_bahp = '" & id & "'")
If dr.HasRows Then
While (dr.Read)
txtid.Text = dr(0)
txtnama.Text = dr(1)
txtlimit.Text = dr(2)
cmbsatuan.SelectedItem = dr(3)
End While
End If
closeCon()
Else : boldml = False

idotomatis()
End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim bol As Boolean = True
For Each i As Object In GroupBox1.Controls
If TypeOf i Is TextBox Then
If i.text = "" Then
bol = False
End If
End If
Next

If bol = True Then


Dim sql As String = ""
If boldml = False Then
database.DML("insert into BAHP values('" & txtid.Text &
"','" & txtnama.Text & "',0," & txtlimit.Text & ",'" & cmbsatuan.Text &
"')", "Insert Data Baru ")
Else
sql = "update BAHP set nama_BAHP = '" & txtnama.Text &
"',"
sql &= "stok_limit = " & txtlimit.Text
sql &= ",satuan = '" & cmbsatuan.Text & "'"
sql &= "where id_Bahp = '" & txtid.Text & "'"
database.DML(sql, "Update data ")
End If
idotomatis()
118

Else
MsgBox("inputan tidak boleh koong", MsgBoxStyle.Critical)
End If
bol = True

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Close()
End Sub
Sub idotomatis()
Dim idlama As Integer = database.cekpass("select count (id_bahp)
from bahp")
Dim idbaru As String = ""
If idlama = 0 Then
idbaru = "BAHP0001"
Else
idlama = idlama + 1
If idlama < 10 Then
idbaru = "BAHP000" & idlama
ElseIf idlama < 100 Then
idbaru = "BAHP00" & idlama
ElseIf idlama < 1000 Then
idbaru = "BAHP0" & idlama
ElseIf idlama < 10000 Then
idbaru = "BAHP" & idlama
End If

End If
For Each i As Object In GroupBox1.Controls
If TypeOf i Is TextBox Then
i.text = ""
End If
Next
txtid.Text = idbaru
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Dim st As New Satuan
st.ShowDialog()
cmbsatuan.Items.Clear()
isicombo()
cmbsatuan.SelectedValue = 0
End Sub

Private Sub txtlimit_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles txtlimit.KeyPress
Dim k As New numeric
k.io(e)
End Sub

End Class
119

4). Class Peramalan

Public Class Peramalan


Dim mseavg As Double
Dim rmlavg As Double
Dim msesmt As Double
Dim rmlsmt As Double
Dim alp As Double
Sub exponensial(ByVal txt As String, ByVal tgl As Date)
Dim STR = ""
Dim str1 = ""
Dim mse As Double = -1
Dim kesalahan As Double
Dim alpha_terpakai As Double
Dim alpa As Double = 0.1
Dim i As Integer = 0
Dim arr() As Double = Module1.getdata(txt, tgl)
Dim hasil(arr.Length - 1) As Double
For cnt As Integer = 0 To 8
str1 = ""
If cnt > 0 Then
STR = STR & " , "
End If
kesalahan = 0
Dim k = 0
hasil(1) = arr(0)
For h As Integer = 2 To arr.Length - 1
hasil(h) = hasil(h - 1) + (alpa * (arr(k + 1) - hasil(h
- 1)))
k = k + 1
Next
For h As Integer = 1 To arr.Length - 1
kesalahan = kesalahan + ((arr(h) - hasil(h)) ^ 2)
str1 = str1 & " , " & hasil(h)
Next
STR = STR & Math.Round(kesalahan, 2) & " , " &
Math.Round(hasil(hasil.Length - 1) + (alpa * (arr(arr.Length - 1) -
hasil(hasil.Length - 1))), 2)
If mse = -1 Then
mse = kesalahan
alpha_terpakai = alpa
End If
If kesalahan < mse Then

mse = kesalahan
alpha_terpakai = alpa
End If
alpa = alpa + 0.1
If cnt = 8 Then
kesalahan = 0
k = 0
For h As Integer = 2 To arr.Length - 1
hasil(h) = hasil(h - 1) + (alpha_terpakai * (arr(k +
1) - hasil(h - 1)))
k = k + 1
Next
For h As Integer = 1 To arr.Length - 1
120

kesalahan = kesalahan + ((arr(h) - hasil(h)) ^ 2)


Next
End If
Next
DML("insert into Rml_Smoothing values(1,'" & txt & "' , " & STR
& " )")
msesmt = kesalahan / (arr.Length - 1)
rmlsmt = hasil(hasil.Length - 1) + (alpha_terpakai *
(arr(arr.Length - 1) - hasil(hasil.Length - 1)))
alp = alpha_terpakai
End Sub
Sub average(ByVal txt As String, ByVal tgl As Date)
Dim kesalahan As Double = 0
Dim arr() As Double = Module1.getdata(txt, tgl)
Dim hasil(arr.Length - 1) As Double
Dim kesalahan_terpakai As Double
Dim n_terpakai As Integer
Dim str As String = ""
Dim ram As Double
For n As Integer = 0 To 2
If n > 0 Then
str = str & " , "
End If
kesalahan = 0
ram = 0
Dim inc As Integer = 0
For h As Integer = 0 To arr.Length - (2 + n)
Dim tmp1 As Integer = 0
For d As Integer = 0 To n
tmp1 = tmp1 + arr(d + inc)
Next
hasil(h) = tmp1 / (n + 1)
kesalahan = kesalahan + (Math.Abs(arr(h + (n + 1)) -
hasil(h)) ^ 2)
inc = h + 1
Next
For d As Integer = 0 To n
ram = ram + arr(arr.Length - 1 - d)
Next
str = str & Math.Round((kesalahan / (arr.Length - (n + 1))),
2) & " , " & Math.Round(ram / (n + 1), 2)
kesalahan = kesalahan / (arr.Length - (n + 1))
If n = 0 Then
kesalahan_terpakai = kesalahan
n_terpakai = n + 1
ElseIf kesalahan_terpakai > kesalahan Then
kesalahan_terpakai = kesalahan
n_terpakai = n + 1
End If
Next
Dim rml As Double = 0
For d As Integer = 0 To n_terpakai - 1
rml = rml + arr(arr.Length - n_terpakai + d)
Next
DML("insert into rml_average values(1,'" & txt & "'," & str &
")")
rmlavg = Math.Round(rml / n_terpakai, 2)
121

mseavg = Math.Round(kesalahan_terpakai, 2)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click

DML("delete ramalan")
DML("delete rml_average")
DML("delete Rml_Smoothing")
For i As Integer = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows.RemoveAt(0)
Next
Dim id(0) As String
Dim nama(0) As String
Dim cntr = 0
getRead("select id_bahp,nama_bahp from bahp")
If dr.HasRows Then
While dr.Read
ReDim Preserve id(cntr)
ReDim Preserve nama(cntr)
id(cntr) = dr(0)
nama(cntr) = dr(1)
cntr = cntr + 1
End While
End If

For i As Integer = 0 To id.Length - 1


If DateDiff(DateInterval.Month, cekpass("select top 1 (tgl)
from detail_penerimaan d,penerimaan_bahp p where id_bahp = '" & id(i) &
"' and p.id_terima = d.id_terima order by tgl asc"), Now.Date) >= 3 Then
If cekpass("select count(tgl) from detail_penerimaan
d,penerimaan_bahp p where id_bahp = '" & id(i) & "' and p.id_terima =
d.id_terima ") > 0 Then
Dim dt As Date = ComboBox1.SelectedIndex + 1 & "/2/"
& NumericUpDown1.Value
If DateDiff(DateInterval.Month, dt, Now.Date) >
DateDiff(DateInterval.Month, cekpass("select top 1 (tgl) from
detail_penerimaan d,penerimaan_bahp p where id_bahp = '" & id(i) & "'
and p.id_terima = d.id_terima order by tgl asc"), Now.Date) Then
average(id(i), cekpass("select top 1 (tgl) from
detail_penerimaan d,penerimaan_bahp p where id_bahp = '" & id(i) & "'
and p.id_terima = d.id_terima order by tgl asc"))
exponensial(id(i), cekpass("select top 1 (tgl)
from detail_penerimaan d,penerimaan_bahp p where id_bahp = '" & id(i) &
"' and p.id_terima = d.id_terima order by tgl asc"))
Else
average(id(i), dt)
exponensial(id(i), dt)
End If
DataGridView1.Rows.Add(1)
DataGridView1.Item(0, DataGridView1.Rows.Count -
1).Value = id(i)
DataGridView1.Item(1, DataGridView1.Rows.Count -
1).Value = nama(i)
DataGridView1.Item(2, DataGridView1.Rows.Count -
1).Value = Math.Round(mseavg, 2)
122

DataGridView1.Item(3, DataGridView1.Rows.Count -
1).Value = Math.Round(msesmt, 2)
DataGridView1.Item(4, DataGridView1.Rows.Count -
1).Value = Math.Round(rmlavg, 2)
DataGridView1.Item(5, DataGridView1.Rows.Count -
1).Value = Math.Round(rmlsmt, 2)

End If
End If
Next

cari()

End Sub
Sub cari()
Dim smt As Integer = 0
Dim avg As Integer = 0
For i As Integer = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Item(2, i).Value > DataGridView1.Item(3,
i).Value Then
DataGridView1.Item(5, i).Style.BackColor = Color.Green
smt = smt + 1
Else
DataGridView1.Item(4, i).Style.BackColor = Color.Green
avg = avg + 1
End If
Next
data.Text = DataGridView1.Rows.Count & " data"
txt_avg.Text = avg & " data"
txt_exp.Text = smt & " data"
periode.Text = ComboBox1.SelectedIndex + 1 & "-" &
NumericUpDown1.Value & " s/d " & Now.AddMonths(-1).Month & "-" &
Now.AddMonths(-1).Year
txt_pavg.Text = avg / DataGridView1.Rows.Count * 100 & "%"
txt_psmt.Text = smt / DataGridView1.Rows.Count * 100 & "%"

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
DML("delete from detail_ramalan")
Dim hsl As Double
Dim mtd As String
For i As Integer = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Item(2, i).Value < DataGridView1.Item(3,
i).Value Then
hsl = DataGridView1.Item(4, i).Value
mtd = "avg"
Else
hsl = DataGridView1.Item(5, i).Value
mtd = "smt"
End If
DML("insert into detail_ramalan values('" &
DataGridView1.Item(0, i).Value & "'," & DataGridView1.Item(2, i).Value &
"," & DataGridView1.Item(3, i).Value & "," & DataGridView1.Item(4,
i).Value & "," & DataGridView1.Item(5, i).Value & "," & hsl & ",'" & mtd
& "')")
123

Next
MsgBox("Input Data Ramalan Kooontol", MsgBoxStyle.Information)
End Sub
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles
DataGridView1.CellDoubleClick
Dim dtl As New Detail_Peramalan
dtl.id = DataGridView1.Item(0,
DataGridView1.CurrentRow.Index).Value
dtl.nm = DataGridView1.Item(1,
DataGridView1.CurrentRow.Index).Value
Dim dt As Date = ComboBox1.SelectedIndex + 1 & "/2/" &
NumericUpDown1.Value
If DateDiff(DateInterval.Month, dt, Now.Date) >
DateDiff(DateInterval.Month, cekpass("select top 1 (tgl) from
detail_penerimaan d,penerimaan_bahp p where id_bahp = '" &
DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value & "' and
p.id_terima = d.id_terima order by tgl asc"), Now.Date) Then
dtl.tgl = cekpass("select top 1 (tgl) from
detail_penerimaan d,penerimaan_bahp p where id_bahp = '" &
DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value & "' and
p.id_terima = d.id_terima order by tgl asc")
Else
dtl.tgl = dt
End If
dtl.ShowDialog()
End Sub

Private Sub Peramalan_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
ComboBox1.SelectedIndex = 0
End Sub
Private Sub NumericUpDown1_ValueChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
NumericUpDown1.ValueChanged
If NumericUpDown1.Value > Now.Year Then
MsgBox("Data Tidak Ada")
NumericUpDown1.Value = Now.Year
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
MsgBox("asu")
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim ctk As New PieRamalan
ctk.ShowDialog()
End Sub
End Class

You might also like