In-Class Assignment: Payment Method

Goal: Create a payment system, with an abstract class called Payment and subclasses CreditCard and PayPal.

Payment

Payment class has:

  • A String field called accountHolder
  • A constructor that takes accountHolder as a parameter
  • A public abstract method processPayment(double amount) that returns a boolean
  • A protected method printReceipt(double amount) that prints: "<accountHolder> paid $<amount> using <payment type>"

CreditCard

  • Additional fields: String cardNumber and double creditLimit
  • Constructor that takes both accountHolder, cardNumber and creditLimit
  • Implement processPayment(double amount) that returns true if amount ≤ creditLimit and prints receipt, returns false and prints "Payment failed, over credit limit!" otherwise
  • Override printReceipt() to show last 4 digits of card: "***1234"

PayPal

  • Additional field: String email and boolean validAccount (set by default to true)
  • Constructor that takes accountHolder and email
  • Implement processPayment(double amount) that returns true and prints receipt if account is valid, returns false and prints "Invalid account!" otherwise
  • Implement invalidateAccount() that returns nothing but sets validAccount to false
  • Override printReceipt() to show email address

Test

In your main method:

  • Create a CreditCard for "John Doe" with card number "1234567890123456" and limit of 5000
  • Create a PayPal for "Jane Smith" with email "jane@email.com"
  • Try processing a $300 payment with both methods
  • Invalidate Jane’s PayPall account, try processing a payment

Expected output:

Payment successful!
John Doe paid $300.0 using CreditCard ***3456
Payment successful!
Jane Smith paid $300.0 using PayPal (jane@email.com)
Invalid account!

Submission

Submit your three classes to gradescope:

  • Payment.java
  • CreditCard.java
  • PayPal.java