Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 20 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<version>6.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -28,9 +28,14 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.15.2</version>
<version>5.21.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -57,6 +62,19 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>1.22.0</version>
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/example/payment/DatabaseConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.payment;

public interface DatabaseConnection {
void executeUpdate(String query);
}
5 changes: 5 additions & 0 deletions src/main/java/com/example/payment/EmailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.payment;

public interface EmailService {
void sendPaymentConfirmation(String email, double amount);
}
5 changes: 5 additions & 0 deletions src/main/java/com/example/payment/PaymentApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.payment;

public interface PaymentApi {
PaymentApiResponse charge(String apiKey, double amount);
}
4 changes: 4 additions & 0 deletions src/main/java/com/example/payment/PaymentApiResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.payment;

public record PaymentApiResponse(boolean success) {
}
51 changes: 30 additions & 21 deletions src/main/java/com/example/payment/PaymentProcessor.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
package com.example.payment;

//public class PaymentProcessor {
// private static final String API_KEY = "sk_test_123456";
//
// public boolean processPayment(double amount) {
// // Anropar extern betaltjänst direkt med statisk API-nyckel
// PaymentApiResponse response = PaymentApi.charge(API_KEY, amount);
//
// // Skriver till databas direkt
// if (response.isSuccess()) {
// DatabaseConnection.getInstance()
// .executeUpdate("INSERT INTO payments (amount, status) VALUES (" + amount + ", 'SUCCESS')");
// }
//
// // Skickar e-post direkt
// if (response.isSuccess()) {
// EmailService.sendPaymentConfirmation("user@example.com", amount);
// }
//
// return response.isSuccess();
// }
//}
public class PaymentProcessor {
private static final String API_KEY = "sk_test_123456";
private final DatabaseConnection databaseConnection;
private final EmailService emailService;
private final PaymentApi paymentApi;

public PaymentProcessor(DatabaseConnection databaseConnection, EmailService emailService, PaymentApi paymentApi) {
this.databaseConnection = databaseConnection;
this.emailService = emailService;
this.paymentApi = paymentApi;
}

public boolean processPayment(double amount) {
// Anropar extern betaltjänst direkt med statisk API-nyckel
PaymentApiResponse response = this.paymentApi.charge(API_KEY, amount);

// Skriver till databas direkt
if (response.success()) {
this.databaseConnection
.executeUpdate("INSERT INTO payments (amount, status) VALUES (" + amount + ", 'SUCCESS')");
}

// Skickar e-post direkt
if (response.success()) {
emailService.sendPaymentConfirmation("user@example.com", amount);
}

return response.success();
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/example/shop/Discount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.shop;

public class Discount {
private int percentage;

public Discount(int percentage) {
this.percentage = percentage;
}

public double applyDiscount(double totalPrice){
double result = totalPrice - (totalPrice * ((double) this.percentage /100));
return Math.max(0, result);
}

public int getPercentage() {
return percentage;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/example/shop/DiscountProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.shop;

public interface DiscountProvider {
double applyDiscount(double totalPrice);
}
27 changes: 27 additions & 0 deletions src/main/java/com/example/shop/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.shop;

public class Item {

private String id;
private double price;
private int quantity;

public Item(String id, double price, int quantity) {
this.id = id;
this.price = price;
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getId() {
return id;
}

}
53 changes: 53 additions & 0 deletions src/main/java/com/example/shop/ShoppingCart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.shop;

import java.util.ArrayList;
import java.util.List;

public class ShoppingCart {

private List<Item> items = new ArrayList<>();
private Discount discount;

public void addItem(Item newItem){

if ( newItem.getQuantity() > 0) {
for (Item existingItem : items) {
if (existingItem.getId().equals(newItem.getId())) {
int updatedQuantity = existingItem.getQuantity() + newItem.getQuantity();
existingItem.setQuantity(updatedQuantity);

return;
}
}
this.items.add(newItem);
}
}

public List<Item> getItems() {
return items;
}

public void removeItem(Item item){
this.items.remove(item);
}

public void setDiscount(Discount discount) {
this.discount = discount;
}

public double getTotalPrice(){
double totalPrice = items.stream()
.mapToDouble(item -> item.getPrice() * item.getQuantity())
.sum();

if (discount != null && discount.getPercentage() <= 100 && discount.getPercentage() > 0) {
return discount.applyDiscount(totalPrice);
}

return totalPrice;

}



}
Loading