停车场管理系统
来源:D:\study\C#1\stu0725\8停车场管理系统
📁 项目结构
8停车场管理系统/
├── Program.cs # 程序入口
├── Car.cs # 车辆基类
├── StopCar.cs # 停车场管理核心类(进场/离场/查询/费率)
└── Properties/
📄 Car.cs — 车辆基类
class Car
{
// readonly 字段:车牌号在构造时确定,之后不可修改
public readonly string CarId;
public DateTime InTime { get; set; }
public DateTime OutTime { get; set; }
public Car(string id)
{
CarId = id;
InTime = DateTime.Now; // 进场时间自动记录
}
}
// Tcar(停车场专用车型):继承 Car,增加车型属性
class Tcar : Car
{
public string Type { get; set; }
// 完整构造函数 — 指定车型
public Tcar(string id, string type) : base(id)
{
Type = type;
}
// 快捷构造函数 — 默认车型为"小车"
public Tcar(string id) : this(id, "小车")
{
}
}
继承关系
Car(基类)
└─ readonly CarId — 车牌号
└─ InTime / OutTime — 进出场时间
Tcar : Car(子类)
└─ Type — 车型(小车/大车/新能源)
📄 StopCar.cs — 停车场管理系统核心
整体架构
| 成员 |
类型 |
用途 |
cars |
List<Tcar> |
存储当前停车场内的所有车辆 |
Count |
int(默认 50) |
剩余车位数量 |
Tax |
decimal[] |
三种车型的每小时费率 |
FindC |
Func<Car, string, bool> |
Lambda 表达式:根据车牌查找车辆 |
核心方法
| 方法 |
功能 |
InS() |
车辆进场 — 输入车牌、选择车型、检查车位 |
OutS() |
车辆离场 — 输入车牌、计算费用、移除车辆 |
ShowS() |
查看当前停车场所有车辆 |
ShowRateInfo() |
显示收费标准 |
Menu() |
主菜单循环 |
完整代码
internal class StopCar
{
List<Tcar> cars = new List<Tcar>();
string Carid { get; set; }
Func<Car, string, bool> FindC = (car, cartest) => car.CarId == cartest;
TimeSpan Timespan { get; set; }
public int Count { get; set; } = 50;
decimal[] Tax { get; } = new decimal[3] { 5.0m, 10.0m, 3.0m };
// ── 进场 ──
public void InS()
{
Console.WriteLine("输入进场车牌号:");
Carid = Console.ReadLine();
Console.WriteLine("选择车型:\n 1.小车 2.大车 3.新能源");
int choose = int.Parse(Console.ReadLine());
string cartype = choose switch
{
1 => "小车",
2 => "大车",
3 => "新能源",
_ => null
};
if (Count > 0)
{
Tcar car = new Tcar(Carid, cartype);
if (cars.Exists(carpai => carpai.CarId == car.CarId))
{
Console.WriteLine("车辆已存在");
return;
}
cars.Add(car);
Console.WriteLine($"欢迎{car.CarId}停车,您入场时间是{car.InTime:yyy-MM-dd HH:mm:ss}");
Count--;
}
else
{
Console.WriteLine("车位已满!不能进入");
}
}
// ── 离场 ──
public void OutS()
{
Console.WriteLine("输入离场车牌号:");
Carid = Console.ReadLine();
Tcar carfi = cars.Find(car => car.CarId == Carid);
if (carfi == null)
{
Console.WriteLine("输入错误");
return;
}
carfi.OutTime = DateTime.Now;
Timespan = carfi.OutTime - carfi.InTime;
Console.WriteLine($"===收费详情===\n车牌号:{carfi.CarId}\n车类型:{carfi.Type}\n入场时间{carfi.InTime}");
decimal fee = carfi.Type switch
{
"小车" => Timespan.Seconds * Tax[0],
"大车" => Timespan.Seconds * Tax[1],
"新能源" => Timespan.Seconds * Tax[2],
_ => 0
};
Console.WriteLine($"收取费用:{fee}");
cars.Remove(carfi);
Count++;
}
// ── 查看停车场详情 ──
public void ShowS()
{
Console.WriteLine("停车场停车详情:");
foreach (var car in cars)
Console.WriteLine($"车牌号:{car.CarId},车类型:{car.Type},入场时间{car.InTime}");
Console.WriteLine($"车位占用情况:{50 - Count}/50");
}
// ── 费率表 ──
public void ShowRateInfo()
{
Console.WriteLine($"\n=== 收费标准 ===");
Console.WriteLine($"┌──────────┬────────────┬────────────┐");
Console.WriteLine($"│ 车型 │ 每小时费率 │每日最高收费 │");
Console.WriteLine($"├──────────┼────────────┼────────────┤");
Console.WriteLine($"│ 小型车 │ 5.0元 │ 50.0元 │");
Console.WriteLine($"│ 大型车 │ 10.0元 │ 100.0元 │");
Console.WriteLine($"│ 新能源车 │ 3.0元 │ 30.0元 │");
Console.WriteLine($"└──────────┴────────────┴────────────┘");
Console.WriteLine($"不足1小时按1小时计算\n");
}
// ── 主菜单 ──
public void Menu()
{
Console.WriteLine("========停车场管理系统========");
bool flag = true;
while (flag)
{
Console.WriteLine("====主菜单====");
Console.WriteLine("====1.进场====\n====2.离场====\n====3.查看====\n====4.费率====\n====0.退出====");
Console.WriteLine("请选择[0-4]:");
int choose = int.Parse(Console.ReadLine());
flag = choose switch
{
0 => { Console.WriteLine("已退出停车场管理系统"); return false; },
1 => { InS(); return true; },
2 => { OutS(); return true; },
3 => { ShowS(); return true; },
4 => { ShowRateInfo(); return true; },
_ => true
};
}
}
}
📄 Program.cs — 程序入口
internal class Program
{
static void Main(string[] args)
{
StopCar car = new StopCar();
car.Menu();
}
}
核心知识点
| 知识点 |
说明 |
readonly |
Car.CarId — 车牌号在构造时确定,终身不可改 |
| 继承 |
Tcar : Car — 继承车辆基本信息,扩展车型 |
| 构造函数链式调用 |
Tcar(string id) → this(id, "小车") → base(id) |
switch 表达式 |
车型选择、费率计算使用 C# 8.0+ switch 表达式 |
switch 语句 |
菜单循环使用传统 switch-case |
| Lambda 表达式 |
Func<Car, string, bool> FindC — 自定义查找条件 |
List<T>.Exists() |
检查车辆是否已在场内 |
List<T>.Find() |
根据车牌号查找车辆 |
DateTime.Now |
自动记录进场/离场时间 |
TimeSpan |
计算停车时长 |
| 字符串插值 + 格式化 |
$"{car.InTime:yyy-MM-dd HH:mm:ss}" 自定义时间格式 |
| ASCII 表格输出 |
费率表使用 ┌─┬─┐ 字符绘制 |
可改进方向
相关笔记