importjava.util.Scanner;//商场系统
public classShoppingManager {//用户数组
User[] users = new User[10];//商品数组
Goods[] goods = new Goods[10];
Scanner input= newScanner(System.in);//当前登录用户
User loginUser;//显示menu之前,先初始化数据
public voidinit() {
goods[0] = newGoods();
goods[0].setData(10101, "海尔冰箱", 3999.9, 50);
goods[1] = newGoods();
goods[1].setData(10102, "格力冰箱", 2888.8, 50);
goods[2] = newGoods();
goods[2].setData(10103, "TCL冰箱", 3333.0, 100);
goods[3] = newGoods();
goods[3].setData(10104, "美的冰箱", 5100.5, 80);
goods[4] = newGoods();
goods[4].setData(10105, "海尔空调", 4622, 50);
goods[5] = newGoods();
goods[5].setData(10106, "格力空调", 1555, 70);
goods[6] = newGoods();
goods[6].setData(10107, "TCL空调", 2100.5, 30);
goods[7] = newGoods();
goods[7].setData(10108, "美的空调", 3355.5, 150);
goods[8] = newGoods();
goods[8].setData(10109, "小天鹅洗衣机", 2500, 30);
goods[9] = newGoods();
goods[9].setData(10110, "西门子冰箱", 3800, 80);//预存用户 测试用的
User user = newUser();
user.nickName= "李四";
user.username= "lisi";
user.password= "123456";
users[0] =user;
}//显示商场菜单
public voidshowMenu() {//初始化商品
init();
String choose= "";do{//打印菜单系统
System.out.println("*****************国美电器*****************");
System.out.println("\t\t1.用户登录");
System.out.println("\t\t2.用户注册");
System.out.println("\t\t3.退出系统");
System.out.println("*****************国美电器*****************");
choose=input.next();switch(choose) {case "1":
userLogin();//登录
break;case "2":
userRegister();//注册
break;case "3":
System.out.println("退出系统成功!");break;default:
System.out.println("输入错误,请重新输入");break;
}
}while (!choose.equals("3"));//退出系统
input.close();
}//用户注册
private voiduserRegister() {
System.out.print("请输入用户名:");
String name=input.next();if(isDuplicationName(name)) {
System.out.println("用户名已存在!注册失败");return;
}//没有重名
System.out.print("请输入密码:");
String password=input.next();if (password.length() < 6) {
System.out.println("密码输入错误!注册失败");return;
}
System.out.print("请输入昵称:");
String nickName=input.next();if(addUser(name, password, nickName)) {
System.out.println("注册成功");
}else{
System.out.println("注册失败,用户已满!");
}
}//添加用户方法,判断用户数组中是否存在该用户
public booleanaddUser(String username, String password, String nickname) {
User user= newUser();
user.username=username;
user.nickName=nickname;
user.password=password;for (int i = 0; i < users.length; i++) {if (users[i] == null) {
users[i]=user;return true;
}
}return false;
}//判断用户名是否存在,返回ture或false
private booleanisDuplicationName(String name) {for (int i = 0; i < users.length; i++) {//为空就跳出循环
if (users[i] == null)break;//有重名
else if(users[i].username.equals(name))return true;
}return false;
}private voiduserLogin() {
System.out.print("请输入用户名:");
String username=input.next();
System.out.print("请输入密码:");
String password=input.next();
User user=login(username, password);if (user == null) {
System.out.println("登录失败!");
}else{//下一级菜单
loginUser =user;
System.out.println("登录成功,你好:" +loginUser.nickName);
userMenu();//用户菜单
}
}publicUser login(String username, String password) {for (int i = 0; i < users.length; i++) {if (users[i] == null)return null;else if(users[i].username.equals(username)&&users[i].password.equals(password)) {returnusers[i];
}
}return null;
}//用户登录后的菜单
public voiduserMenu() {
String choose= "";do{
System.out.println("1.商品购买");
System.out.println("2.我的购物车");
System.out.println("3.个人信息");
System.out.println("4.注销");
choose=input.next();switch(choose) {case "1":
buyGoods();//购买商品
break;case "2":
showMyGoods();//显示购物车
break;case "3":
showUserInfo();//显示用户信息
break;case "4":
System.out.println("注销成功");break;default:
System.out.println("输入错误,请重新输入!");break;
}
}while (!choose.equals("4"));
}private voidshowUserInfo() {
loginUser.showInfo();
}//显示购物车
private voidshowMyGoods() {
System.out.println("********************购物车********************");
loginUser.showGoods();//开始付款
System.out.println("\t\t你购买的商品总价格为:" +loginUser.getGoodsMoney());//是否付款
if (loginUser.getGoodsMoney() == 0)return;
System.out.println("是否付款?Y/N");
String choose=input.next();if (choose.equalsIgnoreCase("y")) {//付款
System.out.println("输入实付金额:");double money =input.nextDouble();if (money
System.out.println("输入的金额不够啊!");
}else{double zero = money -loginUser.getGoodsMoney();
System.out.println("恭喜你付款成功,找您" + zero + "元");
loginUser.clearCart();
}
}
}private voidbuyGoods() {
System.out.println("********************商品购买*********************");
showGoodsList();
String choose= "";while (!choose.equalsIgnoreCase("exit")) {
System.out.print("请输入需要购买的商品编号,输入exit后退出");
choose=input.next();if (!choose.equalsIgnoreCase("exit")) {//购买商品
int index =getGoodsIndex(choose);if (index == -1) {
System.out.println("没有该商品,请重新输入");
}else{
System.out.print("请输入商品数量:");int number =input.nextInt();//判断由没有这么多库存
if (number <=goods[index].number) {//1.商城需要减少数量//2.购物车是增加
goods[index].number -=number;//用户买的商品
Goods good = newGoods();
good.setData(goods[index].no, goods[index].name,
goods[index].price, number);
loginUser.buyGoods(good);
System.out.println("购买成功");
}else{
System.out.println("商品不够了。。。");
}
}
}
}//显示购物车
showMyGoods();
}//显示商品列表
public voidshowGoodsList() {
System.out.println("商品编号\t\t商品名称\t\t商品价格\t\t库存");for (int i = 0; i < goods.length; i++) {
Goods good=goods[i];if (good == null)return;
good.showGoods();
}
}//判断购买的商品在数组中的位置
/***
*@paramno
* 输入的商品编号
*@return找到 返回索引号,没有找到返回-1*/
public intgetGoodsIndex(String no) {//将编号转成int//int iNo = Integer.parseInt(sNo);
for (int i = 0; i < goods.length; i++) {if ((goods[i].no + "").equals(no)) {returni;
}
}return -1;
}
}