Berikut pembahasan dari soal UTS Lab PBO (cont'd Algo), jadikan code berikut hanya sebagai refensi, silahkan dikembangkan jika perlu. Tidak mutlak code harus sama persis seperti di bawah ini.
Source Code :
Imports System.IO
Public Class Form1
Dim arrJenisMobil() As String
Dim arrHargaMobil() As Double
Dim cnt As Integer
Sub ClearInput()
txtNama.Text = ""
txtAlamat.Text = ""
txtNoHP.Text = ""
cboJenisMobil.Text = "(Silahkan Pilih)"
txtHari.Text = 0
txtNama.Focus()
End Sub
Sub fillComboBox()
cboJenisMobil.Items.Clear()
For i As Integer = 0 To arrJenisMobil.GetUpperBound(0)
cboJenisMobil.Items.Add(arrJenisMobil(i).ToString)
Next
End Sub
Sub getJenisMobil()
Dim sr As StreamReader
Dim strJenisMobil As String
Dim dblHargaMobil As Double
If File.Exists("CarPrice.txt") Then
sr = File.OpenText("CarPrice.txt")
Do While sr.Peek <> -1
strJenisMobil = sr.ReadLine
dblHargaMobil = sr.ReadLine
cnt += 1
Loop
sr.Close()
ReDim arrJenisMobil(cnt - 1)
ReDim arrHargaMobil(cnt - 1)
sr = File.OpenText("CarPrice.txt")
cnt = 0
Do While sr.Peek <> -1
arrJenisMobil(cnt) = sr.ReadLine
arrHargaMobil(cnt) = sr.ReadLine
cnt += 1
Loop
End If
End Sub
Function getHargaMobil(ByVal carIndex As Integer) As Double
Return arrHargaMobil(carIndex)
End Function
Function ValidateInput() As Boolean
If txtNama.Text.Trim = "" Then
MessageBox.Show("Silahkan masukkan Nama")
txtNama.Focus()
Return False
ElseIf txtAlamat.Text.Trim = "" Then
MessageBox.Show("Silahkan masukkan Alamat")
txtAlamat.Focus()
Return False
ElseIf txtNoHP.Text.Trim = "" Then
MessageBox.Show("Silahkan masukkan No. HP")
txtNoHP.Focus()
Return False
ElseIf cboJenisMobil.SelectedIndex < 0 Then
MessageBox.Show("Silahkan pilih mobil yang disewa")
cboJenisMobil.Focus()
Return False
ElseIf txtHari.Text.Trim < 1 Then
MessageBox.Show("Minimal sewa 1 hari")
txtHari.Focus()
Return False
Else
Return True
End If
End Function
Private Sub btnHitung_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnHitung.Click
Dim fmtStr As String = "{0,-20}{1,10}"
Dim totalHarga As Double
If ValidateInput() = False Then Exit Sub
totalHarga = getHargaMobil(cboJenisMobil.SelectedIndex) * CInt(txtHari.Text)
lstHasil.Items.Clear()
With lstHasil.Items
.Add("Customer : Mr./Ms. " & txtNama.Text.Trim)
.Add("Alamat : " & txtAlamat.Text.Trim)
.Add("No NP : " & txtNoHP.Text.Trim)
.Add("Jenis Mobil yang disewa : " & cboJenisMobil.SelectedItem)
.Add("Jumlah Hari penyewaan : " & txtHari.Text & "hari Total Rp. " & totalHarga)
End With
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
getJenisMobil()
fillComboBox()
ClearInput()
End Sub
Private Sub txtHari_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtHari.KeyPress
If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then
e.Handled = True
End If
End Sub
Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
If lstHasil.Items.Count < 1 Then
Exit Sub
End If
Dim sw As StreamWriter
If File.Exists("History.txt") Then
File.Delete("History.txt")
End If
sw = File.CreateText("History.txt")
With sw
.WriteLine("Nama : " & txtNama.Text)
.WriteLine("Alamat : " & txtAlamat.Text)
.WriteLine("No HP : " & txtNoHP.Text)
.WriteLine("Jenis Mobil : " & cboJenisMobil.SelectedItem)
.WriteLine("Lama Sewa : " & txtHari.Text & " hari")
.WriteLine("Harga Sewa : " & arrHargaMobil(cboJenisMobil.SelectedIndex))
.WriteLine("Total : " & CDbl(arrHargaMobil(cboJenisMobil.SelectedIndex)) * CInt(txtHari.Text))
End With
sw.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sr As StreamReader
If File.Exists("History.txt") Then
sr = File.OpenText("History.txt")
Form2.ListBox1.Items.Clear()
While sr.Peek <> -1
Form2.ListBox1.Items.Add(sr.ReadLine)
End While
Me.Hide()
Form2.Show()
End If
End Sub
End Class
Berikut form untuk menampilkan hasil:
Source Code:
Public Class Form2
Private Sub btnTutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTutup.Click
Form1.Close()
End Sub
End Class
~ Semoga Bermanfaat ~
No comments:
Post a Comment