美文网首页
MySQL项目:电子购书系统

MySQL项目:电子购书系统

作者: 虞锦雯 | 来源:发表于2017-06-16 17:12 被阅读49次

电子购书系统是针对传统销售方式中管理不便与效率低的缺点,将电子商务技术和计算机技术结合起来,开发出管理更便利、效率更高的系统。主要用于对库存书籍信息、会员信息和会员购买记录的管理。
学习了C++和MySQL之后,我做了一个购书系统。该系统主要分为两个模块:

  1. 前台:用户操作模块
  1. 后台:管理员操作模块

在该系统中,所需实现的基本功能有:

  1. 后台的图书操作
    管理员可以对图书信息进行增、删、改、查的操作。
    管理员可以添加会员信息。会员的级别有2种,分为普通会员和vip会员。
    会员的账户ID在生成会员信息时自动生成,每输入一个人员信息编号顺序加1,人员基数为1000。
  2. 前台用户的操作
    普通用户购买书籍满100元时,可以打9.5折,满150可以包邮,否则邮费10元。
    VIP用户买书直接打9折,当满100时包邮,否则付邮费10元。
  3. 存储书籍、会员信息;并且用户可以察看自己的购买记录。
    后台管理员可以查看所有用户的购买记录。
  4. 要求使用容器来进行存储,管理。

整个系统中,我们需要用到三张表:书籍信息表、会员信息表、购书记录表。
整个系统操作中,最重要的是信息的读取与存储。系统开始运行时,需要将数据库中的数据读出来,存到容器中;退出系统时,同样需要把容器中的数据保存到数据库中去。还有就是删除vector中的元素,如果直接erase会出现问题。
首先,我们需要在数据库中建立三张表格

//创建图书表
create table book(
bookname varchar(100) not null,
author varchar(20) not null,
publisher varchar(40) not null,
money double not null,
bookcount int not null)
default charset = 'utf8';

//创建会员表
create table member(
id int auto_increment not null unique,
name varchar(20) not null,
password varchar(20) not null,
rank varchar(10) not null,
phone varchar(20) not null,
address varchar(100) not null) 
default charset = 'utf8';

//创建购书记录表
create table record(
id int not null,
bookname varchar(100) not null,
buynum int not null,
discount double default 0,
price double not null) 
default charset = 'utf8';

下面是以该项目中对书籍信息的操作为例,列出来的部分代码。

  • 以下为Book类和BookManage类的声明
//"data.h"
class Book{
    public:
        Book(){};
        Book(string bookname,string auther,string publisher,
             double money,int bookcount);
        string bookname; //书名
        string author; //作者
        string publisher; //出版社
        double money; //价格
        int bookcount; //数量
};

//"manager.h"
class BookManage{
    public:
        static vector<Book> vBook;
};
  • 以下为读取数据函数
    将数据库中保存的表中的数据读取到容器中
#include <stdlib.h>
#include <iostream>
#include <string>
#include <mysql/mysql.h>
#include "data.h"
#include "manager.h"
using namespace std;
vector<Book> BookManage::vBook;

int mysql_to_v()
{
    MYSQL * conn = NULL;
    MYSQL_RES * res = NULL;
    MYSQL_ROW row = NULL;
    conn = mysql_init(NULL);
    if(conn == NULL){
        cout << "mysql_init failed!" << endl;
        return -1;
    }
    const char *host = "localhost";
    const char *user = "root";
    const char *pwd = "1";
    const char *db = "qreal";
    conn = mysql_real_connect(conn,host,user,pwd,db,0,NULL,0); //连接
    if(conn == NULL){
        cout << mysql_error(conn) << endl;
        return -1;
    }
    mysql_set_character_set(conn,"utf8");

    mysql_query(conn,"select * from book");
    res = mysql_use_result(conn); //获得结果集
    if(res == NULL){
        cout << "mysql_use_result failed!" <<endl;
        return -1;
    }
    Book s1;
    while((row = mysql_fetch_row(res)) != NULL){
        s1.bookname = row[0];
        s1.author = row[1];
        s1.publisher = row[2];
        s1.money = atof(row[3]);
        s1.bookcount = atoi(row[4]);
        BookManage::vBook.push_back(s1);
    }
    mysql_free_result(res); //释放结果集
    mysql_close(conn);

    return 0;
}
  • 以下为保存数据函数
    将容器中的数据重新保存到数据库中去。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <mysql/mysql.h>
#include "data.h"
#include "manager.h"
using namespace std;
int v_to_mysql()
{
    MYSQL * conn =NULL;
    conn = mysql_init(NULL);
    if(conn == NULL){
        cout << "mysql_init failed!" << endl;
    }
    const char *host = "localhost";
    const char *user = "root";
    const char *pwd = "1";
    const char *db = "qreal";
    conn = mysql_real_connect(conn,host,user,pwd,db,0,NULL,0);
    if(conn == NULL){
        cout << mysql_error(conn) << endl;
        return -1;
    }
    mysql_set_character_set(conn,"utf8");

    char buf[1024] = {'\0'};

    mysql_query(conn,"delete from book");
    vector<Book>::iterator bit = BookManage::vBook.begin();
    while(bit != BookManage::vBook.end()){
        sprintf(buf,"insert book values('%s','%s','%s','%f','%d');"
                ,bit->bookname.c_str()
                ,bit->author.c_str()
                ,bit->publisher.c_str()
                ,bit->money
                ,bit->bookcount
                );
        mysql_query(conn,buf);
        bit++;
    }
    mysql_close(conn);

    return 0;
}
  • 删除vector中的指定元素
void Delete(){
    string bookname;
    cout << "请输入需删除的书名:";
    cin >> bookname;
    vector<Book>::iterator it = vBook.begin();
    if(vBook.empty()){
        cout << "暂无书籍信息!" << endl;
        return ;
    }
    int flag = 0;
    for(it;it != vBook.end();)
    {
        if(bookname == it->bookname){
            it = vBook.erase(it);
            flag = 1;
            cout << "删除成功!" << endl;
        }
        else{
            it++;
        }
    }
    if(0 == flag)
    {
        cout << "未找到该书信息!" << endl;
    }
}

本系统中我一共封装了8个类(class),图书(Book)类、会员(Member)类、购书记录(Record)类(这三个类主要是分别保存图书、会员已经购书记录的信息)、图书管理(BookManage)类、会员管理(MemberManage)类、购书记录管理(RecordManage)类(这三个类主要是对图书、会员以及购书记录信息的操作)、前台(Foreground)类(这个类主要是会员查询书籍、购买书籍等操作)以及后台(Background)类(这个类主要是管理员对系统中所有信息的操作)。
本系统还有许都可以改进的地方,比如说VIP用户的自动升级,购书记录的完善等等。

相关文章

网友评论

      本文标题:MySQL项目:电子购书系统

      本文链接:https://www.haomeiwen.com/subject/hzhsqxtx.html