Add & generate tracking information to (Supplier-)OrderConfirmation. Fixes #110

This commit is contained in:
2020-06-24 00:37:58 +02:00
parent 186db31b17
commit 8d27c486d5
11 changed files with 162 additions and 16 deletions

View File

@ -1,5 +1,9 @@
package org.hso.ecommerce.supplier;
import org.hso.ecommerce.supplier.carrier.Avian;
import org.hso.ecommerce.supplier.carrier.Carrier;
import org.hso.ecommerce.supplier.carrier.Posaidon;
import org.hso.ecommerce.supplier.carrier.Shredder;
import org.hso.ecommerce.supplier.data.Article;
import org.hso.ecommerce.supplier.data.Order;
import org.hso.ecommerce.supplier.data.OrderConfirmation;
@ -19,9 +23,13 @@ import java.util.List;
public class RequestController {
private final HashMap<String, Integer> dailySalesVolumeCent = new HashMap<>();
private final HashMap<String, Supplier> knownSuppliers = new HashMap<>();
private final HashMap<String, Supplier> knownSuppliers = new HashMap<>();
private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
private final Carrier[] carriers = new Carrier[]{
new Avian(), new Posaidon(), new Shredder()
};
@PostConstruct
public void init() throws IOException {
for (Supplier s : ConfigurationReader.read()) {
@ -38,7 +46,7 @@ public class RequestController {
@GetMapping("/{supplier}/")
public Supplier supplier(HttpServletResponse res, @PathVariable("supplier") String supplierName) {
Supplier s = knownSuppliers.get(supplierName);
if(s == null) {
if (s == null) {
res.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
return s;
@ -47,16 +55,16 @@ public class RequestController {
@PostMapping("/{supplier}/order")
public OrderConfirmation order(HttpServletResponse res, @PathVariable("supplier") String supplierName, @RequestBody Order order) {
Supplier s = knownSuppliers.get(supplierName);
if(s == null) {
if (s == null) {
res.setStatus(HttpServletResponse.SC_NOT_FOUND);
return null;
}
String dateKey = simpleDateFormat.format(new Date());
int dailyVolume = dailySalesVolumeCent.getOrDefault(dateKey,0);
int dailyVolume = dailySalesVolumeCent.getOrDefault(dateKey, 0);
Article a = s.findArticle(order.manufacturer, order.articleNumber);
if(a == null) {
if (a == null) {
res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return null;
}
@ -72,6 +80,8 @@ public class RequestController {
}
int discount = (discountableNetAmount * s.discount.percentDiscount) / 100;
Carrier selectedCarrier = carriers[Math.abs((supplierName + java.time.LocalDate.now()).hashCode()) % carriers.length];
OrderConfirmation confirmation = new OrderConfirmation();
confirmation.articleNumber = order.articleNumber;
confirmation.discountNetCent = discount;
@ -79,6 +89,9 @@ public class RequestController {
confirmation.manufacturer = a.manufacturer;
confirmation.quantity = order.quantity;
confirmation.totalPriceNetCharged = priceNet - discount;
confirmation.carrier = selectedCarrier.getName();
confirmation.trackingId = selectedCarrier.generateTrackingId();
confirmation.estimatedArrival = selectedCarrier.arrivalEstimate();
if (confirmation.totalPriceNetCharged > order.maxTotalPriceCentNet) {
res.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);

View File

@ -0,0 +1,28 @@
package org.hso.ecommerce.supplier.carrier;
import java.time.LocalDateTime;
import java.util.Random;
public class Avian implements Carrier {
@Override
public String getName() {
return "Avian Carriers";
}
@Override
public String generateTrackingId() {
Random rnd = new Random();
return "2001-"
+ Integer.toHexString(rnd.nextInt(0xFFFF))
+ "--"
+ Integer.toHexString(rnd.nextInt(0xFFFF))
+ "-"
+ Integer.toHexString(rnd.nextInt(0xFFFF));
}
@Override
public LocalDateTime arrivalEstimate() {
return LocalDateTime.now().plusHours(8);
}
}

View File

@ -0,0 +1,12 @@
package org.hso.ecommerce.supplier.carrier;
import java.time.LocalDateTime;
public interface Carrier {
public String getName();
public String generateTrackingId();
public LocalDateTime arrivalEstimate();
}

View File

@ -0,0 +1,26 @@
package org.hso.ecommerce.supplier.carrier;
import java.time.LocalDateTime;
import java.util.Random;
public class Posaidon implements Carrier {
@Override
public String getName() {
return "Poseidon Inc.";
}
@Override
public String generateTrackingId() {
Random rnd = new Random();
return "WAT"
+ Integer.toString(rnd.nextInt(Short.MAX_VALUE))
+ "3"
+ Integer.toString(rnd.nextInt(Short.MAX_VALUE))
+ "R";
}
@Override
public LocalDateTime arrivalEstimate() {
return LocalDateTime.now().plusHours(50);
}
}

View File

@ -0,0 +1,31 @@
package org.hso.ecommerce.supplier.carrier;
import java.time.LocalDateTime;
import java.util.Random;
public class Shredder implements Carrier {
private Random rnd = new Random();
@Override
public String getName() {
return "Schree & Derr";
}
@Override
public String generateTrackingId() {
return "O" + d() + d() + d() + d() + d() + d() + d() + d() + d() + d() + d() + d() + d() + d() + d() + d() + d() + d() + "0";
}
@Override
public LocalDateTime arrivalEstimate() {
return LocalDateTime.now().plusHours(22);
}
/**
* @return a random digit followed by a dash.
*/
private String d() {
return Integer.toString(rnd.nextInt(9)) + "-";
}
}

View File

@ -1,5 +1,7 @@
package org.hso.ecommerce.supplier.data;
import java.time.LocalDateTime;
public class OrderConfirmation {
public String manufacturer;
public String articleNumber;
@ -9,4 +11,8 @@ public class OrderConfirmation {
public int pricePerUnitNetCent;
public int discountNetCent;
public int totalPriceNetCharged;
public String carrier;
public String trackingId;
public LocalDateTime estimatedArrival;
}