ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

第8章 综合案例:商品管理系统

第8章 综合案例:商品管理系统 一、pojo包下创建Goods类package com.itheima.pojo; ​ public class Goods { private String id; private String name; private double price; private String desc; ​ public Goods(String id, String name, double price, String desc) { this.id id; this.name name; this.price price; this.desc desc; } ​ public Goods() { } ​ public String getId() { return id; } ​ public void setId(String id) { this.id id; } ​ public String getName() { return name; } ​ public void setName(String name) { this.name name; } ​ public double getPrice() { return price; } ​ public void setPrice(double price) { this.price price; } ​ public String getDesc() { return desc; } ​ public void setDesc(String desc) { this.desc desc; } ​ Override public String toString() { return Goods{ id id \ , name name \ , price price , desc desc \ }; } }二、start包下创建App类package com.itheima.start; ​ import com.itheima.pojo.Goods; ​ import java.util.ArrayList; import java.util.Scanner; ​ public class App { //增删改查都是围绕着这个集合进行的所以可以提取到成员变量的位置。要使用静态方法 static ArrayListGoods list new ArrayList(); ​ static Scanner sc new Scanner(System.in); //静态代码块在程序启动时初始化且只执行一次 static { list.add(new Goods(001, 华为平板, 3999, 平板电脑)); list.add(new Goods(002, 华为手机, 6999, 手机)); list.add(new Goods(003, 华为电脑, 8999, 电脑)); } public static void main(String[] args) { ​ lo: while (true) { System.out.println(-------------欢迎使用商品管理系统--------------); System.out.println(1. 添加商品); System.out.println(2. 删除商品); System.out.println(3. 修改商品); System.out.println(4. 查询全部商品); System.out.println(5. 查询单个商品); System.out.println(6. 退出商品); System.out.println(-----------------------------------------------); System.out.println(请输入您的选择); ​ String choice sc.next(); ​ switch (choice) { case 1: addGoods(); break; case 2: deleteGoodsById(); break; case 3: upDateGoodeById(); break; case 4: queryAllGoods(); break; case 5: queryGoodsById(); break; case 6: System.out.println(感谢您的使用再见); System.exit(0);//终止当前虚拟机 高级 break lo;//终止lo: 低级 default: System.out.println(您输入的有误请重新检查后输入); } } } ​ private static void addGoods() { System.out.println(请输入商品编号: ); String id sc.next(); if (getIndex(id) ! -1) { System.out.println(您输入的编号已存在); return; } System.out.println(请输入商品名称: ); String name sc.next(); System.out.println(请输入商品价格: ); double price sc.nextDouble(); System.out.println(请输入商品描述: ); String desc sc.next(); Goods goods new Goods(id, name, price, desc); list.add(goods); } ​ private static void queryGoodsById() { System.out.println(请输入您要查询的商品编号: ); String id sc.next(); int index getIndex(id); if (index -1) { System.out.println(您输入的编号不存在); }else { System.out.println(查询结果如下: ); Goods goods list.get(index); System.out.println(goods); } } ​ private static void upDateGoodeById() { System.out.println(请输入您要修改的商品编号: ); String id sc.next(); int index getIndex(id); if (index -1) { System.out.println(您输入的编号不存在); }else { System.out.println(请输入您要修改的商品名称: ); String name sc.next(); System.out.println(请输入您要修改的商品价格: ); double price sc.nextDouble(); System.out.println(请输入您要修改的商品描述: ); String desc sc.next(); Goods goods new Goods(id, name, price, desc); list.set(index,goods); System.out.println(修改成功); } } ​ private static void deleteGoodsById() { System.out.println(请输入您要删除的商品编号: ); String id sc.next(); ​ int index getIndex(id); if (index -1) { System.out.println(您输入的编号不存在); } else { list.remove(index); System.out.println(删除成功); } } ​ /** * 查看全部商品信息 */ private static void queryAllGoods() { System.out.println(商品信息如下: ); for (int i 0; i list.size(); i) { Goods goods list.get(i); System.out.println(goods); } } ​ public static int getIndex(String id) { for (int i 0; i list.size(); i) { Goods goods list.get(i); if (goods.getId().equals(id)) return i; } return -1;//没找到返回-1 } }
返回列表