Add Order
parent
85eabab106
commit
eb314bcae3
|
|
@ -0,0 +1,8 @@
|
||||||
|
package org.example;
|
||||||
|
|
||||||
|
public class Constants {
|
||||||
|
|
||||||
|
public static final float lowVat = 0.07f;
|
||||||
|
public static final float highVat = 0.19f;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,94 @@
|
||||||
|
package org.example;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Order {
|
||||||
|
|
||||||
|
List<Product> products;
|
||||||
|
|
||||||
|
public Order(List<Product> products) {
|
||||||
|
this.products = products;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float calculateTotalPriceWithVat() {
|
||||||
|
return calculateTotalPrice() + calculateTotalVat();
|
||||||
|
}
|
||||||
|
|
||||||
|
public float calculateTotalPrice() {
|
||||||
|
return getPrice(this.products);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float calculateTotalVat() {
|
||||||
|
return getVat(this.products);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float calculateShippingVat() {
|
||||||
|
float totalWeight = calculateWeight(this.products);
|
||||||
|
float totalPrice = calculateTotalPrice();
|
||||||
|
float baseShippingCost = calculateBaseShippingCost(totalWeight, totalPrice);
|
||||||
|
List<Product> lowVatProducts = filterLowVatProducts(this.products);
|
||||||
|
List<Product> highVatProducts = filterHighVatProducts(this.products);
|
||||||
|
float lowVatPortion = getPrice(lowVatProducts) / totalPrice;
|
||||||
|
float highVatPortion = getPrice(highVatProducts) / totalPrice;
|
||||||
|
return baseShippingCost * lowVatPortion / (1 + Constants.lowVat) + baseShippingCost * highVatPortion / (1 + Constants.highVat);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float calculateBaseShippingCost(float totalWeight, float price) {
|
||||||
|
if (price >= 500f)
|
||||||
|
return 0f;
|
||||||
|
if (totalWeight <= 0.1f)
|
||||||
|
return 3.95f;
|
||||||
|
if (totalWeight <= 1f)
|
||||||
|
return 4.95f;
|
||||||
|
if (totalWeight <= 5f)
|
||||||
|
return 5.95f;
|
||||||
|
return 19.95f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private List<Product> filterLowVatProducts(List<Product> products) {
|
||||||
|
List<Product> lowVatProducts = new ArrayList<>();
|
||||||
|
for (Product product : products) {
|
||||||
|
if (product.vatPortion == Constants.lowVat) {
|
||||||
|
lowVatProducts.add(product);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lowVatProducts;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Product> filterHighVatProducts(List<Product> products) {
|
||||||
|
List<Product> highVatProducts = new ArrayList<>();
|
||||||
|
for (Product product : products) {
|
||||||
|
if (product.vatPortion == Constants.highVat) {
|
||||||
|
highVatProducts.add(product);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return highVatProducts;
|
||||||
|
}
|
||||||
|
|
||||||
|
private float getPrice(List<Product> products) {
|
||||||
|
float total = 0;
|
||||||
|
for (Product product : products) {
|
||||||
|
total += product.price;
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
private float getVat(List<Product> products) {
|
||||||
|
float total = 0;
|
||||||
|
for (Product product : products) {
|
||||||
|
total += product.getVat();
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
private float calculateWeight(List<Product> products) {
|
||||||
|
float totalWeight = 0;
|
||||||
|
for (Product product : products) {
|
||||||
|
totalWeight += product.weight;
|
||||||
|
}
|
||||||
|
return totalWeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -25,4 +25,16 @@ public class ShoppingCart {
|
||||||
public void clearProducts() {
|
public void clearProducts() {
|
||||||
products.clear();
|
products.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Order toOrder() {
|
||||||
|
List<Product> productsInOrder = new ArrayList<>();
|
||||||
|
for (HashMap.Entry<Product, Integer> entry : products.entrySet()) {
|
||||||
|
Product product = entry.getKey();
|
||||||
|
int quantity = entry.getValue();
|
||||||
|
for (int i = 0; i < quantity; i++) {
|
||||||
|
productsInOrder.add(product);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Order(productsInOrder);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue