C++ 仓库管理系统 控制台
生活随笔
收集整理的這篇文章主要介紹了
C++ 仓库管理系统 控制台
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下載該exe程序
需求分析
隨著經濟的發展和社會的進步,人民對物質生活與精神生活的要求也逐日高漲。對生產、生活條件越來越要求方便、舒適、高效、安全以及環保節能?_?倉庫管理系統應廣大人民的強烈需求應運而生。實現了基本的智能管理存儲商品名稱和庫存量的功能O_O
文件結構
開發環境
Windows 10 + Visual Studio 2017 Cummunity
功能
異常處理
源碼
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) { //Menusystem("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;elsecout << "\nUnknown user name or bad password.\n";}else if (ch == "2") {system("cls");if (con.increase())cout << "\nSuccessfully register.\n";elsecout << "\nUsername already exists\n";}else if (ch == "3") {system("cls");if (con.changepassword())cout << "\nSuccessfully change password.\n";elsecout << "\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);}elsegoodsInfo.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, countchar 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 onceclass 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; }下載該exe程序
總結
以上是生活随笔為你收集整理的C++ 仓库管理系统 控制台的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英语听说计算机查分,提醒:今日英语听说考
- 下一篇: 深圳绿色建筑数量和规模居全国榜首 建筑人