下载该exe程序
需求分析
随着经济的发展和社会的进步,人民对物质生活与精神生活的要求也逐日高涨。对生产、生活条件越来越要求方便、舒适、高效、安全以及环保节能¬_¬仓库管理系统应广大人民的强烈需求应运而生。实现了基本的智能管理存储商品名称和库存量的功能O_O
文件结构
开发环境
Windows 10 + Visual Studio 2017 Cummunity
功能
- 用户注册、登录、修改密码、独立仓库
- 商品进货、出货、展示、查询、清空
- 文件存取
异常处理
- 限制数量输入为9位正整数, 拦截字母或小数的输入
- 商品名称只取前100位避免越界
- 商品数量限度999999999, 避免进货时超过
- 菜单选择异常处理, 避免字母或小数或多位数的错误输入
- 检查只有回车的空密码
源码
main.cpp
#include<iostream>
#include<stdio.h>
#include<string>
#include<conio.h>
#include<Windows.h>
#include"cilent.h"
using namespace std;
int main() {
printMark();
cout << "\nLoading...";
Sleep(1000);
userInterface();
cout << "\nSuccessfully login.\n\nLoading...";
Sleep(1000);
Read();
while (1) { //Menu
system("cls");
printMark();
printMenu();
string ch;
cin >> ch;
cin.ignore();
if (ch == "1") AddGoods();
else if (ch == "2") DeleteGoods();
else if (ch == "3") ShowGoods();
else if (ch == "4") FindGoods();
else if (ch == "5") Empty();
else if (ch == "6") SaveAndExit();
else cout << "Error command!\n";
cout << "\nPress any key to return.";
_getch();
}
return 0;
}
cilent.h
#pragma once
//一点装饰品
void printMark();
//菜单
void printMenu();
//进货
void AddGoods();
//出货
void DeleteGoods();
//显示库存
void ShowGoods();
//查询货物
void FindGoods();
//清空仓库
void Empty();
//读取文件
void Read();
//存入文件并退出
void SaveAndExit();
//显示用户界面
void userInterface();
cilent.cpp
#include<iostream>
#include<string>
#include<string.h>
#include<conio.h>
#include<stdio.h>
#include"cilent.h"
#include"manage.h"
#include"user.h"
using namespace std;
manage user;
control con;
int inputNum() {
char number[20] = { 0 };
int Number = 0;
while (!Number) {
std::cout << "Only 0 < integer < 1000000000 available\n";
std::cin >> number;
if (strlen(number) > 9)
continue;
int flag = 0;
for (int j = 0; j < strlen(number); j++)
if (number[j] < '0' || number[j] > '9') {
flag = 1;
break;
}
if(flag)
continue;
Number = atoi(number);
}
return Number;
}
char* inputName() {
char* Name=new char[100]();
int j = 0;
while (Name[0] == 0 || Name[0] == ' ') {
if (j++)
cout << "Error blank. Please retype:\n";
cin.getline(Name, 100);
}
return Name;
}
void printMark() {
cout << "V2.0 (C)2017 Zhang Jiqi,SYSU. All rights reserved.\n\n";
string Mark = "Warehouse Management System";
string spaces(Mark.size() + 2, ' ');
string decor(Mark.size() + 4, '*');
cout << decor << "\n*" << spaces << "*\n* " << Mark << " *\n*" << spaces << "*\n" << decor << "\n\n";
}
void printMenu() {
cout << "1. Add Goods\n\n2. Delete Goods\n\n3. Show Goods\n\n4. Find Goods\n\n5. Empty Warehouse\n\n6. Exit\n\n\n";
cout << "Input number to choose:";
}
void AddGoods() {
system("cls");
printMark();
std::cout << "*** Add Goods ***\n\nPlease enter the name:\n";
char*name = inputName();
std::cout << "Please enter the number:\n";
int number = inputNum();
user.add_goods(name, number);
delete[]name;
}
void DeleteGoods() {
system("cls");
printMark();
cout << "*** Delete Goods ***\n\nPlease enter the name:\n";
char*name = inputName();
cout << "Please enter the number:\n";
int number = inputNum();
user.delete_goods(name, number);
delete[]name;
}
void ShowGoods() {
system("cls");
printMark();
std::cout << "*** Show Goods ***\n\n";
user.show_goods();
}
void FindGoods() {
system("cls");
printMark();
std::cout << "*** Find Goods ***\n\nPlease enter the name:\n";
char*name = inputName();
user.find_goods(name);
delete[]name;
}
void Empty() {
system("cls");
printMark();
user.empty();
cout << "Successfully empty warehouse\n";
}
void Read() {
user.read(con.current);
}
void SaveAndExit() {
user.save_and_exit(con.current);
}
void userInterface() {
con.read();
int flag = 0;
while (1) {
system("cls");
cout << "\n1. Login\n\n2. Register\n\n3. Change Password\n\n4. Exit\n\n\n";
cout << "Input number to choose:";
string ch;
cin >> ch;
cin.ignore();
if (ch == "1") {
system("cls");
if (con.check())
break;
else
cout << "\nUnknown user name or bad password.\n";
}
else if (ch == "2") {
system("cls");
if (con.increase())
cout << "\nSuccessfully register.\n";
else
cout << "\nUsername already exists\n";
}
else if (ch == "3") {
system("cls");
if (con.changepassword())
cout << "\nSuccessfully change password.\n";
else
cout << "\nUnknown user name or bad current password.\n";
}
else if (ch == "4") {
flag = 1;
break;
}
else cout << "\nError command!\n";
cout << "Press any key to return.";
_getch();
}
con.save();
if (flag)
exit(0);
}
user.h
#pragma once
#include<list>
using namespace std;
class User {
private:
string username;
string password;
public:
User(string Username, string Password);
const string getusername() const;
const string getpassword() const;
void changepassword(string change);
};
class control {
private:
list<User> users;
list<User>::iterator match(string Username);
public:
bool check();
bool changepassword();
bool increase();
void read();
void save();
string current;
};
user.cpp
#include"user.h"
#include<list>
#include<string>
#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include<fstream>
using namespace std;
string Password() {
char password[100]="\0";
int index = 0;
while (1) {
char ch;
ch = _getch();
if (ch == 8) {
if (index != 0) {
std::cout << char(8) << " " << char(8);
index--;
}
}
else if (ch == '\r') {
password[index] = '\0';
cout << endl;
if(password[0]!=0)
break;
}
else {
std::cout << "*";
password[index++] = ch;
}
}
string tem(password);
return tem;
}
list<User>::iterator control::match(string Username) {
list<User>::iterator iter;
for (iter = users.begin(); iter != users.end(); iter++)
if (iter->getusername() == Username)
return iter;
return iter = users.end();
}
bool control::check() {
cout << "Username:\n";
string Username;
cin >> Username;
cout << "Password:\n";
string password(Password());
if (match(Username) == users.end())
return false;
if (match(Username)->getpassword() != password)
return false;
current = Username;
return true;
}
bool control::changepassword() {
cout << "Username:\n";
string Username;
cin >> Username;
cout << "Current Password:\n";
string password(Password());
if (match(Username) == users.end())
return false;
if (match(Username)->getpassword() != password)
return false;
cout << "New Password:\n";
string newpassword(Password());
match(Username)->changepassword(newpassword);
return true;
}
bool control::increase() {
cout << "New Username:\n";
string Username;
cin >> Username;
if (match(Username) != users.end())
return false;
cout << "New Password:\n";
users.push_back(User(Username, Password()));
return true;
}
void control::read() {
ifstream file;
file.open("user", ios::in);
if (file.is_open()) {
while (file.peek() != EOF) {
string username, password;
getline(file, username);
getline(file, password);
users.push_back(User(username, password));
}
file.close();
}
}
void control::save() {
ofstream file("user", ios::trunc);
if (file.is_open()) {
for (list<User>::iterator iter = users.begin(); iter != users.end(); iter++)
file << iter->getusername() << endl << iter->getpassword() << endl;
file.close();
}
else {
cout << "Fail to save accounts!";
system("pause");
}
}
User::User(string Username, string Password) {
username = Username;
password = Password;
}
const string User::getusername() const {
return username;
}
const string User::getpassword() const {
return password;
}
void User::changepassword(string change) {
password = change;
}
manage.h
#pragma once
#include<list>
#include"Goods.h"
using namespace std;
class manage {
private:
list<Goods> goodsInfo;
list<Goods>::iterator match(char name[]);
public:
//自动进货
void add_goods(char name[], int count);
//自动出货
void delete_goods(char name[], int count);
//显示当前库存
void show_goods();
//查看仓库中的name商品
void find_goods(char name[]);
void empty();
//进行文件数据读取
void read(string Name);
//进行文件数据存档并退出
void save_and_exit(string Name);
};
manage.cpp
#include<iostream>
#include<string.h>
#include<string>
#include<list>
#include<iomanip>
#include<fstream>
#include"manage.h"
#include"Goods.h"
using namespace std;
list<Goods>::iterator manage::match(char name[]) {
list<Goods>::iterator iter;
for (iter = goodsInfo.begin(); iter != goodsInfo.end(); iter++)
if (strcmp(iter->getName(), name) == 0)
return iter;
return iter = goodsInfo.end();
}
void manage::add_goods(char name[], int count) {
list<Goods>::iterator iter = match(name);
if (iter != goodsInfo.end()) {
if ((iter->getCount() + count) > 999999999) {
cout << "\nFailed! Overflow the max stock range of 999999999\n";
return;
}
iter->deposite(count);
}
else
goodsInfo.push_back(Goods(name, count));
cout << "\nAdd successfully!\nStock " << count << ", remain " << match(name)->getCount() << ".\n";
}
void manage::delete_goods(char name[], int count) {
list<Goods>::iterator iter = match(name);
if (iter == goodsInfo.end()) {
cout << "\nNo such stock\n";
return;
}
if (iter->getCount() < count) { //出库量越界报错
cout << "\nExceed stock balance\n";
return;
}
cout << "\nDelete successfully!\n";
if (iter->getCount() == count) { //出库量判断是否删除商品
goodsInfo.erase(iter);
cout << "Out of stock\n";
return;
}
iter->withdraw(count);
cout << "Remain " << iter->getCount() << "\n";
}
void manage::show_goods() {
if (goodsInfo.empty()) { //判断是否空仓
cout << "\nEmpty warehouse!\n\n";
return;
}
cout << endl << setw(20) << "Name" << " " << setw(10) << "Count" << endl;
for (list<Goods>::iterator iter = goodsInfo.begin(); iter != goodsInfo.end(); iter++)
cout << setw(20) << iter->getName() << " " << setw(10) << iter->getCount() << endl;
}
void manage::find_goods(char name[]) { //返回以输入字符串为首的匹配的首个结果
list<Goods>::iterator iter = match(name);
if (iter == goodsInfo.end()) {
cout << "\nNo such stock\n";
return;
}
cout << "\nRemain " << iter->getCount() << "\n";
}
void manage::empty() {
goodsInfo.clear();
}
void manage::read(string Name) {
ifstream file;
file.open(Name.c_str(), ios::in);
if (file.is_open()) {
while (file.peek() != EOF) { //逐行分别读取name, count
char name[100];
char Count[10];
file.getline(name, 100);
file.getline(Count, 10);
int count = atoi(Count);
goodsInfo.push_back(Goods(name, count));
}
file.close();
}
}
void manage::save_and_exit(string Name) {
if (goodsInfo.empty()) { //仓库空时删除文件以删除上一次的仓库记录
remove(Name.c_str());
exit(0);
}
ofstream file(Name.c_str(), ios::trunc);
if (file.is_open()) { //name, count各占一行,实现name可带空格
for (list<Goods>::iterator iter = goodsInfo.begin(); iter != goodsInfo.end(); iter++)
file << iter->getName() << endl << iter->getCount() << endl;
file.close();
}
else {
cout << "Fail to save the data!";
system("pause");
}
exit(0);
}
Goods.h
#pragma once
class Goods {
private:
char name[100]; //记录货物名
int count; //记录货物数量
public:
Goods(const char* Name, const int Count);
const char* getName() const;
const int getCount() const;
void withdraw(int amount);
void deposite(int amount);
};
Goods.cpp
#include"Goods.h"
#include<cstring>
Goods::Goods(const char* Name, const int Count) {
strcpy_s(name, Name);
count = Count;
}
const char* Goods::getName() const {
return name;
}
const int Goods::getCount() const {
return count;
}
void Goods::withdraw(int amount) {
count -= amount;
}
void Goods::deposite(int amount) {
count += amount;
}