Skip to content

Commit 8544a28

Browse files
authored
383117348 第一次ood作业
383117348 第一次ood作业
2 parents 960560a + 2422056 commit 8544a28

File tree

11 files changed

+386
-0
lines changed

11 files changed

+386
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.coderising</groupId>
6+
<artifactId>ood-assignment</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>ood-assignment</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>4.12</version>
23+
</dependency>
24+
25+
</dependencies>
26+
<repositories>
27+
<repository>
28+
<id>aliyunmaven</id>
29+
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
30+
</repository>
31+
</repositories>
32+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.coderising.ood.srp;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
5+
public class Configuration {
6+
7+
static Map<String,Object> configurations = new HashMap<String, Object>();
8+
static{
9+
configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com");
10+
configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com");
11+
configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com");
12+
configurations.put(ConfigurationKeys.IS_EMAIL_DEBUG, false);
13+
}
14+
/**
15+
* 应该从配置文件读, 但是这里简化为直接从一个map 中去读
16+
* @param key
17+
* @return
18+
*/
19+
public Object getProperty(String key) {
20+
21+
return configurations.get(key);
22+
}
23+
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.coderising.ood.srp;
2+
3+
public class ConfigurationKeys {
4+
5+
public static final String SMTP_SERVER = "smtp.server";
6+
public static final String ALT_SMTP_SERVER = "alt.smtp.server";
7+
public static final String EMAIL_ADMIN = "email.admin";
8+
public static final String IS_EMAIL_DEBUG = "is_email_debug";
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.coderising.ood.srp;
2+
import java.util.ArrayList;
3+
import java.util.HashMap;
4+
import java.util.List;
5+
6+
public class DBUtil {
7+
8+
/**
9+
* 应该从数据库读, 但是简化为直接生成。
10+
* @param sql
11+
* @return
12+
*/
13+
public static List query(String sql){
14+
15+
List userList = new ArrayList();
16+
for (int i = 1; i <= 3; i++) {
17+
HashMap userInfo = new HashMap();
18+
userInfo.put("NAME", "User" + i);
19+
userInfo.put("EMAIL", "aa@bb.com");
20+
userList.add(userInfo);
21+
}
22+
23+
return userList;
24+
}
25+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.coderising.ood.srp;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileReader;
7+
import java.io.IOException;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class FileUtil {
12+
13+
/**
14+
* 根据文件路径获取文件,如果文件不存在,抛出异常
15+
* @param path
16+
* @return
17+
* @throws FileNotFoundException
18+
*/
19+
public static File readFile(String path) throws FileNotFoundException {
20+
File file = new File(path);
21+
if (!file.exists()) {
22+
throw new FileNotFoundException("文件不存在");
23+
}
24+
return file;
25+
}
26+
27+
/**
28+
* 根据正则,将文件中的数据解析成字符串数组形式返回
29+
*
30+
* @param file
31+
* @param regex
32+
* @return
33+
*/
34+
public static List<String[]> parseToString(File file, String regex) {
35+
List<String[]> list = new ArrayList<String[]>();
36+
BufferedReader br = null;
37+
try {
38+
if (file != null && file.exists()) {
39+
40+
br = new BufferedReader(new FileReader(file));
41+
String temp = null;
42+
while ((temp = br.readLine()) != null) {
43+
String[] strs = temp.split(regex);
44+
list.add(strs);
45+
}
46+
}
47+
} catch (IOException e) {
48+
e.printStackTrace();
49+
} finally {
50+
if (br != null) {
51+
try {
52+
br.close();
53+
} catch (IOException e) {
54+
e.printStackTrace();
55+
}
56+
}
57+
}
58+
59+
return list;
60+
}
61+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.coderising.ood.srp;
2+
3+
public class MailUtil {
4+
5+
private static String fromAddress = "";
6+
private static String smtpHost = "";
7+
private static String altSmtpHost = "";
8+
private static boolean debug = false;
9+
10+
private static Configuration config = new Configuration();
11+
12+
private static void ConfigureEmail() {
13+
14+
altSmtpHost = (String) config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER);
15+
fromAddress = (String) config.getProperty(ConfigurationKeys.EMAIL_ADMIN);
16+
smtpHost = (String) config.getProperty(ConfigurationKeys.SMTP_SERVER);
17+
debug = (Boolean) config.getProperty(ConfigurationKeys.IS_EMAIL_DEBUG);
18+
}
19+
/**
20+
* 发送单条邮件
21+
* @param toAddress
22+
* @param subject
23+
* @param message
24+
*/
25+
public static void sendEmail(String toAddress,String subject,String message) {
26+
ConfigureEmail();
27+
if(debug){
28+
System.out.println("测试环境");
29+
}else{
30+
System.out.println("正式环境");
31+
}
32+
try {
33+
//假装发了一封邮件
34+
StringBuilder buffer = new StringBuilder();
35+
buffer.append("From:").append(fromAddress).append("\n");
36+
buffer.append("To:").append(toAddress).append("\n");
37+
buffer.append("Subject:").append(subject).append("\n");
38+
buffer.append("Content:").append(message).append("\n");
39+
System.out.println(buffer.toString());
40+
} catch (Exception e) {
41+
try {
42+
System.out.println("备用SMTP服务器: ");
43+
MailUtil.sendEmail(toAddress, subject, message);
44+
45+
} catch (Exception e2) {
46+
System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage());
47+
}
48+
}
49+
50+
}
51+
52+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.coderising.ood.srp;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
public class PromotionMail {
11+
12+
protected String subject = null;
13+
protected String message = null;
14+
15+
private static List<String[]> products = new ArrayList<String[]>();
16+
17+
private static String filePath = "E:\\coding2017\\students\\383117348\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt";
18+
19+
private UserInfoService uis = new UserInfoServiceImpl();
20+
21+
public PromotionMail(File file) throws Exception {
22+
// 读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8
23+
readFile(file);
24+
}
25+
26+
public static void main(String[] args) throws Exception {
27+
File f = FileUtil.readFile(filePath);
28+
PromotionMail pe = new PromotionMail(f);
29+
// 遍历每条产品信息
30+
for (String[] data : products) {
31+
List<Map<String, String>> list = pe.loadUserInfoList(data[0]);
32+
if (list != null && list.size() > 0) {
33+
pe.sendEMails(list, data[1]);
34+
}
35+
}
36+
}
37+
38+
/**
39+
* 读取产品文件,获得所有的产品信息
40+
*
41+
* @param file
42+
*/
43+
protected void readFile(File file) {
44+
List<String[]> datas = FileUtil.parseToString(file, " ");
45+
if (datas != null && datas.size() > 0) {
46+
products = datas;
47+
for (String[] data : products) {
48+
System.out.print("产品ID = " + data[0] + "\n");
49+
System.out.println("产品描述 = " + data[1] + "\n");
50+
}
51+
}
52+
}
53+
54+
/**
55+
* 根据产品id获取需要发送的用户信息,暂未考虑sql注入的安全问题
56+
*
57+
* @return
58+
* @throws Exception
59+
*/
60+
protected List<Map<String, String>> loadUserInfoList(String productID) throws Exception {
61+
return uis.getList(productID);
62+
}
63+
64+
/**
65+
* 设置发送的消息体
66+
*
67+
* @param name
68+
* @throws IOException
69+
*/
70+
protected void setMessage(String name, String productDesc) throws IOException {
71+
this.subject = "您关注的产品降价了";
72+
this.message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!";
73+
}
74+
75+
/**
76+
* 发送邮件
77+
*
78+
* @param mailingList
79+
* @throws IOException
80+
*/
81+
protected void sendEMails(List<Map<String, String>> mailingList, String productDesc) throws IOException {
82+
83+
System.out.println("开始发送邮件");
84+
85+
if (mailingList != null) {
86+
Iterator<Map<String, String>> iter = mailingList.iterator();
87+
while (iter.hasNext()) {
88+
Map<String, String> map = iter.next();
89+
String toAddress = (String) map.get("EMAIL");
90+
String name = (String) map.get("NAME");
91+
setMessage(name, productDesc);
92+
if (toAddress != null && toAddress.length() > 0)
93+
MailUtil.sendEmail(toAddress, subject, message);
94+
}
95+
} else {
96+
System.out.println("没有邮件发送");
97+
}
98+
99+
}
100+
101+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.coderising.ood.srp;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public interface UserInfoService {
7+
/**
8+
* 根据产品id获取订阅信息用户
9+
* @param productID
10+
* @return
11+
*/
12+
public List<Map<String,String>> getList(String productID);
13+
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.coderising.ood.srp;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
public class UserInfoServiceImpl implements UserInfoService {
8+
9+
public List<Map<String, String>> getList(String productID) {
10+
// TODO Auto-generated method stub
11+
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
12+
String sql = "Select name from subscriptions " + "where product_id= '" + productID + "' "
13+
+ "and send_mail=1 ";
14+
15+
System.out.println("loadQuery set");
16+
try {
17+
list = DBUtil.query(sql);
18+
} catch (Exception e) {
19+
e.printStackTrace();
20+
}
21+
22+
return list;
23+
}
24+
25+
26+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
P8756 iPhone8
2+
P3946 XiaoMi10
3+
P8904 Oppo_R15
4+
P4955 Vivo_X20

0 commit comments

Comments
 (0)