用C#创建疫苗表(编号、名称、注射量、保质期、价格); 添加5条测试数据 实现增删改查
时间: 2024-01-31 14:04:53 浏览: 65
以下是用C#创建疫苗表并实现增删改查的示例代码:
```csharp
using System;
using System.Collections.Generic;
namespace VaccineManagement
{
class Vaccine
{
public int ID { get; set; }
public string Name { get; set; }
public double Dosage { get; set; }
public int ShelfLife { get; set; }
public decimal Price { get; set; }
}
class Program
{
static List<Vaccine> vaccines = new List<Vaccine>();
static void Main(string[] args)
{
// 添加5条测试数据
vaccines.Add(new Vaccine { ID = 1, Name = "狂犬病疫苗", Dosage = 0.5, ShelfLife = 12, Price = 100 });
vaccines.Add(new Vaccine { ID = 2, Name = "水痘疫苗", Dosage = 0.5, ShelfLife = 24, Price = 200 });
vaccines.Add(new Vaccine { ID = 3, Name = "麻疹疫苗", Dosage = 0.5, ShelfLife = 36, Price = 300 });
vaccines.Add(new Vaccine { ID = 4, Name = "百白破疫苗", Dosage = 0.5, ShelfLife = 48, Price = 400 });
vaccines.Add(new Vaccine { ID = 5, Name = "流感疫苗", Dosage = 0.5, ShelfLife = 6, Price = 50 });
while (true)
{
Console.WriteLine("请选择操作:");
Console.WriteLine("1. 查看疫苗列表");
Console.WriteLine("2. 添加疫苗");
Console.WriteLine("3. 删除疫苗");
Console.WriteLine("4. 修改疫苗信息");
Console.WriteLine("5. 退出");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
ShowVaccines();
break;
case "2":
AddVaccine();
break;
case "3":
RemoveVaccine();
break;
case "4":
ModifyVaccine();
break;
case "5":
return;
default:
Console.WriteLine("无效的选择,请重新输入。");
break;
}
}
}
static void ShowVaccines()
{
Console.WriteLine("编号\t名称\t注射量\t保质期\t价格");
foreach (var vaccine in vaccines)
{
Console.WriteLine($"{vaccine.ID}\t{vaccine.Name}\t{vaccine.Dosage}\t{vaccine.ShelfLife}\t{vaccine.Price}");
}
}
static void AddVaccine()
{
Console.WriteLine("请输入疫苗信息:");
Console.Write("编号:");
int id = int.Parse(Console.ReadLine());
Console.Write("名称:");
string name = Console.ReadLine();
Console.Write("注射量:");
double dosage = double.Parse(Console.ReadLine());
Console.Write("保质期:");
int shelfLife = int.Parse(Console.ReadLine());
Console.Write("价格:");
decimal price = decimal.Parse(Console.ReadLine());
vaccines.Add(new Vaccine { ID = id, Name = name, Dosage = dosage, ShelfLife = shelfLife, Price = price });
Console.WriteLine("添加成功!");
}
static void RemoveVaccine()
{
Console.Write("请输入要删除的疫苗编号:");
int id = int.Parse(Console.ReadLine());
Vaccine vaccine = vaccines.Find(v => v.ID == id);
if (vaccine != null)
{
vaccines.Remove(vaccine);
Console.WriteLine("删除成功!");
}
else
{
Console.WriteLine("未找到该疫苗!");
}
}
static void ModifyVaccine()
{
Console.Write("请输入要修改的疫苗编号:");
int id = int.Parse(Console.ReadLine());
Vaccine vaccine = vaccines.Find(v => v.ID == id);
if (vaccine != null)
{
Console.WriteLine($"当前疫苗信息:{vaccine.ID}\t{vaccine.Name}\t{vaccine.Dosage}\t{vaccine.ShelfLife}\t{vaccine.Price}");
Console.WriteLine("请输入修改后的疫苗信息:");
Console.Write("名称:");
vaccine.Name = Console.ReadLine();
Console.Write("注射量:");
vaccine.Dosage = double.Parse(Console.ReadLine());
Console.Write("保质期:");
vaccine.ShelfLife = int.Parse(Console.ReadLine());
Console.Write("价格:");
vaccine.Price = decimal.Parse(Console.ReadLine());
Console.WriteLine("修改成功!");
}
else
{
Console.WriteLine("未找到该疫苗!");
}
}
}
}
```
运行程序后,可按照提示进行操作。例如,选择“1. 查看疫苗列表”时,输出如下:
```
编号 名称 注射量 保质期 价格
1 狂犬病疫苗 0.5 12 100
2 水痘疫苗 0.5 24 200
3 麻疹疫苗 0.5 36 300
4 百白破疫苗 0.5 48 400
5 流感疫苗 0.5 6 50
```
阅读全文