Created
January 6, 2020 22:37
-
-
Save forkercat/6408269e45f77fdfe801866e33b1c372 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
// 记得导入类 | |
/** | |
* @author Junhao Wang | |
* @date 01/05/2020 | |
*/ | |
// 注意不要用下划线命名! | |
// Warehouse.java | |
public class Warehouse { | |
// main | |
public static void main(String[] args) { | |
Warehouse wh = new Warehouse(); | |
// test1 | |
wh.addProduct(new Product("p1")); | |
wh.addProduct(new Product("p1")); | |
wh.addProduct(new Product("p2")); | |
wh.addProduct(new Product("p3")); | |
wh.addProduct(null); | |
System.out.println(wh.productMap); | |
// test2 | |
boolean result = wh.satisfyOrder(Arrays.asList("p1", "p2", "p1")); | |
System.out.println(result); // true | |
result = wh.satisfyOrder(Arrays.asList("p1", "p2", "p2")); | |
System.out.println(result); // false | |
// test3 | |
result = wh.removeProduct("p1"); | |
System.out.println(result); // true | |
System.out.println(wh.productMap); | |
// test4 | |
result = wh.returnOrder(Arrays.asList("p1", "p2")); | |
System.out.println(result); | |
System.out.println(wh.productMap); | |
} | |
private Map<String, List<Product>> productMap; // <productID, product list> | |
private Queue<Product> productQueue; | |
// Constructor | |
public Warehouse() { | |
productMap = new HashMap<>(); | |
productQueue = new LinkedList<>(); | |
} | |
// 1. Returns true if the product has been successfully added | |
public boolean addProduct(Product product) { | |
// check | |
if (product == null) { | |
return false; | |
} | |
String pid = product.getProductID(); | |
List<Product> list = productMap.getOrDefault(pid, new ArrayList<>()); | |
list.add(product); | |
productMap.put(pid, list); | |
return true; | |
} | |
// 2. Returns true if the warehouse satisfies the order list | |
public boolean satisfyOrder(List<String> productIDList) { | |
// check | |
if (productIDList == null || productIDList.size() == 0) { | |
return true; | |
} | |
// count products in the list | |
Map<String, Integer> countMap = new HashMap<>(); | |
for (String pid : productIDList) { | |
int count = countMap.getOrDefault(pid, 0); | |
countMap.put(pid, count + 1); | |
} | |
// compare (for each pid) | |
for (String pid : countMap.keySet()) { | |
int count = countMap.get(pid); | |
List<Product> list = productMap.getOrDefault(pid, null); | |
if (list == null || count < list.size()) { // careful | |
return false; | |
} | |
} | |
return true; | |
} | |
// 3. Returns true if the product has been deleted | |
// 如果是 removeProduct(Product product)? (set? | |
public boolean removeProduct(String productID) { | |
// check | |
if (productID == null) { | |
return false; | |
} | |
if (!productMap.containsKey(productID)) { | |
return false; | |
} | |
// remove | |
List<Product> list = productMap.get(productID); | |
list.remove(list.size() - 1); | |
if (list.size() > 0) { | |
productMap.put(productID, list); | |
} else { | |
productMap.remove(productID); | |
} | |
return true; | |
} | |
// 4. Returns true if an order has been successfully returned | |
public boolean returnOrder(List<String> productIDList) { | |
// check | |
if (productIDList == null || productIDList.size() == 0) { | |
return false; | |
} | |
for (String pid : productIDList) { | |
boolean result = removeProduct(pid); | |
if (result == false) { | |
System.out.println("There is no more \"" + pid + "\" product."); | |
} | |
} | |
return true; | |
} | |
private static class Product { | |
private String productID; | |
public Product(String pid) { | |
productID = pid; | |
} | |
public String getProductID() { | |
return productID; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment