救命呀老师!我想把购物车类里的《商品信息》和《购买数量》添加不进去购物车的信息类里
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | package com.imooc.shoop; public class Goods { //商品类 //属性:编号,名称,价格,描述 private String goodsId; private String goodsName; private double goodsPrice; private String goodsDesp; public Goods() { } public Goods(String goodsId,String goodsName, double goodsPrice,String goodsDesp) { setGoodsId(goodsId); setGoodsName(goodsName); setGoodsPrice(goodsPrice); setGoodsDesp(goodsDesp); } public String getGoodsId() { return goodsId; } public void setGoodsId(String goodsId) { this .goodsId=goodsId; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this .goodsName=goodsName; } public double getGoodsPrice() { return goodsPrice; } public void setGoodsPrice( double goodsPrice) { this .goodsPrice=goodsPrice; } public String getGoodsDesp() { return goodsDesp; } public void setGoodsDesp(String goodsDesp) { this .goodsDesp=goodsDesp; } public String toString() { return "商品信息[商品编号:" +goodsId+ ",商品名称:" +goodsName+ ",商品价格" +goodsPrice+ ",商品描述:" +goodsDesp+ "]" ; } @Override public int hashCode() { final int prime = 31 ; int result = 1 ; result = prime * result + ((goodsDesp == null ) ? 0 : goodsDesp.hashCode()); result = prime * result + ((goodsId == null ) ? 0 : goodsId.hashCode()); result = prime * result + ((goodsName == null ) ? 0 : goodsName.hashCode()); long temp; temp = Double.doubleToLongBits(goodsPrice); 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)&&goods.getGoodsName().equals(goodsName)&& goods.getGoodsPrice()==goodsPrice&&goods.getGoodsDesp().equals(goodsDesp); } return true ; } } ================================================================================================ package com.imooc.shoop; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.omg.PortableServer.ImplicitActivationPolicyOperations; public class GoodsManage { // 商品管理类 // 属性:存放商品的容器goodsSet(HashSet集合,Set类型) private Set<Goods> goodsSet = new HashSet(); // 构造方法 public GoodsManage() { } public Set getGoodsSet() { return goodsSet; } public void setGoodsSet(Set goodsSet) { this .goodsSet = goodsSet; } // 商品信息导入 public void importGoods() { Goods goods1 = new Goods( "goods001" , "水杯" , 56 , "不锈钢水杯" ); Goods goods2 = new Goods( "goods002" , "饮水机" , 299 , "带净化功能的饮水机" ); Goods goods3 = new Goods( "goods003" , "笔记本电脑" , 4999 , "15寸笔记本" ); Goods goods4 = new Goods( "goods004" , "手机" , 2300 , "android手机" ); goodsSet.add(goods1); goodsSet.add(goods2); goodsSet.add(goods3); goodsSet.add(goods4); System.out.println( "商品信息导入" ); System.out.println( "导入成功!" ); } // 显示所有商品信息 public void displayAllGoods() { System.out.println( "所有商品信息为:" ); Iterator<Goods> it = goodsSet.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } public static void mian(String[] args) { } } ============================================================================================== package com.imooc.shoop; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; public class GoodsInCart { // 购物车中信息类(这里打算用HashMap集合来完成对象和购买数量的Key-Value来存储多条信息) // -商品信息(goods):Goods类型 -商品数量(num):int类型 private Goods goods; private int num; Map<Goods, Integer> goodsMap = new HashMap<Goods, Integer>(); public GoodsInCart() { } public GoodsInCart(Goods goods, int num) { goodsMap.put(goods, 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; } // 显示购物车中的所有信息 public void show() { System.out.println( "显示购物车中的所有商品信息" ); if (goodsMap.isEmpty()) { System.out.println( "你的购物车空空如也,赶紧去添加商品吧!" ); } else { Set<Entry<Goods, Integer>> set = goodsMap.entrySet(); for (Entry<Goods, Integer> entry : set) { System.out.println( "商品名称:" + entry.getKey().getGoodsName() + ",商品价格:" + entry.getKey().getGoodsPrice() + ",商品描述:" + entry.getKey().getGoodsDesp() + ",数量:" + entry.getValue()); } } } // 修改购物车中的商品数量 public void modify() { if (goodsMap.isEmpty()) { System.out.println( "您的购物车空空如也,赶紧去添加商品吧!" ); } else { System.out.println( "修改购物车中的商品数量" ); System.out.println( "请输入要修改的商品编号" ); Scanner console = new Scanner(System.in); String strId = console.next(); Set<Entry<Goods, Integer>> set = goodsMap.entrySet(); for (Entry<Goods, Integer> entry : set) { if (entry.getKey().getGoodsId().equals(strId)) { System.out.println( "请输入新的商品数量:" ); int intNum; try { intNum = console.nextInt(); } catch (InputMismatchException e) { System.out.println( "你输入的不是正整数请重新输入!" ); return ; } if (intNum > 0 ) { int i = entry.getValue(); i = intNum; break ; } else if (intNum == 0 ) { goodsMap.remove(entry); break ; } } else { System.out.println( "你输入的商品编号没在你的购物车中,请核对后在输入!" ); } } } } // 结算:这里想用goodsMap集合的把Integer对象转换成int然后调用来求值 } =========================================================================================== package com.imooc.shoop; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Iterator; import java.util.Map; import java.util.Scanner; public class ShoppingCart { // 购物车类 // 属性:购物车(shoppingCart):Map类型 /* * -Key为String类型,商品编号 -Value为GoodsInCart类型,购物车中的信息 */ private Map shoppingCart = new HashMap(); int i; public ShoppingCart() { } public ShoppingCart(Map shoppingCart) { } public Map getShoppingCart() { return shoppingCart; } public void setShoppingCart(Map shoppingCart) { this .shoppingCart = shoppingCart; } Scanner console = new Scanner(System.in); public void addGoodsToCart(GoodsManage gm) { Goods goods = null ; boolean flag = false ; int i = 0 ; GoodsInCart goodsInCart = new GoodsInCart(); for (;;) { System.out.println( "添加商品到购物车" ); gm.importGoods(); gm.displayAllGoods(); System.out.println( "请输入要添加的商品编号:" ); String stId = console.next(); Iterator<Goods> it = gm.getGoodsSet().iterator(); while (it.hasNext()) { goods = it.next(); flag = goods.getGoodsId().equals(stId); if (flag) { break ; } } if (flag) { shoppingCart.put(stId, goods); System.out.println( "请输入要添加的商品数量:" ); try { i = console.nextInt(); // 这里数据数据添加不进去GoodInCart类里的goodsMap (HashMap)集合里面去 goodsInCart.goodsMap.put(goods, i); break ; } catch (InputMismatchException e) { System.out.println( "你输入的格式不正确!不是整数!请重新输入!" ); console.next(); continue ; } } else { System.out.println( "暂时没有你提供商品编号!请重新输入!" ); } } } } =============================================================================================== package com.imooc.shoop; import java.util.InputMismatchException; import java.util.Scanner; public class TestDemo { static GoodsManage goodsManage = new GoodsManage(); public static void main(String[] args) { // 根据需求分析实现主流程(单独定义一个方法把要完成的逻辑操作放单独定义的方法里面在里面) Scanner console = new Scanner(System.in); for (;;) { System.out.println( "******************************************************" ); System.out.println( " *****主菜单*****" ); System.out.println( " 1--商品管理" ); System.out.println( " 2--购物车" ); System.out.println( " 0--退出" ); System.out.println( "******************************************************" ); System.out.println( "请输入对应数字进行操作:" ); int i = 0 ; try { i = console.nextInt(); } catch (InputMismatchException e) { System.out.println( "输入格式不正确,请输入主菜单对应的选项数字!请重新输入!" ); console.next(); continue ; } if (i == 1 ) { commodityManagement(); } else if (i == 2 ) { shoppingCart(); } else if (i == 0 ) { break ; } else { System.out.println( "输入有误,请输入主菜单对应的选项的数字!请重新输入!" ); } } } // 商品管理 public static void commodityManagement() { Scanner console = new Scanner(System.in); for (;;) { System.out.println( "******************************************************" ); System.out.println( " *****商品管理*****" ); System.out.println( " 1--商品信息导入" ); System.out.println( " 2--显示所有商品信息" ); System.out.println( " 9--返回上一级菜单" ); System.out.println( "******************************************************" ); System.out.println( "请输入对应数字进行操作:" ); int i = 0 ; try { i = console.nextInt(); } catch (InputMismatchException e) { System.out.println( "输入格式不正确,请输入主菜单对应的选项数字!请重新输入!" ); console.next(); continue ; } if (i == 1 ) { goodsManage.importGoods(); } else if (i == 2 ) { } else if (i == 9 ) { break ; } else { System.out.println( "输入有误,请输入主菜单对应的选项的数字!请重新输入!" ); } } } // 购物车管理 public static void shoppingCart() { Scanner console = new Scanner(System.in); for (;;) { System.out.println( "******************************************************" ); System.out.println( " *****购物车管理*****" ); System.out.println( " 1--添加商品到购物车" ); System.out.println( " 2--修改购物车中商品的数量" ); System.out.println( " 3--显示购物车中所有的商品" ); System.out.println( " 4--结算" ); System.out.println( " 9--返回上一级菜单" ); System.out.println( "******************************************************" ); System.out.println( "请输入对应数字进行操作:" ); int i = 0 ; try { i = console.nextInt(); } catch (InputMismatchException e) { System.out.println( "输入格式不正确,请输入主菜单对应的选项数字!请重新输入!" ); console.next(); continue ; } ShoppingCart shoppingCart = new ShoppingCart(); GoodsInCart goodsInCart = new GoodsInCart(); if (i == 1 ) { shoppingCart.addGoodsToCart(goodsManage); } else if (i == 2 ) { goodsInCart.modify(); } else if (i == 3 ) { goodsInCart.show(); } else if (i == 4 ) { goodsManage.displayAllGoods(); } else if (i == 9 ) { break ; } else { System.out.println( "输入有误,请输入主菜单对应的选项的数字!请重新输入!" ); } } } } |
正在回答
请参见http://class.imooc.com/course/qadetail/63509
package com.imooc.shoop;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Map.Entry;
public class ShoppingCart {
// 购物车类
// 属性:购物车(shoppingCart):Map类型
/*???????????????????????????????????????
* 在ShoppingCart里的添加商品方法里shoopingCart集合是有数据的可是到了修改购
* 物车中的商品数量和显示购物车中所有商品信息那里判断集合又是空的
*???????????????????????????????????????
* -Key为String类型,商品编号 -Value为GoodsInCart类型,购物车中的信息
*/
private Map<String, GoodsInCart> shoppingCart = new HashMap<String, GoodsInCart>();
int i;
public ShoppingCart() {
}
public ShoppingCart(Map shoppingCart) {
setShoppingCart(shoppingCart);
}
public Map getShoppingCart() {
return shoppingCart;
}
public void setShoppingCart(Map shoppingCart) {
this.shoppingCart = shoppingCart;
}
Scanner console = new Scanner(System.in);
// 添加商品到购物车
public void addGoodsToCart(GoodsManage gm) {
Goods goods = null;
boolean flag = false;
int i = 0;
for (;;) {
System.out.println("添加商品到购物车");
gm.importGoods();
gm.displayAllGoods();
System.out.println("请输入要添加的商品编号:");
String stId = console.next();
Iterator<Goods> it = gm.getGoodsSet().iterator();
while (it.hasNext()) {
goods = it.next();
flag = goods.getGoodsId().equals(stId);
if (flag) {
break;
}
}
if (flag) {
System.out.println("请输入要添加的商品数量:");
try {
i = console.nextInt();
// 这里数据数据添不进去shoppingCart (HashMap)集合里面去,可后面检测集合是空的
GoodsInCart gi = new GoodsInCart(goods, i);
shoppingCart.put(stId, gi);
System.out.println("成功添加商品到购物车!!");
Set<Entry<String,GoodsInCart>>s=shoppingCart.entrySet();
for(Entry<String,GoodsInCart> en:s) {
System.out.println(en);
}
break;
} catch (InputMismatchException e) {
System.out.println("你输入的格式不正确!不是整数!请重新输入!");
console.next();
continue;
}
} else {
System.out.println("暂时没有你提供商品编号!请重新输入!");
}
}
}
// 修改购物车中的商品数量
public void modify() {
//检测集合为空的。
if (shoppingCart.isEmpty()) {
System.out.println("您的购物车空空如也,赶紧去添加商品吧!");
} else {
System.out.println("修改购物车中的商品数量");
System.out.println("请输入要修改的商品编号");
Scanner console = new Scanner(System.in);
String strId = console.next();
Set<Entry<String, GoodsInCart>> set = shoppingCart.entrySet();
for (Entry<String, GoodsInCart> entry : set) {
if (entry.getKey().equals(strId)) {
System.out.println("请输入新的商品数量:");
try {
i = console.nextInt();
} catch (InputMismatchException e) {
System.out.println("你输入的不是正整数请重新输入!");
return;
}
if (i > 0) {
entry.getValue().setNum(i);
break;
// 判断用户输入的是否为0 为0时候删除该商品
} else if (i == 0) {
shoppingCart.remove(entry);
break;
}
} else {
System.out.println("你输入的商品编号没在你的购物车中,请核对后在输入!");
}
}
}
}
// 显示购物车中的所有信息
public void show() {
System.out.println("显示购物车中的所有商品信息");
System.out.println(shoppingCart.isEmpty());
//这里也检测出集合是空的
if (shoppingCart.isEmpty()) {
System.out.println("你的购物车空空如也,赶紧去添加商品吧!");
} else {
Set<Entry<String, GoodsInCart>> set = shoppingCart.entrySet();
for (Entry entry : set) {
System.out.println(entry.getValue());
}
}
}
//结算
public void Settlement() {
System.out.println("结算购物车中的所有商品:");
System.out.println(shoppingCart.isEmpty());
double sum=0;
//这里也检测出集合是空的
if (shoppingCart.isEmpty()) {
System.out.println("你的购物车里没有商品.........");
} else {
Set<Entry<String, GoodsInCart>> set = shoppingCart.entrySet();
for (Entry <String,GoodsInCart>entry : set) {
double d1=entry.getValue().getNum()*entry.getValue().getGoods().getGoodsPrice();
sum=sum+d1;
}
}
show();
System.out.println("你需要付款" + sum + "元");
shoppingCart.clear();
}
}
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧