Public Class Form1
' 定义商品类
Class Commodity
Public Name As String
Public Price As Double
Public Stock As Integer
End Class
' 商品列表
Private commodities As New List(Of Commodity)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 初始化一些商品数据
commodities.Add(New Commodity With {.Name = "苹果",.Price = 5.0,.Stock = 50})
commodities.Add(New Commodity With {.Name = "香蕉",.Price = 3.0,.Stock = 30})
commodities.Add(New Commodity With {.Name = "橙子",.Price = 4.0,.Stock = 40})
' 初始化界面元素
Button1.Text = "输入商品销售信息"
Button2.Text = "查看销售记录"
Button3.Text = "盘点库存"
Button4.Text = "继续"
Button5.Text = "退出"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' 输入商品销售信息的逻辑
Dim sellName As String = InputBox("请输入销售的商品名称")
Dim foundCommodity As Commodity = Nothing
For Each commodity In commodities
If commodity.Name = sellName Then
foundCommodity = commodity
Exit For
End If
Next
If foundCommodity IsNot Nothing Then
Dim sellQuantity As Integer = CInt(InputBox("请输入销售数量"))
If sellQuantity <= foundCommodity.Stock Then
foundCommodity.Stock -= sellQuantity
MessageBox.Show($"销售成功,剩余库存:{foundCommodity.Stock}")
Else
MessageBox.Show("库存不足,无法销售。")
End If
Else
MessageBox.Show("未找到该商品。")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' 查看销售记录的逻辑
' 这里假设没有实际的销售记录存储,仅显示模拟信息
MessageBox.Show("销售记录:暂无数据")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
' 盘点库存的逻辑
Dim listText As String = "库存列表:" & vbCrLf
For Each commodity In commodities
listText &= $"名称:{commodity.Name},价格:{commodity.Price},库存:{commodity.Stock}" & vbCrLf
Next
MessageBox.Show(listText)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
' 继续(返回主菜单)的逻辑
MessageBox.Show("返回主菜单")
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
' 退出的逻辑
Me.Close()
End Sub
End Class