Function Calculate_A1001(ByVal value As Decimal) As Decimal
Dim price As Decimal = value
Dim dis As Decimal = 0
If price <= 500 Then
dis = 0
ElseIf price <= 1000 Then
dis = 300
Else
dis = 500
End If
Return dis
End Function
Function Calculate_B1001(ByVal value As Decimal) As Decimal
Dim price As Decimal = value
Dim dis As Decimal = 0
If price <= 1000 Then
dis = 500
ElseIf price <= 5000 Then
dis = 1000
Else
dis = 1500
End If
Return dis
End Function
Function Calculate_C1001(ByVal value As Decimal) As Decimal
Dim price As Decimal = value
Dim dis As Decimal = 0
If price <= 1500 Then
dis = 500
ElseIf price <= 3000 Then
dis = 1000
ElseIf price <= 6000 Then
dis = 1500
Else
dis = 3000
End If
Return dis
End Function
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim price As Decimal
Dim dis As Decimal
Dim total As Decimal
price = TextBox1.Text
dis = 0
' เงืื่อนไข...
'Select Case ประโยคกำหนดเงื่อนไข
' Case ลำดับที่ n
' .
' .
' Case Else
' .
' .
'End Select
Select Case DropDownList1.SelectedValue
Case "A1001"
dis = Calculate_A1001(price)
Case "B1001"
dis = Calculate_B1001(price)
Case "C1001"
dis = Calculate_C1001(price)
End Select
' / / / / / / / / เเสดงผล / / / / / / / / /
total = price - dis
TextBox2.Text = ("" & price)
TextBox3.Text = (" " & dis)
TextBox4.Text = (" " & total)
End Sub
End Class
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim product_code As String
Dim price As Decimal
Dim dis As Decimal
Dim total As Decimal
If IsNumeric(TextBox1.Text) Then
price = TextBox1.Text
product_code = DropDownList1.SelectedValue
dis = Calculate(product_code, price)
total = price - dis
TextBox2.Text = ("" & price)
TextBox3.Text = (" " & dis)
TextBox4.Text = (" " & total)
Else
End If
End Sub
Function Calculate(ByVal product_code As String, ByVal price As Decimal) As Decimal
Dim dis As Decimal = 0
For Each dr As DataRow In dt.Rows
If (dr.Item("Product") = product_code) Then
If price >= dr.Item("min_p") And price <= dr.Item("max_p") Then
dis = dr.Item("dis")
End If
End If
Next
Return dis
End Function
ขอตั้งชื่อว่า โปรเเกรมคำนวณเงินส่วนลดโปรโมชั่น ของสินค้า A B C
การทำงาน
ใส่ราคาสินค้า เเละ จำนวนสินค้า
เมื่อกดคำนวณ จะเเสดง
- ราคาสินค้าทั้งหมด( ราคาสินค้า * จำนวนสินค้า)
- จำนวนเงินส่วนลด
- จำนวนราคาที่ต้องชำระ เราขอใช้เป็น == [( ราคาสินค้าทั้งหมด * จำนวนสินค้า) - จำนวนเงินส่วนลด ]
หน้าดีไซน์
CODE VB#
Code (VB.NET)
Imports System.Data
Public Class WebForm1
Inherits System.Web.UI.Page
Dim dt As New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dt.Columns.Add("promotion_id", GetType(Integer))
dt.Columns.Add("Product", GetType(String))
dt.Columns.Add("min_p", GetType(Decimal))
dt.Columns.Add("max_p", GetType(Decimal))
dt.Columns.Add("dis", GetType(Decimal))
' Start A1001
Dim drA1 As DataRow = dt.NewRow
drA1.Item("promotion_id") = 1
drA1.Item("Product") = "A1001"
drA1.Item("min_p") = 0
drA1.Item("max_p") = 499.99
drA1.Item("dis") = 0
dt.Rows.Add(drA1)
Dim drA2 As DataRow = dt.NewRow
drA2.Item("promotion_id") = 2
drA2.Item("Product") = "A1001"
drA2.Item("min_p") = 500
drA2.Item("max_p") = 999.99
drA2.Item("dis") = 300
dt.Rows.Add(drA2)
Dim drA3 As DataRow = dt.NewRow
drA3.Item("promotion_id") = 3
drA3.Item("Product") = "A1001"
drA3.Item("min_p") = 1000
drA3.Item("max_p") = DBNull.Value
drA3.Item("dis") = 500
dt.Rows.Add(drA3)
' End A1001
' Start B1001
Dim drB1 As DataRow = dt.NewRow
drB1.Item("promotion_id") = 1
drB1.Item("Product") = "B1001"
drB1.Item("min_p") = 0
drB1.Item("max_p") = 999.99
drB1.Item("dis") = 500
dt.Rows.Add(drB1)
Dim drB2 As DataRow = dt.NewRow
drB2.Item("promotion_id") = 2
drB2.Item("Product") = "B1001"
drB2.Item("min_p") = 1000
drB2.Item("max_p") = 4999.99
drB2.Item("dis") = 1000
dt.Rows.Add(drB2)
Dim drB3 As DataRow = dt.NewRow
drB3.Item("promotion_id") = 3
drB3.Item("Product") = "B1001"
drB3.Item("min_p") = 5000
drB3.Item("max_p") = DBNull.Value
drB3.Item("dis") = 1500
dt.Rows.Add(drB3)
' End B1001
' Start C1001
Dim drC1 As DataRow = dt.NewRow
drC1.Item("promotion_id") = 1
drC1.Item("Product") = "C1001"
drC1.Item("min_p") = 0
drC1.Item("max_p") = 1499.99
drC1.Item("dis") = 500
dt.Rows.Add(drC1)
Dim drC2 As DataRow = dt.NewRow
drC2.Item("promotion_id") = 2
drC2.Item("Product") = "C1001"
drC2.Item("min_p") = 1500
drC2.Item("max_p") = 2999.99
drC2.Item("dis") = 1000
dt.Rows.Add(drC2)
Dim drC3 As DataRow = dt.NewRow
drC3.Item("promotion_id") = 3
drC3.Item("Product") = "C1001"
drC3.Item("min_p") = 3000
drC3.Item("max_p") = 5999.99
drC3.Item("dis") = 1500
dt.Rows.Add(drC3)
Dim drC4 As DataRow = dt.NewRow
drC4.Item("promotion_id") = 4
drC4.Item("Product") = "C1001"
drC4.Item("min_p") = 6000
drC4.Item("max_p") = DBNull.Value
drC4.Item("dis") = 3000
dt.Rows.Add(drC4)
' End C1001
'------------------
GridView1.DataSource = dt
GridView1.DataBind()
' -------------------
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim product_code As String
Dim price As Decimal
Dim dis As Decimal
Dim total As Decimal
Dim num As Decimal
Dim sum As Decimal
If IsNumeric(TextBox1.Text) Then
price = TextBox1.Text
price = (Math.Truncate(price * 100)) / 100
product_code = DropDownList1.SelectedValue
dis = Calculate(product_code, price)
num = TextBox5.Text
sum = price * num
total = price - dis
TextBox2.Text = ("" & price * num)
TextBox3.Text = (" " & dis)
TextBox4.Text = (" " & sum - dis)
Else
End If
End Sub
Function Calculate(ByVal product_code As String, ByVal price As Decimal) As Decimal
Dim dis As Decimal = 0
For Each dr As DataRow In dt.Rows
If (dr.Item("Product") = product_code) Then
If Not dr.Item("max_p").GetType = GetType(Decimal) Then
If price >= dr.Item("min_p") Then
dis = dr.Item("dis")
End If
Else
If price >= dr.Item("min_p") And price <= dr.Item("max_p") Then
dis = dr.Item("dis")
End If
End If
End If
Next
Return dis
End Function
Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
End Class