// 实现类
public class Product implements Comparable<Product>{ private Integer id; private String name; private Double price; private String color; private Integer dimension; private Integer inventory; public Product() { } public Product(Integer id, String name, Double price, String color, Integer dimension, Integer inventory) { this.id = id; this.name = name; this.price = price; this.color = color; this.dimension = dimension; this.inventory = inventory; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Integer getDimension() { return dimension; } public void setDimension(Integer dimension) { this.dimension = dimension; } public Integer getInventory() { return inventory; } public void setInventory(Integer inventory) { this.inventory = inventory; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Product product = (Product) o; return Objects.equals(id, product.id) && Objects.equals(name, product.name) && Objects.equals(price, product.price) && Objects.equals(color, product.color) && Objects.equals(dimension, product.dimension) && Objects.equals(inventory, product.inventory); } @Override public int hashCode() { return Objects.hash(id, name, price, color, dimension, inventory); } @Override public String toString() { return id+"\t" + name + "\t" + price +"\t"+ color + "\t\t" + dimension+"\t\t" + inventory ; } @Override public int compareTo(Product o) { return (int)(this.getPrice() - o.getPrice()); } }
// 接口
public interface inter { // 添加商品 public void Add(TreeSet<Product> set); // 查询遍历所有商品 public void Fial(TreeSet<Product> set); // 查询指定商品 public void Fialid(TreeSet<Product> set,Integer id); //添加到购物车 public void Addcar(TreeMap<Product,Integer> map ,TreeSet<Product> set); // 显示购物车 public void Fialcar(TreeMap<Product,Integer> map); }
// 接口实现类
public class interImpl extends Product implements inter { Scanner input = new Scanner(System.in); // 添加商品 @Override public void Add(TreeSet<Product> set) { System.out.println("请输入需要添加的商品编号"); int id = input.nextInt(); input.nextLine(); System.out.println("请输入需要添加的商品称号"); String name = input.nextLine(); System.out.println("请输入需要添加的商品价格"); Double price = input.nextDouble(); input.nextLine(); System.out.println("请输入需要添加的商品颜色"); String color= input.nextLine(); System.out.println("请输入需要添加的商品尺寸"); int dimension = input.nextInt(); System.out.println("请输入需要添加的商品库存"); int inventory = input.nextInt(); set.add(new Product(id,name,price,color,dimension,inventory)); System.out.println("添加成功"); } // 查询遍历所有商品 @Override public void Fial(TreeSet<Product> set) { System.out.println("编号\t\t"+ "商品名称\t\t"+"价格\t\t"+"颜色\t\t"+"尺寸\t\t"+"库存\t\t"); Iterator<Product> iterator = set.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } // 查询指定商品 @Override public void Fialid(TreeSet<Product> set, Integer id) { Iterator<Product> iterator = set.iterator(); int count=0; while (iterator.hasNext()){ Product next = iterator.next(); if (next.getId().equals(id)){ System.out.println("编号\t\t"+ "商品名称\t\t"+"价格\t\t"+"颜色\t\t"+"尺寸\t\t"+"库存\t\t"); System.out.println(next); break; } count++; } if(count>set.size()){ try { throw new IllegalAccessException("没有找到id是"+id+"的商品"); } catch (IllegalAccessException e) { e.printStackTrace(); } } } // 添加到购物车 @Override public void Addcar(TreeMap<Product, Integer> map, TreeSet<Product> set) { Iterator<Product> iterator = set.iterator(); System.out.println("请输入你要添加到购物车的商品id"); int id = input.nextInt(); int count=0; while (iterator.hasNext()){ if (iterator.next().getId().equals(id)){ System.out.println("请输入你要添加商品数量"); Integer quantity = input.nextInt(); map.put(iterator.next(),quantity); break; } count++; } if(count>set.size()){ try { throw new IllegalAccessException("没有找到id是"+id+"的商品"); } catch (IllegalAccessException e) { e.printStackTrace(); } } } // 显示购物车 @Override public void Fialcar(TreeMap<Product, Integer> map) { Iterator<Product> iterator = map.keySet().iterator(); System.out.println("你的购物车的商品如下:"); System.out.println("编号\t\t"+ "商品名称\t\t\t"+"价格\t\t"+"数量\t\t"+"小计\t\t"); double zong=0; while (iterator.hasNext()){ Product next = iterator.next(); System.out.print(next.getId()+"\t"); System.out.print(next.getName()+"\t"); System.out.print(next.getPrice()+"\t"); System.out.print(map.get(next)+"\t"); double c=map.get(next)*next.getPrice(); System.out.println(c); zong+=c; } System.out.println("总计:"+zong); } }
// 测试类
public class Test { public static final Scanner input = new Scanner(System.in); public static final TreeSet<Product> set = new TreeSet<>(); public static final TreeMap<Product, Integer> map = new TreeMap<>(); public static final interImpl str = new interImpl(); static { set.add(new Product(1111,"蜘蛛王皮鞋",238.0,"黑色",42,1000)); set.add(new Product(3333,"Thinkpad",5929.0,"黑色",12,1000)); set.add(new Product(2222,"iPhone17plus",5632.0,"金色",13,62)); set.add(new Product(4444,"小霸王游戏机",15.0,"绿色",20,1000)); } public static void main(String[] args) { boolean flag=true; while (flag){ System.out.println("欢迎进入京东商城"); System.out.println("1.添加商品"); System.out.println("2.查看所有商品"); System.out.println("3.查看指定编号商品"); System.out.println("4.添加到购物车"); System.out.println("5.显示购物车"); System.out.println("6.退出"); System.out.println("***************************"); System.out.println("请选择:"); int t = input.nextInt(); switch (t){ case 1: // 添加 str.Add(set); break; case 2: str.Fial(set); // 查询所有 break; case 3: System.out.println("请输入你需要查看的商品id"); Integer id = input.nextInt(); str.Fialid(set,id); // 查询指定 break; case 4: // 添加购物车 str.Addcar(map,set); break; case 5: // 显示购物车 str.Fialcar(map); break; case 6: // 退出 System.out.println("感谢你的使用,期待下次见面!"); flag=false; break; default: try { throw new IllegalAccessException("傻叉看着不会输,参数输错了"); } catch (IllegalAccessException e) { e.printStackTrace(); } break; } } } }