正在回答
同学是想说的是添加两个商品,会把之前的商品变成和之后添加的商品一样吗?为了不改变之前的商品信息,同学需要将GoodsInCart类中创建对象时的goodsincart的static去掉 。定义getInstance()方法时的static去掉。如图:
保存后ShoppingCart()类中会报错,在报错的地方,有new对象的方式新建GoodsInCart类对象。如图:
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快!
package com.imooc;
import java.util.Scanner;
import java.util.Set;
import java.util.Map.Entry;
public class TestDemo {
public static void main(String[] args) {
ShoppingCart shoppingcart = ShoppingCart.getInstance();
GoodsManage goodsmanage = GoodsManage.getInstance();
outter: for(;;) {
Scanner input = new Scanner(System.in);
TestDemo.notice();//显示功能菜单
int num = input.nextInt();//用户选的菜单
if(num < 0 || num > 2 ) {//对用户输入的数判断是否合理
System.out.println("亲,咱们商城还没有这样的功能呢!");//超出了菜单范围
TestDemo.notice();
}else {
//菜单选有效
switch (num){
case 1 : //商品管理
outter2: for(;;) {
Scanner in = new Scanner(System.in);
TestDemo.noticeGoods();
int a = in.nextInt();//用户选的菜单
if((a < 3)||(a > 5)) {
System.out.println("亲,咱们商城还没有这样的功能呢!");//超出了菜单范围
}else {
switch(a) {
case 3 : goodsmanage.importGoods();break;
case 4 : goodsmanage.displayAllGoods();break;
case 5 :System.out.println("成功退出商品管理");break outter2;
}
}
}
break;
case 2 ://我的购物车
outter3: for(;;) {
Scanner inp = new Scanner(System.in);
TestDemo.noticeCart();
int b = inp.nextInt();//用户选的菜单
if(b < 5 || b > 10) {
System.out.println("亲,咱们商城还没有这样的功能呢!");//超出了菜单范围
TestDemo.noticeCart();
}else {
switch(b) {
case 6 :shoppingcart.addGoodsToCart(goodsmanage);
Set<Entry<String,GoodsInCart>> set = shoppingcart.getShoppingCart().entrySet();
for (Entry<String, GoodsInCart> entry : set) {
System.out.println(entry.getValue());
}
break;
case 7 :shoppingcart.updateNumInCart(); break;
case 8 :shoppingcart.displayAllInCart();break;
case 9 :shoppingcart.settleAccounts();break;
case 10 :System.out.println("你已退出购物车!");break outter3;
}
}
}
break;
case 0 :
System.out.println("成功退出商品管理系统!");
break outter; //0:退出系统
}
}
}
}
//用户选的菜单
private static void notice() {
System.out.println("***************");
System.out.println("* *");
System.out.println("* 主菜单 *");
System.out.println("* 1:商品管理 *");
System.out.println("* 2:我的购物车 *");
System.out.println("* 0:退出 *");
System.out.println("* *");
System.out.println("***************");
}
//商品管理菜单
private static void noticeGoods() {
System.out.println("******************");
System.out.println("* *");
System.out.println("* 商品管理菜单 *");
System.out.println("* 3:商品导入 *");
System.out.println("* 4:显示所有商品信息 *");
System.out.println("* 5:返回上一级菜单 *");
System.out.println("* *");
System.out.println("******************");
}
//购物车管理菜单
private static void noticeCart() {
System.out.println("*********************");
System.out.println("* *");
System.out.println("* 购物车管理菜单 *");
System.out.println("* 6:添加商品到购物车 *");
System.out.println("* 7:修改购物车中的商品数量*");
System.out.println("* 8:显示购物车中的商品信息*");
System.out.println("* 9:结算 *");
System.out.println("* 10返回上一级菜单 *");
System.out.println("* *");
System.out.println("**********************");
}
}
package com.imooc;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
/*
*
* 购物车类(ShoppingCart)
属性:
- 购物车(shoppingCart):Map类型,其中key为String类型,value为GoodsInCart类型
方法:
-添加商品到购物车:
public void addGoodsToCart(GoodsManage gm);
-修改购物车中的商品数量:public void updateNumInCart();
- 显示购物车中的所有商品:public void displayAllInCart();
- 结算:public void settleAccounts();
- 构造方法及getter和setter方法
*
* */
public class ShoppingCart {
private HashMap<String,GoodsInCart> shoppingCart;
private ShoppingCart() {
}
private ShoppingCart(HashMap<String, GoodsInCart> shoppingCart) {
super();
this.shoppingCart = shoppingCart;
}
//创建对象
static ShoppingCart shoppingcart= null;
public static ShoppingCart getInstance() {
if(shoppingcart == null) {
shoppingcart = new ShoppingCart();
return shoppingcart;
}
return shoppingcart;
}
public HashMap<String, GoodsInCart> getShoppingCart() {
return shoppingCart;
}
public void setShoppingCart(HashMap<String, GoodsInCart> shoppingCart) {
this.shoppingCart = shoppingCart;
}
HashMap<String,GoodsInCart> map = new HashMap<>();
//-添加商品到购物车:
public void addGoodsToCart(GoodsManage gm) {
try {
Scanner input = new Scanner(System.in);
Set<Goods> goodsset = gm.getGoodsSet();
//System.out.println(goodsset.size());
if(goodsset.size() == 0) {
System.out.println("商品未导入!");
}else {
//库中有商品,
System.out.print("请输入商品编号:");
String goodsId = input.next();
//System.out.println(goodsId);
for (Goods goods : goodsset) {
//去判断输入的商品ID是否在库中存在
String goodsid = goods.getGoodsId();
if(goodsid.equals(goodsId)) {
if(shoppingcart.getShoppingCart() == null) {
System.out.println("购物车中还没有商品哦");
GoodsInCart goodsincart = GoodsInCart.getInstance();
goodsincart.setGoods(goods);
System.out.print("请输入要添加的商品数量:");
int num = input.nextInt();
if(num < 0 || num == 0) {
System.out.println("数量要大于0 哦!");
}
goodsincart.setNum(num);
map.put(goodsId, goodsincart);
this.setShoppingCart(map);
System.out.println("添加到购物车成功!");
return;
}else {
if(shoppingcart.getShoppingCart().containsKey(goodsid)) {//如果购物车中已经存在该商品
//就只修改数量
int a = shoppingcart.getShoppingCart().get(goodsid).getNum();
System.out.print("请输入要添加的商品数量:");
int num = input.nextInt();
shoppingcart.getShoppingCart().get(goodsid).setNum(a + num);
System.out.println("添加到购物车成功!");
return;
}
GoodsInCart goodsincart = GoodsInCart.getInstance();
goodsincart.setGoods(goods);
System.out.print("请输入要添加的商品数量:");
int num = input.nextInt();
if(num < 0 || num == 0) {
System.out.println("数量要大于0 哦!");
}
goodsincart.setNum(num);
//map.put(goodsId, goodsincart);
shoppingcart.getShoppingCart().put(goodsid, goodsincart);
System.out.println("添加到购物车成功!");
return;
}
}
}
System.out.println("该商品还没在库中哦!");
}
}catch(NullPointerException e) {
System.out.println("商品未导入!");
}
}
//修改购物车中的商品数量
public void updateNumInCart() {
System.out.print("请选择要修改的商品编号:");
Scanner input = new Scanner(System.in);
String goodsId = input.next();
HashMap<String,GoodsInCart> map = this.getShoppingCart();
//System.out.println(map.size());
if(map.containsKey(goodsId)) {
System.out.print("请输入要修改的数量:");
int num = input.nextInt();
try {
map.get(goodsId).setNum(num);
}catch(NullPointerException e) {
System.out.println("购物车中还没有商品哦!");
}
System.out.println("数量修改成功");
}else {
System.out.println("亲,购物车中没有该商品,无法修改哦!");
}
}
//显示购物车中的所有商品:
public void displayAllInCart() {
System.out.println("亲,这里是你的购物车哟!");
HashMap<String,GoodsInCart> map = this.getShoppingCart();
try{
Set<Entry<String,GoodsInCart>> set = map.entrySet();
//System.out.println(set.size());
for (Entry<String, GoodsInCart> entry : set) {
System.out.println(entry.getValue().toString());
}
}catch(NullPointerException e) {
System.out.println("购物车中暂时还没有商品哦");
}
}
// 结算:
public void settleAccounts() {
System.out.print("购物车结算清单为:");
HashMap<String,GoodsInCart> map = this.getShoppingCart();
Set<Entry<String,GoodsInCart>> set = map.entrySet();
double totalPrice = 0;
for (Entry<String, GoodsInCart> entry : set) {
totalPrice = totalPrice + (entry.getValue().getGoods().getPrice())*(entry.getValue().getNum());
}
System.out.println(totalPrice);
}
}
package com.imooc;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* 商品管理类(GoodsManage)
属性:存放商品的容器(goodsSet):Set类型
方法:
-构造方法
-getter和setter方法
-商品信息导入:public void importGoods();
-显示所有商品信息:public void displayAllGoods();
* @author Willer He
*
*/
public class GoodsManage {
private HashSet<Goods> goodsSet;
//构造方法
private GoodsManage() {
}
private GoodsManage(HashSet<Goods> goodsSet) {
super();
this.goodsSet = goodsSet;
}
//创建对象
static GoodsManage goodsmanage = null;
public static GoodsManage getInstance() {
if(goodsmanage == null) {
goodsmanage = new GoodsManage();
return goodsmanage;
}
return goodsmanage;
}
public HashSet<Goods> getGoodsSet() {
return goodsSet;
}
public void setGoodsSet(HashSet<Goods> goodsSet) {
this.goodsSet = goodsSet;
}
//商品信息导入
public void importGoods() {
Goods g1 = new Goods("014527as","木马人鼠标",99,"木马人,为游戏而生");
Goods g2 = new Goods("017827as","AOC显示器",1000,"视野决定价值,AOC给你更广的视野空间");
Goods g3 = new Goods("016727as","七匹狼夹克",499,"专注品质男装");
Goods g4 = new Goods("014727as","微星笔记本",6000,"发烧级游戏本,畅玩网游");
Goods g5 = new Goods("012427as","三星固态硬盘120G",599,"每一次迈步,只为想更快的你");
Goods g6 = new Goods("014586as","安踏篮球鞋",399,"防滑,更防护你");
Goods g7 = new Goods("014587as","Huawei/华为P20 Pro",4488,"莱卡双摄");
Goods g8 = new Goods("017837as","华为honor/荣耀 荣耀10",2599,"2400万AI摄影 2400万AI摄影,智能识别500多种场景");
Goods g9 = new Goods("075327as","影驰GTX1060 GAMER 6G",2139,"大吉大利,晚上吃鸡");
Goods g10 = new Goods("078427as","Apple/苹果 Ipad 2017款",2158,"方寸间,乐趣全开");
HashSet<Goods> goodsSet = new HashSet<Goods>();
goodsSet.add(g1);
goodsSet.add(g2);
goodsSet.add(g3);
goodsSet.add(g4);
goodsSet.add(g5);
goodsSet.add(g6);
goodsSet.add(g7);
goodsSet.add(g8);
goodsSet.add(g9);
goodsSet.add(g10);
this.setGoodsSet(goodsSet);
System.out.println("商品导入成功!");
}
//显示所有商品信息:
public void displayAllGoods() {
try {
Set<Goods> set = this.getGoodsSet();
if(set.size() == 1) {
System.out.println("商品列表为空,请先导入");
}else {
Iterator<Goods> it = set.iterator();
while(it.hasNext()) {
System.out.println(it.next().toString());
}
}
}catch (NullPointerException e){
System.out.println("商品列表为空,请先导入");
}
}
}
package com.imooc;
//商品类
public class Goods {
private String goodsId;//商品编号
private String goodsName;//商品名称
private double price;//商品价格
private String goodsDesp;//商品描述
//构造方法
public Goods() {
}
public Goods(String goodsId, String goodsName, double price, String goodsDesp) {
super();
this.goodsId = goodsId;
this.goodsName = goodsName;
this.price = price;
this.goodsDesp = 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 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 + ((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(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if( this.equals(obj)) {
return true;
}
if( obj.getClass() == Goods.class) {
Goods goods = (Goods)obj;
return goods.goodsId == goodsId;
}
return false;
}
@Override
public String toString() {
return "商品 [goodsId=" + goodsId + ", 商品名字=" + goodsName + ", 商品价格=" + price + ", 商品描述="
+ goodsDesp + "]";
}
}
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星