C++ 仓库管理系统 控制台

下载该exe程序

需求分析

随着经济的发展和社会的进步,人民对物质生活与精神生活的要求也逐日高涨。对生产、生活条件越来越要求方便、舒适、高效、安全以及环保节能¬_¬仓库管理系统应广大人民的强烈需求应运而生。实现了基本的智能管理存储商品名称和库存量的功能O_O

文件结构

文件结构

开发环境

Windows 10 + Visual Studio 2017 Cummunity

功能

  1. 用户注册、登录、修改密码、独立仓库
  2. 商品进货、出货、展示、查询、清空
  3. 文件存取

异常处理

  1. 限制数量输入为9位正整数, 拦截字母或小数的输入
  2. 商品名称只取前100位避免越界
  3. 商品数量限度999999999, 避免进货时超过
  4. 菜单选择异常处理, 避免字母或小数或多位数的错误输入
  5. 检查只有回车的空密码

源码

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;
}

下载该exe程序

  • 42
    点赞
  • 151
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
公司库存管理系统程序中,系统要求我们设计几个类TV类,DVD类带DVD的TV类,其中带DVD的电视机的售价为普通电视机和DVD单价之和的80%。 在这个系统中我们需要实现的功能有:信息的录入,按品牌名的显示,添加物品,删除物品,查找信息,保存数据信息的功能。 公司库存管理系统主要的作用是帮助管理员有条不紊的管理整个仓库(物品数目的清点);当有物品被顾客相中,购买之后,立即将其信息从仓库记录中消除(删除);当顾客想要查看改仓库到底有哪些物品的,管理员可以非常迅速将其一切信息高速顾客(查询);就是当管理员要进货时,可以通过运行本系统,知道此类货物具体放在那一块;还有当顾客想要查找某一物品的时候,顾客可以输入这一物品的名称然后查看这一物品的详细信息。 此外,在这个系统中,顾客还可以进行留言,以及查看其他顾客的评价! 1. 总体设计 在公司库存管理系统中: 2.1物品的主要信息有:品牌名,型号,数量,价格,生产厂商,使用寿命,生产日期! 2.2主要实现的功能有: 1. 物品的清点:看仓库有哪些物品,可以分无别类的统计查看 2. 管理员还可以对货物进行查询:这块功能主要是帮助管理员管理仓库,管理员可以输入一个具体的信息,来查看仓库是否具有该物品 3. 管理员可以对某些已经的陈旧的没用的物品进行删除:管理员可以输入想要从仓库中清楚的物品,然后便可将其删除(当然这里也包括了,当顾客只买了单件的时候(TV或者是DVD的时候,数据信息的删除) 4. 管理员可以进货:这就是添加功能,当某一类在仓库中没有的时候,管理员可以输入信息,然后讲改类物品添加进仓库 5. 其他的就还有一些是客户可以进行的一些基本操作 下订单以及计算 计算:当决定同时购买DVD跟TV的时候,价格是普通电视机和DVD单价之和的80% 购买:当顾客确定购买时,就意味着该件物品已从仓库中清除了,这时就会执行删除指令 评价以及查看他人留言:当顾客逛完了之后,可以留下意见;当对于买某物品犹豫不决时可以查看以往他人留下的评价来帮助自己决定 6. 最后就是保存功能:当数据源发生改变是,仓库里面的信息也会相应的发生改变,这就是保存

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值