Java语言的一个超级简易的商品管理系统,适合初学者练手
源码包无法上传至资源(blog已经超级完整啦)如果还是需要完整源码src包可私分享
1 项目分析
源码
1.1 用户模块(普通用户、管理员用户)
1.1.1前端系统(普通用户)
- 用户登录
- 用户注册
- 用户信息修改
- 用户充值
1.1.2 后台系统(管理员用户)
- 用户登录
- 分配管理员用户
- 管理普通用户
- 删除用户
1.2 商品模块
1.2.1 前端系统(普通用户)
- 查询
- 搜索
- 购买
1.2.2 后台系统(管理员用户)
- 商品上架
- 商品下架
- 商品查看
- 商品修改
1.3 订单模块(对订单的增删改查)
- 增加订单
- 删除订单
- 修改订单
- 查询订单
1.4 地址模块(订单的地址的增删改查-当前不考虑)
- 增加地址
- 删除地址
- 修改地址
- 查询地址
…
2 项目设计
新建工程 (注意事项)
- 处理后台程(service包)—用户的增删改查
productmanagementsystem.service
- 实体类即JavaBean(数据承载Bean,不做业务逻辑功能) 约定俗成包名为pojo
productmanagementsystem.pojo
- 视图页面-前端页面 view/menu
productmanagementsystem.view
- 客户端(client) —随便叫什么都行client 即main方法
productmanagementsystem.client
3 用户模块实现
3.1 分析
- pojo数据承载实体类JavaBean—按照需求写User模块和Product模块
- service客户端业务处理(用户操作)
- 面向接口设计 interface ProductService 商品增删改查方法
用户接口实现 UserServiceImplimplementsUserService
- 重写UserService所有方法
- 登录 注册 充值 查询 修改 删除 各个方法提供判断
3.2 代码实现
3.2.1 Product
package priv.yinying.productmanagementsystem.pojo; import java.util.Date; /** * @author yinying * @Package priv.yinying.productmanagementsystem.pojo * @Description: * @Date 2023/1/31 22:37 */ public class Product { private int id; private String name; private double price; private Date createTime; // 上架日期 private long num; //库存 public Product() { } public Product(int id, String name, double price, Date createTime, long num) { this.id = id; this.name = name; this.price = price; this.createTime = createTime; this.num = num; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public long getNum() { return num; } public void setNum(long num) { this.num = num; } @Override public String toString() { return "Product{" + "id=" + id + ", name='" + name + '\'' + ", price=" + price + ", createTime=" + createTime + ", num=" + num + '}'; } }
3.2.2 user
package priv.yinying.productmanagementsystem.pojo; /** * @author yinying * @Package priv.yinying.productmanagementsystem.pojo * @Description: * @Date 2023/1/31 22:10 */ public class User { private int id; //表示用户的唯一信息 private String username; private String password; private double money; //余额 private int state; //状态 -用户的角色(0、表示管理员 1、普通用户) //正式开发还需要账户模块(一个人可以有多个账户) //private Order[] order; //用户对应的订单 //private Address[] address; //用户对应的地址-一个用户可以有多个地址 //JavaBean 三连setget方法 有参构造 无参构造 public User() { } public User(int id, String username, String password, double money, int state) { this.id = id; this.username = username; this.password = password; this.money = money; this.state = state; } //toString方法可以提供也可以不提供 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public int getState() { return state; } public void setState(int state) { this.state = state; } } // public User() { // super(); // } // public User(int id, String username, String password, double money, int state) { // super(); // this.id = id; // this.username = username; // this.password = password; // this.money = money; // this.state = state; // }
3.2.3 ProductService
package priv.yinying.productmanagementsystem.service; import priv.yinying.productmanagementsystem.pojo.Product; /** * @author yinying * @Package priv.yinying.productmanagementsystem.service * @Description: * @Date 2023/1/31 22:50 */ //查询搜索 增删改查 public interface ProductService { //查询所有 Product[] selectAll(); //搜索商品(可以重复) Product[] search(String name); //购买商品 boolean buyProduct(int id,int num); //id 数量 库存(已知有的) 全局变量-后面使用static静态变量用户的余额 //商品上架 boolean addProduct(Product product); //商品下架 boolean deleteProduct(int id); //商品修改 boolean updateProduct(Product product); //(先查再改) //查看商品详情-返回product Product selectOne(int id); //id唯一标识 }
3.2.4 UserService
package priv.yinying.productmanagementsystem.service; import priv.yinying.productmanagementsystem.pojo.User; /** * @author yinying * @Package priv.yinying.productmanagementsystem.service * @Description: * @Date 2023/1/31 23:06 */ public interface UserService { //用户登录(成功返回user 失败返回null) // User login(String username,String password); // Missing method body, or declare abstract缺少方法体,或声明抽象 User login(String username,String password); //用户名、密码 //查询用户详情 User selectOne(int id); //用户注册 boolean register(String username,String password); //用户信息修改 boolean updateUser(User user); //用户充值 boolean topup(double money); //删除用户 boolean deleteUser(int id); }
3.2.5 UserServiceImpl
package priv.yinying.productmanagementsystem.service; import priv.yinying.productmanagementsystem.pojo.User; import priv.yinying.productmanagementsystem.view.UserView; import java.util.Arrays; /** * @author yinying * @Package priv.yinying.productmanagementsystem.service * @Description: * @Date 2023/1/31 23:41 */ public class UserServiceImpl implements UserService { static User [] users = new User[1]; static int count = 1; //数组添加变量-有效个数 增删改查下标从1开始 static int id = 1; static{ //初始化管理员账户--只执行一次 users [0] = new User(id,"admin","123",0,0); } //登录方法 @Override public User login(String username,String password) { //判断用户是否存在 int index = exists(username); if(index != -1){ //存在 // 判断密码是否正确 if(users[index].getPassword().equals(password)){ return users[index]; } } return null; } //判断用户是否存在 public int exists(String username){ //返回int类型下标,或者Boolean类型也可以 for (int i = 0; i < count; i++) { if (username.equals(users[i].getUsername())){ return i; } } return -1; } //注册方法 @Override public boolean register(String username, String password) { //判断用户是否存在 int index = exists(username); if(index == -1) { //不存在 //判断数组是否需要扩容 if(users.length == count) { users = Arrays.copyOf(users, users.length + 5); } //将用户信息添加到数组中 users[count] = new User(++id,username,password,0,1);//id先加再用 //数组中有效个数+1 count++; //注册成功 return true; } return false; } //查询 @Override public User selectOne(int id) { for (int i = 1; i < count; i++) { if(id == users[i].getId()) { //遍历id存在 return users[i]; } } return null; //用户不存在没有 } //修改 @Override public boolean updateUser(User user) { //覆盖 user[i]=user int index = 0; //一定存在,先存在再修改 for (int i = 1; i < count; i++) { if(users[i].getId() == user.getId()) { index = i; break; } } users[index] = user; return true; //一定存在, } //充值 @Override public boolean topup(double money) { User user = UserView.user; user.setMoney(user.getMoney()+money); updateUser(user); return false; } //删除 @Override public boolean deleteUser(int id) { int index = -1; for (int i = 1; i < count; i++) { if(users[i].getId() == id) { index = i; break; } } //扩容是考虑这种情况:数组长度==有效个数 (避免临界值存在) users = Arrays.copyOf(users, users.length+5); if(index != -1) { //首先id存在,删除要覆盖,后一个赋给前一个 for (int i = index; i <= count; i++) { users[i] = users[i+1]; } return true; } return false; } }
4 用户界面实现
4.1 分析
- view 用户页面userview
- 登录菜单
- 调用方法实现用户界面【1、登录 2、注册 3、修改用户 4、用户充值 5、退出登录 6、显示用户 7、退出】功能
- 测试
4.2 代码实现
4.2.1 userview
package priv.yinying.productmanagementsystem.view; import priv.yinying.productmanagementsystem.pojo.User; import priv.yinying.productmanagementsystem.service.UserService; import priv.yinying.productmanagementsystem.service.UserServiceImpl; import java.util.Random; import java.util.Scanner; /** * @author yinying * @Package priv.yinying.productmanagementsystem.view * @Description: * @Date 2023/2/1 0:43 */ public class UserView { Scanner sc = new Scanner(System.in); UserService userService = new UserServiceImpl(); //表示登录的用户 public static User user; //首页 public void indexMenu() { System.out.println("***********欢迎使用Java2302商品管理系统***********"); while(true) { System.out.println("1、登录\t2、注册\t3、修改用户\t4、用户充值\t5、退出登录\t6、显示用户\t7、退出"); System.out.println("请输入您的选择"); int option = sc.nextInt(); switch (option) { case 1: loginMenu(); break; case 2: registerMenu(); break; case 3: updateUserMenu(); break; case 4: topupMenu(); break; case 5: user = null; break; case 6: showUser(); break; case 7: System.out.println("欢迎下次使用!!!"); System.exit(0); break; default: System.out.println("输入有误,请重新输入"); break; } } } //用户充值 public void topupMenu() { System.out.println("*********充值页面*********"); //判断是否登录过 if(user == null) { System.out.println("您还未登录,请先登录"); loginMenu(); }else { //判断如果是管理员就不能修改用户信息 if(user.getState() == 0) { System.out.println("您已经是管理员,不能修改用户信息"); }else { System.out.println("请输入充值的金额"); double money = sc.nextDouble(); userService.topup(money); System.out.println("充值成功!!!!"); } } } //修改用户菜单 public void updateUserMenu() { System.out.println("*********修改用户页面*********"); //判断是否登录过 if(user == null) { System.out.println("您还未登录,请先登录"); loginMenu(); }else { //判断如果是管理员就不能修改用户信息 if(user.getState() == 0) { System.out.println("您已经是管理员,不能修改用户信息"); }else { //展示用户的信息(非管理员) showUser(); System.out.println("请输入原密码"); String oldPasswrod = sc.next(); System.out.println("请输入新密码"); String newPasswrod1 = sc.next(); System.out.println("请输入确认信密码"); String newPasswrod2 = sc.next(); if(!oldPasswrod.equals(user.getPassword())) { System.out.println("原密码输入错误,无法修改"); return; } if(!newPasswrod1.equals(newPasswrod2)) { System.out.println("两次密码不一致,无法修改"); return; } user.setPassword(newPasswrod1); userService.updateUser(user); //修改完密码之后要重新登录 user = null; loginMenu(); } } } //展示用户信息 public void showUser() { if(user!=null) { System.out.println("当前用户信息为:"); System.out.println("用户名\t\t密码\t\t用户余额\t\t用户身份"); System.out.println(user.getUsername()+"\t\t"+user.getPassword()+"\t\t"+user.getMoney()+"\t\t"+"普通用户"); //仅仅普通用户 }else { System.out.println("暂未登录!!!"); } } //注册菜单 public void registerMenu(){ System.out.println("*********注册页面*********"); System.out.println("请输入注册用户名"); String username = sc.next(); System.out.println("请输入注册用户密码"); String password = sc.next(); //调用注册方法 boolean flag = userService.register(username, password); if(flag) { System.out.println("注册成功,请先登录"); loginMenu(); //直接进入登录界面 }else { System.out.println("很抱歉,注册失败!!!!"); } } //登录菜单 public void loginMenu() { System.out.println("*********登录页面*********"); System.out.println("请输入用户名"); String username = sc.next(); System.out.println("请输入用户密码"); String password = sc.next(); //随机验证码 String code1 = code(); System.out.println("验证码为:"+code1); System.out.println("请输入验证码"); String code2 = sc.next(); if(code1.equalsIgnoreCase(code2)) { //验证码正确 --(.equalsIgnoreCase 忽略大小写) User loginUser = userService.login(username, password); if(loginUser == null) { System.out.println("用户名或密码不正确"); }else { //登录成功 //进入商品页面 System.out.println("欢迎"+loginUser.getUsername()+"登录成功"); //将登录的用户使用成员变量user保存 user = loginUser; ProductView productView = new ProductView(); //进入商品页面 if(user.getState() == 0) { //menu2页面 productView.menu2(); }else { //menu1页面 productView.menu1(); } } }else { System.out.println("验证码错误"); } } //验证码方法 public String code() { String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; StringBuffer sb = new StringBuffer(); Random r = new Random(); for (int i = 0; i < 4; i++) { int index = r.nextInt(str.length()); sb.append(str.charAt(index)); } return sb.toString(); } }
5 商品模块实现
5.1 分析
- ProductServiceIpl 商品(增删改查)接口
- 重写ProductService所有方法
- 先判断商品是否存在,再进行对商品的增删改查
5.2 代码实现
5.2.1 ProductServiceIpl
package priv.yinying.productmanagementsystem.service; import priv.yinying.productmanagementsystem.pojo.Product; import priv.yinying.productmanagementsystem.view.UserView; import java.util.Arrays; import java.util.Date; /** * @author yinying * @Package priv.yinying.productmanagementsystem.service * @Description: * @Date 2023/2/1 0:47 */ public class ProductServiceImpl implements ProductService { static Product[] products = new Product[5]; public static int count = 5; public static int id = 0; static { for (int i = 0; i < products.length; i++) { products[i] = new Product(++id, "柠檬手机"+i, 2000+i, new Date(), 100+i); } } // 默认5件商品 //查询 @Override public Product[] selectAll() { return products; } //搜索 - 从数组中寻找相同元素product并返回 @Override public Product[] search(String name) { //定义变量保存搜索出来对应商品的个数 int size = 0; for (int i = 0; i < count; i++) { //循环到有效个数 if(products[i].getName().contains(name)) { size++; } } Product[] pros = new Product[size]; int index = 0; for (int i = 0; i < count; i++) { if(products[i].getName().contains(name)) { pros[index] = products[i]; index++; } } return pros; } //增加 @Override public boolean addProduct(Product product) { //判断是否需要扩容 if(count == products.length) { products = Arrays.copyOf(products, products.length+5); } //将product对象放入数组中 products[count] = product; //有效个数+1 count++; return true; } public int exists(int id) { for (int i = 0; i < count; i++) { if(products[i].getId() == id) { return i; } } return -1; } @Override public boolean deleteProduct(int id) { //判断是否存在 int index = exists(id); if(index != -1) {//存在 //扩容:以保证数组的长度与有效个数不相等 products = Arrays.copyOf(products, products.length+5); for (int i = index; i <= count; i++) { products[i] = products[i+1]; } return true; } return false; } @Override public boolean updateProduct(Product product) { //判断是否存在 int index = exists(product.getId()); if(index != -1) {//存在 products[index] = product; return true; } return false; } @Override public Product selectOne(int id) { for (int i = 0; i < count; i++) { if(products[i].getId() == id) { return products[i]; } } return null; } @Override public boolean buyProduct(int id, int num) { //判断是否存在 int index = exists(id); if(index != -1) { //判断库存是否充足 if(num < products[index].getNum()) { if(UserView.user.getMoney() >= ( products[index].getPrice() * num)) { //修改全局保存的登录用户的余额 UserView.user.setMoney(UserView.user.getMoney() - ( products[index].getPrice() * num) ); //修改用户数组中的用户的余额 UserService userService = new UserServiceImpl(); userService.updateUser(UserView.user); //修改商品库存 products[index].setNum(products[index].getNum() - num); return true; } } } return false; } }
6 商品界面实现
6.1 分析
- view 产品页面productview
- 登录菜单
- 实现商品界面对商品的增删改查
6.2 代码实现
6.2.1 productview
package priv.yinying.productmanagementsystem.view; import priv.yinying.productmanagementsystem.pojo.Product; import priv.yinying.productmanagementsystem.service.ProductService; import priv.yinying.productmanagementsystem.service.ProductServiceImpl; import java.util.Date; import java.util.Scanner; /** * @author yinying * @Package priv.yinying.productmanagementsystem.view * @Description: * @Date 2023/2/1 0:45 */ public class ProductView { Scanner sc = new Scanner(System.in); ProductService productService = new ProductServiceImpl(); //普通用户 public void menu1() { while(true) { System.out.println("1、查询\t2、详情\t3、搜索\t4、购买\t5、返回"); int option = sc.nextInt(); switch (option) { case 1: showProducts(); break; case 2: showProduct(); break; case 3: searchProduct(); break; case 4: buyProduct(); break; case 5: return; default: System.out.println("输入有误请重新输入!!!"); break; } } } //查询操作 public void showProducts() { Product[] products = productService.selectAll(); for(int i = 0; i < ProductServiceImpl.count; i++) { System.out.println(products[i]); } } //详情操作 public void showProduct() { System.out.println("请输入商品id"); int option = sc.nextInt(); Product product = productService.selectOne(option); System.out.println(product); } //搜索操作 public void searchProduct() { System.out.println("请输入商品名称"); String name = sc.next(); Product[] products = productService.search(name); for(int i = 0; i < ProductServiceImpl.count; i++) { System.out.println(products[i]); } } //购买操作 public void buyProduct() { System.out.println("请输入商品id"); int id = sc.nextInt(); Product product = productService.selectOne(id); System.out.println(product); System.out.println("请输入购买的数量"); int num = sc.nextInt(); boolean flag = productService.buyProduct(id, num); if(flag) { System.out.println("购买成功"); }else { System.out.println("购买失败"); } } //管理员用户 public void menu2() { while(true) { System.out.println("1、查询\t2、搜索\t3、上架\t4、下架\t5、修改\t6、返回"); int option = sc.nextInt(); switch (option) { case 1: showProducts(); break; case 2: searchProduct(); break; case 3: addProduct(); break; case 4: deleteProduct(); break; case 5: updateProduct(); break; case 6: return; default: System.out.println("输入有误请重新输入!!!"); break; } } } //上架商品 public void addProduct() { System.out.println("********上架页面********"); System.out.println("请输入商品名称"); String name = sc.next(); System.out.println("请输入商品价格"); double price = sc.nextDouble(); System.out.println("请输入商品库存"); int num = sc.nextInt(); Product product = new Product(++ProductServiceImpl.id, name, price, new Date(), num); productService.addProduct(product); System.out.println("添加成功"); } //下架商品 public void deleteProduct() { System.out.println("请输入商品id"); int id = sc.nextInt(); boolean flag = productService.deleteProduct(id); if(flag) { System.out.println("下架成功"); }else { System.out.println("操作失败"); } } //修改商品 public void updateProduct() { System.out.println("请输入要修改商品id"); int id = sc.nextInt(); Product product = productService.selectOne(id); if(product == null) { System.out.println("商品不存在!!!"); }else { System.out.println(product); System.out.println("请输入商品名称"); String name = sc.next(); System.out.println("请输入商品价格"); double price = sc.nextDouble(); System.out.println("请输入商品库存"); int num = sc.nextInt(); product.setName(name); product.setPrice(price); product.setNum(num); boolean flag = productService.updateProduct(product); if(flag) { System.out.println("修改成功"); }else { System.out.println("修改失败"); } } } }
7 测试
7.1 Test
package priv.yinying.productmanagementsystem.client; import priv.yinying.productmanagementsystem.view.UserView; /** * @author yinying * @Package priv.yinying.productmanagementsystem.client * @Description: * @Date 2023/2/1 0:50 */ public class Test { public static void main(String[] args) { start(); } public static void start(){ UserView userView = new UserView(); userView.indexMenu(); } }