Goods: package demo3; public class Goods { private int id; private String goodsName; private double price; private String desc; public Goods(int id, String goodsName, double price, String desc) { this.id = id; this.goodsName = goodsName; this.price = price; this.desc = desc; } public Goods() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public double getPrice() { return price; } public void setPrice(int 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 + ", goodsName='" + goodsName + '\'' + ", price='" + price + '\'' + ", desc='" + desc + '\'' + '}'; } }
Inpututils:
public class Inpututils { public static int getNum(){ int n = 0; try { n = new Scanner(System.in).nextInt(); }catch (Exception e){ //e.printStackTrace(); //如果输入了非数字就会抛出异常 被catch语句捕获。在这里处理异常 System.out.println("-->警告,输入非法数字!!,请重新输入"); n =getNum(); } if (n<0||n>4) { System.out.println("警告:非法命令!!请重新输入"); n = getNum(); } return n; } public static int getGoodsId(){ int n = 0; try { n = new Scanner(System.in).nextInt(); } catch (Exception e) { e.printStackTrace(); System.out.println("--->警告:输入非法数字!!请重新输入"); n=getGoodsId(); } return n; } public static double getPrice(){ double n =0; try { n = new Scanner(System.in).nextDouble(); } catch (Exception e) { e.printStackTrace(); System.out.println("---->警告:输入非法数字!!请重新输入"); } return n; } }
Counter:
public class Counter { private Goods[] goodses = new Goods[10]; private int num; // int num = 0; public Counter() { this.goodses[0] = new Goods(1001, "巧克力", 25, "美味可口,恋爱必备!"); this.goodses[1] = new Goods(1002, "卫龙辣条", 1, "隔壁小孩馋哭了!"); num=2; } public void show() { System.out.println("-----柜台的商品列表"); if (num == 0) { System.out.println("没有"); return; } for (int i = 0; i < goodses.length; i++) { if (this.goodses[i] != null) { System.out.println(this.goodses[i]); } } System.out.println("------------"); } public void main() { while (true) { System.out.println("-------------"); System.out.println("---1.展示商品 2.上架商品 -----"); System.out.println("------3.下架商品 4.调整价格----"); System.out.println("------0退出-----"); //选择功能,控制台输入. System.out.println("---->请输入功能编号:"); int key = Inpututils.getNum(); switch (key) { case 1: show(); break; case 2: add(); break; case 3: delete(); break; case 4: update(); break; case 0: System.exit(0); } } } private void update() { System.out.println("--请输入要修改的商品id--"); int id = new Scanner(System.in).nextInt(); if (id>5 && id<1 ) { System.out.println("--商品不存在---"); return; }else { System.out.println("修改名字"); String name = new Scanner(System.in).nextLine(); System.out.println("修改价格"); Double price = Inpututils.getPrice(); System.out.println("---商品描述----"); String desc = new Scanner(System.in).next(); //将输入的信息转载到goods对象 Goods newGoods = new Goods(id,name,price,desc); for (int i = 0; i < this.goodses.length; i++) { if (i==id-1){ this.goodses[i]=newGoods; System.out.println("修改成功"); return; } } } } private void delete() { System.out.println("----请输入要下架的商品编号;"); int goodsId = Inpututils.getGoodsId(); //第一件事,找到这个对应商品id的商品对象 for (int i = 0; i < this.goodses.length; i++) { if (this.goodses[i] != null && this.goodses[i].getId() == goodsId) { //找到以后,删除? this.goodses[i] = null; //商品数量 this.num--; System.out.println("-->提示下架成功!!当前商品数量" + this.num); return; //还需不需要继续循环? // break;//结束循环 } } System.out.println("--->警告:未找到商品!!"); } private void add() { //判断是否空余位置 if (this.num == this.goodses.length) { System.out.println("--->警告:柜台已满,不能再添加商品了"); return; } System.out.println("--->请输入商品id:"); int goodsId = Inpututils.getGoodsId(); for (int i = 0; i < this.goodses.length; i++) { if (this.goodses[i] != null && this.goodses[i].getId() == goodsId) { System.out.println("--->警告,当前编号已存在!!请重新输入"); add(); return; } } //程序能走到这里说明没有重复 System.out.println("--->提示:商品id可以使用!!!"); System.out.println("--->请输入商品名称"); String name = new Scanner(System.in).next(); System.out.println("-->请输入商品价格"); double price = Inpututils.getPrice(); System.out.println("-->请输入商品描述"); String desc = new Scanner(System.in).next(); Goods newGoods = new Goods(goodsId,name,price,desc); //判断是否有空余位置 for (int i = 0; i < this.goodses.length; i++) { if (this.goodses[i] == null) { this.goodses[i] = newGoods; this.num++; System.out.println("-->提示:商品上架成功!!柜台商品数量"+this.num); return; } } } }
CounterTest public class CounterTest { public static void main(String[] args) { Counter c = new Counter(); c.main(); } }