文件管理系统初步

//Project.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include<conio.h>
#include <iomanip>
#include "Project1.h"
using namespace std;

list<Goods> goods;

void PrintMenu() {
	cout << "1 : 新增商品\n2 : 增加数量\n3 : 生成增加后表文件\n4 : 删除商品\n5 : 减少数量\n6 : 生成减少后表文件\n7 : 展示表\n8 : 查找某商品\n9 : 退出\n" << endl;
	cout << "please enter an integer from 1 to 9" << endl;
	return ;
}
list<Goods>::iterator match(char name[]) {
	list<Goods>::iterator iter;
	for (iter = goods.begin(); iter != goods.end(); iter++)
		if (strcmp(iter->name, name) == 0)
			return iter;
	return iter = goods.end();
}bool add_goods(char name[], int count) {
	list<Goods>::iterator iter = match(name);
	if (iter == goods.end()) {
		Goods temp;
		temp.count = count;
		strcpy(temp.name, name);
		goods.push_back(temp);
		cout << "\nAdd sucessfully" << endl;
	}
	else {
		iter->count += count;
	}
	return 1;
}
void AddGoods() {
	cout << "please enter the good's name" << endl;
	char* temp=new char[100]();
	cin.getline(temp, 100);
	int count;
	cout << "please enter the accountment" << endl;
	cin >> count;
	add_goods(temp, count);
	delete[] temp;
	cout << "please enter your integer" << endl;
}
bool increase_count(char name[], int count) {
	list<Goods>::iterator iter = match(name);
	if (iter == goods.end()) {
		cout << "this kind of good dosn't exist" << endl;
		return 0;
	}
	else {
		iter->count += count;
		return 1;
	}
}
void IncreseCount() {
	cout << "please enter the good's name and the count you are willing to increase\n" << endl;
	char* temp = new char[100]();
	cin.getline(temp, 100);
	int count;
	cin >> count;
	increase_count(temp, count);
	delete[] temp;
	cout << "please enter your integer" << endl;
}
bool add_to_list(char name[], int count) {
	add_goods(name, count);
	ofstream file("date.txt");
	if (file.is_open()) {
		for (list<Goods>::iterator iter = goods.begin(); iter != goods.end(); iter++) {
			file <<"商品名称:  "<< iter->name <<"   商品数目:  "<< iter->count << endl;
		}
		file.close();
	}
	else {
		cout << "fail to save the data";
	}
	return 1;
}
void AddToList() {
	cout << "please enter the good's name and the count you are willing to increase\n" << endl;
	char* temp = new char[100]();
	cin.getline(temp, 100);
	int count;
	cin >> count;
	add_to_list(temp, count);
	delete[] temp;
	cout << "please enter your integer" << endl;
}
bool delete_goods(char name[], int count) {
	list<Goods>::iterator iter = match(name);
	if (iter == goods.end()) {
		cout << "this kind of good dosen;t exist\n" << endl;
		return 0;
	}
	if (iter->count == count) {
		goods.erase(iter);
	}
	if (iter->count < count) {
		cout << "Exceed\n" << endl;
		return 0;
	}
	if (iter->count > count) {
		iter->count -= count;
		cout << iter->name << "remain " << iter->count << endl;
		return 1;
	}
}
void DeleteGoods() {
	cout << "please enter the name" << endl;
	char* temp = new char[100]();
	cin.getline(temp, 100);
	cout << "please enter the count" << endl;
	int count;
	cin >> count;
	delete_goods(temp, count);
	cout << "please enter your integer" << endl;
}
bool decrease_count(char name[], int count) {
	list<Goods>::iterator iter = match(name);
	if (iter == goods.end()) {
		cout << "this kind of good dosen;t exist\n" << endl;
		return 0;
	}
	else {
		iter->count -= count;
		cout << iter->name << "remain " << iter->count << endl;
		return 1;
	}
}
void DecreaseCount() {
	cout << "please enter the name" << endl;
	char* temp = new char[100]();
	cin.getline(temp, 100);
	cout << "please enter the count" << endl;
	int count;
	cin >> count;
	decrease_count(temp, count);
	delete[] temp;
	cout << "please enter your integer" << endl;
}
bool delete_from_list(char name[]) {
	list<Goods>::iterator iter = match(name);
	goods.erase(iter);
	ofstream file("date.txt");
	if (file.is_open()) {
		for (list<Goods>::iterator iter = goods.begin(); iter != goods.end(); iter++) {
			file << "商品名称:  " << iter->name << "   商品数目:  " << iter->count << endl;
		}
		file.close();
	}
	else {
		cout << "fail to save the data";
	}
	return 1;
}
void DeleteFromList() {
	cout << "please enter the name" << endl;
	char* temp = new char[100]();
	cin.getline(temp, 100);
	delete_from_list(temp);
	delete[] temp;
	cout << "please enter your integer" << endl;
}
void show_goods() {
	cout << endl << setw(20) << "Name" << " " << setw(10) << "Count" << endl;
	for (list<Goods>::iterator iter = goods.begin(); iter != goods.end(); iter++)
		cout << setw(20) << iter->name << " " << setw(10) << iter->count << endl;
}
void ShowGoods() {
	show_goods();
	cout << "please enter your integer" << endl;
}
void find_goods(char* name) {
	list<Goods>::iterator iter = match(name);
	if (iter == goods.end()) {
		cout << "the good you want dosen't exist\n" << endl;
	}
	else {
		cout << setw(20) << iter->name << " " << setw(10) << iter->count << endl;
	}
}
void FindGoods() {
	cout << "please enter the name" << endl;
	char* temp = new char[100]();
	cin.getline(temp, 100);
	find_goods(temp);
	delete[] temp;
	cout << "please enter your integer" << endl;
}
void EscGoods() {
	cout << "thanks for using this project\n" << endl;
	exit(0);
}
//Project1.h
#include <iostream>
#include <list>
using namespace std;
typedef struct Goods {
	char name[100];//记录商品名称
	int count;//商品数目
}Goods;//结构体的声明
void PrintMenu();//打印菜单
list<Goods>::iterator match(char name[]);//匹配函数

///内围函数
//进货,对应进货模块,表示当前进货一批数量为count的name商品
bool add_goods(char name[], int count);
//更新库存信息,对应增加库存子功能,对name商品新增count数量
bool increase_count(char name[], int count);
//更新库存列表,对应新增商品子功能,新增name商品且初始数量为count
bool add_to_list(char name[], int count);
//出货,对应出货模块,表示当前出货一批数量为count的name商品
bool delete_goods(char name[], int count);
//更新库存信息,对应减少库存子功能,对name商品减少count数量
bool decrease_count(char name[], int count);
//更新库存列表,对应删除商品子功能,删除商品列表中name商品
bool delete_from_list(char name[]);
//显示当前库存列表,包括商品名及其库存量
void show_goods();//显示所有既有商品
void find_goods(char* name);
///外围函数
void AddGoods();//新增商品
void IncreseCount();//增加数量
void AddToList();//生成表
void DeleteGoods();//删除商品
void DecreaseCount();//减少数量
void DeleteFromList();//生成表
void ShowGoods();//展示表
void FindGoods();//查找对应商品
void EscGoods();//结束程序
//查看仓库中的name商品
//main.cpp
#include <string>
#include <iostream>
#include <fstream>
#include <list>
#include<conio.h>
#include "Project1.h"
using namespace std;

int main()
{
	int t;
	char _t;
	PrintMenu();//打印操作菜单栏
	for (;;) {
		cin >> _t;
		t = _t - '0';//switch 仅识别int
		cin.ignore();//清除缓冲区
		switch (t) {
			case 1:AddGoods(); 
				break;
			case 2:IncreseCount();
				break;
			case 3:AddToList();
				break;
			case 4:DeleteGoods();
				break;
			case 5:DecreaseCount();
				break;
			case 6:DeleteFromList();
				break;
			case 7:ShowGoods();
				break;
			case 8:FindGoods();
				break;
			case 9:EscGoods();
				break;
			default:
				cout << "wrong operation\n"<<"please reenter an integer\n";
				break;
		}
		
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值