Java实现商品管理系统代码实例讲解
实现功能:商品查询,新增,更改价格,以及删除
首先是三个基本类的构建
商品类、账号类、品牌类
1、商品类
publicclassGoods{ //商品信息:商品名称,商品价格,商品销量,商品种类,商品品牌对应编号 privateStringgoodsName; privatedoublegoodsPrice; privateintgoodsSales; privateStringgoodsCategories; privateintbrandsNum; privateStringGoodsNum; publicGoods(StringgoodsName,doublegoodsPrice,intgoodsSales,StringgoodsCategories,intbrandsNum,StringgoodsNum){ this.goodsName=goodsName; this.goodsPrice=goodsPrice; this.goodsSales=goodsSales; this.goodsCategories=goodsCategories; this.brandsNum=brandsNum; GoodsNum=goodsNum; } publicintgetBrandsNum(){ returnbrandsNum; } publicvoidsetBrandsNum(intbrandsNum){ this.brandsNum=brandsNum; } publicGoods(){ } publicStringgetGoodsNum(){ returnGoodsNum; } publicvoidsetGoodsNum(StringgoodsNum){ GoodsNum=goodsNum; } publicStringgetGoodsName(){ returngoodsName; } publicvoidsetGoodsName(StringgoodsName){ this.goodsName=goodsName; } publicdoublegetGoodsPrice(){ returngoodsPrice; } publicvoidsetGoodsPrice(doublegoodsPrice){ this.goodsPrice=goodsPrice; } publicintgetGoodsSales(){ returngoodsSales; } publicvoidsetGoodsSales(intgoodsSales){ this.goodsSales=goodsSales; } publicStringgetGoodsCategories(){ returngoodsCategories; } publicvoidsetGoodsCategories(StringgoodsCategories){ this.goodsCategories=goodsCategories; } publicintgetBrands(){ returnbrandsNum; } publicvoidsetBrands(intbrandsNum){ this.brandsNum=brandsNum; } }
实现了其各个属性的get和set方法
账户类
publicclassAccounts{ //账户信息:账户名称,账户密码 privateStringuserName; privateStringuserPassword; publicAccounts(StringuserName,StringuserPassword){ this.userName=userName; this.userPassword=userPassword; } publicAccounts(){ } publicStringgetUserName(){ returnuserName; } publicvoidsetUserName(StringuserName){ this.userName=userName; } publicStringgetUserPassword(){ returnuserPassword; } publicvoidsetUserPassword(StringuserPassword){ this.userPassword=userPassword; } }
品牌类
publicclassBrands{ //商品品牌信息:商品品牌名称,商品品牌对应编号 privateStringbrandsName; privateintbrandsNum; publicBrands(StringbrandsName,intbrandsNum){ this.brandsName=brandsName; this.brandsNum=brandsNum; } publicStringgetBrandsName(){ returnbrandsName; } publicvoidsetBrandsName(StringbrandsName){ this.brandsName=brandsName; } publicintgetBrandsNum(){ returnbrandsNum; } publicvoidsetBrandsNum(intbrandsNum){ this.brandsNum=brandsNum; } }
之后是一个存储这三个基本类信息的数据类,通过定义List实现三个类的存储
数据类
importjava.util.ArrayList; importjava.util.List; importjava.util.UUID; publicclassDatabase{ //存储用户信息 privateListaccountsList=newArrayList<>(); //存储商品信息 privateList goodsList=newArrayList<>(); //存储品牌信息 privateList brandsList=newArrayList<>(); publicDatabase(){ //添加初始化信息 此处自己添加商品,用户信息,品牌信息 } publicList getAccountsList(){ returnaccountsList; } publicvoidsetAccountsList(List accountsList){ this.accountsList=accountsList; } publicList getGoodsList(){ returngoodsList; } publicvoidsetGoodsList(List goodsList){ this.goodsList=goodsList; } publicList getBrandsList(){ returnbrandsList; } publicvoidsetBrandsList(List brandsList){ this.brandsList=brandsList; } }
之后是三个基本类各个增删改查方法的实现,这个根据程序需求写
商品交互类
importjava.util.ArrayList; importjava.util.List; publicclassGoodsDAO{ //调用数据库 privateDatabasedatabase; //初始化商品DAO类,引入数据库 publicGoodsDAO(Databasedatabase){ this.database=database; } //返回销量最高的商品 publicListhotSell(){ List temporList=database.getGoodsList(); for(inti=0;i searchByBranchId(intbrandsId){ List temporList=newArrayList<>(); for(Goodseach:database.getGoodsList()){ if(each.getBrandsNum()==brandsId) temporList.add(each); } if(temporList.isEmpty()) temporList=null; returntemporList; } //根据价格区间查询商品 publicList searchByPrice(doubleminPrice,doublemaxPrice){ List temporList=newArrayList<>(); for(Goodseach:database.getGoodsList()){ if(each.getGoodsPrice()>=minPrice&&each.getGoodsPrice()<=maxPrice){ temporList.add(each); } } if(temporList.isEmpty()) temporList=null; returntemporList; } //新增商品 publicvoidaddGoods(Goodsgoods){ database.getGoodsList().add(goods); } //修改商品价格 publicvoidmodify(StringgoodsId,doubleprice){ for(Goodseach:database.getGoodsList()){ if(each.getGoodsNum().equals(goodsId)) each.setGoodsPrice(price); } } //删除商品 publicvoiddelete(StringgoodsId){ Goodsgoods=null; for(Goodseach:database.getGoodsList()){ if(each.getGoodsNum().equals(goodsId)) goods=each; } database.getGoodsList().remove(goods); //迭代操作时不允许ArrayList被改变,因此重新定义一个对象指向他,迭代完后进行操作。 } //更改商品品牌编号 publicvoidchangeByBranchId(intbrandsId){ for(Goodseach:database.getGoodsList()){ if(each.getBrandsNum()==brandsId) each.setBrandsNum(brandsId-1); } } }
账户交互类实现注册,登录的需求
publicclassAccountDAO{ //调用数据库 privateDatabasedatabase; //初始化账户DAO类,引入数据库 publicAccountDAO(Databasedatabase){ this.database=database; } //根据账户名返回账户类 publicAccountssearchAccounts(StringuserName){ Accountsaccounts=null; for(Accountseach:database.getAccountsList()){ if(each.getUserName().equals(userName)) accounts=each; } returnaccounts; } //添加账户类 publicvoidaddAccount(Accountsaccounts){ database.getAccountsList().add(accounts); } }
品牌类
publicclassBrandsDAO{ //调用数据库 privateDatabasedatabase; //初始化品牌DAO类,引入数据库 publicBrandsDAO(Databasedatabase){ this.database=database; } //根据品牌名称查询 publicBrandsfindByName(StringbrandName){ Brandsbrands=null; for(Brandseach:database.getBrandsList()){ if(each.getBrandsName().equals(brandName)){ brands=each; break; } } returnbrands; } //查询所有品牌 publicvoidfindAll(){ System.out.println("现有品牌:"); for(Brandseach:database.getBrandsList()){ System.out.println(each.getBrandsNum()+"\t"+each.getBrandsName()); } } //根据品牌编号查询品牌名称 publicStringfindNameById(intid){ StringbrandName; Brandsbrands=null; for(Brandseach:database.getBrandsList()){ if(each.getBrandsNum()==id){ brands=each; } } returnbrands.getBrandsName(); } //查询品牌是否拥有 publicbooleanfindBranch(StringbrandsName){ booleanchoose=false; for(Brandseach:database.getBrandsList()) { if(each.getBrandsName().equals(brandsName)) choose=true; } returnchoose; } //新建一个品牌 publicvoidaddBranch(Brandsbrands){ database.getBrandsList().add(brands); } //查找当前品牌的的数量 publicintbrandsNumbers(){ intnum=database.getBrandsList().size(); returnnum; } //查找要删除的品牌 publicvoiddeleteBrands(intnum){ Brandsbrands=null; for(Brandseach:database.getBrandsList()){ if(each.getBrandsNum()==num) brands=each; } database.getBrandsList().remove(brands); } //移动编号 publicvoidmoveBrandsNum(intbrandsNum){ intindex; for(inti=0;ibrandsNum) database.getBrandsList().get(i).setBrandsNum(database.getBrandsList().get(i).getBrandsNum()-1); } } }
服务类实现项目需要的功能,构建交互界面
publicclassGoodsSystem{ Scannerscanner=newScanner(System.in); Randomrandom=newRandom(); AccountDAOaccountDAO; BrandsDAObrandsDAO; GoodsDAOgoodsDAO; //初始化DAO publicGoodsSystem(Databasedatabase){ accountDAO=newAccountDAO(database); brandsDAO=newBrandsDAO(database); goodsDAO=newGoodsDAO(database); } publicvoidstart(){ //1.登陆 System.out.println("----------------商品系统---------------"); System.out.println("1.登录"); //2.注册 System.out.println("2.注册"); //3.退出系统 System.out.println("3.退出系统"); System.out.println("请选择:"); Stringchoose=scanner.next(); switch(choose){ //登录 case"1": login(); break; //注册 case"2": regist(); break; //退出系统 case"3": System.out.println("系统已经退出"); break; default: System.out.println("输入错误,请重新输入:"); start(); } } //登录 publicvoidlogin(){ System.out.println("请输入用户名:"); StringuserName=scanner.next(); System.out.println("请输入密码:"); StringpassWord=scanner.next(); Accountsaccounts=accountDAO.searchAccounts(userName); StringtestNum=""; if(accounts!=null){ if(accounts.getUserPassword().equals(passWord)){ for(inti=0;i<4;i++){ intnum=random.nextInt(10); testNum=testNum+num; } System.out.println("验证码:"+testNum); System.out.println("请输入验证码:"); StringtestNumInput=scanner.next(); if(testNumInput.equals(testNum)){ System.out.println("登录成功"); mainMenu(); } else{ System.out.println("验证码错误,请重新登录"); login(); } } else{ System.out.println("密码错误,请重新登录"); System.out.println("输入任意键执行操作,或输入0返回上一层目录"); Stringchoose=scanner.next(); if(choose.equals("0")) start(); else login(); } } else{ System.out.println("该账户不存在,请重新输入:"); System.out.println("输入任意键执行操作,或输入0返回上一层目录"); Stringchoose=scanner.next(); if(choose.equals("0")) start(); else login(); } } //注册 publicvoidregist(){ System.out.println("----注册----"); //用户名 System.out.println("请输入用户名:"); StringuserName=scanner.next(); Accountsaccounts=accountDAO.searchAccounts(userName); while(accounts!=null){ System.out.println("用户名已经被使用,请重新输入"); userName=scanner.next(); accounts=accountDAO.searchAccounts(userName); } //密码 System.out.println("请输入密码:"); StringuserPassWord=scanner.next(); System.out.println("确认密码:"); StringtestPassWord=scanner.next(); while(!userPassWord.equals(testPassWord)){ System.out.println("确认密码与注册密码不相符,请重新输入密码"); System.out.println("请输入密码:"); userPassWord=scanner.next(); System.out.println("确认密码:"); testPassWord=scanner.next(); } //验证码 StringtestNum=""; for(inti=0;i<4;i++){ intnum=random.nextInt(10); testNum=testNum+num; } System.out.println("验证码:"+testNum); System.out.println("请输入验证码:"); StringtestNumInput=scanner.next(); if(testNumInput.equals(testNum)){ accountDAO.addAccount(newAccounts(userName,userPassWord)); System.out.println("注册成功"); start(); } else{ System.out.println("验证码错误,请重新注册"); System.out.println("输入任意键执行操作,或输入0返回上一层目录"); Stringchoose=scanner.next(); if(choose.equals("0")) start(); else regist(); } } //主菜单 publicvoidmainMenu(){ 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("请输入你的选择:"); Stringchoose=scanner.next(); switch(choose){ case"1"://热门商品 hotSell(); break; case"2"://商品查询 searchGoods(); break; case"3"://后台管理 controlGoods(); break; case"4"://退出系统 System.out.println("系统已经退出"); break; default: mainMenu(); } } //热卖商品 publicvoidhotSell(){ System.out.println("------------热销商品------------"); Listtempor=goodsDAO.hotSell(); System.out.println("序号\t\t"+"商品"+"\t\t\t\t\t销量\t"); intnum=10; if(tempor.size()<10) num=tempor.size(); for(inti=0;i =0){ Goodsgoods=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(); } } //商品查询 publicvoidsearchGoods(){ System.out.println("---------商品查询----------"); //1.品牌查询 System.out.println("1.品牌查询"); //2.价格查询 System.out.println("2.价格查询"); System.out.println("3.返回上一层目录"); System.out.println("请输入你的选择:"); Stringchoose=scanner.next(); switch(choose){ case"1"://品牌查询 searchByBranch(); break; case"2"://价格查询 searchByPrice(); break; case"3": mainMenu(); break; default: searchGoods(); } } //后台管理 publicvoidcontrolGoods(){ 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("请输入选择:"); Stringchoose=scanner.next(); switch(choose){ case"1"://新增商品 addGoods(); break; case"2"://修改价格 changePrice(); break; case"3"://删除商品 deleteGoods(); break; case"4"://返回主菜单 mainMenu(); break; default: controlGoods(); } } //根据品牌查询 publicvoidsearchByBranch(){ brandsDAO.findAll(); System.out.println("请输入要查询的品牌编号:"); StringbrandsNum1=scanner.next(); if(isNumeric(brandsNum1)){ intbrandsNum=Integer.parseInt(brandsNum1); List temporlist=goodsDAO.searchByBranchId(brandsNum); if(temporlist==null){ System.out.println("没有该品牌的商品"); searchGoods(); } for(inti=0;i =1&&choose<=temporlist.size()){ Goodsgoods=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(); } } //根据价格查询 publicvoidsearchByPrice(){ System.out.println("请输入要查询的价格区间:最小价格最大价格"); doubleminPrice=scanner.nextDouble(); doublemaxPrice=scanner.nextDouble(); List temporlist=goodsDAO.searchByPrice(minPrice,maxPrice); if(temporlist==null){ System.out.println("没有该区间的商品"); searchGoods(); } for(inti=0;i =1&&choose<=temporlist.size()){ Goodsgoods=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(); } } //新增商品 publicvoidaddGoods(){ System.out.println("请输入商品名称:"); StringgoodsName=scanner.next(); System.out.println("请输入商品价格:"); doublegoodsPrice=scanner.nextDouble(); System.out.println("请输入品牌"); StringbrandsName=scanner.next(); intbrandsNum; if(brandsDAO.findBranch(brandsName)){ brandsNum=brandsDAO.findByName(brandsName).getBrandsNum(); } else{ brandsNum=brandsDAO.brandsNumbers()+1; Brandsbrands=newBrands(brandsName,brandsNum); brandsDAO.addBranch(brands); } System.out.println("请输入商品类型"); StringgoodsCate=scanner.next(); Goodsgoods=newGoods(goodsName,goodsPrice,0,goodsCate,brandsNum,UUID.randomUUID().toString()); goodsDAO.addGoods(goods); System.out.println("商品添加成功"); controlGoods(); } //改变价格 publicvoidchangePrice(){ System.out.println("--------修改价格------"); brandsDAO.findAll(); System.out.println("请输入要查询的品牌编号:"); StringbrandsNum1=scanner.next(); if(isNumeric(brandsNum1)){ intbrandsNum=Integer.parseInt(brandsNum1); List temporlist=goodsDAO.searchByBranchId(brandsNum); if(temporlist==null){ System.out.println("没有该品牌的商品"); changePrice(); } for(inti=0;i temporlist=goodsDAO.searchByBranchId(brandsNum); if(temporlist==null){ System.out.println("没有该品牌的商品"); deleteGoods(); } for(inti=0;i =0;){ if(!Character.isDigit(str.charAt(i))){ returnfalse; } } returntrue; } }
注:以上代码部分内部类的导入未填写
还需要一个测试类运行
暂且略过
到此这篇关于Java实现商品管理系统代码实例讲解的文章就介绍到这了,更多相关Java实现商品管理系统内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。