软件工程第三次作业——ATM管理系统

博客班级 软件工程
作业要求 作业要求
作业目标 根据ATM机的功能设计代码,并完成ATM系统代码的创建
学号 3180701219

题目要求:

编写一个ATM管理系统,语言不限,要求应包括以下主要功能:
(1)开户,销户
(2)查询账户余额
(3)存款
(4)取款
(5)转账(一个账户转到另一个账户)等...

代码提交

头文件

#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
using namespace std;

结构体

typedef struct {
	string name;			//用户名
	string password;		//密码
	string accountNumber;	        //账户
	double money;			//余额
}AccountNode;

typedef struct node{
	AccountNode acc;
	struct node* next;
}Node;

函数声明

void Menu();					//主菜单
void CreateAccount(Node* &head);		//开户
void Login(Node* head);				//登录
bool Cancel(Node* &head);			//销户
void LoginMenu();				//登录菜单
Node* Find(Node* head, string accountNumber);	//根据账号找客户

主菜单

void Menu()//主菜单
{
	printf("\n\n\n\t\t\t\t\t ————————————\n");
	printf("\t\t\t\t\t|\tATM管理系统\t |\n");
	printf("\t\t\t\t\t|\t        \t |\n");
	printf("\t\t\t\t\t|\t  1.开户\t |\n");
	printf("\t\t\t\t\t|\t  2.登录\t |\n");
	printf("\t\t\t\t\t|\t  3.销户\t |\n");
	printf("\t\t\t\t\t|\t  4.退出\t |\n");
	printf("\t\t\t\t\t ————————————\n");
	printf("\t\t\t\t\t\t\t请选择:");
}

开户

void CreateAccount(Node* &head)
{
	printf("\n\n\n\t\t\t\t\t请输入您的姓名:");
	string name;
	cin >> name;
	
	printf("\n\t\t\t\t\t请设置您的银行卡密码:");
	string password;
	cin >> password;

	printf("\n\t\t\t\t\t请再次输入您的银行卡密码:");
	string repassword;
	cin >> repassword;

	//判断两次输入的密码是否一致
	while (password != repassword) {
		printf("\n\t\t\t\t\t两次输入密码不一致,请重新输入:");
		cin >> repassword;
	}

	//随机生成银行账号
	char temp[20];//0000 0000 0000 0000 0 0
	string accountNumber;
	srand((unsigned int)time(NULL));
	sprintf(temp, "%d%d%d%d%d%d", rand() % 9000 + 1000,
		rand() % 9000 + 1000, rand() % 9000 + 1000, rand() % 9000 + 1000, rand() % 10, rand() % 10);//sprintf格式化字符串 
	accountNumber = temp;

	Node* pNode = new Node;
	pNode->acc.name = name;
	pNode->acc.password = password;
	pNode->acc.accountNumber = accountNumber;
	pNode->acc.money = 0;
	pNode->next = NULL;

	Node* p = head;
	while (p != NULL && p->next != NULL)
		p = p->next;
	if (head == NULL)
		head = pNode;
	else
		p->next = pNode;

	printf("\n\n\t\t\t\t\t恭喜您,开户成功!\n");
	cout << "\n\t\t\t\t\t您的账号为:" << accountNumber << endl;
}

登录菜单

void LoginMenu()
{
	printf("\n\n\n\t\t\t\t\t ————————————\n");
	printf("\t\t\t\t\t|\t  欢迎您!\t |\n");
	printf("\t\t\t\t\t|\t        \t |\n");
	printf("\t\t\t\t\t|\t  1.存款\t |\n");
	printf("\t\t\t\t\t|\t  2.取款\t |\n");
	printf("\t\t\t\t\t|\t  3.转账\t |\n");
	printf("\t\t\t\t\t|\t  4.查询余额\t |\n");
	printf("\t\t\t\t\t|\t  5.修改密码\t |\n");
	printf("\t\t\t\t\t|\t  6.退出\t |\n");
	printf("\t\t\t\t\t ————————————\n");
	printf("\t\t\t\t\t\t\t请选择:");
}

登录及(存取款、查询余额、转账、修改密码)

void Login(Node* head)
{
	string accountNumber;
	string password;
	printf("\n\n\n\t\t\t\t\t请输入您的账号:");
	cin >> accountNumber;
	Node* p = Find(head, accountNumber);
	if (!p) {
		printf("\n\t\t\t\t\t账号输入有误!");
		return;
	}
	printf("\n\t\t\t\t\t请输入您的密码:");
	cin >> password;
	while (p->acc.password != password) {
		printf("\n\t\t\t\t\t密码错误,请重新输入:");
		cin >> password;
	}
	while (1) {
		system("cls");
		LoginMenu();
		int choice;
		string otherAccountNumber;
		double money;
		string newPassword;
		Node* temp;
		cin >> choice;
		switch (choice)
		{
		case 1:
			printf("\n\n\t\t\t\t\t请输入您的存款金额:");
			cin >> money;
			p->acc.money += money;
			printf("\n\t\t\t\t\t\t存款成功!\n");
			break;
		case 2:
			printf("\n\n\t\t\t\t\t请输入您的取款金额:");
			cin >> money;
			if (money > p->acc.money)
				printf("\n\t\t\t\t\t\t余额不足!\n");
			else {
				p->acc.money -= money;
				printf("\n\t\t\t\t\t\t取款成功!\n");
			}
			break;
		case 3:
			printf("\n\n\t\t\t\t\t请输入您的转账的账号:");
			cin >> otherAccountNumber;
			printf("\n\t\t\t\t\t请输入您的转账金额:");
			cin >> money;
			temp = Find(head, otherAccountNumber);
			if (!temp) {
				printf("\n\t\t\t\t\t账号输入有误!");
				system("pause");
				return;
			}
			p->acc.money -= money;
			temp->acc.money += money;
			printf("\n\t\t\t\t\t\t转账成功!\n");
			break;
		case 4:
			printf("\n\n\t\t\t\t\t您的余额为:%.2f\n", p->acc.money);
			break;
		case 5:
			printf("\n\n\t\t\t\t\t请输入您的新密码:");
			cin >> newPassword;
			p->acc.password = newPassword;
			printf("\n\t\t\t\t\t\t密码修改成功!\n");
			break;
		case 6:
			return;
		default:
			printf("\t\t\t\t\t\t\t输入有误!\n");
		}
		system("pause");
	}
}

查找函数

Node* Find(Node* head, string accountNumber)
{
	Node* p = head;
	while (p != NULL) {
		if (p->acc.accountNumber == accountNumber)
			break;
		p = p->next;
	}
	return p;
}

销户

bool Cancel(Node* &head)
{
	string accountNumber;
	printf("\n\n\t\t\t\t\t请输入您要注销的账号:");
	cin >> accountNumber;
	Node* p = head,* q;
	if (p != NULL && p->acc.accountNumber == accountNumber) {
		head = p->next;
		free(p);
		printf("\t\t\t\t\t\t\t注销成功!\n");
		return true;
	}
	q = p;
	p = p->next;
	while (p != NULL) {
		if (p->acc.accountNumber == accountNumber) {
			q->next = p->next;
			free(p);
			printf("\t\t\t\t\t\t\t注销成功!\n");
			return true;
		}
		q = p;
	}
	return false;
}

主函数

int main()
{
	Node* head = NULL;

	while (1) {
		system("cls");
		Menu();
		int choice;
		cin >> choice;
		switch (choice)
		{
		case 1:
			CreateAccount(head);
			break;
		case 2:
			Login(head);
			break;
		case 3:
			Cancel(head);
			break;
		case 4:
			return 0;
		default:
			printf("\t\t\t\t\t\t\t输入有误!\n");
		}
		system("pause");
	}
	return 0;
}

程序运行界面

主菜单

创建账户

存款界面

转账界面

注销账户

三.个人小结

psp2.1 任务内容 计划完成需要的时间(min) 实际完成需要的时间(min)
Planning 计划 20 15
Estimate 估计这个任务需要多少时间,并规划大致工作步骤 200 240
Development 开发 10 30
Analysis 需求分析(包括学习新技术) 10 10
Design Spec 生成设计文档 10 20
Design Review 设计复审 10 20
Coding Standard 代码规范 10 10
Design 具体设计 20 30
Coding 具体编码 30 20
Code Review 代码复审 20 30
Test 测试(自我测试,修改代码,提交修改) 20 30
Reporting 报告 10 20
Test Report 测试报告 10 15
Size Measurement 计算工作量 20 20
Postmortem & Process Improvement Plan 事后总结,并提出过程改进计划 5 10

个人总结

本次对ATM管理系统的编写主要使用C++来进行编写,复习了一些C++的基本语法,其中在创建新节点的时候,由于使用了string容器,导致malloc不能使用,不过一开始并没有发现这个问题,导致我走了很多的弯路,好在后期及时发现,使用new来创建新的节点,才解决了问题。

posted @ 2020-11-18 20:12  WLongwx  阅读(346)  评论(0编辑  收藏  举报