系统使用C和MySQL实现自动取款机系统(C mysql实现atm)
自动取款机是一种常见的金融设备,它使得用户能够随时随地取款。而如今,自动取款机系统的使用已经远远超出了传统的金融领域,它已经成为了人们日常生活中不可或缺的一部分。本文将介绍使用C和MySQL实现自动取款机系统的方法。
1.需求分析
在设计自动取款机系统时,需要考虑以下几个方面:
1)登陆系统
2)查询余额
3)取款
4)提前还款
5)更改密码
2.数据库设计
使用MySQL数据库来存储自动取款机系统的数据,其中需要创建以下两个表:
1)用户表
该表存储用户的基本信息,包括用户的姓名、账号、密码等。
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`balance` decimal(10,2) NOT NULL DEFAULT ‘0.00’,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
2)记录表
该表用于存储用户的交易记录,包括交易时间、交易类型、交易金额等。
CREATE TABLE `transaction` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`type` varchar(20) NOT NULL DEFAULT ”,
`amount` decimal(10,2) NOT NULL DEFAULT ‘0.00’,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
CONSTRNT `transaction_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
3.系统实现
在实现自动取款机系统时,需要按照以下步骤进行:
1)连接数据库并且读取用户数据:
conn = mysql_init(NULL);
mysql_real_connect(conn, “localhost”, “root”, “password”, “dbname”, 3306, NULL, 0);
result = mysql_query(conn, “SELECT * FROM `user` WHERE `username` = ‘test'”);
row = mysql_fetch_row(result);
2)用户登陆:
printf(“Please enter your username: “);
scanf(“%s”, &username);
printf(“Please enter your password: “);
scanf(“%s”, &password);
if(strcmp(username, row[1]) == 0 && strcmp(password, row[2]) == 0) {
printf(“Welcome, %s”, row[1]);
} else {
printf(“Invalid username or password”);
}
3)查询余额:
result = mysql_query(conn, “SELECT `balance` FROM `user` WHERE `id` = 1”);
row = mysql_fetch_row(result);
printf(“Your balance is: $%d”, row[0]);
4)用户取款:
printf(“Please enter the amount you want to withdraw: “);
scanf(“%f”, &amount);
if(amount > row[3]) {
printf(“Insufficient balance in your account”);
} else {
mysql_query(conn, “UPDATE `user` SET `balance` = `balance` – amount WHERE `id` = 1”);
mysql_query(conn, “INSERT INTO `transaction`(`user_id`, `type`, `amount`) VALUES (1, ‘withdraw’, amount)”);
printf(“You have successfully withdrawn $%d from your account.”, amount);
}
5)提前还款:
printf(“Please enter the amount you want to repay: “);
scanf(“%f”, &amount);
mysql_query(conn, “UPDATE `user` SET `balance` = `balance` + amount WHERE `id` = 1”);
mysql_query(conn, “INSERT INTO `transaction`(`user_id`, `type`, `amount`) VALUES (1, ‘repay’, amount)”);
printf(“You have successfully repd $%d.”, amount);
6)更改密码:
printf(“Please enter your new password: “);
scanf(“%s”, &new_password);
mysql_query(conn, “UPDATE `user` SET `password` = new_password WHERE `id` = 1”);
printf(“Your password has been changed.”);
4.总结
本文介绍了使用C和MySQL实现自动取款机系统的步骤。在实现过程中需要对数据库进行设计,并且需要按照需求进行系统实现。C和MySQL都是优秀的开发工具,它们可以帮助我们快速地构建出高效且易于维护的自动取款机系统。