-
Notifications
You must be signed in to change notification settings - Fork 0
Order #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Order #9
Changes from all commits
b8bf8e9
e1e3408
ada14e7
835eb6d
f2b3607
5de0d95
9054ef3
2c7b1d1
dddbcf1
107ff0b
dc84225
40955c5
664b105
85ee0c9
6a74a14
3f0ecb3
81ef66a
8171939
bf295bf
bc0f630
a261e32
55f7922
55e29e6
f841918
87632c5
8a1d439
80dc0c9
9b427bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package id.ac.ui.cs.advprog.eshop.enums; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public enum OrderStatus { | ||
| WAITING_PAYMENT("WAITING_PAYMENT"), | ||
| FAILED("FAILED"), | ||
| SUCCESS("SUCCESS"), | ||
| CANCELLED("CANCELLED"); | ||
|
|
||
| private final String value; | ||
|
|
||
| private OrderStatus(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public static boolean contains(String param) { | ||
| for (OrderStatus orderStatus : OrderStatus.values()) { | ||
Check warningCode scanning / PMD Unnecessary qualifier 'PaymentStatus': 'values' is already in scope
Unnecessary qualifier 'OrderStatus': 'values' is already in scope
|
||
| if (orderStatus.name().equals(param)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package id.ac.ui.cs.advprog.eshop.enums; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public enum PaymentMethod { | ||
| VOUCHER_CODE("VOUCHER_CODE"), | ||
| BANK_TRANSFER("BANK_TRANSFER"), | ||
| CASH_ON_DELIVERY("CASH_ON_DELIVERY"); | ||
|
|
||
| private final String value; | ||
|
|
||
| private PaymentMethod(String value) { | ||
Check warningCode scanning / PMD Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type
Unnecessary modifier 'private' on constructor 'PaymentMethod(String)': enum constructors are implicitly private
|
||
| this.value = value; | ||
| } | ||
|
|
||
| public static boolean contains(String param) { | ||
| for (PaymentMethod paymentMethod : PaymentMethod.values()) { | ||
Check warningCode scanning / PMD Unnecessary qualifier 'PaymentStatus': 'values' is already in scope
Unnecessary qualifier 'PaymentMethod': 'values' is already in scope
|
||
| if (paymentMethod.name().equals(param)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package id.ac.ui.cs.advprog.eshop.enums; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public enum PaymentStatus { | ||
| SUCCESS("SUCCESS"), | ||
| REJECTED("REJECTED"); | ||
|
|
||
| private final String value; | ||
|
|
||
| private PaymentStatus(String value) { | ||
Check warningCode scanning / PMD Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type
Unnecessary modifier 'private' on constructor 'PaymentStatus(String)': enum constructors are implicitly private
|
||
| this.value = value; | ||
| } | ||
|
|
||
| public static boolean contains(String param) { | ||
| for (PaymentStatus paymentStatus : PaymentStatus.values()) { | ||
Check warningCode scanning / PMD Unnecessary qualifier 'PaymentStatus': 'values' is already in scope
Unnecessary qualifier 'PaymentStatus': 'values' is already in scope
|
||
| if (paymentStatus.name().equals(param)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package id.ac.ui.cs.advprog.eshop.model; | ||
|
|
||
| import id.ac.ui.cs.advprog.eshop.enums.OrderStatus; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Builder | ||
| @Getter | ||
| public class Order { | ||
| String id; | ||
| List<Product> products; | ||
| Long orderTime; | ||
| String author; | ||
| String status; | ||
|
|
||
| public Order(String id, List<Product> products, Long orderTime, String author) { | ||
| this.id = id; | ||
| this.orderTime = orderTime; | ||
| this.author = author; | ||
| this.status = OrderStatus.WAITING_PAYMENT.getValue(); | ||
|
|
||
| if (products.isEmpty()) { | ||
| throw new IllegalArgumentException(); | ||
| } else { | ||
| this.products = products; | ||
| } | ||
| } | ||
|
|
||
| public Order(String id, List<Product> products, Long orderTime, String author, String status) { | ||
| this(id, products, orderTime, author); | ||
| this.setStatus(status); | ||
| } | ||
|
|
||
| public void setStatus(String status) { | ||
| if (OrderStatus.contains(status)) { | ||
| this.status = status; | ||
| } else { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package id.ac.ui.cs.advprog.eshop.model; | ||
|
|
||
| import id.ac.ui.cs.advprog.eshop.enums.PaymentStatus; | ||
| import id.ac.ui.cs.advprog.eshop.enums.PaymentMethod; | ||
| import lombok.Getter; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @Getter | ||
| public class Payment { | ||
| String id; | ||
| String method; | ||
| String status; | ||
| Map<String, String> paymentData; | ||
| Order order; | ||
|
|
||
| public Payment(String id, String method, Order order, Map<String, String> paymentData) { | ||
| this.id = id; | ||
| this.method = method; | ||
| this.order = order; | ||
| this.paymentData = paymentData; | ||
|
|
||
| if (this.method.equals(PaymentMethod.VOUCHER_CODE.getValue())) { | ||
| this.status = checkVoucherCode(); | ||
| } else if (this.method.equals(PaymentMethod.BANK_TRANSFER.getValue())) { | ||
| this.status = checkBankTransfer(); | ||
| } else if (this.method.equals(PaymentMethod.CASH_ON_DELIVERY.getValue())) { | ||
| this.status = checkCashOnDelivery(); | ||
| } | ||
| } | ||
|
|
||
| public void setStatus(String status) { | ||
| if (PaymentStatus.contains(status)) { | ||
| this.status = status; | ||
| } else { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| public String checkVoucherCode() { | ||
| String voucherCode = this.paymentData.get("voucherCode"); | ||
| if (voucherCode == null) { | ||
| return PaymentStatus.REJECTED.getValue(); | ||
| } | ||
|
|
||
| if (voucherCode.length() != 16) { | ||
| return PaymentStatus.REJECTED.getValue(); | ||
| } | ||
|
|
||
| if (!voucherCode.startsWith("ESHOP")) { | ||
| return PaymentStatus.REJECTED.getValue(); | ||
| } | ||
|
|
||
| int counter = 0; | ||
| for (char character: voucherCode.toCharArray()) { | ||
| if (Character.isDigit(character)) { | ||
| counter += 1; | ||
| } | ||
| } | ||
| if (counter != 8) { | ||
| return PaymentStatus.REJECTED.getValue(); | ||
| } | ||
|
|
||
| return PaymentStatus.SUCCESS.getValue(); | ||
| } | ||
|
|
||
| public String checkBankTransfer() { | ||
| String bankName = this.paymentData.get("bankName"); | ||
| String referenceCode = this.paymentData.get("referenceCode"); | ||
|
|
||
| if (bankName == null || bankName.isEmpty()) { | ||
| return PaymentStatus.REJECTED.getValue(); | ||
| } else if (referenceCode == null || referenceCode.isEmpty()) { | ||
| return PaymentStatus.REJECTED.getValue(); | ||
| } | ||
|
|
||
| return PaymentStatus.SUCCESS.getValue(); | ||
| } | ||
|
|
||
| public String checkCashOnDelivery() { | ||
| String address = this.paymentData.get("address"); | ||
| String deliveryFee = this.paymentData.get("deliveryFee"); | ||
|
|
||
| if (address == null || address.isEmpty()) { | ||
| return PaymentStatus.REJECTED.getValue(); | ||
| } else if (deliveryFee == null || deliveryFee.isEmpty()) { | ||
| return PaymentStatus.REJECTED.getValue(); | ||
| } | ||
|
|
||
| return PaymentStatus.SUCCESS.getValue(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package id.ac.ui.cs.advprog.eshop.repository; | ||
|
|
||
| import id.ac.ui.cs.advprog.eshop.model.Order; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @Repository | ||
| public class OrderRepository { | ||
| private List<Order> orderData = new ArrayList<>(); | ||
|
|
||
| public Order save(Order order) { | ||
| int i = 0; | ||
| for (Order savedOrder : orderData) { | ||
| if (savedOrder.getId().equals(order.getId())) { | ||
| orderData.remove(i); | ||
| orderData.add(i, order); | ||
| return order; | ||
| } | ||
| i += 1; | ||
| } | ||
|
|
||
| orderData.add(order); | ||
| return order; | ||
| } | ||
|
|
||
| public Order findById(String id) { | ||
| for (Order savedOrder : orderData) { | ||
| if (savedOrder.getId().equals(id)) { | ||
| return savedOrder; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public List<Order> findAllByAuthor(String author) { | ||
| List<Order> result = new ArrayList<>(); | ||
| for (Order savedOrder : orderData) { | ||
| if (savedOrder.getAuthor().equals(author)) { | ||
| result.add(savedOrder); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package id.ac.ui.cs.advprog.eshop.repository; | ||
|
|
||
| import id.ac.ui.cs.advprog.eshop.model.Payment; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class PaymentRepository { | ||
| private List<Payment> payments = new ArrayList<>(); | ||
|
|
||
| public Payment save(Payment payment) { | ||
| int i = 0; | ||
| for (Payment savedPayment : payments) { | ||
| if (savedPayment.getId().equals(payment.getId())) { | ||
| payments.remove(i); | ||
| payments.add(i, payment); | ||
| return payment; | ||
| } | ||
| i += 1; | ||
| } | ||
| payments.add(payment); | ||
| return payment; | ||
| } | ||
|
|
||
| public Payment findById(String id) { | ||
| for (Payment savedPayment : payments) { | ||
| if (savedPayment.getId().equals(id)) { | ||
| return savedPayment; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public List<Payment> getAllPayment() { | ||
| List<Payment> result = new ArrayList<>(); | ||
| for (Payment savedPayment : payments) { | ||
| result.add(savedPayment); | ||
| } | ||
| return result; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package id.ac.ui.cs.advprog.eshop.service; | ||
|
|
||
| import id.ac.ui.cs.advprog.eshop.model.Order; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface OrderService { | ||
| public Order createOrder(Order order); | ||
Check warningCode scanning / PMD Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type
Unnecessary modifier 'public' on method 'createOrder': the method is declared in an interface type
|
||
| public Order updateStatus(String orderId, String status); | ||
Check warningCode scanning / PMD Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type
Unnecessary modifier 'public' on method 'updateStatus': the method is declared in an interface type
|
||
| public Order findById(String orderId); | ||
Check warningCode scanning / PMD Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type
Unnecessary modifier 'public' on method 'findById': the method is declared in an interface type
|
||
| public List<Order> findAllByAuthor(String author); | ||
Check warningCode scanning / PMD Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type
Unnecessary modifier 'public' on method 'findAllByAuthor': the method is declared in an interface type
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package id.ac.ui.cs.advprog.eshop.service; | ||
|
|
||
| import id.ac.ui.cs.advprog.eshop.model.Order; | ||
| import id.ac.ui.cs.advprog.eshop.repository.OrderRepository; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| @Service | ||
| public class OrderServiceImpl implements OrderService{ | ||
| @Autowired | ||
| private OrderRepository orderRepository; | ||
|
|
||
| @Override | ||
| public Order createOrder(Order order) { | ||
| if (orderRepository.findById(order.getId()) == null) { | ||
| orderRepository.save(order); | ||
| return order; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Order updateStatus(String orderId, String status) { | ||
| Order order = orderRepository.findById(orderId); | ||
| if (order != null) { | ||
| Order newOrder = new Order(order.getId(), order.getProducts(), | ||
| order.getOrderTime(), order.getAuthor(), status); | ||
| orderRepository.save(newOrder); | ||
| return newOrder; | ||
| } else { | ||
| throw new NoSuchElementException(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public List<Order> findAllByAuthor(String author) { | ||
| return orderRepository.findAllByAuthor(author); | ||
| } | ||
|
|
||
| @Override | ||
| public Order findById(String orderId) { | ||
| return orderRepository.findById(orderId); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package id.ac.ui.cs.advprog.eshop.service; | ||
|
|
||
| import id.ac.ui.cs.advprog.eshop.model.Payment; | ||
| import id.ac.ui.cs.advprog.eshop.model.Order; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.List; | ||
|
|
||
| public interface PaymentService { | ||
| public Payment addPayment(String paymentId, Order order, String method, Map<String, String> paymentData); | ||
Check warningCode scanning / PMD Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type
Unnecessary modifier 'public' on method 'addPayment': the method is declared in an interface type
|
||
| public Payment setStatus(Payment payment, String status); | ||
Check warningCode scanning / PMD Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type
Unnecessary modifier 'public' on method 'setStatus': the method is declared in an interface type
|
||
| public Payment getPayment(String paymentId); | ||
Check warningCode scanning / PMD Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type
Unnecessary modifier 'public' on method 'getPayment': the method is declared in an interface type
|
||
| public List<Payment> getAllPayments(); | ||
Check warningCode scanning / PMD Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type
Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type
|
||
| } | ||
Check warning
Code scanning / PMD
Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type