Skip to content

Commit 744a317

Browse files
committed
feat: implement PaymentMethod interface with CreditCard and PayPal classes, add processPayment function, and update README instructions
1 parent dc18574 commit 744a317

2 files changed

Lines changed: 36 additions & 22 deletions

File tree

  • exercises/02.interfaces-and-classes/02.problem.programming-to-abstractions

exercises/02.interfaces-and-classes/02.problem.programming-to-abstractions/README.mdx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ concrete classes.
66

77
🐨 Open <InlineFile file="index.ts" /> and:
88

9-
1. Use the `PaymentMethod` interface from the previous exercise
10-
2. Create a `PaymentProcessor` class with a method that accepts `PaymentMethod`
11-
3. Write a function that processes payments using the interface type
12-
4. Test with different payment method implementations
9+
1. Create a `processPayment` function that accepts a `PaymentMethod` parameter
10+
2. The function should call `method.pay(amount)` and return the result
11+
3. Test it with both `CreditCard` and `PayPal` instances
1312

14-
💰 Use the interface type for parameters so different implementations work.
13+
💰 Use the interface type (`PaymentMethod`) for the parameter instead of a
14+
concrete class type. This way, the function works with any class that
15+
implements the interface.
1516

1617
📜 [TypeScript Handbook - Using Interfaces](https://www.typescriptlang.org/docs/handbook/2/objects.html#using-interfaces)
Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
11
// Programming to Abstractions
22

3-
// 🐨 Define the PaymentMethod interface:
4-
// - Method: pay(amount: number) returns string
3+
interface PaymentMethod {
4+
pay(amount: number): string
5+
}
56

6-
// 🐨 Create a CreditCard class implementing PaymentMethod:
7-
// - Field: cardNumber (string)
8-
// - Constructor that takes cardNumber
9-
// - Implement pay(amount: number) returns "Paid $${amount} with credit card ${cardNumber}"
7+
class CreditCard implements PaymentMethod {
8+
cardNumber: string
109

11-
// Test CreditCard
12-
// const creditCard = new CreditCard('1234-5678-9012-3456')
13-
// console.log(creditCard)
10+
constructor(cardNumber: string) {
11+
this.cardNumber = cardNumber
12+
}
1413

15-
// 🐨 Create a PayPal class implementing PaymentMethod:
16-
// - Field: email (string)
17-
// - Constructor that takes email
18-
// - Implement pay(amount: number) returns "Paid $${amount} with PayPal ${email}"
14+
pay(amount: number): string {
15+
return `Paid $${amount} with credit card ${this.cardNumber}`
16+
}
17+
}
1918

20-
// Test PayPal
21-
// const paypal = new PayPal('user@example.com')
22-
// console.log(paypal)
19+
class PayPal implements PaymentMethod {
20+
email: string
21+
22+
constructor(email: string) {
23+
this.email = email
24+
}
25+
26+
pay(amount: number): string {
27+
return `Paid $${amount} with PayPal ${this.email}`
28+
}
29+
}
2330

2431
// 🐨 Create a processPayment function:
2532
// - Parameters: method (PaymentMethod), amount (number)
2633
// - Returns: string
2734
// - Calls method.pay(amount)
2835

2936
// Test processPayment
37+
// const creditCard = new CreditCard('1234-5678-9012-3456')
38+
// const paypal = new PayPal('user@example.com')
3039
// console.log(processPayment(creditCard, 100))
3140
// console.log(processPayment(paypal, 50))
3241

33-
// export { CreditCard, PayPal, processPayment }
42+
export {
43+
CreditCard,
44+
PayPal,
45+
// processPayment
46+
}

0 commit comments

Comments
 (0)