Skip to content

Commit 6ea33cd

Browse files
author
luoziyihao
committed
optimize promotioMail v1 todo 壮性
1 parent 4264457 commit 6ea33cd

File tree

7 files changed

+177
-20
lines changed

7 files changed

+177
-20
lines changed

students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@
1111
*/
1212
public class ProductParser {
1313

14-
private String productClassPath;
15-
16-
public void setProductClassPath(String productClassPath) {
17-
this.productClassPath = productClassPath;
18-
}
19-
20-
public List<Product> parse() {
14+
public List<Product> parse(String productClassPath) {
2115
List<String> stringList = readToStringList(openStream(productClassPath));
2216
return stringList.stream()
2317
.map(String::trim)

students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@
77
*/
88
public class PromotionMailApp {
99

10-
public static void main(String args[]){
11-
ProductParser productParser = new ProductParser();
12-
productParser.setProductClassPath("product_promotion.txt");
13-
List<Product> products = productParser.parse();
10+
public static void main(String args[]) {
11+
List<Product> products = new ProductParser().parse("product_promotion.txt");
12+
13+
List<User> users = new UserService().loadMailingList();
14+
15+
List<PromotionMailClaim> promotionMailClaims = new PromotionMailClaim()
16+
.load(products, users, new SmptPropeties(), true);
17+
18+
new PromotionMailableBehavior().send(promotionMailClaims);
19+
1420
}
21+
1522
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.coderising.ood.srp.optimize;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
/**
47
* Created by luoziyihao on 6/12/17.
58
*/
@@ -9,9 +12,87 @@ public class PromotionMailClaim {
912
private String subject;
1013
private String message;
1114
private String smtpHost;
12-
private String debug;
15+
private String altSmtpHost;
16+
private Boolean mailDebug;
17+
18+
private PromotionMailClaim init(Product product, User user, SmptPropeties smptPropeties, Boolean mailDebug) {
19+
this.toAddress = user.getEmail();
20+
this.fromAddress = smptPropeties.getFromAddress();
21+
this.subject = "您关注的产品降价了";
22+
this.message = "尊敬的 " + user.getName() + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!";
23+
this.smtpHost = smptPropeties.getSmtpHost();
24+
this.altSmtpHost = smptPropeties.getAltSmtpHost();
25+
this.mailDebug = mailDebug;
26+
return this;
27+
}
28+
29+
public List<PromotionMailClaim> load(List<Product> products, List<User> users, SmptPropeties smptPropeties
30+
, boolean mailDebug) {
31+
List<PromotionMailClaim> promotionMailClaims = new ArrayList<>();
32+
for (Product product : products) {
33+
for (User user : users) {
34+
PromotionMailClaim promotionMailClaim = new PromotionMailClaim()
35+
.init(product, user, smptPropeties, true);
36+
promotionMailClaims.add(promotionMailClaim);
37+
}
38+
}
39+
return promotionMailClaims;
40+
}
41+
42+
public String getAltSmtpHost() {
43+
return altSmtpHost;
44+
}
45+
46+
public void setAltSmtpHost(String altSmtpHost) {
47+
this.altSmtpHost = altSmtpHost;
48+
}
49+
50+
public Boolean getMailDebug() {
51+
return mailDebug;
52+
}
53+
54+
public void setMailDebug(Boolean mailDebug) {
55+
this.mailDebug = mailDebug;
56+
}
57+
58+
public String getToAddress() {
59+
return toAddress;
60+
}
61+
62+
public void setToAddress(String toAddress) {
63+
this.toAddress = toAddress;
64+
}
65+
66+
public String getFromAddress() {
67+
return fromAddress;
68+
}
69+
70+
public void setFromAddress(String fromAddress) {
71+
this.fromAddress = fromAddress;
72+
}
73+
74+
public String getSubject() {
75+
return subject;
76+
}
77+
78+
public void setSubject(String subject) {
79+
this.subject = subject;
80+
}
81+
82+
public String getMessage() {
83+
return message;
84+
}
85+
86+
public void setMessage(String message) {
87+
this.message = message;
88+
}
89+
90+
public String getSmtpHost() {
91+
return smtpHost;
92+
}
1393

14-
public void setMessage(Product product, User user) {
15-
this.message = null;
94+
public void setSmtpHost(String smtpHost) {
95+
this.smtpHost = smtpHost;
1696
}
97+
1798
}

students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,34 @@
77
*/
88
public class PromotionMailableBehavior {
99

10-
void send(List<PromotionMailClaim> emailSendClaimList) {
10+
public void send(List<PromotionMailClaim> emailSendClaimList) {
11+
for (PromotionMailClaim promotionMailClaim : emailSendClaimList) {
12+
sendEmailForOneClaim(promotionMailClaim);
13+
}
14+
}
15+
16+
private void sendEmailForOneClaim(PromotionMailClaim promotionMailClaim) {
17+
System.out.println("开始发送邮件");
18+
19+
try {
20+
doSendMail(promotionMailClaim);
21+
} catch (Exception e) {
22+
promotionMailClaim.setSmtpHost(promotionMailClaim.getAltSmtpHost());
23+
try {
24+
doSendMail(promotionMailClaim);
25+
} catch (Exception e1) {
26+
System.out.println("通过备用 SMTP服务器发送邮件失败: " + e.getMessage());
27+
}
28+
}
29+
}
1130

31+
private void doSendMail(PromotionMailClaim promotionMailClaim) {
32+
//假装发了一封邮件
33+
StringBuilder buffer = new StringBuilder();
34+
buffer.append("From:").append(promotionMailClaim.getFromAddress()).append("\n");
35+
buffer.append("To:").append(promotionMailClaim.getToAddress()).append("\n");
36+
buffer.append("Subject:").append(promotionMailClaim.getSubject()).append("\n");
37+
buffer.append("Content:").append(promotionMailClaim.getMessage()).append("\n");
38+
System.out.println(buffer.toString());
1239
}
1340
}

students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,31 @@
44
* Created by luoziyihao on 6/12/17.
55
*/
66
public class SmptPropeties {
7-
private String smtpHost;
8-
private String altSmtpHost;
9-
private String fromAddress;
7+
private String smtpHost = "smtp.server";;
8+
private String altSmtpHost = "smtp1.163.com";
9+
private String fromAddress = " admin@company.com";
10+
11+
public String getSmtpHost() {
12+
return smtpHost;
13+
}
14+
15+
public void setSmtpHost(String smtpHost) {
16+
this.smtpHost = smtpHost;
17+
}
18+
19+
public String getAltSmtpHost() {
20+
return altSmtpHost;
21+
}
22+
23+
public void setAltSmtpHost(String altSmtpHost) {
24+
this.altSmtpHost = altSmtpHost;
25+
}
26+
27+
public String getFromAddress() {
28+
return fromAddress;
29+
}
30+
31+
public void setFromAddress(String fromAddress) {
32+
this.fromAddress = fromAddress;
33+
}
1034
}

students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,20 @@
66
public class User {
77
private String name;
88
private String email;
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
18+
public String getEmail() {
19+
return email;
20+
}
21+
22+
public void setEmail(String email) {
23+
this.email = email;
24+
}
925
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package com.coderising.ood.srp.optimize;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
/**
67
* Created by luoziyihao on 6/12/17.
78
*/
89
public class UserService {
910

10-
List<User> loadMailingList(){
11-
return null;
11+
List<User> loadMailingList() {
12+
List<User> userList = new ArrayList<>();
13+
for (int i = 1; i <= 3; i++) {
14+
User user = new User();
15+
user.setName("User" + i);
16+
user.setEmail("aa@bb.com");
17+
userList.add(user);
18+
}
19+
return userList;
1220
}
1321
}

0 commit comments

Comments
 (0)