請問 我的 修改商品數量的方法要怎麼改寫才不會發生空指針
//測試主體 package com.imooc.test; import java.util.Scanner; import com.imooc.model.GoodsInCart; import com.imooc.model.GoodsMange; import com.imooc.model.ShoppingCart; public class TestDemo { public static void main(String[] args) { boolean button1 = true; boolean button2 = true; boolean button3 = true; ShoppingCart shopCart=new ShoppingCart(); Scanner input = new Scanner(System.in); GoodsMange gM = new GoodsMange(); int menu1 = 0; int menu2 = 0; while (button1) { button2=true; button3=true; System.out.println("**************"); System.out.println("menu"); System.out.println("1.商品管理"); System.out.println("2.購物車"); System.out.println("0.退出"); System.out.println("**************"); System.out.println("輸入對應數字"); try { menu1 = input.nextInt(); } catch (Exception e) { System.out.println("請輸入整數"); input.next(); continue; } switch (menu1) { case 1: { while (button2) { System.out.println("商品管理"); System.out.println("1.商品訊息導入"); System.out.println("2.顯示商品訊息"); System.out.println("9.返回"); System.out.println("輸入對應數字"); try { menu2 = input.nextInt(); } catch (Exception e) { System.out.println("請輸入整數"); input.next(); continue; } switch (menu2) { case 1: { gM.importGoods(); break; } case 2: { gM.displayAllGoods(); break; } case 9: { button2=false; break; } default: { System.out.println("輸入錯誤請重新輸入對應數字"); } } } break; } case 2: { while (button3) { System.out.println("購物車管理"); System.out.println("1.添家商品到購物車"); System.out.println("2.修改購物車商品數量"); System.out.println("3.顯示購物車中所有商品"); System.out.println("4.結算"); System.out.println("9.返回"); try { menu2 = input.nextInt(); } catch (Exception e) { System.out.println("請輸入整數"); input.next(); continue; } switch (menu2) { case 1: { shopCart.addGoodsToCart(gM); break; } case 2: { shopCart.updateNumInCart(); break; } case 3: { shopCart.displayAllInCart(); break; } case 4: { shopCart.settleAccounts(); break; } case 9: { button3=false; break; } default: { System.out.println("輸入錯誤請重新輸入對應數字"); } } } break; } case 0: { button1 = false; break; } default: { System.out.println("輸入錯誤請重新輸入對應數字"); } } } } }
//購物車 package com.imooc.model; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Scanner; import java.util.Set; public class ShoppingCart { private Map<String, GoodsInCart> shoppingCart = new HashMap<String, GoodsInCart>(); Scanner input = new Scanner(System.in); public ShoppingCart() { } public ShoppingCart(Map<String, GoodsInCart> shoppingCart) { this.shoppingCart = shoppingCart; } public Map<String, GoodsInCart> getShoppingCart() { return shoppingCart; } public void setShoppingCart(Map<String, GoodsInCart> shoppingCart) { this.shoppingCart = shoppingCart; } // 添家商品到購物車裡 /** * @addGoodsToCart 加入商品到購物車裡 * @param gm * 導入商品列表gm */ public void addGoodsToCart(GoodsMange gm) { // 把商品存到set Set<Goods> set = gm.getGoodsSet(); String a = null; boolean flag = false; Goods goods = null; int num = 0; // 如果商品列表的長度==0的話代表gm集合中沒有商品列 if (gm.getGoodsSet().size() == 0) { System.out.println("無商品表"); } else { // 先秀出商品列表 gm.displayAllGoods(); // 開始輸入要購買的編號 System.out.println("請輸入要購買的編號:"); a = input.next(); // 利用for強化迴圈進行掃描 for (Goods o : set) { // 如果有跟鍵盤輸入編號一樣的話 if (o.getGoodsId().equals(a)) { // 開啟外部購買數量flag開關 flag = true; // 把"o"附值到goods類方便HashMap集合使用 goods = o; // 跳出迴圈 break; } } } // 如果flag=true開啟數量開關 if (flag) { System.out.println("請輸入要購買的數量:"); // 使用try catch 防止輸入數字以外的輸入 try { num = input.nextInt(); } catch (Exception e) { System.out.println("請輸入整數"); input.next(); } // 使用 GoodsInCart 來封裝 "goods 商品以及 數量"方便 map類的shoppingCart 來使用 GoodsInCart gic = new GoodsInCart(goods, num); // shoppingCart.put(K,V) this.shoppingCart.put(a, gic); } else { System.out.println("無此商品"); } } /** * @displayAllInCart() 顯示購物車裡面的商品 */ public void displayAllInCart() { // 如果HashMap類的shoppingCart 商品長度 >0的話往下執行 if (shoppingCart.size() > 0) { // 創建一個迭代器 it 用while 迴圈來執行商品輸出 Iterator<GoodsInCart> it = shoppingCart.values().iterator(); while (it.hasNext()) { // 使用附值轉換(拆箱) shoppingCart(map(k(商品編號),v(GoodsInCart gic)))->Iterator -> // GoodsInCart(商品,數量)-> Goods 在使用goods 來讀取裡面的值 System.out.println("商品訊息"); GoodsInCart gic = it.next(); Goods g = gic.getGoods(); System.out.println("商品編號:" + g.getGoodsId() + ",商品名稱:" + g.getGoodName() + ",商品價格:" + g.getPrice() + ",商品介紹:" + g.getGoodsDesp()+",商品數量:"+gic.getNum()); } } } /** * @settleAccounts 結算 */ public void settleAccounts() { double sum = 0; if (shoppingCart.size() <= 0) { System.out.println("購物車內無商品不可結算"); } else { Set<String> set = shoppingCart.keySet(); for (String key : set) { sum = sum + (shoppingCart.get(key).getGoods().getPrice()) * (shoppingCart.get(key).getNum()); } System.out.println("總金額為:" + sum); } } /** * @updateNumInCart() 修改商品數量 */ public void updateNumInCart() { Set<String> set =shoppingCart.keySet(); String s=null; int n=0; for(String str:set) { System.out.println("要修改的編號:"); s=input.next(); if(str.equals(s)) { System.out.println("商品數量:"); n=input.nextInt(); if(n==00) { shoppingCart.remove(str); } shoppingCart.get(set).setNum(n); } } } }
//購物車數量 package com.imooc.model; public class GoodsInCart { private Goods goods; private int num; public GoodsInCart() { } public GoodsInCart(Goods goods, int num) { this.setGoods(goods); this.setNum(num); } public Goods getGoods() { return goods; } public void setGoods(Goods goods) { this.goods = goods; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } }
//商品類 package com.imooc.model; public class Goods { private String goodsId; private String goodName; private double price; private String goodsDesp; public Goods() { } public Goods(String goodsId, String goodName, double price, String goodsDesp) { this.setGoodsId(goodsId); this.setGoodName(goodName); this.setPrice(price); this.setGoodsDesp(goodsDesp); } public String getGoodsId() { return goodsId; } public void setGoodsId(String goodsId) { this.goodsId = goodsId; } public String getGoodName() { return goodName; } public void setGoodName(String goodName) { this.goodName = goodName; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getGoodsDesp() { return goodsDesp; } public void setGoodsDesp(String goodsDesp) { this.goodsDesp = goodsDesp; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((goodName == null) ? 0 : goodName.hashCode()); result = prime * result + ((goodsDesp == null) ? 0 : goodsDesp.hashCode()); result = prime * result + ((goodsId == null) ? 0 : goodsId.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if(this==obj) { return true; } if(obj.getClass()==Goods.class) { Goods goods=(Goods)obj; return (goods.getGoodsId().equals(goodsId)); } return false; } public String toString() { return "商品編號:"+this.getGoodsId()+" 商品名稱:"+this.getGoodName()+" 商品售價:"+this.getPrice() +" 商品介紹:"+this.getGoodsDesp(); } }
//商品管理 package com.imooc.model; import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; import java.util.Set; public class GoodsMange { Scanner input = new Scanner(System.in); private Set<Goods> goodsSet = new HashSet<Goods>(); public GoodsMange() { } public GoodsMange(Set<Goods> goodsSet) { this.setGoodsSet(goodsSet); } public Set<Goods> getGoodsSet() { return goodsSet; } public void setGoodsSet(Set<Goods> goodsSet) { this.goodsSet = goodsSet; } public void importGoods() { double goodsPrice = 0; boolean button = true; boolean button1 = true; int a = 0; Goods goods1 = new Goods("a01", "手機", 200, "手機"); Goods goods2 = new Goods("a02", "電線", 200, "電線"); Goods goods3 = new Goods("a03", "電", 200, "電"); Goods goods4 = new Goods("a04", "手", 200, "手"); goodsSet.add(goods1); goodsSet.add(goods2); goodsSet.add(goods3); goodsSet.add(goods4); System.out.println("導入成功"); } public void displayAllGoods() { Iterator<Goods> it = goodsSet.iterator(); while (it.hasNext()) { System.out.println(it.next()); } System.out.println(); } }
在 執行到 商品數量修改時候
購物車方法 169行 就跳出空指針錯誤
請問該怎麼改寫才不會發生??
24
收起
正在回答
4回答
建议同学将输入修改编号的代码写到循环外面;
另外获取shoppingCart中的某个商品,应该是get(key)也就是str。
祝学习愉快!
慕后端1192210
2018-04-20 18:00:39
還是一樣
慕后端1192210
2018-04-20 15:00:41
抱歉 172行才對 shoppingCart.get(set).setNum(n); 發生空指針
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星