Java实现商品管理系统代码实例讲解
实现功能:商品查询,新增,更改价格,以及删除
首先是三个基本类的构建
商品类、账号类、品牌类
1、商品类
public class Goods { //商品信息:商品名称,商品价格,商品销量,商品种类,商品品牌对应编号 private String goodsName; private double goodsPrice; private int goodsSales; private String goodsCategories; private int brandsNum; private String GoodsNum; public Goods(String goodsName, double goodsPrice, int goodsSales, String goodsCategories,int brandsNum, String goodsNum) { this.goodsName = goodsName; this.goodsPrice = goodsPrice; this.goodsSales = goodsSales; this.goodsCategories = goodsCategories; this.brandsNum = brandsNum; GoodsNum = goodsNum; } public int getBrandsNum() { return brandsNum; } public void setBrandsNum(int brandsNum) { this.brandsNum = brandsNum; } public Goods() { } public String getGoodsNum() { return GoodsNum; } public void setGoodsNum(String goodsNum) { GoodsNum = goodsNum; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public double getGoodsPrice() { return goodsPrice; } public void setGoodsPrice(double goodsPrice) { this.goodsPrice = goodsPrice; } public int getGoodsSales() { return goodsSales; } public void setGoodsSales(int goodsSales) { this.goodsSales = goodsSales; } public String getGoodsCategories() { return goodsCategories; } public void setGoodsCategories(String goodsCategories) { this.goodsCategories = goodsCategories; } public int getBrands() { return brandsNum; } public void setBrands(int brandsNum) { this.brandsNum = brandsNum; } }
实现了其各个属性的get和set方法
账户类
public class Accounts { //账户信息:账户名称,账户密码 private String userName; private String userPassword; public Accounts(String userName, String userPassword) { this.userName = userName; this.userPassword = userPassword; } public Accounts() { } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } }
品牌类
public class Brands { //商品品牌信息:商品品牌名称,商品品牌对应编号 private String brandsName; private int brandsNum; public Brands(String brandsName, int brandsNum) { this.brandsName = brandsName; this.brandsNum = brandsNum; } public String getBrandsName() { return brandsName; } public void setBrandsName(String brandsName) { this.brandsName = brandsName; } public int getBrandsNum() { return brandsNum; } public void setBrandsNum(int brandsNum) { this.brandsNum = brandsNum; } }
之后是一个存储这三个基本类信息的数据类,通过定义List实现三个类的存储
数据类
import java.util.ArrayList; import java.util.List; import java.util.UUID; public class Database { //存储用户信息 private List<Accounts>accountsList=new ArrayList<>(); //存储商品信息 private List<Goods>goodsList=new ArrayList<>(); //存储品牌信息 private List<Brands>brandsList=new ArrayList<>(); public Database(){ //添加初始化信息 此处自己添加商品,用户信息,品牌信息 } public List<Accounts> getAccountsList() { return accountsList; } public void setAccountsList(List<Accounts> accountsList) { this.accountsList = accountsList; } public List<Goods> getGoodsList() { return goodsList; } public void setGoodsList(List<Goods> goodsList) { this.goodsList = goodsList; } public List<Brands> getBrandsList() { return brandsList; } public void setBrandsList(List<Brands> brandsList) { this.brandsList = brandsList; } }
之后是三个基本类各个增删改查方法的实现,这个根据程序需求写
商品交互类
import java.util.ArrayList; import java.util.List; public class GoodsDAO { //调用数据库 private Database database; //初始化商品DAO类,引入数据库 public GoodsDAO(Database database){ this.database=database; } //返回销量最高的商品 public List<Goods>hotSell(){ List<Goods> temporList=database.getGoodsList(); for(int i=0;i<temporList.size();i++){ for(int j=0;j<temporList.size()-1-i;j++){ if(temporList.get(j).getGoodsSales()<temporList.get(j+1).getGoodsSales()) { Goods e=temporList.get(j+1); temporList.set(j+1,temporList.get(j)); temporList.set(j,e); } } } return temporList; } //根据商品序号查询商品 public Goods searchById(int id){ Goods goods; goods=database.getGoodsList().get(id-1); return goods; } //根据品牌编号查询商品 public List<Goods>searchByBranchId(int brandsId){ List<Goods>temporList=new ArrayList<>(); for(Goods each:database.getGoodsList()){ if(each.getBrandsNum()==brandsId) temporList.add(each); } if(temporList.isEmpty()) temporList=null; return temporList; } //根据价格区间查询商品 public List<Goods>searchByPrice(double minPrice,double maxPrice){ List<Goods>temporList=new ArrayList<>(); for(Goods each:database.getGoodsList()){ if(each.getGoodsPrice()>=minPrice&&each.getGoodsPrice()<=maxPrice) { temporList.add(each); } } if(temporList.isEmpty()) temporList=null; return temporList; } //新增商品 public void addGoods(Goods goods){ database.getGoodsList().add(goods); } //修改商品价格 public void modify(String goodsId,double price){ for(Goods each:database.getGoodsList()){ if(each.getGoodsNum().equals(goodsId)) each.setGoodsPrice(price); } } //删除商品 public void delete(String goodsId){ Goods goods=null; for(Goods each:database.getGoodsList()){ if(each.getGoodsNum().equals(goodsId)) goods=each; } database.getGoodsList().remove(goods); //迭代操作时不允许ArrayList被改变,因此重新定义一个对象指向他,迭代完后进行操作。 } //更改商品品牌编号 public void changeByBranchId(int brandsId){ for(Goods each:database.getGoodsList()){ if(each.getBrandsNum()==brandsId) each.setBrandsNum(brandsId-1); } } }
账户交互类实现注册,登录的需求
public class AccountDAO { //调用数据库 private Database database; //初始化账户DAO类,引入数据库 public AccountDAO(Database database){ this.database=database; } //根据账户名返回账户类 public Accounts searchAccounts(String userName){ Accounts accounts=null; for(Accounts each:database.getAccountsList()){ if(each.getUserName().equals(userName)) accounts=each; } return accounts; } //添加账户类 public void addAccount(Accounts accounts){ database.getAccountsList().add(accounts); } }
品牌类
public class BrandsDAO { //调用数据库 private Database database; //初始化品牌DAO类,引入数据库 public BrandsDAO(Database database){ this.database=database; } //根据品牌名称查询 public Brands findByName(String brandName){ Brands brands=null; for(Brands each:database.getBrandsList()){ if(each.getBrandsName().equals(brandName)) { brands = each; break; } } return brands; } //查询所有品牌 public void findAll(){ System.out.println("现有品牌:"); for(Brands each:database.getBrandsList()){ System.out.println(each.getBrandsNum()+"\t"+each.getBrandsName()); } } //根据品牌编号查询品牌名称 public String findNameById(int id){ String brandName; Brands brands=null; for(Brands each:database.getBrandsList()){ if(each.getBrandsNum()==id){ brands=each; } } return brands.getBrandsName(); } //查询品牌是否拥有 public boolean findBranch(String brandsName){ boolean choose=false; for(Brands each:database.getBrandsList()) { if(each.getBrandsName().equals(brandsName)) choose=true; } return choose; } //新建一个品牌 public void addBranch(Brands brands){ database.getBrandsList().add(brands); } //查找当前品牌的的数量 public int brandsNumbers(){ int num=database.getBrandsList().size(); return num; } //查找要删除的品牌 public void deleteBrands(int num){ Brands brands=null; for(Brands each:database.getBrandsList()){ if(each.getBrandsNum()==num) brands=each; } database.getBrandsList().remove(brands); } //移动编号 public void moveBrandsNum(int brandsNum){ int index; for(int i=0;i<database.getBrandsList().size();i++){ if(database.getBrandsList().get(i).getBrandsNum()>brandsNum) database.getBrandsList().get(i).setBrandsNum(database.getBrandsList().get(i).getBrandsNum()-1); } } }
服务类实现项目需要的功能,构建交互界面
public class GoodsSystem { Scanner scanner=new Scanner(System.in); Random random=new Random(); AccountDAO accountDAO; BrandsDAO brandsDAO; GoodsDAO goodsDAO; //初始化DAO public GoodsSystem(Database database){ accountDAO=new AccountDAO(database); brandsDAO=new BrandsDAO(database); goodsDAO=new GoodsDAO(database); } public void start(){ // 1.登陆 System.out.println("----------------商品系统---------------"); System.out.println("1.登录"); // 2.注册 System.out.println("2.注册"); // 3.退出系统 System.out.println("3.退出系统"); System.out.println("请选择:"); String choose=scanner.next(); switch(choose){ //登录 case "1": login(); break; //注册 case "2": regist(); break; //退出系统 case "3": System.out.println("系统已经退出"); break; default: System.out.println("输入错误,请重新输入:"); start(); } } //登录 public void login(){ System.out.println("请输入用户名:"); String userName=scanner.next(); System.out.println("请输入密码:"); String passWord=scanner.next(); Accounts accounts=accountDAO.searchAccounts(userName); String testNum=""; if(accounts!=null){ if(accounts.getUserPassword().equals(passWord)){ for(int i=0;i<4;i++){ int num=random.nextInt(10); testNum=testNum+num; } System.out.println("验证码:"+testNum); System.out.println("请输入验证码:"); String testNumInput=scanner.next(); if(testNumInput.equals(testNum)) { System.out.println("登录成功"); mainMenu(); } else{ System.out.println("验证码错误,请重新登录"); login(); } } else{ System.out.println("密码错误,请重新登录"); System.out.println("输入任意键执行操作,或输入0返回上一层目录"); String choose=scanner.next(); if(choose.equals("0")) start(); else login(); } } else{ System.out.println("该账户不存在,请重新输入:"); System.out.println("输入任意键执行操作,或输入0返回上一层目录"); String choose=scanner.next(); if(choose.equals("0")) start(); else login(); } } //注册 public void regist(){ System.out.println("----注册----"); //用户名 System.out.println("请输入用户名:"); String userName=scanner.next(); Accounts accounts=accountDAO.searchAccounts(userName); while(accounts!=null){ System.out.println("用户名已经被使用,请重新输入"); userName=scanner.next(); accounts=accountDAO.searchAccounts(userName); } //密码 System.out.println("请输入密码:"); String userPassWord=scanner.next(); System.out.println("确认密码:"); String testPassWord=scanner.next(); while(!userPassWord.equals(testPassWord)){ System.out.println("确认密码与注册密码不相符,请重新输入密码"); System.out.println("请输入密码:"); userPassWord=scanner.next(); System.out.println("确认密码:"); testPassWord=scanner.next(); } //验证码 String testNum=""; for(int i=0;i<4;i++){ int num=random.nextInt(10); testNum=testNum+num; } System.out.println("验证码:"+testNum); System.out.println("请输入验证码:"); String testNumInput=scanner.next(); if(testNumInput.equals(testNum)) { accountDAO.addAccount(new Accounts(userName,userPassWord)); System.out.println("注册成功"); start(); } else{ System.out.println("验证码错误,请重新注册"); System.out.println("输入任意键执行操作,或输入0返回上一层目录"); String choose=scanner.next(); if(choose.equals("0")) start(); else regist(); } } //主菜单 public void mainMenu(){ System.out.println("-------------主菜单------------"); // 1.热门商品 System.out.println("1.热门商品"); // 2.商品查询 System.out.println("2.商品查询"); // 3.后台管理 System.out.println("3.后台管理"); System.out.println("4.退出系统"); System.out.println("请输入你的选择:"); String choose=scanner.next(); switch (choose){ case "1"://热门商品 hotSell(); break; case "2"://商品查询 searchGoods(); break; case "3"://后台管理 controlGoods(); break; case "4"://退出系统 System.out.println("系统已经退出"); break; default: mainMenu(); } } //热卖商品 public void hotSell(){ System.out.println("------------热销商品------------"); List <Goods>tempor=goodsDAO.hotSell(); System.out.println("序号\t\t"+"商品"+"\t\t\t\t\t销量\t"); int num=10; if(tempor.size()<10) num=tempor.size(); for(int i=0;i<num;i++){ System.out.println((i+1)+"\t\t"+tempor.get(i).getGoodsName()+"\t\t\t\t\t"+tempor.get(i).getGoodsSales()+"\t"); } System.out.println("请输入要查询的商品ID或输入0返回上一层菜单。"); String chooseEnd=scanner.next(); if(isNumeric(chooseEnd)==true) { int choose=Integer.parseInt(chooseEnd); if(choose==0){ mainMenu(); } else { if(choose<=num&&choose>=0) { Goods goods = goodsDAO.searchById(choose); System.out.println("商品名称\t\t品牌\t\t价格\t\t类型"); System.out.println(goods.getGoodsName() + "\t\t\t" + brandsDAO.findNameById(goods.getBrandsNum()) + "\t" + goods.getGoodsPrice() + "元\t" + goods.getGoodsCategories()); mainMenu(); } else { System.out.println("查询的序号超过限定,请重新输入"); hotSell(); } } } else { System.out.println("输入错误符号,请重新选择"); hotSell(); } } //商品查询 public void searchGoods(){ System.out.println("---------商品查询----------"); // 1.品牌查询 System.out.println("1.品牌查询"); // 2.价格查询 System.out.println("2.价格查询"); System.out.println("3.返回上一层目录"); System.out.println("请输入你的选择:"); String choose=scanner.next(); switch(choose){ case "1"://品牌查询 searchByBranch(); break; case "2"://价格查询 searchByPrice(); break; case "3": mainMenu(); break; default: searchGoods(); } } //后台管理 public void controlGoods(){ System.out.println("--------后台管理--------"); // 1.新增商品 System.out.println("1.新增商品"); // 2.修改价格 System.out.println("2.修改价格"); // 3.删除商品 System.out.println("3.删除商品"); System.out.println("4.返回主菜单"); System.out.println("请输入选择:"); String choose=scanner.next(); switch (choose){ case "1"://新增商品 addGoods(); break; case "2"://修改价格 changePrice(); break; case "3"://删除商品 deleteGoods(); break; case "4"://返回主菜单 mainMenu(); break; default: controlGoods(); } } //根据品牌查询 public void searchByBranch(){ brandsDAO.findAll(); System.out.println("请输入要查询的品牌编号:"); String brandsNum1=scanner.next(); if(isNumeric(brandsNum1)) { int brandsNum=Integer.parseInt(brandsNum1); List<Goods> temporlist = goodsDAO.searchByBranchId(brandsNum); if (temporlist == null) { System.out.println("没有该品牌的商品"); searchGoods(); } for (int i = 0; i < temporlist.size(); i++) { System.out.println("序号" + (i + 1) + "\t" + "商品名字:" + temporlist.get(i).getGoodsName()); } System.out.println("请输入要查询的商品ID或输入0返回上一层菜单。"); String choose1 = scanner.next(); if(isNumeric(choose1)) { int choose=Integer.parseInt(choose1); if (choose == 0) { searchGoods(); } else { if(choose>=1&&choose<=temporlist.size()){ Goods goods = temporlist.get(choose - 1); System.out.println("商品名称:" + goods.getGoodsName() + "\t" + "品牌:" + brandsDAO.findNameById(goods.getBrandsNum()) + "\t" + "价格:" + goods.getGoodsPrice() + "元\t" + "类型:" + goods.getGoodsCategories()); searchGoods(); } else{ System.out.println("输入序号超过边界范围,请重新输入"); searchByBranch(); } } } else{ System.out.println("输入错误符号,请重新选择"); searchByBranch(); } } else{ System.out.println("输入错误符号,请重新选择"); searchByBranch(); } } //根据价格查询 public void searchByPrice(){ System.out.println("请输入要查询的价格区间:最小价格 最大价格"); double minPrice=scanner.nextDouble(); double maxPrice=scanner.nextDouble(); List<Goods>temporlist=goodsDAO.searchByPrice(minPrice,maxPrice); if(temporlist==null){ System.out.println("没有该区间的商品"); searchGoods(); } for(int i=0;i<temporlist.size();i++){ System.out.println("序号"+(i+1)+"\t"+"商品名字:"+temporlist.get(i).getGoodsName()); } System.out.println("请输入要查询的商品ID或输入0返回上一层菜单。"); String choose1 = scanner.next(); if(isNumeric(choose1)) { int choose=Integer.parseInt(choose1); if (choose == 0) { searchGoods(); } else { if(choose>=1&&choose<=temporlist.size()) { Goods goods = temporlist.get(choose - 1); System.out.println("商品名称:" + goods.getGoodsName() + "\t" + "品牌:" + brandsDAO.findNameById(goods.getBrandsNum()) + "\t" + "价格:" + goods.getGoodsPrice() + "元\t" + "类型:" + goods.getGoodsCategories()); searchGoods(); } else{ System.out.println("输入数字超过边界,请重新输入"); searchByPrice(); } } } else{ System.out.println("输入错误符号,请重新选择"); searchByPrice(); } } //新增商品 public void addGoods(){ System.out.println("请输入商品名称:"); String goodsName=scanner.next(); System.out.println("请输入商品价格:"); double goodsPrice=scanner.nextDouble(); System.out.println("请输入品牌"); String brandsName=scanner.next(); int brandsNum; if(brandsDAO.findBranch(brandsName)){ brandsNum=brandsDAO.findByName(brandsName).getBrandsNum(); } else{ brandsNum=brandsDAO.brandsNumbers()+1; Brands brands=new Brands(brandsName,brandsNum); brandsDAO.addBranch(brands); } System.out.println("请输入商品类型"); String goodsCate=scanner.next(); Goods goods=new Goods(goodsName,goodsPrice,0,goodsCate,brandsNum, UUID.randomUUID().toString()); goodsDAO.addGoods(goods); System.out.println("商品添加成功"); controlGoods(); } //改变价格 public void changePrice(){ System.out.println("--------修改价格------"); brandsDAO.findAll(); System.out.println("请输入要查询的品牌编号:"); String brandsNum1=scanner.next(); if(isNumeric(brandsNum1)) { int brandsNum=Integer.parseInt(brandsNum1); List<Goods> temporlist = goodsDAO.searchByBranchId(brandsNum); if (temporlist == null) { System.out.println("没有该品牌的商品"); changePrice(); } for (int i = 0; i < temporlist.size(); i++) { System.out.println("序号" + (i + 1) + "\t" + "商品名字:" + temporlist.get(i).getGoodsName() + "\t价格" + temporlist.get(i).getGoodsPrice() + "元"); } System.out.println("请输入要修改价格的商品ID或输入0返回上一层菜单。"); String choose1 = scanner.next(); if(isNumeric(choose1)) { int choose=Integer.parseInt(choose1); if (choose == 0) { controlGoods(); } else if(choose<=temporlist.size()) { System.out.println("请输入要修改的价格:"); double price = scanner.nextDouble(); System.out.println("是否确定修改价格:是/否"); String endChoose = scanner.next(); if (endChoose.equals("是")) { goodsDAO.modify(temporlist.get(choose - 1).getGoodsNum(), price); System.out.println("价格修改完成"); controlGoods(); } else { controlGoods(); } } else{ System.out.println("输入序号超过边界,请重新输入"); changePrice(); } } else{ System.out.println("输入错误符号,请重新输入"); changePrice(); } } else{ System.out.println("输入错误符号,请重新输入"); changePrice(); } } //删除商品 public void deleteGoods(){ System.out.println("--------删除商品------"); brandsDAO.findAll(); System.out.println("请输入要查询的品牌编号:"); String brandsNum2=scanner.next(); if(isNumeric(brandsNum2)) { int brandsNum=Integer.parseInt(brandsNum2); List<Goods> temporlist = goodsDAO.searchByBranchId(brandsNum); if (temporlist == null) { System.out.println("没有该品牌的商品"); deleteGoods(); } for (int i = 0; i < temporlist.size(); i++) { System.out.println("序号" + (i + 1) + "\t" + "商品名字:" + temporlist.get(i).getGoodsName()); } System.out.println("请输入要删除的商品ID或输入0返回上一层菜单。"); String choose1 = scanner.next(); if(isNumeric(choose1)) { int choose=Integer.parseInt(choose1); if (choose == 0) { controlGoods(); } else if(choose<=temporlist.size()){ System.out.println("是否确定删除:是/否"); String endChoose = scanner.next(); if (endChoose.equals("是")) { int brandsNum1 = temporlist.get(choose - 1).getBrandsNum(); goodsDAO.delete(temporlist.get(choose - 1).getGoodsNum()); System.out.println("删除成功"); if (goodsDAO.searchByBranchId(brandsNum1) == null) { brandsDAO.deleteBrands(brandsNum1); brandsDAO.moveBrandsNum(brandsNum1); for (int i = brandsNum1; i <= brandsDAO.brandsNumbers(); i++) goodsDAO.changeByBranchId(i + 1); } controlGoods(); } else { controlGoods(); } } else{ System.out.println("输入序号超过边界,请重新输入"); deleteGoods(); } } else{ System.out.println("输入错误符号,请重新输入"); deleteGoods(); } } else{ System.out.println("输入错误符号,请重新输入"); deleteGoods(); } } public boolean isNumeric(String str){ for (int i = str.length();--i>=0;){ if (!Character.isDigit(str.charAt(i))){ return false; } } return true; } }
注:以上代码部分内部类的导入未填写
还需要一个测试类运行
暂且略过
到此这篇关于Java实现商品管理系统代码实例讲解的文章就介绍到这了,更多相关Java实现商品管理系统内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)