using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _08停车场管理系统
{
class Program
{
//1.创建停车位数组
/// <summary>
/// 停车位置的数组,用来存储停车位置
/// </summary>
static string[,] parking = new string[10, 10];
//3.判断车位是否为空,记录空车位个数,true车位空,false车位不为空
/// <summary>
/// 定义一个bool类型的数组用来判断每个车位是否为空
/// </summary>
static bool[,] parkTag = new bool[10, 10];
static void Main(string[] args)
{
//2.创建一个方法给停车位数组填充内容,标号,调用次方法
InitianlParking();
//4.创建一个方法用来绘制系统的标题,并调用,绘制标题时输出空车位数
SetTitle();
//5.绘制车位,既输出数组,创建一个绘制车位的方法并调用
DrawParking();
Console.ReadLine();
//6.选择车辆的状态入场/出场
while (true)
{
Console.WriteLine("请选择车辆状态:A--入场 B--出厂");
string choose = Console.ReadLine();//用一个变量接收输入的车辆状态
while (true)
{
//7.判断输入的状态
//入场
if (choose=="A")
{
Console.WriteLine("请输入入场车牌");//输入入场车牌号
string plant = Console.ReadLine();//变量接收入场车牌号
//8.创建一个方法给入场车辆指定车位(存储位置)
string location = SetParkCar(plant);
Console.Clear();//清空屏幕
//9.重新绘制标题,改变车位信息,添加入场标语
SetTitle();
Console.WriteLine("------------------欢迎【{0}】入场,您的车位是【{1}】------------------", plant, location);
Console.WriteLine("------------------------------------------------------------------------------------------------------------", plant, location);
//10.绘制刷新新的车位
DrawParking();
Console.ReadLine();
break;
}
//出场
else if (choose=="B")
{
Console.WriteLine("请输入出场车牌");//输入出场车牌号
string plant = Console.ReadLine();//接收出场车牌号
Console.Clear();//清空屏幕
//11.创建一个方法设置车出厂,清理出车位,既车辆出场刷新车位信息
SetOutParking(plant);
//12.绘制标题添加出场标语
SetTitle();
Console.WriteLine("------------------欢迎下次光临,一路顺风!------------------");
//13.车辆出场刷新车位信息后绘制车位信息
DrawParking();
Console.ReadLine();
break;
}
}
}
}
/// <summary>
/// 此方法用来给停车位置数组填写内容,给停车位标号
/// </summary>
static void InitianlParking()
{
string row = "";
for (int i=0;i<parking.GetLength(0);i++)
{
for (int j=0;j<parking.GetLength(1);j++)
{
switch (i)
{
case 0:
row = "A";
break;
case 1:
row = "B";
break;
case 2:
row = "C";
break;
case 3:
row = "D";
break;
case 4:
row = "E";
break;
case 5:
row = "F";
break;
case 6:
row = "G";
break;
case 7:
row = "H";
break;
case 8:
row = "i";
break;
case 9:
row = "j";
break;
}
parking[i, j] = String.Format("车位{0}", row + (j + 1));
parkTag[i, j] = true;//初始时车位都为空
}
}
}
/// <summary>
/// 此方法用来绘制标题
/// </summary>
static void SetTitle()
{
//count变量用来记录空车位数
int count = CountNull();//绘制标题时记录空车位个数
Console.WriteLine("---------------------------------------------------------------------------");
Console.WriteLine("-------------------------------欢迎进入停车场------------------------------");
Console.WriteLine("-------------------------------当前剩余空车位:{0}--------------------------", count);
Console.WriteLine("---------------------------------------------------------------------------");
}
/// <summary>
/// 此方法用来记录返回空车位个数
/// </summary>
static int CountNull()
{
int count = 0;
for (int i = 0; i < parking.GetLength(0); i++)
{
for (int j = 0; j < parking.GetLength(1); j++)
{
if (parkTag[i, j])
{
count++;
}
}
}
return count;
}
/// <summary>
/// 此方法用来绘制车位输出数组
/// </summary>
static void DrawParking()
{
for (int i = 0; i < parking.GetLength(0); i++)
{
for (int j = 0; j < parking.GetLength(1); j++)
{
Console.Write(parking[i, j] + " \t");
}
Console.WriteLine();
}
}
/// <summary>
/// 此方法计算车位,给入场车辆指定车位
/// </summary>
static string SetParkCar(string plant)
{
string str = "";
//遍历车位
for (int i = 0; i < parking.GetLength(0); i++)
{
for (int j = 0; j < parking.GetLength(1); j++)
{
//从A1-J10车位一个接一个进行判断寻找空车位,找到则提示
if (parkTag[i, j] == true)
{
str = parking[i, j];
str = str.Substring(2);
parking[i, j] = plant;//将车牌给空车位
parkTag[i, j] = false;//改变车位状态
return str;//数组中返回车牌号表示次车位不在为空,停有车辆被占用
}
}
}
return "停车场已满!";//没找到空车位返回提示停车场已满
}
/// <summary>
/// 此方法设置车出厂,清理出车位
/// </summary>
static void SetOutParking(string plant)
{
string row = "";
//遍历所有车位
for (int i=0;i<parking.GetLength(0);i++)
{
for (int j=0;j<parking.GetLength(1);j++)
{
//找到要出场的车位
if (parking[i, j] == plant)
{
parkTag[i, j] = true;// 将车位状态改成空车位状态
switch (i)
{
case 0:
row = "A";
break;
case 1:
row = "B";
break;
case 2:
row = "C";
break;
case 3:
row = "D";
break;
case 4:
row = "E";
break;
case 5:
row = "F";
break;
case 6:
row = "G";
break;
case 7:
row = "H";
break;
case 8:
row = "I";
break;
case 9:
row = "J";
break;
default:
break;
}
parking[i, j] = String.Format("车位{0}", row + (j + 1));
}
}
}
}
}
}
浙公网安备 33010602011771号