-
Notifications
You must be signed in to change notification settings - Fork 0
Exercise Tutorial 4: Implement a New Feature #12
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: main
Are you sure you want to change the base?
Changes from all commits
46e0d05
8985e9d
3787cab
f31cdf7
3f7e796
1e7af95
34cdb67
753b009
be5349d
b43e5b0
4b86e98
5a02932
127ffe5
1b9271d
a684f35
0172151
2bb3f47
67518c0
bec102e
75c5200
d9aeb22
1fcb422
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,23 @@ | ||
| 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,45 @@ | ||
| 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,58 @@ | ||
| package id.ac.ui.cs.advprog.eshop.model; | ||
|
|
||
| import id.ac.ui.cs.advprog.eshop.enums.OrderStatus; | ||
Check warningCode scanning / PMD Unused import 'id.ac.ui.cs.advprog.eshop.enums.PaymentStatus'
Unused import 'id.ac.ui.cs.advprog.eshop.enums.OrderStatus'
|
||
| import id.ac.ui.cs.advprog.eshop.enums.PaymentStatus; | ||
| import lombok.Builder; | ||
Check warningCode scanning / PMD Unused import 'id.ac.ui.cs.advprog.eshop.enums.PaymentStatus'
Unused import 'lombok.Builder'
|
||
| import lombok.Getter; | ||
|
|
||
|
|
||
| import java.util.Map; | ||
|
|
||
|
|
||
| @Getter | ||
| public abstract class Payment { | ||
|
|
||
| String id; | ||
| Order order; | ||
| Map<String, String> paymentData; | ||
|
|
||
| String status; | ||
|
|
||
|
|
||
|
|
||
| public Payment(String id, Order order, Map<String, String> paymentData) { | ||
| this.id = id; | ||
| this.order = order; | ||
| this.paymentData = paymentData; | ||
|
|
||
|
|
||
| if (paymentDataIsValid()){ | ||
| setStatus("SUCCESS"); | ||
| } else{ | ||
| setStatus("REJECTED"); | ||
| } | ||
| } | ||
| public Payment(String id, Order order, Map<String, String> paymentData, String status) { | ||
| this.id = id; | ||
| this.order = order; | ||
| this.paymentData = paymentData; | ||
| setStatus(status); | ||
| } | ||
| public void setStatus(String status){ | ||
| if (PaymentStatus.contains(status)){ | ||
| this.status = status; | ||
|
|
||
| if (status.equals("SUCCESS")){ | ||
Check warningCode scanning / PMD Position literals first in String comparisons
Position literals first in String comparisons
|
||
| order.setStatus("SUCCESS"); | ||
| } else if (status.equals("REJECTED")){ | ||
Check warningCode scanning / PMD Position literals first in String comparisons
Position literals first in String comparisons
|
||
| order.setStatus("FAILED"); | ||
| } | ||
|
|
||
| } else{ | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| abstract boolean paymentDataIsValid(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package id.ac.ui.cs.advprog.eshop.model; | ||
|
|
||
|
|
||
| import lombok.Builder; | ||
Check warningCode scanning / PMD Unused import 'id.ac.ui.cs.advprog.eshop.enums.PaymentStatus'
Unused import 'lombok.Builder'
|
||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class PaymentCashOnDelivery extends Payment{ | ||
| public PaymentCashOnDelivery(String id, Order order, Map<String, String> paymentData) { | ||
| super(id, order, paymentData); | ||
| } | ||
|
|
||
| @Override | ||
| boolean paymentDataIsValid() { | ||
| String address = paymentData.get("address"); | ||
| String deliveryFee = paymentData.get("deliveryFee"); | ||
|
|
||
| return address != null && !address.isEmpty() && deliveryFee != null && !deliveryFee.isEmpty(); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package id.ac.ui.cs.advprog.eshop.model; | ||
|
|
||
| import lombok.Builder; | ||
Check warningCode scanning / PMD Unused import 'id.ac.ui.cs.advprog.eshop.enums.PaymentStatus'
Unused import 'lombok.Builder'
|
||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class PaymentVoucherCode extends Payment{ | ||
| public PaymentVoucherCode(String id, Order order, Map<String, String> paymentData) { | ||
| super(id, order, paymentData); | ||
| } | ||
|
|
||
| @Override | ||
| boolean paymentDataIsValid() { | ||
| String voucherCode = paymentData.get("voucherCode"); | ||
| if (voucherCode == null || voucherCode.length() != 16 || !voucherCode.startsWith("ESHOP")) { | ||
| return false; | ||
| } | ||
|
|
||
| int numCharCount = 0; | ||
| for (int i = 0; i < voucherCode.length(); i++){ | ||
| if (Character.isDigit(voucherCode.charAt(i))){ | ||
| numCharCount++; | ||
| } | ||
| } | ||
|
|
||
| if (numCharCount != 8){ | ||
| return false; | ||
| } | ||
|
Comment on lines
+26
to
+28
Check warningCode scanning / PMD This if statement can be replaced by `return !{condition};`
This if statement can be replaced by `return !{condition};`
|
||
|
|
||
|
|
||
| return true; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package id.ac.ui.cs.advprog.eshop.repository; | ||
|
|
||
| import org.springframework.stereotype.Repository; | ||
| import id.ac.ui.cs.advprog.eshop.model.Order; | ||
|
|
||
| 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,37 @@ | ||
| 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> paymentData = new ArrayList<>(); | ||
|
|
||
| public Payment save(Payment payment) { | ||
| int i = 0; | ||
| for (Payment savedPayment : paymentData){ | ||
| if (savedPayment.getId().equals(payment.getId())){ | ||
| paymentData.remove(i); | ||
| paymentData.add(i, payment); | ||
| return payment; | ||
| } | ||
| i += 1; | ||
| } | ||
|
|
||
| paymentData.add(payment); | ||
|
|
||
| return payment; | ||
| } | ||
| public Payment findById(String id){ | ||
| for (Payment savedPayment : paymentData){ | ||
| if (savedPayment.getId().equals(id)){ | ||
| return savedPayment; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| public List<Payment> getAllPayment(){ | ||
| return paymentData; | ||
| } | ||
| } |
| 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,43 @@ | ||
| 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); | ||
| } | ||
| } |
Check warning
Code scanning / PMD
Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type