1、java购物车系统源代码java购物车系统源代码 import java.util.Scanner;public class ShopCar_Client ShopCar_Manager manager = new ShopCar_Manager();Scanner input = new Scanner(System.in);/ 购物车int saveNo = new int10; / 存储商品编号String saveName = new String10; / 存储商品名称int savePrice = new int10; / 存储商品价格String saveInfo = new S
2、tring10; / 存储商品信息int saveAmount = new int10; / 存储商品数量int shuliang = 0; / 购买商品的数量int Num; / 购买商品的编号/ 显示仓库中的商品public void showShop(int quotID, String quotName, int quotNum,String quotInfo, int quotPrice) System.out.println("现在库存里的商品数量有:");System.out.println("商品编号tt" + "商品名称tt&
3、quot; + "商品数量" + "tt商品信息"+ "tt" + "商品价格");for (int i = 0; i < quotID.length; i+) if (quotIDi = 0) break;System.out.println(quotIDi + "tt" + quotNamei + "tt"+ quotNumi + "tt" + quotInfoi + "t" + quotPricei);/ 购买商品publi
4、c void goumai(int quotID, String quotName, int quotNum,String quotInfo, int quotPrice) / 购买商品String answer = ""do System.out.println("*");System.out.println("请选择 1.购买商品 2.查询购物车 3.保存商品订单 4.退出");System.out.println("*");int num1 = input.nextInt();switch (num1) ca
5、se 1: / 购买商品System.out.print("请输入你要够买的商品的编号:");Num = input.nextInt();int index = -1;for (int x = 0; x < quotID.length; x+) / 在库存中循环找到需购买商品的编号if (quotIDx = 0) break;if (quotIDx = Num) index = x;break;if (index != -1) System.out.print("请输入你要够买的商品的数量:"); / 若有此商品就提示用户输入购买的数量shulia
6、ng = input.nextInt();if (shuliang > 0) / 判断输入的数量是否大于0for (int i = 0; i < quotNum.length; i+) if (saveNoi = Num) / 判断购物车里是否有此商品quotNumindex = quotNumindex + saveAmounti; / 货架上商品数量saveAmounti = saveAmounti + shuliang; / 购物车内商品数量manager.quotNumindex = manager.quotNumindex- saveAmountindex; / 购买商品
7、后台减少数量showShop(manager.quotID, manager.quotName,manager.quotNum, manager.quotInfo,manager.quotPrice);break;if (saveNoi = 0) / 找到存储商品的空数组saveNoi = quotIDindex; / 编号saveNamei = quotNameindex; / 名称savePricei = quotPriceindex; / 价格saveInfoi = quotInfoindex; / 信息if (shuliang <= quotNumindex) saveAmoun
8、ti = saveAmounti + shuliang; / 购物车内的商品数量manager.quotNumindex = manager.quotNumindex- saveAmounti; / 购买商品后台减少数量System.out.println("操作完成!");showShop(manager.quotID, manager.quotName,manager.quotNum, manager.quotInfo,manager.quotPrice); else System.out.println("抱歉,商品数量只有:"+ quotNumi
9、ndex);break; else System.out.println("抱歉,你的输入有错误!");/ else System.out.println("抱歉,没有该商品!");break;case 2:purchasesn(); / 查询购物车内商品break;case 3: / 确定购买商品结账退出getAllMoney(); / 打折前的总价格getScore(); / 计算积分getZheKou(); / 计算折扣payPrice(); / 打折后的价格saveMenu(); / 结账菜单break;case 4:break;default:
10、System.out.println("没有你所选的选项!");System.out.println("是否继续:(y/n)"); / 是否回到购买菜单answer = input.next(); while (answer.equals("y");/* 查看商品 修改商品 删除商品* */String answer = ""boolean big = true;public void purchasesn() int index1 = -1;int index2 = -1;System.out.println(&
11、quot;商品编号tt" + "商品名称tt" + "商品数量" + "tt商品信息"+ "tt商品价格");for (int a = 0; a < saveNo.length; a+) / 循环输出购物车里的商品if (saveNoa != 0) System.out.println(saveNoa + "tt" + saveNamea + "tt"+ saveAmounta + "tt" + saveInfoa + "tt&q
12、uot;+ savePricea);big = false;System.out.println("n1.修 改 商 品 2.删 除 商 品 3.返 回");System.out.println("(如果你要查询库存请去购买商品)");int num = input.nextInt();switch (num) case 1:System.out.print("请选择需修改的商品编号:");int num1 = input.nextInt();for (int i = 0; i < saveNo.length; i+) / 循环
13、查找购物车内相对应是商品if (saveNoi = 0) break;if (saveNoi = num1) index1 = i;break;/for (int i = 0;i< manager.quotID.length; i+) / 循环查找货架上相对应是商品if (manager.quotIDi = 0) break;if (manager.quotIDi = num1) index2 = i;if (index1 != -1) System.out.print("请输入要修改的商品数量:");int xiugaiNum = input.nextInt();i
14、f (xiugaiNum > 0&& xiugaiNum < (manager.quotNumindex2 + saveAmountindex1) / 修改的数量的小于总数量manager.quotNumindex2 = manager.quotNumindex2+ saveAmountindex1;saveAmountindex1 = xiugaiNu m;manager.quotNumindex2 = manager.quotNumindex2- saveAmountindex1;System.out.println("修改成功!"); el
15、se System.out.println("库存商品不足,您不能修改!"); else System.out.println("没有您要修改的商品!");break;case 2:System.out.print("请输入要删除的商品编号:");int num2 = input.nextInt();for (int i = 0; i < saveNo.length; i+) / 循环查找购物车内的相对应是商品if (saveNoi = 0) break;if (saveNoi = num2) index1 = i;for (i
16、nt i = 0; i < manager.quotID.length; i+) / 循环查找货架上的相对应是商品if (manager.quotIDi = 0) break;if (manager.quotIDi = num2) index2 = i;if (index1 != -1) && (index2 != -1) /if (num2 = saveNoindex1) / 删除manager.quotNumindex2 = manager.quotNumindex2+ saveAmountindex1;if (index1 = saveNo.length - 1)
17、/ 删除最后一个商品saveNoindex1 = 0;/ 编号saveNameindex1 = null; / 名称savePriceindex1 = 0; / 价格saveInfoindex1 = null; / 信息saveAmountindex1 = 0; / 数量for (int i = index1; i < saveNo.length - 1; i+) saveNoi = saveNoi + 1; / 编号saveNamei = saveNamei + 1; / 名称savePricei = savePricei + 1; / 价格saveInfoi = saveInfoi
18、+ 1; / 信息saveAmounti = saveAmounti + 1; / 数量System.out.println("删除成功!"); else System.out.println("没有您要删除的商品!");/break;default:showShop(manager.quotID, manager.quotName, manager.quotNum,manager.quotInfo, manager.quotPrice);goumai(manager.quotID, manager.quotName, manager.quotNum,m
19、anager.quotInfo, manager.quotPrice);if (big) System.out.println("购物车暂时没有商品,请先购买商品然后再查看!");/* 用户在购买商品时,根据用户购买商品所花的钱来给用户加积分* */int allMoney = 0; / 购买商品,计算折扣前的总价int score = 100; / 定义初始积分。这里大家可以有多个会员进行登录,每个会员设置不同的积分变量存放各自的积分double zhekou = 0.0; / 具体的折扣数double pay; / 实际应付款数public double getAllM
20、oney() / 购买商品,计算折扣前的总价for (int i = 0; i < saveNo.length; i+) if (saveNoi = 0) break;allMoney = allMoney + savePricei * saveAmounti; / 价格累加return allMoney;public int getScore() / 购物所得的积分score = score + (int) (allMoney * 3 / 100);return score;/ 根据积分求折扣public double getZheKou() if (score < 200) z
21、hekou = 0.9; else if (score >= 200 && score < 400) zhekou = 0.8; else if (score >= 400 && score < 800) zhekou = 0.7; else if (score >= 800) zhekou = 0.6;return zhekou;/* 用户在选择 “保存商品订单”的时候,用来下订单,根据用户积分来算折扣,然后求出实际应该付的钱数* * param money:* 用户输入的钱数* */public double payPrice(
22、) / 打折后的价格pay = zhekou * allMoney;return pay;/ 用户窗体菜单类,调用总金额,积分,折扣等方法public void saveMenu() String answer = ""System.out.println("您购买商品的总价为:t" + allMoney + "元");System.out.println("您目前的积分是:t" + score + "分");System.out.println("根据您购物的金额,您享有:t&quo
23、t; + zhekou + "的折扣优惠!");System.out.println("您最后应付款为:t" + pay);do System.out.println("请输入您要付款的金额:t");double money = input.nextDouble();if (money < pay) / 输入的金额不足时System.out.println("不好意思您输入的金额不足!不能完成交易!" + "n是否继续交易(y/n):");answer = input.next(); el
24、se System.out.println("您好!交易成功!" + "n找回您:" + (money - pay);System.out.println("感谢您的购物,你可以享受本超市的抽奖活动!");System.out.println("规则:输入13"); / 结账完了进行抽奖活动int shuzi = input.nextInt();int random = (int) (Math.random() * 3) + 1);if (random = shuzi) System.out.println(&qu
25、ot;恭喜你,获得苹果电脑一台!欢迎下次光临"); else System.out.println("谢谢参与,欢迎下次光临!");break; while (answer.equals("y");import java.util.Scanner;public class ShopCar_Manager String userName = "manager" / 定义管理员的用户名和密码String password = "0000"String user = "" / 定义输入的管理
26、员用户名和密码String pwd = ""public int i = 0; / 用来记录输入的管理员用户名或密码错误的次数.定义成全局的是为了在测试类中能用到public int quotID = new int10; / 商品编号public String quotName = new String10; / 商品名称public int quotNum = new int10; / 商品数量public String quotInfo = new String10; / 商品介绍public int quotPrice = new int10; / 商品价格Scan
27、ner input = new Scanner(System.in);/* 初始化库存的商品信息*/public void initial() quotID0 = 1;quotName0 = "java"quotNum0 = 20;quotInfo0 = "真是太牛了!"quotPrice0 = 87;quotID1 = 2;quotName1 = "net"quotNum1 = 28;quotInfo1 = "太好 用了!"quotPrice1 = 69;quotID2 = 3;quotName2 = "
28、;c#"quotNum2 = 34;quotInfo2 = "太方便了!"quotPrice2 = 56;quotID3 = 4;quotName3 = "sql"quotNum3 = 42;quotInfo3 = "太实用啦!"quotPrice3 = 59;/编写管理员用户登录的方法public void login() boolean big = true;for (int i = 3; i > 0; i-) System.out.print("请输入管理员用户名:");user = input.next();System.out.print("请输入管理员密码:");
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2