Skip to content

Commit fda9200

Browse files
authored
Merge pull request onlyliuxin#1 from onlyliuxin/master
update
2 parents 9adb2b1 + dc3c9d3 commit fda9200

File tree

21 files changed

+612
-1
lines changed

21 files changed

+612
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.tm.test;
2+
3+
public class Test {
4+
5+
public static void main(String[] args) {
6+
System.out.println("Hello World!");
7+
}
8+
}

students/14703250/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
愿意自荐代码的,可以每个人一个目录 以自己的QQ号命名 ,把自荐的代码放到里边去
1+
愿意自荐代码的,可以每个人一个目录 以自己的QQ号命名 ,把自荐的代码放到里边去

students/2842295913/Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### 学着使用git
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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,String> configurations = new HashMap<>();
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+
}
13+
/**
14+
* 应该从配置文件读, 但是这里简化为直接从一个map 中去读
15+
* @param key
16+
* @return
17+
*/
18+
public String getProperty(String key) {
19+
20+
return configurations.get(key);
21+
}
22+
23+
}
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+
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+
/**
2+
* 版权 (c) 2017 palmshe.com
3+
* 保留所有权利。
4+
*/
5+
package com.coderising.ood.srp;
6+
7+
import java.io.BufferedReader;
8+
import java.io.File;
9+
import java.io.FileReader;
10+
import java.io.IOException;
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
/**
17+
* @Description: 加载所需要的数据,以便PromotionMail取用
18+
* @author palmshe
19+
* @date 2017年6月11日 下午10:24:46
20+
*/
21+
public class InitDataUtils {
22+
23+
protected static final String GOOD_NAME= "good_name";
24+
protected static final String GOOD_ID= "good_id";
25+
26+
/**
27+
* @Description:生产商品
28+
* @param file
29+
* @return
30+
* @throws IOException
31+
*/
32+
public static List generateGoods(File file) throws IOException{
33+
List goods= new ArrayList();
34+
BufferedReader br = null;
35+
try {
36+
br = new BufferedReader(new FileReader(file));
37+
String temp = br.readLine();
38+
String[] data = temp.split(" ");
39+
40+
for (int i = 0; i < 1; i++) {
41+
Map goodMaps= new HashMap();
42+
goodMaps.put(GOOD_ID, data[0]);
43+
goodMaps.put(GOOD_NAME, data[1]);
44+
goods.add(goodMaps);
45+
}
46+
47+
// setProductID(data[0]);
48+
// setProductDesc(data[1]);
49+
50+
// System.out.println("产品ID = " + productID + "\n");
51+
// System.out.println("产品描述 = " + productDesc + "\n");
52+
53+
} catch (IOException e) {
54+
throw new IOException(e.getMessage());
55+
} finally {
56+
br.close();
57+
}
58+
59+
return goods;
60+
}
61+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.coderising.ood.srp;
2+
3+
public class MailUtil {
4+
5+
public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost,
6+
boolean debug) {
7+
//假装发了一封邮件
8+
StringBuilder buffer = new StringBuilder();
9+
buffer.append("From:").append(fromAddress).append("\n");
10+
buffer.append("To:").append(toAddress).append("\n");
11+
buffer.append("Subject:").append(subject).append("\n");
12+
buffer.append("Content:").append(message).append("\n");
13+
System.out.println(buffer.toString());
14+
15+
}
16+
17+
18+
}
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
package com.coderising.ood.srp;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.util.HashMap;
8+
import java.util.Iterator;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class PromotionMail {
13+
14+
15+
protected String sendMailQuery = null;
16+
17+
18+
protected String smtpHost = null;
19+
protected String altSmtpHost = null;
20+
protected String fromAddress = null;
21+
protected String toAddress = null;
22+
protected String subject = null;
23+
protected String message = null;
24+
25+
protected String productID = null;
26+
protected String productDesc = null;
27+
28+
private static Configuration config;
29+
30+
31+
32+
private static final String NAME_KEY = "NAME";
33+
private static final String EMAIL_KEY = "EMAIL";
34+
35+
36+
public static void main(String[] args) throws Exception {
37+
38+
File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt");
39+
boolean emailDebug = false;
40+
41+
PromotionMail pe = new PromotionMail(f, emailDebug);
42+
43+
}
44+
45+
46+
public PromotionMail(File file, boolean mailDebug) throws Exception {
47+
48+
//读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8
49+
readFile(file);
50+
51+
52+
config = new Configuration();
53+
54+
setSMTPHost();
55+
setAltSMTPHost();
56+
57+
58+
setFromAddress();
59+
60+
61+
setLoadQuery();
62+
63+
sendEMails(mailDebug, loadMailingList());
64+
65+
66+
}
67+
68+
69+
70+
71+
private void setProductID(String productID)
72+
{
73+
this.productID = productID;
74+
75+
}
76+
77+
protected String getproductID()
78+
{
79+
return productID;
80+
}
81+
82+
protected void setLoadQuery() throws Exception {
83+
84+
sendMailQuery = "Select name from subscriptions "
85+
+ "where product_id= '" + productID +"' "
86+
+ "and send_mail=1 ";
87+
88+
89+
System.out.println("loadQuery set");
90+
}
91+
92+
93+
protected void setSMTPHost()
94+
{
95+
smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER);
96+
}
97+
98+
99+
protected void setAltSMTPHost()
100+
{
101+
altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER);
102+
103+
}
104+
105+
106+
protected void setFromAddress()
107+
{
108+
fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN);
109+
}
110+
111+
protected void setMessage(HashMap userInfo) throws IOException
112+
{
113+
114+
String name = (String) userInfo.get(NAME_KEY);
115+
116+
subject = "您关注的产品降价了";
117+
message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ;
118+
119+
120+
121+
}
122+
123+
124+
protected void readFile(File file) throws IOException // @02C
125+
{
126+
BufferedReader br = null;
127+
try {
128+
br = new BufferedReader(new FileReader(file));
129+
String temp = br.readLine();
130+
String[] data = temp.split(" ");
131+
132+
setProductID(data[0]);
133+
setProductDesc(data[1]);
134+
135+
System.out.println("产品ID = " + productID + "\n");
136+
System.out.println("产品描述 = " + productDesc + "\n");
137+
138+
} catch (IOException e) {
139+
throw new IOException(e.getMessage());
140+
} finally {
141+
br.close();
142+
}
143+
}
144+
145+
private void setProductDesc(String desc) {
146+
this.productDesc = desc;
147+
}
148+
149+
150+
protected void configureEMail(HashMap userInfo) throws IOException
151+
{
152+
toAddress = (String) userInfo.get(EMAIL_KEY);
153+
if (toAddress.length() > 0)
154+
setMessage(userInfo);
155+
}
156+
157+
protected List loadMailingList() throws Exception {
158+
return DBUtil.query(this.sendMailQuery);
159+
}
160+
161+
162+
protected void sendEMails(boolean debug, List mailingList) throws IOException
163+
{
164+
165+
System.out.println("开始发送邮件");
166+
167+
168+
if (mailingList != null) {
169+
Iterator iter = mailingList.iterator();
170+
while (iter.hasNext()) {
171+
configureEMail((HashMap) iter.next());
172+
try
173+
{
174+
if (toAddress.length() > 0)
175+
MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug);
176+
}
177+
catch (Exception e)
178+
{
179+
180+
try {
181+
MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug);
182+
183+
} catch (Exception e2)
184+
{
185+
System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage());
186+
}
187+
}
188+
}
189+
190+
191+
}
192+
193+
else {
194+
System.out.println("没有邮件发送");
195+
196+
}
197+
198+
}
199+
200+
/**
201+
* @Description:设置商品信息
202+
* @param goods
203+
*/
204+
public void setGoods(List goods){
205+
Map goodMap= (HashMap)goods.get(0);
206+
setProductDesc((String)goodMap.get(InitDataUtils.GOOD_NAME));
207+
setProductID((String)goodMap.get(InitDataUtils.GOOD_ID));
208+
}
209+
}

0 commit comments

Comments
 (0)