#include <cstdio>
#include <cstring>
#include <Windows.h>
#include "Interface.h"
COnlineStore onlineStore;
void Main()
{
onlineStore.loadData();
/*onlineStore.goodList.addFood("rice", 100, 2.5, 2, CDate(2015, 6, 15));
onlineStore.goodList.addFood("bread", 100, 5, 2, CDate(2015, 6, 15));
onlineStore.goodList.addCloth("T-shirt", 100, 25, 45);
onlineStore.goodList.addCloth("coat", 100, 50, 45);
onlineStore.userList.addAdmin("admin", "admin", CDate(2015, 6, 15));
onlineStore.userList.addAdmin("admin2", "admin2", CDate(2015, 6, 15));
onlineStore.userList.addGuest("user", "user", CDate(2015, 6, 15));
onlineStore.userList.addGuest("user2", "user2", CDate(2015, 6, 15));*/
while (mainMenu())
{
}
}
bool mainMenu()
{
setTitle("选择登录");
printf("欢迎使用在线商城...\n\n1.管理员登录\n\n2.用户登录\n\n3.用户注册\n\n4.保存并退出\n\n\\> ");
int choice = getChoice(1, 4);
if (choice == -1)
{
printf("\n输入有误,请重试...\n\n");
pause();
return true;
}
int errorTimes = 0;
char userName[MAXUSERNAMELEN] = "";
switch (choice)
{
case 1:
do
{
if (!loginInterface(ADMIN, userName, errorTimes))
return true;
} while (!onlineStore.getLogin());
while (adminMenu())
{
}
break;
case 2:
do
{
if (!loginInterface(GUEST, userName, errorTimes))
return true;
} while (!onlineStore.getLogin());
while (guestMenu())
{
}
break;
case 3:
registerUser(GUEST);
break;
case 4:
onlineStore.saveData();
return false;
}
return true;
}
bool loginInterface(int limite, char *lastUserName, int &errorTimes)
{
setTitle(limite == ADMIN ? "管理员登录" : "用户登录");
printf("请输入账号和密码以登录...\n\n账号: ");
char userName[MAXUSERNAMELEN] = "";
scanf("%s", userName);
printf("\n密码: ");
char passWord[MAXUSERNAMELEN];
scanf("%s", passWord);
if (!onlineStore.userList.isExist(userName))
{
printf("\n账号不存在...\n\n");
pause();
return false;
}
if (onlineStore.userList.getUser(userName)->getLimite() != limite)
{
printf("\n账号权限不匹配...\n\n");
pause();
return false;
}
if (onlineStore.userList.getUser(userName)->getLock())
{
printf("\n账号已锁定...\n\n");
pause();
return false;
}
if (!onlineStore.login(userName, passWord))
{
if (strcmp(lastUserName, userName))
errorTimes++;
else
{
errorTimes = 1;
strcpy(lastUserName, userName);
}
if (errorTimes < 3)
{
printf("\n密码错误...\n\n");
pause();
}
else
{
onlineStore.userList.getUser(userName)->setLock();
printf("\n密码错误次数过多,账号已被锁定...\n\n");
pause();
return false;
}
}
return true;
}
bool adminMenu()
{
setTitle("管理员界面");
printf("欢迎管理员: %s 使用在线商城...\n\n1.商品管理\n\n2.用户管理\n\n3.账号信息\n\n4.修改密码\n\n5.注销\n\n\\> ", ((CUser *)onlineStore.getCrtUser())->getUserName());
int choice = getChoice(1, 5);
if (choice == -1)
{
printf("\n输入有误,请重试...\n\n");
pause();
return true;
}
switch (choice)
{
case 1:
while (goodsMenu(((CUser *)onlineStore.getCrtUser())->getLimite()))
{
}
break;
case 2:
while (usersMenu())
{
}
break;
case 3:
userInfo(ADMIN);
break;
case 4:
changePassWord();
break;
case 5:
onlineStore.logout();
return false;
}
return true;
}
bool guestMenu()
{
setTitle("用户界面");
printf("欢迎用户: %s 使用在线商城...\n\n1.商品信息\n\n2.账号信息\n\n3.修改密码\n\n4.注销\n\n\\> ", ((CGuest *)onlineStore.getCrtUser())->getUserName());
int choice = getChoice(1, 4);
if (choice == -1)
{
printf("\n输入有误,请重试...\n\n");
pause();
return true;
}
switch (choice)
{
case 1:
while (goodsMenu(((CGuest *)onlineStore.getCrtUser())->getLimite()))
{
}
break;
case 2:
userInfo(GUEST);
break;
case 3:
changePassWord();
break;
case 4:
onlineStore.logout();
return false;
}
return true;
}
bool goodsMenu(int limite)
{
setTitle("");
char title[MAXTITLELEN] = "商品";
if (limite == ADMIN)
{
strcat(title, "管理");
printf("请选择以...\n\n1.浏览商品\n\n2.查询商品\n\n3.新增商品\n\n4.修改商品\n\n5.删除商品\n\n6.返回\n\n\\> ");
}
else if (limite == GUEST)
{
printf("请选择以...\n\n1.浏览商品\n\n2.查询商品\n\n3.购买商品\n\n4.返回\n\n\\> ");
strcat(title, "信息");
}
strcat(title, "界面");
setTitle(title, false);
int choice = getChoice(1, 6);
if (limite == GUEST && choice == 3)
choice = 7;
else if (limite == GUEST && choice == 4)
choice = 6;
else if (limite == GUEST && choice > 4)
choice = -1;
if (choice == -1)
{
printf("\n输入有误,请重试...\n\n");
pause();
return true;
}
switch (choice)
{
case 1:
enumGoods();
break;
case 2:
checkGood();
break;
case 3:
while (addGood())
{
}
break;
case 4:
while (changeGood())
{
}
break;
case 5:
delGood();
break;
case 6:
return false;
break;
case 7:
buyGood();
}
return true;
}
void enumGoods()
{
int i;
setTitle("浏览商品");
printf("以下列出商品清单...\n\n");
int foodcut = 0, clothcut = 0;
void *foods[MAXGOODNUM], *cloths[MAXGOODNUM];
memset(foods, 0, sizeof(foods));
memset(cloths, 0, sizeof(cloths));
onlineStore.goodList.enumGoods(foodcut, foods, clothcut, cloths);
printf("食物 %d 种:\n\n编号\t名称\t数量\t价格\t保质期(月)\t生产日期\t保质日期\n", foodcut);
for (int i = 0; i < 72; i++) printf("=");
printf("\n");
for ( i = 0; i < foodcut; i++)
{
CFood *food = (CFood *)foods[i];
printf("%d\t%s\t%d\t%.2lf\t%d\t\t%d/%d/%d\t%d/%d/%d\n", food->getId(), food->getName(), food->getCount(), food->getPrice(), food->getProtectLenth(), food->getProductTime().getYear(), food->getProductTime().getMonth(), food->getProductTime().getDay(), food->getProtectTime().getYear(), food->getProtectTime().getMonth(), food->getProtectTime().getDay());
}
printf("\n\n\n");
printf("衣服 %d 种:\n\n编号\t名称\t数量\t价格\t尺寸\n", clothcut);
for ( i = 0; i < 36; i++) printf("=");
printf("\n");
for ( i = 0; i < clothcut; i++)
{
CCloth *cloth = (CCloth *)cloths[i];
printf("%d\t%s\t%d\t%.2lf\t%d\n", cloth->getId(), cloth->getName(), cloth->getCount(), cloth->getPrice(), cloth->getSize());
}
printf("\n");
pause();
}
void checkGood()
{
setTitle("查询商品");
printf("请输入所需要查询的商品名称以查询...\n\n名称: ");
char name[MAXGOODNAMELEN];
scanf("%s", name);
if (!onlineStore.goodList.isExist(name))
printf("\n查询无此商品...\n\n");
else if (onlineStore.goodList.getGood(name)->getType() == FOOD)
{
printf("\n查询结果: 食物类\n\n编号\t名称\t数量\t价格\t保质期(月)\t生产日期\t保质日期\n");
for (int i = 0; i < 72; i++) printf("=");
CFood *food = onlineStore.goodList.getFood(name);
printf("\n%d\t%s\t%d\t%.2lf\t%d\t\t%d/%d/%d\t%d/%d/%d\n\n", food->getId(), food->getName(), food->getCount(), food->getPrice(), food->getProtectLenth(), food->getProductTime().getYear(), food->getProductTime().getMonth(), food->getProductTime().getDay(), food->getProtectTime().getYear(), food->getProtectTime().getMonth(), food->getProtectTime().getDay());
}
else if (onlineStore.goodList.getGood(name)->getType() == CLOTH)
{
printf("\n查询结果: 衣服类\n\n编号\t名称\t数量\t价格\t尺寸\n");
for (int i = 0; i < 36; i++) printf("=");
CCloth *cloth = onlineStore.goodList.getCloth(name);
printf("\n%d\t%s\t%d\t%.2lf\t%d\n\n", cloth->getId(), cloth->getName(), cloth->getCount(), cloth->getPrice(), cloth->getSize());
}
pause();
return;
}
bool addGood()
{
setTitle("新增商品");
printf("请选择所需要新增商品的类型...\n\n商品类型:\n\n1.食物\n\n2.衣服\n\n3.返回\n\n\\> ");
int type = getChoice(1, 3);
if (type =