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
11 changes: 11 additions & 0 deletions src/CardPayment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CardPayment implements PaymentMethod {
@Override
public void pay(int amount) {
System.out.println("Pay card: " + amount + " грн");
}

@Override
public String name() {
return "Card";
}
}
16 changes: 16 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
PaymentProcessor processor = new PaymentProcessor();
PaymentMethod card = new CardPayment();
PaymentMethod paypal = new PaypalPayment();
processor.process(card, 1000);
processor.process(paypal, 500);
System.out.println();
card.payWithFee(1000, 25);
paypal.payWithFee(500, 10);
}
}


14 changes: 14 additions & 0 deletions src/PaymentMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
interface PaymentMethod {
String name();

void pay(int amount);

default void payWithFee(int amount, int fee) {
int totalAmount = amount + fee;
System.out.println("Total amount to pay: " + totalAmount);
pay(totalAmount);
}
}



6 changes: 6 additions & 0 deletions src/PaymentProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class PaymentProcessor{
public void process(PaymentMethod method, int amount) {
System.out.println("Processing payment with " + method.name());
method.pay(amount);
}
}
11 changes: 11 additions & 0 deletions src/PaypalPayment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class PaypalPayment implements PaymentMethod {
@Override
public String name() {
return "PayPal";
}

@Override
public void pay(int amount) {
System.out.println("Paying " + amount + " using PayPal.");
}
}