Public Class Form1
' 定义食品类
Class Food
Public Name As String
Public Price As Double
Public Stock As Integer
End Class
' 食品列表
Private foods As New List(Of Food)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 初始化一些食品数据
foods.Add(New Food With {.Name = "面包",.Price = 5.0,.Stock = 50})
foods.Add(New Food With {.Name = "牛奶",.Price = 10.0,.Stock = 30})
foods.Add(New Food With {.Name = "鸡蛋",.Price = 8.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 name As String = InputBox("请输入食品名称")
Dim price As Double = CDbl(InputBox("请输入食品价格"))
Dim stock As Integer = CInt(InputBox("请输入食品库存数量"))
Dim newFood As New Food
newFood.Name = name
newFood.Price = price
newFood.Stock = stock
foods.Add(newFood)
MessageBox.Show("食品信息输入成功!")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' 查看食品列表的逻辑
Dim listText As String = "食品列表:" & vbCrLf
For Each food In foods
listText &= $"名称:{food.Name},价格:{food.Price},库存:{food.Stock}" & vbCrLf
Next
MessageBox.Show(listText)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
' 销售食品的逻辑
Dim sellName As String = InputBox("请输入要销售的食品名称")
Dim foundFood As Food = Nothing
For Each food In foods
If food.Name = sellName Then
foundFood = food
Exit For
End If
Next
If foundFood IsNot Nothing Then
Dim sellQuantity As Integer = CInt(InputBox("请输入销售数量"))
If sellQuantity <= foundFood.Stock Then
foundFood.Stock -= sellQuantity
MessageBox.Show($"销售成功,剩余库存:{foundFood.Stock}")
Else
MessageBox.Show("库存不足,无法销售。")
End If
Else
MessageBox.Show("未找到该食品。")
End If
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