面向对象和集合编程题 代码过程package com.sy.homework4; public class Item { private String name; // 商品名称 private double price; // 商品价格 private int count; // 商品数量 // 计算总价 public double total(){ return price * count; } public Item() { } public Item(String name, double price, int count) { this.name name; this.price price; this.count count; } 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 int getCount() { return count; } public void setCount(int count) { this.count count; } Override public String toString() { return Item{ , name name \ , price price , count count }; } }package com.sy.homework4; import java.util.List; public class Order { private String orderId; //订单编号 private String userId; //用户ID private ListItem items; // 订单中的商品列表 public double amount(){ double total 0; for (Item item : items) { total item.total(); } return total; } public Order(String o001, String u001, Item 手机, Item 电脑) { } public Order(String orderId, String userId, ListItem items) { this.orderId orderId; this.userId userId; this.items items; } public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId orderId; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId userId; } public ListItem getItems() { return items; } public void setItems(ListItem items) { this.items items; } Override public String toString() { return Order{ orderId orderId \ , userId userId \ , items items }; } }package com.sy.homework4; import java.util.*; public class Test { public static void main(String[] args) { ListOrder list new ArrayList(); //订单U001 list.add(new Order(O001, U001, Arrays.asList( new Item(手机, 5999, 1), new Item(电脑, 9999, 1)))); list.add(new Order(O002, U002, Arrays.asList( new Item(耳机, 199, 1)))); //订单U002 list.add(new Order(O003, U002, Arrays.asList( new Item(鼠标, 199, 1), new Item(键盘, 288, 1)))); list.add(new Order(O004, U001, Arrays.asList( new Item(鼠标垫, 59, 1)))); MapString, ListOrder map new HashMap(); //遍历所有订单 for (Object o : list) { Order order (Order) o; String userId order.getUserId(); if (!map.containsKey(userId)) { map.put(userId, new ArrayList()); } map.get(userId).add(order); } System.out.println(用户消费统计); HashMapString, Double hashMap new HashMap(); for (Map.EntryString, ListOrder entry : map.entrySet()) { String userId entry.getKey(); ListOrder orders entry.getValue(); double total 0; for (Order order : orders) { total order.amount(); } hashMap.put(userId, total); System.out.println(用户 userId 总消费了 total); } //统计总消费 double totals 0; for (double s : hashMap.values()) { totals s; } System.out.println(全平台总营业额 totals); //统计销量最高的商品 MapString, Integer map1 new HashMap(); for (Order order : list) { for (Item item : order.getItems()) { String name item.getName(); int count item.getCount(); if (map1.containsKey(name)){ map1.put(name,map1.get(name) count); }else { map1.put(name,count); } } } //找出销量最高的商品 String maxName null; int maxCount 0; for (Map.EntryString, Integer entry : map1.entrySet()) { if (entry.getValue() maxCount){ maxName entry.getKey(); maxCount entry.getValue(); } } System.out.println(销量最高的商品是 maxName 数量是 maxCount); } }代码过程package com.sy.homework5; import java.util.Objects; public abstract class Vehicle { private String license; //车牌 private String brand; // 品牌 public abstract String getType(); public Vehicle() { } public Vehicle(String license, String brand) { if (license null || license.isBlank()) { throw new IllegalArgumentException(车牌号不能为空); } this.license license; this.brand brand; } public String getLicense() { return license; } public void setLicense(String license) { this.license license; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand brand; } Override public boolean equals(Object o) { if (this o) return true; if (o null || getClass() ! o.getClass()) return false; Vehicle vehicle (Vehicle) o; return Objects.equals(license, vehicle.license) Objects.equals(brand, vehicle.brand); } Override public int hashCode() { return Objects.hash(license, brand); } Override public String toString() { return Vehicle{ license license \ , brand brand \ }; } }package com.sy.homework5; public class Bus extends Vehicle{ public Bus(String license, String brand) { super(license, brand); } Override public String getType() { System.out.println(公交); return null; } }package com.sy.homework5; public class Car extends Vehicle{ public Car(String license, String brand) { super(license, brand); } Override public String getType() { System.out.println(轿车); return null; } }package com.sy.homework5; public class Truck extends Vehicle{ public Truck(String license, String brand) { super(license, brand); } Override public String getType() { System.out.println(卡车); return null; } }package com.sy.homework5; import java.util.*; public class Test { public static void main(String[] args) { ArrayListVehicle list new ArrayList(); list.add(new Car(京C00001, 特斯拉)); list.add(new Car(京A12345, 比亚迪)); list.add(new Bus(京B67890, 宇通)); list.add(new Truck(京D54321, 福田)); list.add(new Car(京C00001, 特斯拉)); list.add(new Car(, 比亚迪)); SetVehicle vehicleSet new LinkedHashSet(list); ListVehicle vehicles new ArrayList(vehicleSet); MapString, ListVehicle typeMap new HashMap(); for (Vehicle v : list) { String type v.getType(); if (!typeMap.containsKey(type)) { typeMap.put(type, new ArrayList()); } typeMap.get(type).add(v); } // 打印按车型分组结果 System.out.println(按车型分组); for (Map.EntryString, ListVehicle entry : typeMap.entrySet()) { System.out.println(entry.getKey() : entry.getValue()); } // 5. 筛选品牌为比亚迪的车辆 System.out.println(比亚迪车辆); ListVehicle bydVehicles new ArrayList(); for (Vehicle v : vehicles) { if (比亚迪.equals(v.getBrand())) { bydVehicles.add(v); } } for (Vehicle v : bydVehicles) { System.out.println(v); } } }