数据结构大作业——超市商品分类管理系统(树结构的应用)

数据结构大作业——超市商品分类管理系统(树结构的应用)

源码在文章末尾

一、题目要求

1.问题描述

设计一个简单商品分类管理系统,对某超市商品进行分类存储和管理. (孩子兄弟法+父指针);
商品信息描述如下:商品代码,商品名称,商品类别,商品价格,商品数量。

2.基本要求

(1) 浏览当前商品分类目录的所有内容(子分类和当前目录下的商品);

(2) 切换当前分类目录到上一级分类目录或下一级子分类目录(扩展,切换到任何一个目录)或直接切换到根目录;

(3) 目录和商品可以任意扩充,在当前分类目录下既可以添加新商品目录, 也可以添加新商品信息;

(4) 在当前商品分类目录下删除某个子商品分类或某个商品信息;

(5) 在当前目录下修改某个商品或分类目录的信息;

(6) 根据某个商品编号(或名称)在整个系统中查找某个商品并显示全部信息;

(7) 至少 3 级目录,第一级目录不少于 3 个分类; 设计合适的交互界面;

(8) 把当前所有分类和商品存入文件中;

(9) 初始化时从文件中读取所有分类和商品内容。

(10) 可以任意扩充,设计良好的操作界面;

(11) 在需求分析阶段完成未尽功能需求,适当扩充功能(至少扩充 3-5 个功能)。

3.设计要求

a) 数据描述:设计合适的数据信息,比如商品,分类目录等等

b) 设计合适的数据结构组织数据,以及设计算法实现需求

二、数据结构设计

实际上,该题目就是要求做一个类似文件树的功能,实现在当前商品目录下增删查改等功能。

在这里插入图片描述

数据结构对象设计

商品对象设计:

class Product {
public:
	int id;						//商品代码
	string productName;			//商品名称
	double productPrice;		//商品价格
	int	quantity;				//商品数量
	Category* category;			//商品类别
};

订单对象设计

class Order {
public:
	string orderName;				//订单名称
	double price;					//支付价格
};

用户对象设计

class Manager {
public:
	string username;			//管理员用户名
	string password;			//登录密码
};
class Customer {
public:
	string name;					//用户名
	string passWord;				//用户密码
	vector<Order> orders;			//用户所有订单
};

数据结构设计

(1)采用孩子兄弟表示法+父指针的树作为数据结构来存放所有的超市商品信息

class Category {
public:
	Category();
	~Category();
	Category(string categoryName);
	string categoryName;					//商品分类名字
	Category *parent;					//上一级分类
	Category *children;					//下一级分类
	Category *rightsib;					//右兄弟分类
	vector<Product> products;		 	//本级商品信息
};

(2)采用动态有序顺序表作为数据结构来存放所有的用户信息,按照用户编号有序存放

vector<Customer>customers;	//所有客户

(3)设计动态有序顺序表存放购买商品记录

vector<Order> orders;       //用户所有订单

三、主要功能实现

1.顾客功能实现

头文件

#pragma once
#include<string>
#include<vector>
#include<iostream>
#include"product.h"
#include"order.h"
#include"manager.h"
using namespace std;
class Customer {

public:

	//无参构造
	Customer();

	//有参构造
	Customer(string name, string passWord);


	//操作菜单
	void showMenu();

	//账号管理修改用户名或密码
	void accountManagement();

	//查询订单
	void searchOrders();

	//购买商品
	void buyProduct(Manager* manager, Category*root, vector<Product>allProduct);

	string name;				//用户名
	string passWord;			//用户密码
	vector<Order> orders;		//用户所有订单

};

具体实现

#include"customer.h"


//无参构造
Customer::Customer() {

}

//有参构造
Customer::Customer(string name, string passWord) {
	this->name = name;
	this->passWord = passWord;
}


//操作菜单
void Customer::showMenu() {
	cout << "********************************欢迎使用超市商品分类管理系统***********************************" << endl;
	cout << endl;
	cout << "\t\t\t1. 查询订单";
	cout << "\t\t\t2. 修改基本信息" << endl;
	cout << "\t\t\t3. 购买商品" ;
	cout << "\t\t\t0. 保存并退出" << endl;
	cout << endl;
	cout << "***************************************************************************************************" << endl;
}

//账号管理修改用户名或密码
void Customer::accountManagement() {
	cout << "用户名:" << endl;
	cin >> name;
	this->name = name;
	cout << "密码:" << endl;
	cin >> passWord;
	this->passWord = passWord;
	cout << "信息已更新" << endl;
}


//查询订单
void Customer::searchOrders() {
	if (orders.empty()) {
		cout << "没有找到订单" << endl;
		return;
	}
	cout << "订单列表:" << endl;
	for (auto& order : orders) {
		cout << "订单名称: " << order.orderName << endl;
		cout << "支付价格:" << order.price << endl;
	}
}




//购买商品
void Customer::buyProduct(Manager* manager,Category*root,vector<Product>allProduct) {
	string name;
	bool flag=false;
	cout << "购买商品名称:" << endl;
	cin >> name;
	double price;
	for (auto &it : allProduct) {
		if (it.productName == name) {
			flag = true;
			it.quantity--;
			price = it.productPrice;
		}
	}
	auto p=manager->searchProduct(name, root);
	for (auto &it : p->products) {
		if (it.productName == name) {
			 
			it.quantity--;
		}
	}
	 

	if (flag) {
		Order o;
		o.orderName = name;
		o.price = price;
		orders.push_back(o);
		cout << "购买成功!" << endl;
	}

	else
		cout << "未找到该商品!" << endl;

}

2.商品功能

头文件

#pragma once
#include<string>
#include<vector>
using namespace std;
class Category;
class Product {
public:
	int id;						//商品代码
	string productName;			//商品名称
	double productPrice;		//商品价格
	int	quantity;				//商品数量
	Category* category;			//商品类别

};
class Category {
public:
	Category();
	~Category();
	Category(string categoryName);
	string categoryName;				//商品分类名字
	Category *parent;					//上一级分类
	Category *children;					//下一级分类
	Category *rightsib;					//右兄弟分类
	vector<Product> products;		 //本级商品信息
};

具体实现

#include"product.h"

//无参构造
Category::Category() {
	this->children = nullptr;
	this->parent = nullptr;
	this->rightsib = nullptr;
}
Category::Category(string categoryName) {
	this->categoryName = categoryName;
	this->children = nullptr;
	this->parent = nullptr;
	this->rightsib = nullptr;
}
Category::~Category() {
	// 释放本级商品信息的内存资源
	products.clear();

	// 递归删除子分类
	if (children != nullptr) {
		Category* child = children;
		while (child != nullptr) {
			Category* nextChild = child->rightsib;
			delete child;
			child = nextChild;
		}
	}
}

3.管理功能

头文件

#pragma once
#include<string>
#include<vector>
#include<iostream>
#include<fstream>
#include<algorithm>
#include"order.h"
#include"product.h"
#include <iomanip>
using namespace std;

//升序
bool asc(Product&p1, Product&p2);
//降序
bool desc(Product&p1, Product&p2);

class Manager {
public:

	//无参构造
	Manager();

	//有参构造
	Manager(string username, string password);

	//操作菜单
	void showMenu();

	//库存预警
	void stockWarn(vector<Product>allProduct,int threshold);

	//显示商品信息
	void  showProduct(Product p);

	// 浏览当前分类目录的所有内容
	void browse(Category *category);

	// 切换到上一级分类目录
	Category *switchToParent(Category *category);

	// 切换到下一级子分类目录
	Category *switchToChild(Category *category);

	// 切换到根目录
	Category *switchToRoot(Category *category);

	// 添加子分类目录
	void addChildCategory(string childName, Category *category);

	//当前目录下添加商品
	void addProduct(Category *category,int &id, vector<Product>allProduct);

	// 删除子分类目录
	void deleteChildCategory(Category *category);

	//当前目录删除商品
	void deleteProduct(Category *category);

	// 修改商品信息
	void modifyProduct(Category *category);

	// 修改分类目录信息
	void modifyCategory(Category *category);

	// 根据商品编号或名称查找商品信息
	Category* searchProduct(string keyword, Category *category);

	//商品价格排序
	void sortPrice(vector<Product>allProduct);
 

	string username;			//管理员用户名
	string password;			//登录密码
};

具体实现

#include"manager.h"

//无参构造
Manager::Manager() {

}

//有参构造

Manager::Manager(string username, string password) {
	this->username = username;
	this->password = password;
}

 
//登录函数
void Manager::showMenu() {
		cout << "***************************欢迎使用超市商品分类管理系统********************************" << endl;
		cout << endl;
		cout << "1.浏览当前分类目录的所有内容";
		cout << "\t\t2.切换到上一级分类目录" ;
		cout << "\t\t3.切换到下一级子分类目录" <<endl;
		cout << "4.切换到根目录"  ;
		cout << "\t\t\t\t5.添加子分类目录"  ;
		cout << "\t\t6.添加商品信息" << endl;
		cout << "7.删除子分类目录"  ;
		cout << "\t\t\t8.删除商品信息"  ;
		cout << "\t\t\t9.修改商品信息" << endl;
		cout << "10.根据商品编号或名称查找商品信息"  ;
		cout << "\t11.查询用户信息"  ;
		cout << "\t\t\t12.商品价格排序" << endl;
		cout << "13.库存预警"  ;
		cout << "\t\t\t\t0.保存并退出" << endl;
		cout << endl;
		cout << "***************************************************************************************************" << endl;
}


//库存预警
void Manager::stockWarn(vector<Product>allProduct,int threshold) {
	if (allProduct.size()==0)return;
	bool hasLowStock = false;
	for (auto product : allProduct) {
		if (product.quantity < threshold) {
			cout << "商品ID: " << product.id << endl;
			cout << "商品名称: " << product.productName << endl;
			cout << "当前库存: " << product.quantity << endl;
			cout << "库存低于预设阈值" << endl;
			hasLowStock = true;
		}
	}
	if (!hasLowStock) {
		cout << "所有商品库存正常" << endl;
	}

}


//显示商品信息
void Manager::showProduct(Product p) {
	cout << setw(16)<< setiosflags(ios::left) << setfill(' ') << p.productName;
	printf("%4d\t\t", p.id);
	cout << setw(16) << setiosflags(ios::left)<< setfill(' ') << p.category->categoryName;
	printf("%-18.2f", p.productPrice);
	printf("%-10d\n", p.quantity);
}



// 浏览当前分类目录的所有内容
void Manager::browse(Category* category) {
	if (this != nullptr) {
		cout << "当前目录:" << category->categoryName << endl;
		cout << "子分类目录:" << endl;
		auto it = category->children;
		while (it != nullptr) {
			cout << it->categoryName << endl;
			it = it->rightsib;
		}
		cout << "当前目录商品:" << endl;
		cout << "商品名称\t商品编号\t商品类别\t商品价格\t商品数量" << endl;
		auto p = category->products;
		int len = p.size();
		for (int i = 0; i < len; i++) {
			showProduct(p[i]);
		}

	}
}

// 切换到上一级分类目录
Category* Manager::switchToParent(Category* category) {
	if (category->parent != nullptr) {
		return category->parent;
	}
	else {
		cout << "已经是根目录了!" << endl;
		system("pause");
		system("cls");
		return category;
		
	}
}

// 切换到下一级子分类目录
Category* Manager::switchToChild(Category* category) {
	cout << "子分类目录:" << endl;
	auto it = category->children;
	if (it == nullptr) {
		cout << "无子分类目录" << endl;
		return category;
	}
	int i = 1;
	vector<Category*>temp;
	while (it != nullptr) {
		cout << i++ << "." << it->categoryName << endl;
		temp.push_back(it);
		it = it->rightsib;
	}
	cout << "请选择:(0退出)" << endl;
	int choice = 0;
	while (true) {
		cin >> choice;
		if (choice == 0) {
			system("pause");
			system("cls");
			return category;
		}
		if (choice >= 1 && choice < i)
			return temp[choice - 1];
		else
			cout << "请正确输入!" << endl;
	}
}

// 切换到根目录
Category* Manager::switchToRoot(Category* category) {
	auto it = category->parent;
	if (it == nullptr){
		cout << "切换成功!" << endl;
		system("pause");
		system("cls");
		return category;
	}
	while (it != nullptr) {
		if (it->parent != nullptr)
			it = it->parent;
		else
			break;
	}
	cout << "切换成功!" << endl;
	system("pause");
	system("cls");
	return it;
}

// 添加子分类目录
void Manager::addChildCategory(string childName, Category *category) {
 
	Category *child = new Category();
	child->categoryName = childName;
	child->parent = category;

	// 判断 category 是否存在子节点
	if (category->children == nullptr) {
		category->children = child;
	}
	else {
		// 遍历到最后一个子节点
		auto it = category->children;
		while (it->rightsib != nullptr) {
			it = it->rightsib;
		}
		it->rightsib = child;
	}

}

// 添加商品信息
void Manager::addProduct(Category* category,int &id, vector<Product>allProduct) {
	Product p;
	p.id = id++;
	cout << "商品名称:";
	cin >> p.productName;
	p.category = category;
	cout << "商品价格:";
	cin >> p.productPrice;
	cout << "商品数量:";
	cin >> p.quantity;
	for (auto it : allProduct) {
		if (p.productName == it.productName) {
			cout << "商品已存在!" << endl;
			id--;
			return;
		}
	}
	cout << "商品信息添加成功!" << endl;
	category->products.push_back(p);

}

// 删除子分类目录
void Manager::deleteChildCategory(Category* category) {
	cout << "子分类目录:" << endl;
	auto it = category->children;
	if (it == nullptr)cout << "无子分类目录" << endl;
	int i = 1;
	vector<Category*>temp;
	while (it != nullptr) {
		cout << i++ << "." << it->categoryName << endl;
		temp.push_back(it);
		it = it->rightsib;
	}
	cout << "请选择:(0退出)" << endl;
	int choice = 0;
	while (true) {
		cin >> choice;
		if (choice == 0) {
			system("pause");
			system("cls");
			return;
		}
		//只有一个目录的情况下
		else if (i == 2) {
			if (choice == 1) {
				delete temp[choice - 1];
				category->children = nullptr;
				cout << "删除成功!" << endl;
				break;
			}
			else
				cout << "请正确输入!" << endl;
		}
		else {
			//删除第一个
			if (choice == 1) {
				category->children = temp[1];
				delete temp[0];
				temp[0] = nullptr;
				cout << "删除成功!" << endl;
				break;
			}
			//删除最后一个
			else if (choice == i - 1) {
				delete temp[choice - 1];
				temp[choice - 1] = nullptr;
				temp[choice - 2]->rightsib = nullptr;
				cout << "删除成功!" << endl;
				break;
			}
			//处于中间
			else if (choice > 1 && choice < i - 1) {
				temp[i - 2]->rightsib = temp[i]->rightsib;
				delete temp[i - 1];
				temp[i - 1] = nullptr;
				cout << "删除成功!" << endl;
				break;
			}
			else
				cout << "请正确输入!" << endl;
		}

	}
}

//当前目录删除商品
void Manager::deleteProduct(Category* category) {
	cout << "当前目录下商品:" << endl;
	cout << "商品名称\t商品编号\t商品类别\t商品价格\t商品数量" << endl;
	auto p = category->products;
	int sum = 0;
	int len = p.size();
	for (int i = 0; i < len; i++) {
		sum++;
		cout << sum << ".";
		showProduct(p[i]);
	}
	int choice = 0;
	while (true) {
		cout << "请选择:(0退出)" << endl;
		cin >> choice;
		if (choice == 0) {
			system("pause");
			system("cls");
			return;
		}
		else if (choice <= sum) {
			category->products.erase(category->products.begin() + choice - 1);
			cout << "删除成功!" << endl;
			break;
		}
		else
			cout << "请正确输入!" << endl;
	}
}

// 修改商品信息
void Manager::modifyProduct(Category* category) {
	cout << "当前目录下商品:" << endl;
	cout << "商品名称\t商品编号\t商品类别\t商品价格\t商品数量" << endl;
	auto p = category->products;
	int sum = 0;
	int len = p.size();
	for (int i = 0; i < len; i++) {
		sum++;
		cout << sum << ".";
		showProduct(p[i]);
	}
	int choice = 0;
	while (true) {
		cout << "请选择:(0退出)" << endl;
		cin >> choice;
		if (choice >= 1 && choice <= sum) {
			cout << "商品名称:";
			cin >> category->products[choice - 1].productName;
			cout << "商品价格:";
			cin >> category->products[choice - 1].productPrice;
			cout << "商品数量:";
			cin >> category->products[choice - 1].quantity;
			cout << "修改商品信息成功!" << endl;
			break;
		}
		else if (choice == 0) {
			system("pause");
			system("cls");
			return;
		}
		else
			cout << "请正确输入!" << endl;
	}
}

// 修改分类目录信息
void Manager::modifyCategory(Category* category) {
	cout << "子分类目录:" << endl;
	if (category->children == nullptr)cout << "无子目录!" << endl;
	else {
		int i = 0;
		auto it =category->children;
		while (it != nullptr) {
			cout << ++i << "." << it->categoryName << endl;
			it = it->rightsib;
		}
		int choice;
		while (true) {
			cout << "请选择:(0退出)" << endl;
			cin >> choice;
			if (choice >= 1 && choice <= i) {
				cout << "分类名称:";
				cin >> category->categoryName;
				break;
			}
			else if (choice == 0) {
				system("pause");
				system("cls");
				return;
			}
			else cout << "无效输入!" << endl;
		}
	}
}

// 根据商品编号或名称查找商品信息
Category* Manager::searchProduct(string keyword, Category* category) {
	if (category == nullptr) return category;
	bool flag = false;
	for (Product& product : category->products) {
		if (product.productName == keyword || to_string(product.id) == keyword) {
			cout << "商品代码:" << product.id << endl;
			cout << "商品名称:" << product.productName << endl;
			cout << "商品类别:" << category->categoryName << endl;
			cout << "商品价格:" << product.productPrice << endl;
			cout << "商品数量:" << product.quantity << endl;
			cout << "-----------------------" << endl;
			flag = true;
			return category;
		}
	}
	if (!flag) {
		auto p = category->children;
		while (p != nullptr) {
			Category* result = searchProduct(keyword, p);
			if (result != nullptr)
				return result;
			p = p->rightsib;
		}
	}
	return nullptr;					// 没有找到符合条件的商品,返回 nullptr
}


bool asc(Product&p1, Product&p2) {
	if (p1.productPrice == p2.productPrice)
		return p1.id < p2.id;
	return p1.productPrice < p2.productPrice;
}
bool desc(Product&p1, Product&p2) {
	if (p1.productPrice == p2.productPrice)
		return p1.id < p2.id;
	return p1.productPrice > p2.productPrice;
}
//商品价格排序
void Manager::sortPrice(vector<Product>allProduct) {
	if (!allProduct.empty()) {
		cout << "********************************欢迎使用超市商品分类管理系统***********************************" << endl;
		cout << endl;
		cout << "1.升序\t\t\t\t\t2.降序\t\t\t\t3.返回" << endl;
		cout << endl;
		cout << "***************************************************************************************************" << endl;
		int choice;
		bool flag = true;
		while (flag) {
			cin >> choice;
			switch (choice) {
			case 1: {
				sort(allProduct.begin(), allProduct.end(), asc);
				cout << "商品名称\t商品编号\t商品类别\t商品价格\t商品数量" << endl;
				for (auto it : allProduct) {
					showProduct(it);
				}
				flag = false;
				break;
			}
			case 2: {
				sort(allProduct.begin(), allProduct.end(), desc);
				cout << "商品名称\t商品编号\t商品类别\t商品价格\t商品数量" << endl;
				for (auto it : allProduct) {
					showProduct(it);
				}
				flag = false;
				break;
			}
			case 3: {
				return;
				break;
			}
			default:
				cout << "输入错误!" << endl;
			}
		}
	}
	else
		cout << "无商品信息!" << endl;
}

4.数据存储

#pragma once
#include"manager.h"
#include"customer.h"
#include<sstream>
// 初始化时从文件中读取所有分类和商品内容
Category* readFromFile(const string& filepath,vector<Product>&allProduct);
void readCategory(ifstream& file, Category* parentCategory, vector<Product>&allProduct);


// 将所有分类和商品存入文件
void saveToFile( Category* category, const string& filepath);
void saveCategory( Category* category, ofstream& file, int level, string status);


//存取编号
void saveId(int productID,string filePath);
void loadId(int &productID,string filePath);

//存取用户信息
void saveCustomerToFile( vector< Customer>&customer, const string& filePath);
void loadCustomerFromFile(vector<Customer>& customer, const string& filePath);

具体实现

#include"file.h"
// 初始化时从文件中读取所有分类和商品内容
Category* readFromFile(const string& filepath, vector<Product>&allProduct) {
	ifstream file(filepath);
	if (file.is_open()) {
		Category *rootCategory = new Category("超市商品目录");
		readCategory(file, rootCategory,allProduct);
		file.close();
		return rootCategory;
	}
	else {
		cout << "无法打开文件:" << filepath << endl;
		return nullptr;
	}
}


void readCategory(ifstream& file, Category* parentCategory, vector<Product>&allProduct) {
	string line;
	Category*last;
	last = parentCategory;
	int flag = 0;
	while (getline(file, line)) {
		if (flag == 0) {
			flag = 1;
			continue;
		}
		// 检测是否是分类名称行
		if (flag!=0&&line.find("category: ") != string::npos) {
			// 解析字符串
			istringstream iss(line);
			string lev,status,ignore,name;
			iss >> ignore >>lev>>status>>name;
			Category* category = new Category(name);
			
			if (status == "c") {
				last->children = category;
				category->parent = last;
			}
			else if (status == "r") {
				int t=atoi(lev.c_str());
				t--;
				auto it = parentCategory->children;
				while (it->rightsib!=nullptr) {
					it = it->rightsib;
				}
				if (it->children == nullptr) {
					category->parent = it;
					it->children = category;
				}
				else {
					while (t--) {
						if (it->children != nullptr) {
							it = it->children;
							while (it->rightsib != nullptr)it = it->rightsib;
						}
					}
					it->rightsib = category;
					category->parent = it->parent;
				 
				}
				 
			}
			last = category;
		}

		else if (line.find("product: ") != string::npos) {
			int id, quantity;
			double price;
			string productName;

			// 使用istringstream解析商品信息
			istringstream iss(line);
			string ignore;
			iss >> ignore >>id >> productName >> price >> quantity;

			// 创建商品对象并添加到当前分类的商品列表中
			Product product;
			product.id = id;
			product.productName = productName;
			product.productPrice = price;
			product.quantity = quantity;
			product.category = last;
			last->products.push_back(product);
			allProduct.push_back(product);
		}

	}
}

// 将所有分类和商品存入文件
void saveToFile(Category* category, const string& filepath) {
	ofstream file(filepath);
	if (file.is_open()) {
		// 递归保存商品分类及其产品信息
		saveCategory(category, file, 0," c ");
		file.close();
		cout << "数据保存成功!" << endl;
	}
	else {
		cout << "无法打开文件:" << filepath << endl;
	}
}

void saveCategory(Category* category, ofstream& file, int level,string status) {
	if (category == nullptr)return;
	// 保存分类名和本级商品信息
	file << "category: " <<level<<status<< category->categoryName << endl;
	for (const auto& product : category->products) {
		file << "product: " << product.id << " " << product.productName
			<< " " << product.productPrice << " " << product.quantity << endl;
	}

	// 递归保存子分类
	if (category->children != nullptr) {
		saveCategory(category->children, file, level + 1," c ");
	}

	// 保存右兄弟分类
	if (category->rightsib != nullptr) {
		saveCategory(category->rightsib, file, level," r ");
	}
}


void saveId(int productID,string filePath) {
	ofstream file(filePath);
	if (file.is_open()) {
		file << productID << endl;
		file.close();
	}
	else {
		cout << "无法打开文件:" << filePath << endl;
	}
}
void loadId(int &productID, string filePath) {
	string line;
	ifstream file(filePath);
	if (file.is_open()) {
		getline(file, line);
		istringstream ss(line);
		ss >> productID;
		file.close();
	}
	else {
		cout << "无法打开文件:" << filePath << endl;
	}

}

//存取用户信息
void saveCustomerToFile( vector<Customer>&customer, const string& filePath) {
	ofstream file(filePath);
	int i = 0;
	if (file.is_open()) {
		while (i<customer.size()) {
			file << "customer: ";
			file << customer[i].name ;
			file << " "<<customer[i].passWord << endl;
			// 存储用户订单
			for (const Order& order : customer[i].orders) {
				file << order.orderName << " " << order.price <<endl ;
			}
			i++;
		}
		file.close();
	}
	else {
		cout << "无法打开文件:" << filePath << endl;
	}
}

void loadCustomerFromFile(vector<Customer>& customer, const string& filePath) {
	ifstream file(filePath);
	string line;
	if (file.is_open()) {
		while (getline(file, line)) {
			if (line.find("customer: ") != string::npos) {
				istringstream iss(line);
				string ignore,name, password;
				iss >>ignore>> name >> password;
				Customer c;
				c.name = name;
				c.passWord = password;
				customer.push_back(c);
			}
			else {
				istringstream is(line);
				string orderName, price;
				is >> orderName >> price;
				Order order;
				order.orderName = orderName;
				order.price = stod(price);
				customer[customer.size()-1].orders.push_back(order);
			}
		}
		file.close();
	}
	else {
		cout << "无法打开文件:" << filePath << endl;
	}
}


5.订单类

#pragma once
#include<string>
using namespace std;
class Order {
public:
	string orderName;				//订单名称
	double price;					//支付价格
};
//这个不用具体实现

6.主函数

#include<iostream>
#include"file.h"
#include"customer.h"
#define CATEGORY "category.txt"
#define CUSTOMER "customer.txt"
#define ID       "id.txt"
using namespace std;
static int productId;
Manager *manager = new Manager("admin", "123456");
vector<Customer>customers;	//所有客户
vector<Product>allProduct;	//所有商品
Category* root1, *root;


void adminMenu(Manager *manager, Category* category) {
	int choice = -1;
	while (choice != 0) {
		manager->showMenu();
		cin >> choice;
		system("cls");
		switch (choice) {
			case 1:
				manager->browse(category);
				system("pause");
				system("cls");
				break;
			case 2: {
				auto parent = manager->switchToParent(category);
				adminMenu(manager, parent);
				system("pause");
				system("cls");
				break;
			}

			case 3: {
				auto child = manager->switchToChild(category);
				adminMenu(manager, child);
				system("pause");
				system("cls");
				break;
			}

			case 4: {
				auto root = manager->switchToRoot(category);
				adminMenu(manager, root);
				system("pause");
				system("cls");
				break;
			}

			case 5: {
				string childName;
				cout << "请输入子分类目录名称:";
				cin >> childName;
				manager->addChildCategory(childName, category);
				cout << "子分类目录添加成功!" << endl;
				system("pause");
				system("cls");
				break;
			}
			case 6: {
				manager->addProduct(category, productId,allProduct);
				system("pause");
				system("cls");
				break;
			}
			case 7: {
				manager->deleteChildCategory(category);
				system("pause");
				system("cls");
				break;
			}
			case 8: {
				manager->deleteProduct(category);
				system("pause");
				system("cls");
				break;
			}
			case 9: {
				manager->modifyProduct(category);
				system("pause");
				system("cls");
				break;
			}
			case 10: {
				string keyword;
				double price;
				cout << "请输入商品编号或名称:";
				cin >> keyword;
				if (manager->searchProduct(keyword, category) == nullptr)
					cout << "未找到该商品!" << endl;
				system("pause");
				system("cls");
				break;
			}
			case 11: {
				//查询用户信息
				string searchName;
				cout << "请输入要查询的客户姓名:";
				cin >> searchName;
				bool found = false;
				for (const auto& customer : customers) {
					if (customer.name == searchName) {
						cout << "客户姓名: " << customer.name << endl;
						cout << "客户订单:" << endl;
						for (auto it : customer.orders) {
							cout << "订单名称:" << it.orderName;
							cout << "\t支付价格:" << it.price << endl;
						}
						found = true;
						break;
					}
				}
				if (!found) {
					cout << "未找到该客户" << endl;
				}
				system("pause");
				system("cls");
				break;
			}
			case 12: {
				manager->sortPrice(allProduct);
				system("pause");
				system("cls");
				break;
			}
			case 13: {
				cout << "请设置阈值:" << endl;
				int num;
				cin >> num;
				manager->stockWarn(allProduct,num);
				system("pause");
				system("cls");
				break;
			}
			case 0: {
				saveId(productId, ID);
				saveToFile(manager->switchToRoot(category), CATEGORY);
				saveCustomerToFile(customers, CUSTOMER);
				exit(0);
				break;
			}
			default: {
				cout << "无效的操作!" << endl;
				system("pause");
				system("cls");
				break;
			}
		}
	}
}

//用户操作函数
void custmomerMenu(Customer &customer,int size) {
	int choice = -1;
	while (choice != 0) {
		customer.showMenu();
		cin >> choice;
		system("cls");
		switch (choice) {
			case 1: {
				customer.searchOrders();
				system("pause");
				system("cls");
				break;
			}
			case 2: {
				customer.accountManagement();
				customers[size] = customer;
				system("pause");
				system("cls");
				break;
			}
			case 3: {
				customer.buyProduct(manager, root1, allProduct);
				customers[size] = customer;
				system("pause");
				system("cls");
				break;
			}
			case 0: {
				saveId(productId, ID);
				saveToFile(root, CATEGORY);
				saveCustomerToFile(customers, CUSTOMER);
				exit(0);
			}
				
			default: {
				cout << "无效的操作!" << endl;
				system("pause");
				system("cls");
				break;
			}
		}
	}
}


int login(string name, string password) {

	if (name == "admin") {
		if (password == "123456") {
			system("cls");
			adminMenu(manager, root);
			return 0;
		}
		else {
			cout << "密码错误!" << endl;
			return 2;
		}
	}
	else {
		int i = 0;
		for (auto it : customers) {
			if (it.name == name) {
				if (it.passWord == password) {
					system("cls");
					custmomerMenu(it,i);
					return 1;
				}
				else {
					cout << "密码错误!" << endl;
					return 2;
				}
			}
			i++;
		}
		cout << "用户不存在!" << endl;
		return -1;
	}

}


int main() {
	loadId(productId, ID);
	loadCustomerFromFile(customers, CUSTOMER);
	root = readFromFile(CATEGORY, allProduct);
	root1 = root;
	string name, password;
	while (true) {
		cout << "\t\t*******************欢迎使用超市商品分类管理系统(按0退出系统)********************" << endl;
		cout << "\t\t用户名:";cin >> name;
		if (name[0]== '0') {
			return 0;
		}
		cout << "\t\t密码:";cin >> password;
		login(name, password);
		system("pause");
		system("cls");
	}

	return 0;
}

四、效果截图

  • 管理员视角

登录后 界面
在这里插入图片描述

  • 用户视角
    在这里插入图片描述
    已购买列表
    在这里插入图片描述

核心要理解树的结构还有如何搜索到树节点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值