Berikut pembahasan dari soal UTS Lab Algoritma dan Pemrograman, jadikan code berikut hanya sebagai refensi, silahkan dikembangkan jika perlu. Tidak mutlak code harus sama persis seperti di bawah ini.
Source Code:
Public Class Form1
Sub ClearInput()
txtNama.Text = ""
txtAlamat.Text = ""
txtNoHP.Text = ""
cboJenisMobil.Text = "(Silahkan Pilih)"
txtHari.Text = 0
txtNama.Focus()
End Sub
Function getHargaMobil(ByVal JenisMobil As String) As Double
Dim hrgMobil As Double
Select Case JenisMobil
Case Is = "Toyota Avanza"
hrgMobil = 250000
Case Is = "Toyota Innova"
hrgMobil = 350000
Case Is = "Toyota Corolla"
hrgMobil = 200000
Case Is = "Honda Jazz"
hrgMobil = 300000
Case Is = "Honda Accord"
hrgMobil = 250000
Case Is = "Nissan Sunny"
hrgMobil = 200000
Case Else
hrgMobil = 180000
End Select
Return hrgMobil
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.SelectedItem) * 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
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
End Class
~ Semoga Bermanfaat ~
No comments:
Post a Comment