Browse Source

credit card validation

feature-pr-feature-paymentOptions
Imron 3 years ago
parent
commit
368a7679bd
  1. 8
      src/main/java/hs/fulda/de/ci/exam/project/BankTransaction.java
  2. 20
      src/main/java/hs/fulda/de/ci/exam/project/CreditCardTransaction.java
  3. 15
      src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java
  4. 4
      src/main/java/hs/fulda/de/ci/exam/project/Payment.java
  5. 11
      src/test/java/hs/fulda/de/ci/exam/project/PaymentTest.java

8
src/main/java/hs/fulda/de/ci/exam/project/BankTransaction.java

@ -25,4 +25,12 @@ public class BankTransaction extends Payment {
public void setIban(String iban) { public void setIban(String iban) {
this.iban = iban; this.iban = iban;
} }
@Override
public boolean isValid() {
if (this.iban.length() != 22) {
return false;
}
return true;
}
} }

20
src/main/java/hs/fulda/de/ci/exam/project/CreditCardTransaction.java

@ -1,10 +1,24 @@
package hs.fulda.de.ci.exam.project; package hs.fulda.de.ci.exam.project;
public class CreditCardTransaction extends Payment { 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); 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;
} }
} }

15
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 java.util.HashMap;
import static hs.fulda.de.ci.exam.project.PaymentStatus.Completed; 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; import static hs.fulda.de.ci.exam.project.ReservationStatus.Confirmed;
public class FlightReservation { public class FlightReservation {
@ -55,10 +57,17 @@ public class FlightReservation {
this.status = status; this.status = status;
} }
public void makePayment(Payment credit_card_payment) {
public void makePayment(Payment payment) {
System.out.println("processing 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) { public boolean notifyUser(String type, String to, String content) {

4
src/main/java/hs/fulda/de/ci/exam/project/Payment.java

@ -1,6 +1,6 @@
package hs.fulda.de.ci.exam.project; package hs.fulda.de.ci.exam.project;
public class Payment {
public abstract class Payment {
int id; int id;
String amount; String amount;
PaymentStatus status; PaymentStatus status;
@ -34,4 +34,6 @@ public class Payment {
public void setStatus(PaymentStatus status) { public void setStatus(PaymentStatus status) {
this.status = status; this.status = status;
} }
public abstract boolean isValid();
} }

11
src/test/java/hs/fulda/de/ci/exam/project/PaymentTest.java

@ -9,7 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class PaymentTest { public class PaymentTest {
@Test @Test
public void should_be_possible_to_pay_with_credit_card(){ 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(); FlightReservation reservation = new FlightReservation();
reservation.makePayment(credit_card_payment); reservation.makePayment(credit_card_payment);
assertEquals(Confirmed, reservation.getStatus()); assertEquals(Confirmed, reservation.getStatus());
@ -25,5 +25,14 @@ public class PaymentTest {
assertEquals(Completed, payment.status); 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);
}
} }
Loading…
Cancel
Save