-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy path13_homework_03_answer.cpp
144 lines (126 loc) · 3.03 KB
/
13_homework_03_answer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <bits/stdc++.h>
using namespace std;
// Below: What we did with payments satisfies Dependency inversion principle
// High-level modules (wesite class) should not depend on low-level modules (PaypalPayment APIs).
// Both should depend on abstractions (e.g. IPayment).
class PayPalCreditCard {
public:
string name;
string address;
string id;
string expire_date;
int ccv;
};
class PayPalOnlinePaymentAPI {
public:
void SetCardInfo(const PayPalCreditCard* const card) {
}
bool MakePayment(double money) {
return true;
}
};
class StripeUserInfo {
public:
string name;
string address;
};
class StripeCardInfo {
public:
string id;
string expire_date;
};
class StripePaymentAPI {
public:
bool static WithDrawMoney(StripeUserInfo user,
StripeCardInfo card,
double money) {
return true;
}
};
/////////////////////
class IPayment {
public:
virtual void SetUserInfo(string name, string address) = 0;
virtual void SetCardInfo(string id, string expire_date, int ccv) = 0;
virtual bool MakePayment(double money) = 0;
virtual ~IPayment() {
}
};
class PayPalPayment: public IPayment {
private:
PayPalOnlinePaymentAPI paypal;
PayPalCreditCard card;
public:
virtual void SetUserInfo(string name, string address) {
card.name = name;
card.address = address;
}
virtual void SetCardInfo(string id, string expire_date, int ccv) {
card.id = id;
card.expire_date = expire_date;
card.ccv = ccv;
}
virtual bool MakePayment(double money) {
paypal.SetCardInfo(&card);
return paypal.MakePayment(money);
}
};
class StripePayment: public IPayment {
private:
StripeCardInfo card;
StripeUserInfo user;
public:
virtual void SetUserInfo(string name, string address) {
user.name = name;
user.address = address;
}
virtual void SetCardInfo(string id, string expire_date, int ccv) {
card.id = id;
card.expire_date = expire_date;
}
virtual bool MakePayment(double money) {
return StripePaymentAPI::WithDrawMoney(user, card, money);
}
};
class Factory {
public:
// In single place, gather all payments
// In future a change happens here
// Called Factory method design pattern
static IPayment* GetPaymentHelper() {
if (true)
return new PayPalPayment();
else
return new StripePayment();
}
};
//////////////////////////////
class TransactionInfo {
public:
double required_money_amount;
string name;
string address;
string id;
string expire_date;
int ccv;
};
class Craigslist {
private:
IPayment* payment;
public:
Craigslist() {
// Craigslist knows nothing about PayPal
payment = Factory::GetPaymentHelper();
}
bool Pay(TransactionInfo info) {
payment->SetUserInfo(info.name, info.address);
payment->SetCardInfo(info.id, info.expire_date, info.ccv);
return payment->MakePayment(info.required_money_amount);
}
};
int main() {
TransactionInfo info = { 20.5, "mostafa", "canada", "11-22-33-44", "09-2021", 333 };
Craigslist site;
site.Pay(info);
return 0;
}