Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b8bf8e9
[RED] add tests for Order model
astrialyanda Mar 5, 2024
e1e3408
[RED] add Order model skeleton
astrialyanda Mar 5, 2024
ada14e7
[GREEN] implement Order model
astrialyanda Mar 5, 2024
835eb6d
[REFACTOR] add OrderStatus enum
astrialyanda Mar 5, 2024
f2b3607
[REFACTOR] apply OrderStatus enum check to Order model
astrialyanda Mar 5, 2024
5de0d95
[REFACTOR] apply OrderStatus enum to Order model tests
astrialyanda Mar 5, 2024
9054ef3
[RED] add tests for OrderRepository
astrialyanda Mar 5, 2024
2c7b1d1
[RED] add OrderRepository skeleton
astrialyanda Mar 5, 2024
dddbcf1
[GREEN] implement OrderRepository class
astrialyanda Mar 6, 2024
107ff0b
[RED] add tests for OrderServiceImpl
astrialyanda Mar 6, 2024
dc84225
[RED] add OrderServiceImpl skeleton
astrialyanda Mar 6, 2024
40955c5
[GREEN] implement OrderService class
astrialyanda Mar 6, 2024
664b105
[RED] add tests for Payment model
astrialyanda Mar 6, 2024
85ee0c9
[RED] add Payment model skeleton
astrialyanda Mar 6, 2024
6a74a14
[GREEN] implement Payment model
astrialyanda Mar 6, 2024
3f0ecb3
[REFACTOR] add PaymentStatus enum
astrialyanda Mar 6, 2024
81ef66a
[REFACTOR] add PaymentMethod enum
astrialyanda Mar 6, 2024
8171939
[REFACTOR] apply PaymentStatus and PaymentMethod enum check on Paymen…
astrialyanda Mar 6, 2024
bf295bf
[REFACTOR] apply PaymentStatus and PaymentMethod enum to Payment mode…
astrialyanda Mar 6, 2024
bc0f630
[RED] add tests for PaymentRepository
astrialyanda Mar 6, 2024
a261e32
[RED] add PaymentRepository skeleton
astrialyanda Mar 6, 2024
55f7922
Fix PaymentRepository test
astrialyanda Mar 6, 2024
55e29e6
[GREEN] implement PaymentRepository class
astrialyanda Mar 6, 2024
f841918
[RED] add tests for PaymentServiceImpl
astrialyanda Mar 6, 2024
87632c5
[RED] add PaymentServiceImpl skeleton
astrialyanda Mar 6, 2024
8a1d439
Fix: fix PaymentService tests
astrialyanda Mar 6, 2024
80dc0c9
[GREEN] implement PaymentService class
astrialyanda Mar 6, 2024
9b427bb
add reflection 4
astrialyanda Mar 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
link web app: https://advshop-aya.koyeb.app/

## Tutorial-4
### Reflection
1. menurut saya flow TDD berguna untuk saya dalam membuat suatu code. Dengan menggunakan flow TDD, saya jadi langsung mengetahui apabila ada kesalahan/bug pada kode saya sehingga bisa segera diperbaiki. Selain itu, karena sudah terlebih dahulu membuat testing, saya jadi mengetahui bagian bagian mana yang mungkin menghasilkan bug sehingga bisa dihindari lebih awal. Hal-hal lain yang harus saya lakukan ke depannya adalah memastikan testing sudah mencakup sebanyak-banyaknya kemungkinan bug.

2. menurut saya F.I.R.S.T. principle yaitu fast, isolated/independent, repeatable, self-validating, dan thorough/timely

## Tutorial-3
### Reflection
1. Explain what principles you apply to your project!
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/id/ac/ui/cs/advprog/eshop/enums/OrderStatus.java
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) {

Check warning

Code scanning / PMD

Unnecessary modifier 'public' on method 'getAllPayments': the method is declared in an interface type

Unnecessary modifier 'private' on constructor 'OrderStatus(String)': enum constructors are implicitly private
this.value = value;
}

public static boolean contains(String param) {
for (OrderStatus orderStatus : OrderStatus.values()) {

Check warning

Code 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;
}
}
25 changes: 25 additions & 0 deletions src/main/java/id/ac/ui/cs/advprog/eshop/enums/PaymentMethod.java
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 warning

Code 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 warning

Code 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;
}
}
24 changes: 24 additions & 0 deletions src/main/java/id/ac/ui/cs/advprog/eshop/enums/PaymentStatus.java
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 warning

Code 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 warning

Code 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;
}
}
43 changes: 43 additions & 0 deletions src/main/java/id/ac/ui/cs/advprog/eshop/model/Order.java
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();
}
}
}
92 changes: 92 additions & 0 deletions src/main/java/id/ac/ui/cs/advprog/eshop/model/Payment.java
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;
}
}
12 changes: 12 additions & 0 deletions src/main/java/id/ac/ui/cs/advprog/eshop/service/OrderService.java
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 warning

Code 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 warning

Code 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 warning

Code 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 warning

Code 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 warning

Code 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 warning

Code 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 warning

Code 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 warning

Code 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
}
Loading