• 抽象轻松java


    嗨嗨嗨!

    没想到吧,出现了抽象轻松第4种语言系列(我也没想到)

    简单的java程序,看完就懂的简单逻辑——购物车系统

    购物车,首先要有商品吧,现实中的商品有什么属性?

    名字,价格,数量,编码等等

    知道了这些属性,就应该考虑用什么来存放它们!

    是整数型?是浮点型?是对象?还是数组?

    很显然,数组就成了我们的首选!

    声明一个数组,用来存放商品的属性

    购物车系统包括了增删改查结算5个功能,除了这五个功能还有一个首页选择功能

    也就是说,一个购物车系统包括了6个功能,通过选择进入剩下的五个模块

    开始编写,为了照顾情绪,我们先做一件特别简单的事

    1. public class GoodsShop {
    2. public static void main(String[] args) {
    3. System.out.println("欢迎来到购物车管理系统");
    4. }
    5. }

    没错,先简单的输出一句话

    定义一个类,用来存放商品对象

    1. class obj {
    2. String name;//商品名
    3. int numb;//商品数量
    4. int id;//商品编码
    5. double money;//商品价格
    6. }

    商品的属性有了,该用车子装起来了吧,声明数组

    1. System.out.println("欢迎来到购物车管理系统");
    2. obj [] arr = new obj[50];//obj[50]为购物车的数量上限

    又因为我们的商品对象和测试类不在一起,所以想要使用obj类里面的方法是需要调用的

    加上我们要输入内容,随便也把Scanner一起调用

    1. System.out.println("欢迎来到购物车管理系统");
    2. obj [] arr = new obj[50];//obj[50]为购物车的数量上限
    3. obj obj = new obj();//调用obj
    4. Scanner scanner = new Scanner(System.in);//输入

    基础的工作做完了,改进入功能的编写了

    第一个功能编写,选择页面,我们使用循环输入值,通过判断,进入对应的方法

    循环有三个:do...while , while ,for

    选择语句,if ,if...else ,switch

    因为运行完一个功能就要跳回改页面,为了简单可以用while

    选择语句,总不可能写5个if...else吧,但是如果你愿意还是可以写5个的,因为他们有相同的逻辑

    使用switch()选择会更加的简单

    1. while (true) {
    2. System.out.println("输入1进入增加页面");
    3. System.out.println("输入2进入查看页面");
    4. System.out.println("输入3进入修改页面");
    5. System.out.println("输入4进入删除页面");
    6. System.out.println("输入5进入结算页面");
    7. double shuzi = scanner.nextDouble();
    8. switch ((int) shuzi) {
    9. case 1 : obj.add();break;//调用增
    10. case 2 : obj.cha();break;//调用查
    11. case 3 : obj.del();break;//调用删
    12. case 4 : obj.gai();break;//调用改
    13. case 5 : obj.sum();break;//调用结算
    14. default:
    15. System.out.println("请重新输入");
    16. }
    17. }

    现在代码就变成这样了,调用要有方法原型吧,在类里面把方法写入

    1. class obj {
    2. String name;//商品名
    3. int numb;//商品数量
    4. int id;//商品编码
    5. double money;//商品价格
    6. public static void add(){
    7. System.out.println("欢迎来到增加页面");
    8. }
    9. public static void cha(){
    10. System.out.println("欢迎来到查看商品页面");
    11. }
    12. public static void del(){
    13. System.out.println("欢迎来到删除页面");
    14. }
    15. public static void gai(){
    16. System.out.println("欢迎来到修改页面");
    17. }
    18. public static void sum(){
    19. System.out.println("欢迎来到结算页面");
    20. }
    21. }

    到了这里就先停下来了

    先完成大体的框架

    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();break;//调用增
    17. case 2 : obj.cha();break;//调用查
    18. case 3 : obj.del();break;//调用删
    19. case 4 : obj.gai();break;//调用改
    20. case 5 : obj.sum();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(){
    33. System.out.println("欢迎来到增加页面");
    34. }
    35. public static void cha(){
    36. System.out.println("欢迎来到查看商品页面");
    37. }
    38. public static void del(){
    39. System.out.println("欢迎来到删除页面");
    40. }
    41. public static void gai(){
    42. System.out.println("欢迎来到修改页面");
    43. }
    44. public static void sum(){
    45. System.out.println("欢迎来到结算页面");
    46. }
    47. }

    你的每一次输入,都会进入不同的页面

  • 相关阅读:
    webpack打包gz文件,nginx开启gzip压缩
    孙卫琴的《精通Vue.js》读书笔记-分割setup()函数
    【Redis】基础数据结构-ziplist压缩列表
    Springboot+vue微信小程序的学生选课系统#毕业设计
    解决 Tomcat 跨域问题 - Tomcat 配置静态文件和 Java Web 服务(Spring MVC Springboot)同时允许跨域
    JVM学习(三)--运行时数据区
    如何使用Spring提供的Retry
    css基本选择器
    JavaScript中 对象解构详解
    WFST--学习笔记
  • 原文地址:https://blog.csdn.net/c_yanxin_ru/article/details/133325891