|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import lombok.Data; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.HashSet; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.Set; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Data | 
|---|
|  |  |  | public class ItemUtilParam { | 
|---|
|  |  |  | String name; | 
|---|
|  |  |  | double unitSpace; | 
|---|
|  |  |  | int maxCapacity; | 
|---|
|  |  |  | int stock; | 
|---|
|  |  |  | int remaining; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public ItemUtilParam(String name, int maxCapacity, int stock) { | 
|---|
|  |  |  | this.name = name; | 
|---|
|  |  |  | this.maxCapacity = maxCapacity; | 
|---|
|  |  |  | this.unitSpace = 1.0 / maxCapacity; | 
|---|
|  |  |  | this.stock = stock; | 
|---|
|  |  |  | this.remaining = stock; | 
|---|
|  |  |  | @Data | 
|---|
|  |  |  | public static class Item { | 
|---|
|  |  |  | String name; | 
|---|
|  |  |  | double unitSpace; | 
|---|
|  |  |  | int maxCapacity; | 
|---|
|  |  |  | int stock; | 
|---|
|  |  |  | int remaining; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Item(String name, int maxCapacity, int stock) { | 
|---|
|  |  |  | this.name = name; | 
|---|
|  |  |  | this.maxCapacity = maxCapacity; | 
|---|
|  |  |  | this.unitSpace = 1.0 / maxCapacity; | 
|---|
|  |  |  | this.stock = stock; | 
|---|
|  |  |  | this.remaining = stock; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public String toString() { | 
|---|
|  |  |  | return name + "(单放" + maxCapacity + "个, 库存" + stock + "个)"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public String toString() { | 
|---|
|  |  |  | return name + "(单放" + maxCapacity + "个, 库存" + stock + "个)"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | static class Locker { | 
|---|
|  |  |  | int id; | 
|---|
|  |  |  | double remainingSpace; | 
|---|
|  |  |  | long bindingTags; | 
|---|
|  |  |  | Map<String, Integer> contents; | 
|---|
|  |  |  | Set<String> itemTypes; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Locker(int id) { | 
|---|
|  |  |  | this.id = id; | 
|---|
|  |  |  | this.remainingSpace = 1.0; | 
|---|
|  |  |  | this.contents = new HashMap<>(); | 
|---|
|  |  |  | this.itemTypes = new HashSet<>(); | 
|---|
|  |  |  | this.bindingTags = System.currentTimeMillis(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public boolean canAdd(ItemUtilParam.Item item, int quantity) { | 
|---|
|  |  |  | return remainingSpace >= quantity * item.unitSpace; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void addItem(ItemUtilParam.Item item, int quantity) { | 
|---|
|  |  |  | double spaceUsed = quantity * item.unitSpace; | 
|---|
|  |  |  | remainingSpace -= spaceUsed; | 
|---|
|  |  |  | contents.put(item.name, contents.getOrDefault(item.name, 0) + quantity); | 
|---|
|  |  |  | itemTypes.add(item.name); | 
|---|
|  |  |  | item.remaining -= quantity; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public boolean containsItemType(String itemName) { | 
|---|
|  |  |  | return itemTypes.contains(itemName); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public String toString() { | 
|---|
|  |  |  | StringBuilder sb = new StringBuilder(); | 
|---|
|  |  |  | sb.append("储物柜").append(id).append(": "); | 
|---|
|  |  |  | sb.append(String.format("剩余空间%.4f", remainingSpace)).append("\n"); | 
|---|
|  |  |  | for (Map.Entry<String, Integer> entry : contents.entrySet()) { | 
|---|
|  |  |  | sb.append("  ").append(entry.getKey()).append(": ").append(entry.getValue()).append("个\n"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return sb.toString(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|