//商场类public class Goods {
int no; //编号 String name; //商品名称 double price; //商品价格 int number; //商品数量 //初始化数据方法 public void setData(int iNo, String sName, double dPrice, int iNumber) { no = iNo; name = sName; price = dPrice; number = iNumber; } // 按格式输出 public void showGoods() { System.out.println(no + "\t\t" + name + (name.getBytes().length > 10 ? "\t" : "\t\t") + price + "\t\t" + number); } }
//用户类public class User { String username; //用户名 String password; //密码 String nickName; //昵称// 购物车 Goods[] cart = new Goods[10]; //显示用户信息方法 public void showInfo() { System.out.println("用户名:\t" + username); System.out.println("密码:\t" + password); System.out.println("昵称:\t" + nickName); } //购买商品 --> 传入商品参数 public void buyGoods(Goods goods) { // 1.购物车中没有该商品 // 2.购物车中有了该商品 int index = searchGoodsByName(goods.no); if (index != -1) { // 购物车已经有了该商品,直接增加数量 cart[index].number += goods.number; } else { // 没有呢,就把他放入 第一个为null的位置 for (int i = 0; i < cart.length; i++) { if (cart[i] == null) { cart[i] = goods; return; } } System.out.println("商品已满,请清空购物车后,在来购买"); } } //查找购物车是否有相同的商品 public int searchGoodsByName(int goodsNo) { for (int i = 0; i < cart.length; i++) { if (cart[i] == null) return -1; else if (goodsNo == cart[i].no) { return i; } } return -1; } //显示商品 public void showGoods() { System.out.println("商品编号\t\t商品名称\t\t商品价格\t\t库存"); for (int i = 0; i < cart.length; i++) { if (cart[i] == null) return; cart[i].showGoods(); //调用商场的显示方法 显示商品 } } // 返回购物车中需要的金额 public double getGoodsMoney() { double sum = 0; for (int i = 0; i < cart.length; i++) { if (cart[i] == null) break; double value = cart[i].price * cart[i].number; sum += value; } return sum; } // 清空购物车 public void clearCart() { for (int i = 0; i < cart.length; i++) { cart[i] = null; } } }
import java.util.Scanner; //商场系统 public class ShoppingManager { //用户数组 User[] users = new User[10]; //商品数组 Goods[] goods = new Goods[10]; Scanner input = new Scanner(System.in); //当前登录用户 User loginUser; // 显示menu之前,先初始化数据 public void init() { goods[0] = new Goods(); goods[0].setData(10101, "海尔冰箱", 3999.9, 50); goods[1] = new Goods(); goods[1].setData(10102, "格力冰箱", 2888.8, 50); goods[2] = new Goods(); goods[2].setData(10103, "TCL冰箱", 3333.0, 100); goods[3] = new Goods(); goods[3].setData(10104, "美的冰箱", 5100.5, 80); goods[4] = new Goods(); goods[4].setData(10105, "海尔空调", 4622, 50); goods[5] = new Goods(); goods[5].setData(10106, "格力空调", 1555, 70); goods[6] = new Goods(); goods[6].setData(10107, "TCL空调", 2100.5, 30); goods[7] = new Goods(); goods[7].setData(10108, "美的空调", 3355.5, 150); goods[8] = new Goods(); goods[8].setData(10109, "小天鹅洗衣机", 2500, 30); goods[9] = new Goods(); goods[9].setData(10110, "西门子冰箱", 3800, 80); // 预存用户 测试用的 User user = new User(); user.nickName = "李四"; user.username = "lisi"; user.password = "123456"; users[0] = user; } //显示商场菜单 public void showMenu() { // 初始化商品 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 void userRegister() { 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 boolean addUser(String username, String password, String nickname) { User user = new User(); 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 boolean isDuplicationName(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 void userLogin() { 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(); //用户菜单 } } public User 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)) { return users[i]; } } return null; } // 用户登录后的菜单 public void userMenu() { 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 void showUserInfo() { loginUser.showInfo(); } //显示购物车 private void showMyGoods() { 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 < loginUser.getGoodsMoney()) { System.out.println("输入的金额不够啊!"); } else { double zero = money - loginUser.getGoodsMoney(); System.out.println("恭喜你付款成功,找您" + zero + "元"); loginUser.clearCart(); } } } private void buyGoods() { 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 = new Goods(); good.setData(goods[index].no, goods[index].name, goods[index].price, number); loginUser.buyGoods(good); System.out.println("购买成功"); } else { System.out.println("商品不够了。。。"); } } } } // 显示购物车 showMyGoods(); } //显示商品列表 public void showGoodsList() { 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(); } } // 判断购买的商品在数组中的位置 /** * * @param no * 输入的商品编号 * @return 找到 返回索引号,没有找到返回-1 */ public int getGoodsIndex(String no) { // 将编号转成int // int iNo = Integer.parseInt(sNo); for (int i = 0; i < goods.length; i++) { if ((goods[i].no + "").equals(no)) { return i; } } return -1; } }
//测试类 public class Test { public static void main(String[] args) { ShoppingManager manager = new ShoppingManager(); manager.showMenu(); } }
时间: 2024-09-30 07:48:04