HashSet模拟网上购物车

mmmm0303 2020-05-19 09:41:05
用HashSet模拟一个网上购物车。要求:从键盘输入5个商品的编号、名称、单价、购买数量,将这些信息存入一个HashSet,(如果编号相同,则可以在数量上加1,不能重复存入购物车)然后将该HashSet作为参数调用方法getSum(HashSet items),该方法用于计算商品的总价并返回
...全文
731 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmmm0303 2020-05-19
  • 打赏
  • 举报
回复
请问有人教一下我吗
mmmm0303 2020-05-19
  • 打赏
  • 举报
回复
嗯嗯 判断这个商品+1的懂了 但是那这个getSum方法我还是不懂应该怎么写
亦夜 2020-05-19
  • 打赏
  • 举报
回复
引用 7 楼 mmmm0303 的回复:
[quote=引用 5 楼 亦夜 的回复:][quote=引用 4 楼 mmmm0303 的回复:] class Goods{ private String number; private String name; private double unitprice; private int quantity; //重写hashCode方法 public int hashCode() { return number.hashCode(); } //重写equals方法 public boolean equals(Object obj) { if(this==obj) return true; if(!(obj instanceof Goods)) { return false; } Goods goods=(Goods) obj; boolean b=this.number.equals(goods.number); return b; } } 我写到这里就卡住了 有人提点一下吗
就用idea自带生成的equals方法就行了,然后通过构造函数给成员变量赋值就行了,或者通过set方法也可以[/quote]可以帮我看看我 我现在的代码应该怎么改吗 刚刚新发的那个 谢谢哥[/quote]

HashSet<Students> hashSet = new HashSet<>();
        hashSet.add(zs);
        boolean flag = hashSet.contains(zs1);
        if (flag) {
            for (Students students : hashSet) {
                if (students.getId() == id) {
                    students.getNum ++; // 商品加1
                }
            }
        }
伪代码,类似这种
mmmm0303 2020-05-19
  • 打赏
  • 举报
回复
引用 5 楼 亦夜 的回复:
[quote=引用 4 楼 mmmm0303 的回复:] class Goods{ private String number; private String name; private double unitprice; private int quantity; //重写hashCode方法 public int hashCode() { return number.hashCode(); } //重写equals方法 public boolean equals(Object obj) { if(this==obj) return true; if(!(obj instanceof Goods)) { return false; } Goods goods=(Goods) obj; boolean b=this.number.equals(goods.number); return b; } } 我写到这里就卡住了 有人提点一下吗
就用idea自带生成的equals方法就行了,然后通过构造函数给成员变量赋值就行了,或者通过set方法也可以[/quote]可以帮我看看我 我现在的代码应该怎么改吗 刚刚新发的那个 谢谢哥
mmmm0303 2020-05-19
  • 打赏
  • 举报
回复
import java.util.HashSet; import java.util.Scanner; public class Testa { public static double getSum(HashSet items) { double sum=0; return sum; } public static void main(String[]ags) { HashSet<Object> gds=new HashSet<Object>(); Scanner sc=new Scanner(System.in); int i=0; String number; String name; double unitprice; int quantity=0; while(i<5) { System.out.println("请输入商品的编号"); number=sc.nextLine(); System.out.println("请输入商品的名字"); name=sc.nextLine(); System.out.println("请输入商品的单价"); unitprice=sc.nextDouble(); System.out.println("请输入商品的数量"); quantity=sc.nextInt(); Goods goods=new Goods(number,name,unitprice,quantity); gds.add(goods); } } } class Goods{ private String number; private String name; private double unitprice; private int quantity; public Goods(String number,String name,double unitprice,int quantity) { this.number=number; this.name=name; this.unitprice=unitprice; this.quantity=quantity; } //重写hashCode方法 public int hashCode() { return number.hashCode(); } //重写equals方法 public boolean equals(Object obj) { if(this==obj) return true; if(!(obj instanceof Goods)) { return false; } Goods goods=(Goods) obj; boolean b=this.number.equals(goods.number); return b; } } 我写到这里 但是不知道这个getSum方法应该怎么写 还有如何判断如果是同一个编号的商品,如何能直接在数量上+1 谁能指点一下吗
亦夜 2020-05-19
  • 打赏
  • 举报
回复
引用 4 楼 mmmm0303 的回复:
class Goods{ private String number; private String name; private double unitprice; private int quantity; //重写hashCode方法 public int hashCode() { return number.hashCode(); } //重写equals方法 public boolean equals(Object obj) { if(this==obj) return true; if(!(obj instanceof Goods)) { return false; } Goods goods=(Goods) obj; boolean b=this.number.equals(goods.number); return b; } } 我写到这里就卡住了 有人提点一下吗
就用idea自带生成的equals方法就行了,然后通过构造函数给成员变量赋值就行了,或者通过set方法也可以
mmmm0303 2020-05-19
  • 打赏
  • 举报
回复
class Goods{ private String number; private String name; private double unitprice; private int quantity; //重写hashCode方法 public int hashCode() { return number.hashCode(); } //重写equals方法 public boolean equals(Object obj) { if(this==obj) return true; if(!(obj instanceof Goods)) { return false; } Goods goods=(Goods) obj; boolean b=this.number.equals(goods.number); return b; } } 我写到这里就卡住了 有人提点一下吗
mmmm0303 2020-05-19
  • 打赏
  • 举报
回复
最好是有大佬给个代码会更好噢
mmmm0303 2020-05-19
  • 打赏
  • 举报
回复
是只需要重写equals方法就行了嘛
亦夜 2020-05-19
  • 打赏
  • 举报
回复
可以给你个思路,自己写出来,你会更有成就感而且才算真正的学习 1、需要一个商品类,获取键盘数据初始化商品类 2、初始化商品类后,查看HashSet中是否存在该商品,思路:可以重写equals方法,通过编号来进行判断 3、写一个getSum方法遍历商品计算总价就行了
qq_39936465 2020-05-19
  • 打赏
  • 举报
回复
引用 19 楼 mmmm0303 的回复:
题目要求的...

public class Testa {
	static HashSet<String> gds = new HashSet<String>();

	public static void main(String[] args) {
		// TODO Auto-generated method stub
       int count=0;
       Scanner sc = new Scanner(System.in);
		String str;

		while (count< 5) {
			System.out.println("请输入商品的编号");
			String number=sc.next();
			System.out.println("请输入商品的名字");
			String name=sc.next();
			System.out.println("请输入商品的单价");
			double unitprice=sc.nextDouble();
			System.out.println("请输入商品的数量");
			int quantity=sc.nextInt();
			//判断商品是否已经存在
			if((str=check(number))!=null) {
				String[] s=str.split(",");
				//移除原来的数据String
				gds.remove(str);
				//数量累加后生成新的String
				str=number+","+name+","+unitprice+","+(Integer.valueOf(s[3])+quantity);		
			}else {
				//新的String
				str=number+","+name+","+unitprice+","+quantity;
			}
			gds.add(str);
			count++;
		}
		System.out.println("总价:"+getSum());

	}
	
	public static String check(String str) {
		String temp;
		Iterator<String> it=gds.iterator();
		while (it.hasNext()) {
			temp=it.next();
			String[] s=temp.split(",");
			//判断是否存在商品编号,存在的话直接返回该String值
			if(s[0].equals(str)) {
				return temp;
			}
		}
		return null;
	}
	
	public static double getSum() {
		double sum=0;
		Iterator<String> it=gds.iterator();
		while(it.hasNext()) {
			String[] s=it.next().split(",");
			sum=sum+(Double.valueOf(s[2])*Integer.valueOf(s[3]));
		}
		return sum;
	}

}

mmmm0303 2020-05-19
  • 打赏
  • 举报
回复
题目要求的...
qq_39936465 2020-05-19
  • 打赏
  • 举报
回复
引用 17 楼 mmmm0303 的回复:
我可能表达的不是很清楚 你们可以看看题目 (如果编号相同,则可以在数量上加1,不能重复存入购物车)就是这句话
我还是不明白你为什么一定要用hashset做购物车,hashmap不好么?
mmmm0303 2020-05-19
  • 打赏
  • 举报
回复
我可能表达的不是很清楚 你们可以看看题目 (如果编号相同,则可以在数量上加1,不能重复存入购物车)就是这句话
qq_39936465 2020-05-19
  • 打赏
  • 举报
回复
引用 15 楼 mmmm0303 的回复:
这是我现在的代码 如果是不同的商品 算出来的商品总价是ok的 现在唯一的问题是如果是输入同一个编号的商品 怎么实现购物车数量同一种的商品数量+1 有人能指点一下吗
我很不明白你的逻辑,商品本身就有数量,为什么是数量加1?
mmmm0303 2020-05-19
  • 打赏
  • 举报
回复
import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; public class Testa { /*getSum方法 获得总额*/ public static double getSum(HashSet <Goods> goods) { double sum=0; double unitprice=0; int quantity=0; /*添加迭代器 获取每一个商品*/ Iterator<Goods> it=goods.iterator(); while(it.hasNext()) { Goods x=new Goods(); x=it.next(); /*获取总额 商品单价*数量*/ sum+=(x.unitprice*x.quantity); } return sum; } public static void main(String[]ags) { HashSet<Goods> gds=new HashSet<Goods>(); Scanner sc=new Scanner(System.in); int i=0; /*for循环 获得每一个商品对应的信息*/ for(i=0;i<5;i++) { Goods goods=new Goods(); System.out.println("请输入商品的编号"); goods.number=sc.next(); System.out.println("请输入商品的名字"); goods.name=sc.next(); System.out.println("请输入商品的单价"); goods.unitprice=sc.nextDouble(); System.out.println("请输入商品的数量"); goods.quantity=sc.nextInt(); gds.add(goods); } /*打印总额*/ System.out.println("您一共消费了"+getSum(gds)+"元!"); } } class Goods{ String number; String name; double unitprice; int quantity; //重写hashCode方法 public int hashCode() { return number.hashCode(); } //重写equals方法 public boolean equals(Object obj) { if(this==obj) return true; if(!(obj instanceof Goods)) { return false; } Goods goods=(Goods) obj; boolean b=this.number.equals(goods.number); return b; } } 这是我现在的代码 如果是不同的商品 算出来的商品总价是ok的 现在唯一的问题是如果是输入同一个编号的商品 怎么实现购物车数量同一种的商品数量+1 有人能指点一下吗
mmmm0303 2020-05-19
  • 打赏
  • 举报
回复
getSum是没问题了 但是这个如何判断是同一个商品 然后购物车加1 这个好像还是不对哎 用楼上的方法还是不行 有没有人知道应该放在哪里这个判断 然后具体应该怎么写
nayi_224 2020-05-19
  • 打赏
  • 举报
回复
		Set set = new HashSet();
		set.add("1");
		set.add("2");
		set.add("3");
		
		Iterator it = set.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
mmmm0303 2020-05-19
  • 打赏
  • 举报
回复
对的 所有的商品总价 但是定义hashset之后 商品都在hashset里面了 怎么拿出来遍历每一个的价格和数量呢 这个就是我不懂的地方
亦夜 2020-05-19
  • 打赏
  • 举报
回复
引用 9 楼 mmmm0303 的回复:
嗯嗯 判断这个商品+1的懂了 但是那这个getSum方法我还是不懂应该怎么写
就是我上面说的第三步啊,定义一个变量,遍历所有商品 单价 x 数量不就是该总价了吗,但是我不知道你这里是计算商品总价,还是所有买的商品总价

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧