From 2294ce535143585163ecc3226a99807426649918 Mon Sep 17 00:00:00 2001 From: Feror Date: Thu, 23 Jan 2025 12:13:56 +0100 Subject: [PATCH] Utilisation du design pattern strategy --- .../FeeCalculationStartegyForChildren.java | 13 +++++++++++++ .../org/example/FeeCalculationStrategy.java | 5 +++++ .../FeeCalculationStrategyForAdults.java | 13 +++++++++++++ .../main/java/org/example/FeeCalculator.java | 18 +----------------- .../src/main/java/org/example/Visitor.java | 10 ++++++++++ 5 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 feeCalculator/src/main/java/org/example/FeeCalculationStartegyForChildren.java create mode 100644 feeCalculator/src/main/java/org/example/FeeCalculationStrategy.java create mode 100644 feeCalculator/src/main/java/org/example/FeeCalculationStrategyForAdults.java diff --git a/feeCalculator/src/main/java/org/example/FeeCalculationStartegyForChildren.java b/feeCalculator/src/main/java/org/example/FeeCalculationStartegyForChildren.java new file mode 100644 index 0000000..47681e0 --- /dev/null +++ b/feeCalculator/src/main/java/org/example/FeeCalculationStartegyForChildren.java @@ -0,0 +1,13 @@ +package org.example; + +public class FeeCalculationStartegyForChildren implements FeeCalculationStrategy { + private static final double CHILD_PRICE_BASE = 100; + public double calculateFee(TicketType ticketType) { + if (TicketType.HALF_DAY == ticketType) { + return CHILD_PRICE_BASE * 0.2; + } else if (TicketType.FULL_DAY == ticketType) { + return CHILD_PRICE_BASE * 0.5; + } + return 0; + } +} diff --git a/feeCalculator/src/main/java/org/example/FeeCalculationStrategy.java b/feeCalculator/src/main/java/org/example/FeeCalculationStrategy.java new file mode 100644 index 0000000..b73c403 --- /dev/null +++ b/feeCalculator/src/main/java/org/example/FeeCalculationStrategy.java @@ -0,0 +1,5 @@ +package org.example; + +public interface FeeCalculationStrategy { + public double calculateFee(TicketType ticketType); +} diff --git a/feeCalculator/src/main/java/org/example/FeeCalculationStrategyForAdults.java b/feeCalculator/src/main/java/org/example/FeeCalculationStrategyForAdults.java new file mode 100644 index 0000000..02ae9b5 --- /dev/null +++ b/feeCalculator/src/main/java/org/example/FeeCalculationStrategyForAdults.java @@ -0,0 +1,13 @@ +package org.example; + +public class FeeCalculationStrategyForAdults implements FeeCalculationStrategy { + private static final double ADULT_PRICE_BASE = 100; + public double calculateFee(TicketType ticketType) { + if (TicketType.HALF_DAY == ticketType) { + return ADULT_PRICE_BASE * 0.6; + } else if (TicketType.FULL_DAY == ticketType) { + return ADULT_PRICE_BASE * 1.2; + } + return 0; + } +} diff --git a/feeCalculator/src/main/java/org/example/FeeCalculator.java b/feeCalculator/src/main/java/org/example/FeeCalculator.java index 61b9e23..f7ecd2c 100644 --- a/feeCalculator/src/main/java/org/example/FeeCalculator.java +++ b/feeCalculator/src/main/java/org/example/FeeCalculator.java @@ -1,25 +1,9 @@ package org.example; public class FeeCalculator { - private static final double CHILD_PRICE_BASE = 100; - private static final double ADULT_PRICE_BASE = 100; public static double calculateFee(Visitor visitor, TicketType ticketType) { - double fee = 0; - - // calculate price for adults - if ((visitor.age > 14) && (TicketType.HALF_DAY == ticketType)) { - fee = ADULT_PRICE_BASE * 0.6; - } else if ((visitor.age > 14) && (TicketType.FULL_DAY == ticketType)) { - fee = ADULT_PRICE_BASE * 1.2; - } - - // calculate price for children - if ((visitor.age <= 14) && (TicketType.HALF_DAY == ticketType)) { - fee = CHILD_PRICE_BASE * 0.2; - } else if ((visitor.age <= 14) && (TicketType.FULL_DAY == ticketType)) { - fee = CHILD_PRICE_BASE * 0.5; - } + double fee = visitor.calculateFee(ticketType); return fee; } } diff --git a/feeCalculator/src/main/java/org/example/Visitor.java b/feeCalculator/src/main/java/org/example/Visitor.java index 7193d18..d4a9588 100644 --- a/feeCalculator/src/main/java/org/example/Visitor.java +++ b/feeCalculator/src/main/java/org/example/Visitor.java @@ -2,8 +2,18 @@ package org.example; public class Visitor { public int age; // years counter + private FeeCalculationStrategy feeCalculationStrategy; public Visitor (int age) { + if (age < 14) { + feeCalculationStrategy = new FeeCalculationStartegyForChildren(); + } else { + feeCalculationStrategy = new FeeCalculationStrategyForAdults(); + } this.age = age; } + + public double calculateFee(TicketType ticketType) { + return feeCalculationStrategy.calculateFee(ticketType); + } }