Parking lot management system (database)

Posted by cschotch on Thu, 03 Mar 2022 16:36:31 +0100

1, Preface

The parking lot management system written by sqlite3
Its main functions are
1. Check the garage vacancy 2 Car owners choose parking spaces
3. System billing 4 View parking records of specified vehicles
5. Check the parking records of all vehicles 6 sign out

2, Explain time_t() function (because it is used)

Let's explain here
Where is the function time to get the time_t( )
Its header file is #include < time h>
Function: get system time

Method 1:
Output by string. The format has been specified. You can't change the format yourself
Format: Thu Feb 3 21:16:17 2022

#include <time.h>

int main()
{
    time_t timep;

    time(&timep); //Get the number of seconds since 1970 and save it in time_ timep of type T
    printf("%s", ctime(&timep));//Use ctime to convert the seconds into string format and output: Thu Feb 3 21:16:17 2022
    
    return 0;
}

Method 2:
With stuct tm structure, you can change the format yourself
Output: 2022 / 3 / 03 21:12:31

#include <time.h>

int main()
{
    time_t timep;
    struct tm *p;

    time(&timep); //Get the number of seconds since 1970 and save it in time_ timep of type T
    p = localtime(&timep);//Convert seconds into struct tm structure with localtime

    printf("%d/%d/%d %02d:%02d:%02d\n", 1900 + p->tm_year, 1+ p->tm_mon, p->tm_mday,p->tm_hour, p->tm_min, p->tm_sec);//2022/3/03 21:12:31

    return 0;
}

3. General idea

Header file #include < time. Required h>
(about local time, I gave a brief introduction when I used it in the third part. Now I'm afraid I'll forget hhhh when I use it later)
Here, in the simplest words, I will briefly introduce the time I understand_ T type and struct tm structure.

The utc time seen in the computer is calculated from (0:00:00, January 1, 1970).
We can use the function time() to get how many seconds have passed since 1970 and define time_t this type (long in essence) stores how many seconds have passed since 1970.
Now we have the number of seconds since 1970, but we can't directly see the current month, year and day through the number of seconds, so we need to use a function to convert. (for example, it's 1551193610 seconds from 1970. You don't know today's date. You have to convert it into the form of "Tue Feb 26 20:32:53 2019" through function)
The functions used for transformation here are called time functions for the time being. They have many functions.
There are two types of output parameters,
The first is the direct output character pointer type. The advantage of this function is that it can directly print the string (such as Tue Feb 26 20:32:53 2022), but its disadvantage is that it cannot be output in the desired format.
The second function is to output the struct tm structure type pointer. This structure defines the year, day, hour, minute and second respectively. We can use it to output to the format we want (e.g. 2022 / 3 / 03 21:01:38).

4. Specific use and analysis

1. Define time_ A variable of type t used to store the number of seconds since 1970
Essentially time_t is a long integer. It doesn't make any difference when I replace it with long

time_t timep;

2. Use the function time() to get the number of seconds since 1970

 time(&timep);

Function prototype: time_t time(time_t *t);

 time(&timep);
 printf("%d",timep);

Output: 1551193610

If you still don't understand time_t function, I won't talk about it in detail

3, Parking lot management system code (operable)

If you can leave a message where you don't understand, I will answer it at the first time

1. Main function

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sqlite3.h>
#include<time.h>
#include"paking_admin.h"

#include <stddef.h>

int main(){
	sqlite3 * pdb;
	int ret;
	int num;

	ret = sqlite3_open("database.db",&pdb);//Open or create a database
	if(ret == SQLITE_OK){
		printf("open database success!\n");
	}
	else{
		printf("open database fail\n");
		exit(-1);
	}

	create_table(pdb);//Create garage table
	create_all_table(pdb);//Create parking records for all previous vehicles

//	init_table(pdb);

//	welcome();

	while(1){
		welcome();
		printf("Please enter your choice:\n");
		scanf("%d",&num);

		switch(num){
			case 1:
				select_all(pdb);//View garage vacancy and parking information
				break;

			case 2:
				insert_values(pdb);//The owner chooses to park according to the parking space
				break;

			case 3:
				count_fee(pdb);//Ex warehouse and calculate the price
				break;

			case 4:
				select_paking(pdb);//Query the parking record of the vehicle according to the license plate
				break;
				
			case 5:
				select_allpaking(pdb);//View the parking records of all previous cars in this garage
				break;
			case 6:
				sqlite3_close(pdb);//sign out
				printf("Exited\n");
				exit(-1);
		}
	}
	return 0;
}

2. Function

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sqlite3.h>
#include<time.h>
#include"paking_admin.h"

#include <stddef.h>

#define MAX 10

void welcome(){
	printf("*************************************************************************\n");
	printf("****************                                        *****************\n");
	printf("****************               Parking charge                 *****************\n");
	printf("****************       ((RMB 1000 will be fined if the railing is damaged)          *****************\n");
	printf("*************************************************************************\n");
	printf("\n");
	printf("****************      what can I do for you ,sir        *****************\n");
	printf("\n");
	printf("******         1 View all parking spaces         2. Car owners choose parking spaces           ******\n");
	printf("******         3 The car leaves the warehouse and calculates the cost. 4. View the vehicle parking record           ******\n");
	printf("******         5 View all vehicle parking records 6 exit                       ******\n");
	printf("******                Charging standard 5¥/s((no counter-offer)                    ******\n");
	printf("*************************************************************************\n");
}


int create_table(sqlite3 *pdb){//Create this table and turn it into a garage 
	char *sql = NULL;
	char *errmsg = NULL;
	int ret;

	sql = "create table if not exists mytable (id integer primary key,name text,entertm integer,exittm integer,stall text,entertime text,exittime text);";
	ret = sqlite3_exec(pdb,sql,NULL,NULL,&errmsg);
	if(ret == SQLITE_OK){
		printf("create mytable success!\n");
		return SQLITE_OK;
	}
	else{
		printf("create mytable fail! %s\n",errmsg);
		exit(-1);
	}
}
int create_all_table(sqlite3 *pdb){//Create this table to store the parking records of previous parking in this garage
	char *sql = NULL;
	char *errmsg = NULL;
	int ret;

	sql = "create table if not exists alltable (id integer primary key,name text,entertime text,exittime text);";
	ret = sqlite3_exec(pdb,sql,NULL,NULL,&errmsg);
	if(ret == SQLITE_OK){
		printf("create alltable success!\n");
		return SQLITE_OK;
	}
	else{
		printf("create alltable fail! %s\n",errmsg);
		return -1;
	}
}
void init_table(sqlite3 *pdb){//Initialize garage

	int i;

	for(i = 0;i < MAX;i++){
		char *sql = NULL;
		sql = "insert into mytable (id,name,entertm,exittm,stall,entertime,exittime) values (NULL,'no',NULL,NULL,'no','no','no');";
		char *errmsg = NULL;
		int ret;
		ret = sqlite3_exec(pdb,sql,NULL,NULL,&errmsg);
		
		if(ret != SQLITE_OK){
			printf("init table fail! %s",errmsg);
			exit(-1);
		}
	}
	
}

void select_all(sqlite3 *pdb){//Find the current parking information in the garage
	char * sql = NULL;
	char * errmsg = NULL;
	char ** ret_val = NULL;
	int nrow;
	int ncol;
	int ret;
	int i;

	sql = "select * from mytable;";

	ret = sqlite3_get_table(pdb,sql,&ret_val,&nrow,&ncol,&errmsg);
	if(ret != SQLITE_OK){
		printf("select all fail! %s",errmsg);
		sqlite3_free_table(ret_val);
		return;
	}
	else{
		for(i = 0;i < (nrow + 1) * ncol;i++){
			printf("%-20s",ret_val[i]);
			if((i + 1) % ncol == 0){
				printf("\n");
			}
		}
	}
	sqlite3_free_table(ret_val);
}

void insert_values(sqlite3 * pdb){//The owner chooses which garage to park according to the id
	char sql[200];
	char *errmsg = NULL;
	int ret;
	char name[20];
	int id;
	time_t t;

	printf("Please enter the license plate number\n");
	scanf("%s",name);

	printf("Please select a parking space id:\n");
	scanf("%d",&id);

	t = time(NULL);
	struct tm *tb;
	tb = localtime(&t);

	sprintf(sql,"update mytable set name = '%s',entertm = %ld,stall = 'paking',entertime = '%d/%d/%d %d:%d:%d' where id = %d;",name,t,tb->tm_year+1900,tb->tm_mon+1,tb->tm_mday,tb->tm_hour,tb->tm_min,tb->tm_sec,id);

	ret = sqlite3_exec(pdb,sql,NULL,NULL,&errmsg);

	if(ret == SQLITE_OK){
		printf("Successful parking\n");
		return;
	}
	else{
		printf("Parking failure! %s\n",errmsg);
		return;
	}
}

void count_fee(sqlite3 *pdb){ //First assign the current time to exittm, then take the car out of the warehouse, calculate the parking time through entertm and exittm, and record the parking in the alltable table
	char sql1[200];
	char sql2[200];
	char *errmsg = NULL;
	int ret1;
	int ret2;
	char name[20];
	time_t t;
	char ** ret_val;
	int nrow;
	int ncol;
	int i;
	int a,b;
	char sql3[200];
	int ret3;
	char sql4[200];

	t = time(NULL);
	struct tm * tb;
	tb = localtime(&t);

	printf("License plate number of outgoing vehicle:\n");
	scanf("%s",name);

	sprintf(sql1,"update mytable set exittm = %ld,exittime = '%d/%d/%d %d:%d:%d' where name = '%s';",t,tb->tm_year + 1900,tb->tm_mon+1,tb->tm_mday,tb->tm_hour,tb->tm_min,tb->tm_sec,name);

	ret1 = sqlite3_exec(pdb,sql1,NULL,NULL,&errmsg);

	if(ret1 == SQLITE_OK){
		printf("exit time succcess!\n");
	}
	else{
		printf("exit time fail! %s",errmsg);
		exit(-1);
	}

	sprintf(sql2,"select entertm,exittm from mytable where name = '%s';",name);
	ret2 = sqlite3_get_table(pdb,sql2,&ret_val,&nrow,&ncol,&errmsg);
	
	a = atoi(ret_val[2]);
	b = atoi(ret_val[3]);

	printf("The cost is:%d element\n",(b-a)*5);

	sprintf(sql4,"insert into alltable(id,name,entertime,exittime) select id,name,entertime,exittime from mytable where name like '%s';",name);
	sqlite3_exec(pdb,sql4,NULL,NULL,&errmsg);

	sprintf(sql3,"update mytable set stall = 'no',name = 'no',entertm = NULL,exittm = NULL,entertime='no',exittime = 'no' where name = '%s';",name);
	ret3 = sqlite3_exec(pdb,sql3,NULL,NULL,&errmsg);
	if(ret3 == SQLITE_OK){
		printf("Parking space is empty\n");
	}
	else{
		printf("satll no is fail! %s\n",errmsg);
	}

}

void select_paking(sqlite3 *pdb){//Find the parking record of the car according to the license plate number 
	char sql[200];
	char *errmsg = NULL;
	int ret;
	int nrow;
	int ncol;
	int i;
	char ** ret_val = NULL;
	char name[20];
	
	printf("Please enter the license plate number of the car you want to query\n");
	scanf("%s",name);

	sprintf(sql,"select id,name,entertime,exittime from alltable where name = '%s';",name);
	ret = sqlite3_get_table(pdb,sql,&ret_val,&nrow,&ncol,&errmsg);

	if(ret != SQLITE_OK){
		printf("select_paking error! %s\n",errmsg);
		sqlite3_free_table(ret_val);
		return;
	}
	else{
		printf("select_paking success!\n");
	}

	for(i = 0;i < (nrow + 1) * ncol;i++){
		printf("%-20s",ret_val[i]);

		if((i + 1) % ncol == 0){
			printf("\n");
		}
	}
	sqlite3_free_table(ret_val);
}

void select_allpaking(sqlite3 *pdb){//Check the parking records of all previous parking cars in this garage
	char *sql = NULL;
	char *errmsg = NULL;
	char **ret_val = NULL;
	int nrow;
	int ncol;
	int ret;
	int i;

	sql = "select * from alltable;";
	ret = sqlite3_get_table(pdb,sql,&ret_val,&nrow,&ncol,&errmsg);

	if(ret != SQLITE_OK){
		printf("select_allpaking error! %s\n",errmsg);
		sqlite3_free_table(ret_val);
		return;
	}
	else{
		printf("select_allpaking success!\n");
	}

	for(i = 0;i < (nrow + 1) * ncol;i++){
		printf("%-20s",ret_val[i]);

		if((i + 1) % ncol == 0){
			printf("\n");
		}
	}
	sqlite3_free_table(ret_val);
}

3. Function declaration

#ifndef paking_admin
#define paking_admin
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sqlite3.h>

#define MAX 10
void welcome();
int create_table(sqlite3 *pdb);
int create_all_table(sqlite3 *pdb);
void init_table(sqlite3 *pdb);
void select_all(sqlite3 * pdb);
void insert_values(sqlite3 *pdb);
void count_fee(sqlite3 *pdb);
void select_paking(sqlite3 *pdb);
void select_allpaking(sqlite3 *pdb);
#endif

Topics: Linux Operation & Maintenance server