简单泊车管理系统-c++实现

泊车系统

该系统要求对一个文件中所存储的汽车数据进行各种常规操作,如:查找、计费、显示等功能。目的是熟练掌握文件、数组的各种操作,以及一些算法思想的应用,实现一个简单的泊车管理系统。

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
using namespace std;

struct Location       //车位信息
{
public:
	int num = -1;      //车位编号     
	char ID[10] ="0";     //车牌号码
	char carClass = -1;       //车辆类型
	int startTime = -1;     //入库时间
	int carState = -1;       //泊车状态(空或已泊车)
	int carInfo = -1;          //车位状态(正常使用,维修)
};

class Manager      //管理人员
{
public:
	//Manager();
	Location carport[100];     //共100个车位
	int carportLength = sizeof(carport) / sizeof(Location);  //车位数量
	int price = 50;       //每小时费用
	//初始化停车位信息
	void Init()
	{
		for (int i = 0;i < carportLength;i++)
		{
			carport[i].num = i + 1;        //编号
			//carport[i].ID
			carport[i].startTime = -1;   //时间默认-1
			carport[i].carState = 0;    //0表示空,1表示泊车
			carport[i].carInfo = 0;     //0表示正常使用,1表示正在维修
		}
	}
	//停车登记      停车位为第一个为空的位置,自动查询给出车牌号,车位满给出提示
	void Add()
	{
		cout << "停车登记" << endl;
		string idAdd;   //停车牌号
		char carClass;  //车辆类型
		int startTime = -1;
		int num = -1;
		//遍历车位  找到第一个空车位
		for (int i = 0;i < carportLength;i++)
		{
			if (carport[i].carState == 0 && carport[i].carInfo == 0)    //判断是否是空车位并且正常使用状态
			{
				num = i;
				break;
			}
			else
			{
				num = -1;
			}
		}
		if (num == -1)    //车位已满
		{
			cout << "车位已满" << endl;
			system("pause");
			system("cls");
		}
		else
		{
			cout << "请输入车牌号(9位),输入-1取消登记" << endl;
			cin >> idAdd;
			if (idAdd == "-1")
			{
				return;
			}
			strcpy(carport[num].ID, idAdd.c_str());    //string 导入char数组
			cout << "请输入车辆类型(a,b,c)" << endl;
			cin >> carClass;
			carport[num].carClass = carClass;
			//车辆入库时间
			carport[num].startTime = time(NULL);
			//标记为已经停车
			carport[num].carState = 1;              
		    cout << "车辆停泊在" << num + 1 << "号车位" << endl;
			system("pause");
			system("cls");
		}
	}
	//取车登记(按车牌/车位查找)  根据车辆停泊时间自动计算费用并显示
	void Reduce()
	{
		cout << "取车登记" << endl;
		int funReduce = -1; 
		string idReduce;      //取车车牌
		int numReduce;         //取车车位
		int timeReduce;          //取车时间
		char tempReduce[10];      //临时数组

		cout << "请输入: [1]:按车牌登记 [2]:按车位号登记 [3]:返回主菜单" << endl;
		cin >> funReduce;
		switch (funReduce)
		{
		case 1:
			cout << "请输入车牌号码:" << endl;
			cin >> idReduce;
			strcpy(tempReduce, idReduce.c_str());
			for (int i = 0;i < carportLength;i++)
			{
				if (carport[i].ID == tempReduce)  //找到指定车牌
				{
					timeReduce = time(NULL);
					//共计时间
					int lastTime = timeReduce - carport[i].startTime;   //计算停车时间
																		//计算费用
					int totalPrice = (lastTime / 60) / 60 * price;
					cout << "停车时间: " << lastTime << endl;
					cout << "停车费用: " << totalPrice << "\n" << "当前费率: " << price << endl;
					memset(carport[i].ID, 0, sizeof(carport[i].ID));         //数组置零
					carport[i].carState = 0;
					system("pause");
					system("cls");
				}
			}
			break;
		case 2:
			cout << "请输入车位号码:" << endl;
			cin >> numReduce;
			for (int i = 0;i < carportLength;i++)
			{
				if (carport[i].num == numReduce && carport[i].carState == 1)  //找到指定车位且车位有车
				{
					timeReduce = time(NULL);
					//共计时间
					int lastTime = timeReduce - carport[i].startTime;   //计算停车时间
																		//计算费用
					int totalPrice = (lastTime / 60) / 60 * price;
					cout << "停车时间: " << lastTime << endl;
					cout << "停车费用: " << totalPrice << "\n" << "当前费率: " << price << endl;
					memset(carport[i].ID, 0, sizeof(carport[i].ID));         //数组置零
					carport[i].carState = 0;       
					system("pause");
					system("cls");
				}
			}
			break;
		case 3:
			break;
		default:
			break;
		}
		
	}
    //查找 根据车牌、车位号
	void Search()
	{
		int fun = -1;
		int num = -1;
		string id;
		cout << "查询功能" << endl;		
		cout << "请输入:[1]:按车牌查找,[2]:按车位号查找,[3]:返回主菜单" << endl;
		cin >> fun;
		switch (fun)	
		{
		case 1:
			cout << "请输入车牌(9位数字)" << endl;
			cin >> id;
			char temp[11];
			strcpy(temp, id.c_str());
			for (int i = 0;i < carportLength;i++)
			{
				if (carport[i].ID == temp)     //找到车牌
				{
					cout << "已找到" << "\n" << "车位号: " << carport[i].num << "\t车类型: " << carport[i].carClass << endl;
					system("pause");
					system("cls");
					break;
				}
				else
				{
					cout << "没有找到指定车牌" << endl;
					//Search();
				}
			}
			break;
		case 2:
			cout << "请输入车位(1-100)" << endl;
			cin >> num;
			for (int i = 0;i < carportLength;i++)
			{
				if (carport[i].num == num)     //找到车位
				{
					cout << "已找到" << "\n" << "车位状态: " << carport[i].carState << "\t车位信息: " << carport[i].carInfo << endl;
					if (carport[i].carState == 1)     //车位有车
					{
						cout << "车牌号: " << carport[i].ID << "\t车类型: " << carport[i].carClass << endl;
						system("pause");
						system("cls");
						break;
					}
					
				}
				else
				{
					cout << "请输入正确车位" << endl;
					//Search();
				}
			}

			break;
		case 3:
			break;
		default:
			cout << "Error Input" << endl;
			Search();
			break;
		}
	}
    //修改车位信息(状态:正常使用,维修)
	void ChangeInfo()
	{
		int num;
		int info = -1;          //要修改的状态
		cout << "请输入指定车位号(1-100)" << endl;
		cin >> num;
		if (num>=1 && num<=100)
		{
			for (int i = 0;i < carportLength;i++)
			{
				if (carport[i].num == num)
				{
					cout << num << "号车位当前状态: " << carport[i].carInfo << endl;
					cout << "请输入要修改的状态: [0]:正常使用,[1]:正在维修" << endl;
					cin >> info;
					carport[i].carInfo = info;    //修改状态
					cout << "修改成功\t" <<num<<"号车位状态为"<<info<< endl;
					system("pause");
					system("cls");
				}
			}
		}
		else
		{
			cout << "输入错误" << endl;
			ChangeInfo();
		}
	}
	//改变每小时收费比率
	void ChangePrice()
	{
		int targetPrice = -1;
		cout << "当前费率: " << price << "\n"<<"请输入要修改的费率: "<<endl;
		cin >> targetPrice;
		if (targetPrice != -1)
		{
			price = targetPrice;
			cout << "修改成功!\n" << "当前费率: " << price << endl;
			system("pause");
			system("cls");
		}
	}
	void ShowState()
	{
		for (int i = 0;i < carportLength;i++)
		{
			cout << "车位号码:"<<carport[i].num<<"\t车牌:"<< carport[i].ID << "\t入库时间:" << carport[i].startTime << "\t泊车状态:" << carport[i].carState << "\t车位信息:" << carport[i].carInfo <<endl;
		}
		system("pause");
		system("cls");
	}
	          
};



class UserSystem
{
public:
	string id;
	string password;
	string idAdmin;
	string passwordAdmin;

	//登录
	void login()
	{
		id = "abc";
		password = "123";

		string idInput;
		string passwordInput;
		int count = 0;       //输入次数
		cout << "**********欢迎使用泊车管理系统**********" << endl;
		cout << "\t\t请登录系统\t\t" << endl;

		while (count == 0)
		{
			cout << "请输入用户名:" << endl;
			cin >> idInput;
			cout << "请输入密码:" << endl;
			cin >> passwordInput;
			if (idInput == id && passwordInput == password)    //用户名密码输入正确
			{
				cout << "\t\t登录成功\t\t" << endl;
				system("pause");
				system("cls");       //清屏
				int funUse;
				Manager manager;
				UserSystem user;
				cout << "**********欢迎进入泊车管理系统**********" << endl;
				manager.Init();
				while (1)
				{
					cout << "菜单\t\t" << endl;
					cout << "1.停车登记\t2.取车登记\t3.查询车辆信息\t4.修改车位状态\t5.改变费率\t6.显示所有车位信息" << endl;
					cin >> funUse;
					switch (funUse)
					{
					case 1:  //停车登记
						manager.Add();
						break;
					case 2:  //取车登记
						manager.Reduce();
						break;
					case 3:  //查询车辆信息
						manager.Search();
						break;
					case 4:  //修改车位状态
						manager.ChangeInfo();
						break;
					case 5:  //改变费率
						manager.ChangePrice();
						break;
					case 6:  //显示所有车位信息
						manager.ShowState();
						break;
					default:
						cout << "输入错误,请重新输入" << endl;
						system("pause");
						break;
					}
				}
			}
			else
			{
				system("cls");
				cout << "账号名或密码错误,请重新输入" << endl;
			}
		}
		
	}
};

int main()
{   
	Manager manager;
	UserSystem user;
	//manager.Init();
	user.login();

	return 0;
}


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值