diff --git a/src/main/java/hs/fulda/de/ci/exam/project/BankTransaction.java b/src/main/java/hs/fulda/de/ci/exam/project/BankTransaction.java new file mode 100644 index 0000000..26be339 --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/BankTransaction.java @@ -0,0 +1,36 @@ +package hs.fulda.de.ci.exam.project; + +public class BankTransaction extends Payment { + String bankName; + String iban; + + public BankTransaction(int id, String amount, PaymentStatus status, String bankName, String iban) { + super(id, amount, status); + this.bankName = bankName; + this.iban = iban; + } + + public String getBankName() { + return bankName; + } + + public void setBankName(String bankName) { + this.bankName = bankName; + } + + public String getIban() { + return iban; + } + + public void setIban(String iban) { + this.iban = iban; + } + + @Override + public boolean isValid() { + if (this.iban.length() != 22) { + return false; + } + return true; + } +} diff --git a/src/main/java/hs/fulda/de/ci/exam/project/CashTransaction.java b/src/main/java/hs/fulda/de/ci/exam/project/CashTransaction.java new file mode 100644 index 0000000..7c17bde --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/CashTransaction.java @@ -0,0 +1,12 @@ +package hs.fulda.de.ci.exam.project; + +public class CashTransaction extends Payment { + public CashTransaction(int id, String amount, PaymentStatus status) { + super(id, amount, status); + } + + @Override + public boolean isValid() { + return true; + } +} diff --git a/src/main/java/hs/fulda/de/ci/exam/project/CreditCardTransaction.java b/src/main/java/hs/fulda/de/ci/exam/project/CreditCardTransaction.java index c2ac2da..742bda6 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/CreditCardTransaction.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/CreditCardTransaction.java @@ -1,10 +1,24 @@ package hs.fulda.de.ci.exam.project; public class CreditCardTransaction extends Payment { - String nameOnCard; + String cardNumber; - public CreditCardTransaction(int id, String amount, PaymentStatus status, String nameOnCard) { + public CreditCardTransaction(int id, String amount, PaymentStatus status, String cardNumber) { super(id, amount, status); - this.nameOnCard = nameOnCard; + this.cardNumber = cardNumber; + } + + public String getCardNumber() { + return cardNumber; + } + + public void setCardNumber(String cardNumber) { + this.cardNumber = cardNumber; + } + + @Override + public boolean isValid() { + if (this.cardNumber.length() != 20) return false; + return true; } } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java b/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java index 55c0bb8..2ccfa34 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java @@ -3,6 +3,8 @@ package hs.fulda.de.ci.exam.project; import java.util.HashMap; import static hs.fulda.de.ci.exam.project.PaymentStatus.Completed; +import static hs.fulda.de.ci.exam.project.PaymentStatus.Failed; +import static hs.fulda.de.ci.exam.project.ReservationStatus.Cancelled; import static hs.fulda.de.ci.exam.project.ReservationStatus.Confirmed; public class FlightReservation { @@ -55,10 +57,17 @@ public class FlightReservation { this.status = status; } - public void makePayment(Payment credit_card_payment) { + public void makePayment(Payment payment) { System.out.println("processing payment"); - credit_card_payment.status = Completed; - status = Confirmed; + if (payment.isValid()) { + payment.status = Completed; + setStatus(Confirmed); + } else { + payment.status = Failed; + setStatus(Cancelled); + } + + } public boolean notifyUser(String type, String to, String content) { diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Payment.java b/src/main/java/hs/fulda/de/ci/exam/project/Payment.java index f5bc310..21ece7c 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Payment.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Payment.java @@ -1,6 +1,6 @@ package hs.fulda.de.ci.exam.project; -public class Payment { +public abstract class Payment { int id; String amount; PaymentStatus status; @@ -34,4 +34,6 @@ public class Payment { public void setStatus(PaymentStatus status) { this.status = status; } + + public abstract boolean isValid(); } diff --git a/src/test/java/hs/fulda/de/ci/exam/project/PaymentTest.java b/src/test/java/hs/fulda/de/ci/exam/project/PaymentTest.java index d969b5e..9e1a2b4 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/PaymentTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/PaymentTest.java @@ -9,10 +9,49 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class PaymentTest { @Test public void should_be_possible_to_pay_with_credit_card(){ - Payment credit_card_payment = new CreditCardTransaction(11, "139€", Unpaid, "Imron Gamidli"); + Payment credit_card_payment = new CreditCardTransaction(11, "139€", Unpaid, "01234567890123456789"); FlightReservation reservation = new FlightReservation(); reservation.makePayment(credit_card_payment); assertEquals(Confirmed, reservation.getStatus()); assertEquals(Completed, credit_card_payment.status); } + + @Test + public void should_be_possible_to_pay_with_bank_account(){ + FlightReservation reservation = new FlightReservation(); + BankTransaction payment = new BankTransaction(11, "139€", Unpaid, "Sparkasse Fulda", "DE01234567890123456789"); + reservation.makePayment(payment); + assertEquals(Confirmed, reservation.getStatus()); + assertEquals(Completed, payment.status); + } + + @Test + public void paymentShould_not_be_possible_if_credit_card_deatils_is_wrong(){ + CreditCardTransaction credit_card_payment = new CreditCardTransaction(11, "139€", Unpaid, "123"); + FlightReservation reservation = new FlightReservation(); + reservation.makePayment(credit_card_payment); + assertEquals(ReservationStatus.Cancelled, reservation.getStatus()); + assertEquals(Failed, credit_card_payment.status); + } + + @Test + public void paymentShould_not_be_possible_if_iban_is_wrong(){ + BankTransaction bank_payment = new BankTransaction(11, "139€", Unpaid, "Sparkasse Fulda", "DE012"); + FlightReservation reservation = new FlightReservation(); + reservation.makePayment(bank_payment); + assertEquals(ReservationStatus.Cancelled, reservation.getStatus()); + assertEquals(Failed, bank_payment.status); + } + + @Test + public void cashPaymentShouldBePossible(){ + CashTransaction cash_payment = new CashTransaction(11, "139€", Unpaid); + FlightReservation reservation = new FlightReservation(); + reservation.makePayment(cash_payment); + assertEquals(Confirmed, reservation.getStatus()); + assertEquals(Completed, cash_payment.status); + } + + + }