• 抽象轻松的java——简单的购物车系统


    1. public class GoodsShop {
    2. public static void main(String[] args) {
    3. System.out.println("欢迎来到购物车管理系统");
    4. obj [] arr = new obj[50];//obj[50]为购物车的数量上限
    5. obj obj = new obj();//调用obj
    6. Scanner scanner = new Scanner(System.in);//输入
    7. while (true) {
    8. System.out.println("输入1进入增加页面");
    9. System.out.println("输入2进入查看页面");
    10. System.out.println("输入3进入修改页面");
    11. System.out.println("输入4进入删除页面");
    12. System.out.println("输入5进入结算页面");
    13. double shuzi = scanner.nextDouble();
    14. switch ((int) shuzi) {
    15. case 1 : obj.add();break;//调用增
    16. case 2 : obj.cha();break;//调用查
    17. case 3 : obj.del();break;//调用删
    18. case 4 : obj.gai();break;//调用改
    19. case 5 : obj.sum();break;//调用结算
    20. default:
    21. System.out.println("请重新输入");
    22. }
    23. }
    24. }
    25. }
    26. class obj {
    27. String name;//商品名
    28. int numb;//商品数量
    29. int id;//商品编码
    30. double money;//商品价格
    31. public static void add(){
    32. System.out.println("欢迎来到增加页面");
    33. }
    34. public static void cha(){
    35. System.out.println("欢迎来到查看商品页面");
    36. }
    37. public static void del(){
    38. System.out.println("欢迎来到删除页面");
    39. }
    40. public static void gai(){
    41. System.out.println("欢迎来到修改页面");
    42. }
    43. public static void sum(){
    44. System.out.println("欢迎来到结算页面");
    45. }
    46. }

    上面部分的图片与代码配合来看更容易理解

    正式进入个个部分的具体内容编程

    第一部分:添加商品页面

    这一模块的作用,使用者通过键盘输入内容,将内容以一个数组存放

    我们就要传入两个值,一个是键盘输入,一个是数组

    1. public static void add(obj[]arr,Scanner scanner){//传值
    2. System.out.println("欢迎来到增加页面");
    3. System.out.println("输入商品名");
    4. String name1 = scanner.next();
    5. System.out.println("输入商品ID");
    6. int id1 = scanner.nextInt();
    7. System.out.println("输入商品数量");
    8. int shuliang1 = scanner.nextInt();
    9. System.out.println("输入商品价格");
    10. double money1 = scanner.nextDouble();
    11. }

    提示与输入值顺带的也写好

    将输入的值,封装成goods对象

    1. //封装成一个对象
    2. obj goods = new obj();
    3. goods.id = id1;
    4. goods.money = money1;
    5. goods.name = name1;
    6. goods.numb = shuliang1;

    然后封装的对象用循环挨个装进数组中

    1. //挨个装进去
    2. for (int i = 0; i < arr.length; i++)
    3. {
    4. arr[i] = goods;
    5. }
    6. System.out.println("成功将"+ goods.name + "装入购物车");

    第二个模块,查看商品

    商品是用一个数组装着的,那么怎么查看数组就是怎么查看商品

    所以,需要给cha()方法传入一个数组

    1. public static void cha(obj [] arr){//传入方法
    2. System.out.println("欢迎来到查看商品页面");
    3. System.out.println("===========================");
    4. System.out.println("ID" + "\t" + "\t" +"商品名" + "\t" + "商品数量" + "\t" + "商品价格");
    5. }

    然后输出数组

    1. for (int i = 0 ; i < arr.length ; i++)
    2. {
    3. obj good = arr[i];
    4. if (arr[i] != null)
    5. {
    6. System.out.println(good.id + "\t" + good.name + "\t" + "\t" +good.numb +"\t"+"\t" + good.money);
    7. }
    8. else
    9. break;
    10. }
    11. }

    为什么要用选择语句?看看不用的情况

    没错,他直接把整个购物车充满了,我们定义了数组的数量为50,又因为传址,直接将全部值变成了一样

    这个选择语句的作用是,当arr[i] 无值的时候跳出程序,这样就不会有多余的数组了

    当你用了if判断的时候,又出现bug了,还是一直输出怎么办!

    回过头来看看

    1. //挨个装进去
    2. for (int i = 0; i < arr.length; i++)
    3. {
    4. if (arr[i] == null) {
    5. arr[i] = goods;
    6. break;
    7. }
    8. }

    应该要在挨个添加进数组的时候进行一个选择,这样就可以防止传址

    大致的介绍一下

    他们用的是同一个东西,A改B改,B改A改

    第三个模块——删除商品

    前面介绍了null这个值,就是当数组是空的时候会显示bull

    现在数组不是空的,就让它变成空的

    先完成一个简单,传入一个数组

    1. public static void del(obj [] arr){
    2. System.out.println("欢迎来到删除页面");
    3. }

    用循环将每个数组输出,如果有值就将值变成null,没值就跳出

    1. for (int i = 0;i < arr.length;i++)
    2. {
    3. obj goods = arr[i];
    4. if (arr[i] != null)
    5. {
    6. arr[i] = null;
    7. }
    8. else
    9. break;
    10. }

    第四个模块——修改商品数量

     商品是数组,修改要输入,所以需要传入一个数组,一个输入

    1. public static void gai(obj [] arr , Scanner scanner){
    2. System.out.println("欢迎来到修改页面");
    3. }

    第二步,要进行一个选择,判断它到底是不是购物车里面的值

    为了区分修改和重复使用改功能,我们另外声明一个方法

    传入两个值,一个商品,一个商品值,根据id会简单点,因为id在现实中是很难进行改变的

    1. public static obj getID(obj [] arr,int id)
    2. {
    3. }

    还是通过循环,将对象的值传给数组,进行选择,如果等于返回对象

    1. for (int i = 0 ; i < arr.length ; i++)
    2. {
    3. obj goods = arr[i];
    4. if (goods.id == id)
    5. return goods;
    6. else
    7. return null;
    8. }
    9. return null;//遍历50次还是空

    正式的开始写修改

    先来点简单的——来个提示和输入

    1. System.out.println("欢迎来到修改页面");
    2. System.out.println("输入修改商品的ID");
    3. int nub = scanner.nextInt();

    用封装对象调用刚刚的方法

    obj goods = getID(arr,nub);

    根据那个方法,如果我们输入的值,符合购物车里的id,就会将与这个id相同的数组返回

    进入选择语句,如果等于就修改,不等于就输入提示

    数字的交换,如何将A,b的值交换,就能完成修改

    1. while (true) {
    2. System.out.println("输入修改商品的ID");
    3. int nub = scanner.nextInt();
    4. obj goods = getID(arr, nub);
    5. if (nub != goods.id) {
    6. System.out.println("没有该商品");
    7. } else {
    8. System.out.println("修改" + goods.name + "的数量");
    9. int nub1 = scanner.nextInt();
    10. goods.numb = nub1;
    11. System.out.println("修改成功");
    12. cha(arr);
    13. break;
    14. }
    15. }

    因为不可能只用一次,可能会进行多次修改,所以使用一个循环

    为了更好的看出来,我们调用一次cha()方法

    哎!到这里为什么会变成删除,那是因为我在开始的代码中把3和4的提示打错字了!!!

    第五个模块——结算金额

    给个思路,自己写

    因为是结算购物车的金额,所以需要传入一个数组

    传入完数组后,通过循环,将封装对象挨个传入数组

    进行IF选择语句,当数组的值不为空,就调用个数,金额进行一个乘法运算

    *

    *

    *

    *

    *

    *

    *

    全代码

    1. import java.util.Scanner;
    2. public class GoodsShop {
    3. public static void main(String[] args) {
    4. System.out.println("欢迎来到购物车管理系统");
    5. obj [] arr = new obj[50];//obj[50]为购物车的数量上限
    6. obj obj = new obj();//调用obj
    7. Scanner scanner = new Scanner(System.in);//输入
    8. while (true) {
    9. System.out.println("输入1进入增加页面");
    10. System.out.println("输入2进入查看页面");
    11. System.out.println("输入3进入删除页面");
    12. System.out.println("输入4进入修改页面");
    13. System.out.println("输入5进入结算页面");
    14. double shuzi = scanner.nextDouble();
    15. switch ((int) shuzi) {
    16. case 1 : obj.add(arr,scanner);break;//调用增
    17. case 2 : obj.cha(arr);break;//调用查
    18. case 3 : obj.del(arr);break;//调用删
    19. case 4 : obj.gai(arr,scanner);break;//调用改
    20. case 5 : obj.sum(arr);break;//调用结算
    21. default:
    22. System.out.println("请重新输入");
    23. }
    24. }
    25. }
    26. }
    27. class obj {
    28. String name;//商品名
    29. int numb;//商品数量
    30. int id;//商品编码
    31. double money;//商品价格
    32. public static void add(obj[]arr,Scanner scanner){//传值
    33. System.out.println("欢迎来到增加页面");
    34. System.out.println("输入商品名");
    35. String name1 = scanner.next();
    36. System.out.println("输入商品ID");
    37. int id1 = scanner.nextInt();
    38. System.out.println("输入商品数量");
    39. int shuliang1 = scanner.nextInt();
    40. System.out.println("输入商品价格");
    41. double money1 = scanner.nextDouble();
    42. //封装成一个对象
    43. obj goods = new obj();
    44. goods.id = id1;
    45. goods.money = money1;
    46. goods.name = name1;
    47. goods.numb = shuliang1;
    48. //挨个装进去
    49. for (int i = 0; i < arr.length; i++)
    50. {
    51. if (arr[i] == null) {
    52. arr[i] = goods;
    53. break;
    54. }
    55. }
    56. System.out.println("成功将"+ goods.name + "装入购物车");
    57. }
    58. public static void cha(obj [] arr){
    59. System.out.println("欢迎来到查看商品页面");
    60. System.out.println("===========================");
    61. System.out.println("ID" + "\t" + "\t" +"商品名" + "\t" + "商品数量" + "\t" + "商品价格");
    62. for (int i = 0 ; i < arr.length ; i++)
    63. {
    64. obj good = arr[i];
    65. if (arr[i] != null)
    66. {
    67. System.out.println(good.id + "\t" + good.name + "\t" + "\t" +good.numb +"\t"+"\t" + good.money);
    68. }
    69. else
    70. break;
    71. }
    72. }
    73. public static void del(obj [] arr){
    74. System.out.println("欢迎来到删除页面");
    75. for (int i = 0;i < arr.length;i++)
    76. {
    77. obj goods = arr[i];
    78. if (arr[i] != null)
    79. {
    80. arr[i] = null;
    81. }
    82. else
    83. break;
    84. }
    85. }
    86. public static void gai(obj [] arr , Scanner scanner){
    87. System.out.println("欢迎来到修改页面");
    88. while (true) {
    89. System.out.println("输入修改商品的ID");
    90. int nub = scanner.nextInt();
    91. obj goods = getID(arr, nub);
    92. if (nub != goods.id) {
    93. System.out.println("没有该商品");
    94. } else {
    95. System.out.println("修改" + goods.name + "的数量");
    96. int nub1 = scanner.nextInt();
    97. goods.numb = nub1;
    98. System.out.println("修改成功");
    99. break;
    100. }
    101. }
    102. }
    103. public static obj getID(obj [] arr,int id)
    104. {
    105. for (int i = 0 ; i < arr.length ; i++)
    106. {
    107. obj goods = arr[i];
    108. if (goods.id == id)
    109. return goods;
    110. else
    111. return null;
    112. }
    113. return null;//遍历50次还是空
    114. }
    115. public static void sum(obj[]arr){
    116. System.out.println("欢迎来到结算页面");
    117. for (int i = 0 ; i < arr.length ; i++)
    118. {
    119. obj goods = arr[i];
    120. if (arr[i] != null)
    121. {
    122. System.out.println(goods.numb * goods.money + "元");
    123. }
    124. else
    125. break;
    126. }
    127. }
    128. }

    其中用的最多的语句是迭代语句和选择语句

    其次就是封装的使用

    本质上就是通过不同的语句对封装对象的操作

  • 相关阅读:
    大模型LLM深入浅出、主打通俗易懂
    rsync下行同步+inotify实时同步部署
    JS: 运算符
    4本期刊被踢!11月SCI/SSCI目录已更新
    argparse.ArgumentParser() 用法解析
    广西螺蛳粉:“超级农货”养成记
    MySQL数据库与表管理《三国志》为例
    [AHK]安信猎豹自动下单
    CANoe面板中的控件:Switch/Indicator
    浅谈叉车车载电脑的市场现状
  • 原文地址:https://blog.csdn.net/c_yanxin_ru/article/details/133344369