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
Stringfield calledaccountHolder - A constructor that takes
accountHolderas a parameter - A public abstract method
processPayment(double amount)that returns aboolean - A protected method
printReceipt(double amount)that prints:"<accountHolder> paid $<amount> using <payment type>"
CreditCard
- Additional fields:
String cardNumberanddouble creditLimit - Constructor that takes both
accountHolder,cardNumberandcreditLimit - Implement
processPayment(double amount)that returnstrueif amount ≤creditLimitand prints receipt, returnsfalseand prints"Payment failed, over credit limit!"otherwise - Override
printReceipt()to show last 4 digits of card:"***1234"
PayPal
- Additional field:
String emailandboolean validAccount(set by default totrue) - Constructor that takes
accountHolderandemail - Implement
processPayment(double amount)that returnstrueand prints receipt if account is valid, returnsfalseand prints"Invalid account!"otherwise - Implement
invalidateAccount()that returns nothing but setsvalidAccounttofalse - Override
printReceipt()to show email address
Test
In your main method:
- Create a
CreditCardfor"John Doe"with card number"1234567890123456"and limit of5000 - Create a
PayPalfor"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.javaCreditCard.javaPayPal.java