簡單泊車管理系統-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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章