Skip to content

Instantly share code, notes, and snippets.

@forkercat
Created January 6, 2020 22:38
Show Gist options
  • Save forkercat/fee4de6c1f07a040eb3f7508ec7ccd20 to your computer and use it in GitHub Desktop.
Save forkercat/fee4de6c1f07a040eb3f7508ec7ccd20 to your computer and use it in GitHub Desktop.
import java.util.HashMap;
import java.util.Map;
/**
* @author Junhao Wang
* @date 01/05/2020
*/
// WayBid.java
public class WayBid {
public static void main(String[] args) {
WayBid wb = new WayBid();
// test1
System.out.println(wb.calculateWayBid(1.0, 2.0)); // 1.534618
// test2
System.out.println(wb.calculateWayBid("LLX", 1.0, 2.0));
System.out.println(wb.customerMap);
// test3
System.out.println(wb.calculateWayBid("LLX", "iPhone 7", 1.0, 2.0));
System.out.println(wb.calculateWayBid("LLX", "iPhone 8", 1.0, 2.0));
System.out.println(wb.calculateWayBid("Rita", "Android", 1.0, 2.0));
System.out.println(wb.customerPlatformMap);
}
private Map<String, Double> customerMap;
private Map<String, Map<String, Double>> customerPlatformMap;
public WayBid() {
customerMap = new HashMap<>();
customerPlatformMap = new HashMap<>();
}
// 1. Returns WayBid
public double calculateWayBid(double arg1, double arg2) {
// check
double waybid = Math.sin(arg1) + Math.log(arg2);
return waybid;
}
// 2. Returns WayBid given a customer ID
// overload
public double calculateWayBid(String customerID, double arg1, double arg2) {
// check customerID, arguments' validity
double waybid = Math.sin(arg1) + Math.log(arg2);
customerMap.put(customerID, waybid);
return waybid;
}
// overload
public double getWayBid(String customerID) {
// check customerID
return customerMap.get(customerID);
}
// 3. Returns WayBid given a customer ID and a platform type
// overload
public double calculateWayBid(String customerID, String platform, double arg1, double arg2) {
// check customerID, platform, arguments' validity
double waybid = Math.sin(arg1) + Math.log(arg2);
Map<String, Double> platformMap = customerPlatformMap.getOrDefault(customerID, new HashMap<>());
platformMap.put(platform, waybid);
customerPlatformMap.put(customerID, platformMap);
return waybid;
}
public double getWayBid(String customerID, String platform) {
// check customerID, platform
return customerPlatformMap.get(customerID).get(platform);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment