Skip to content

Commit e69f85c

Browse files
committed
第一次作业linxin目录下文件修改误提交,现在重提交
1 parent a98dea4 commit e69f85c

21 files changed

+748
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.settings\
2+
target\
3+
.classpath
4+
.project
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
25+
<dependency>
26+
<groupId>org.apache.logging.log4j</groupId>
27+
<artifactId>log4j-core</artifactId>
28+
<version>2.8.2</version>
29+
</dependency>
30+
31+
</dependencies>
32+
<!-- <repositories>
33+
<repository>
34+
<id>aliyunmaven</id>
35+
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
36+
</repository>
37+
</repositories> -->
38+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.coderising.ood.answer;
2+
3+
import java.util.List;
4+
5+
import org.apache.logging.log4j.LogManager;
6+
import org.apache.logging.log4j.Logger;
7+
8+
import com.coderising.ood.answer.entity.Product;
9+
import com.coderising.ood.answer.service.MailService;
10+
import com.coderising.ood.answer.utils.FileUtils;
11+
import com.coderising.ood.answer.utils.ProductUtils;
12+
13+
public class Test {
14+
private static final Logger log = LogManager.getLogger(Test.class);
15+
public static void main(String[] args) {
16+
MailService service = new MailService();
17+
List<Product> list = ProductUtils.getList(FileUtils.readFile());
18+
service.sendMail(list);
19+
20+
}
21+
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
smtp.server=smtp.163.com
2+
alt.smtp.server=smtp1.163.com
3+
email.admin=admin@company.com
4+
5+
product.txt=com/coderising/ood/answer/config/product_promotion.txt
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
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.coderising.ood.answer.entity;
2+
3+
import java.io.Serializable;
4+
5+
import com.coderising.ood.answer.utils.ConfigUtils;
6+
7+
/**
8+
* 邮件类
9+
* @author readke
10+
*
11+
*/
12+
public class MailMessage implements Serializable{
13+
14+
private static final long serialVersionUID = -3221160075625357827L;
15+
16+
private String toAddr;
17+
private String fromAddr;
18+
private String subject;
19+
private String content;
20+
21+
public static MailMessage getMessage(String from,String subject,String content,Product p,User u){
22+
MailMessage m = new MailMessage();
23+
if(content != null && !content.isEmpty())
24+
content = "尊敬的 "+u.getName()+", 您关注的产品 " + p.getpDec() + " 降价了,欢迎购买!" ;
25+
if(subject != null && !subject.isEmpty()){
26+
subject = "您关注的产品降价了";
27+
}
28+
if(from != null && !from.isEmpty()){
29+
from = ConfigUtils.getProperty("smtp.server");
30+
}
31+
m.setFromAddr(from);
32+
m.setToAddr(u.getEmail());
33+
m.setSubject(subject);
34+
m.setContent(content);
35+
return m;
36+
}
37+
38+
public String getToAddr() {
39+
return toAddr;
40+
}
41+
public void setToAddr(String toAddr) {
42+
this.toAddr = toAddr;
43+
}
44+
public String getFromAddr() {
45+
return fromAddr;
46+
}
47+
public void setFromAddr(String fromAddr) {
48+
this.fromAddr = fromAddr;
49+
}
50+
public String getSubject() {
51+
return subject;
52+
}
53+
public void setSubject(String subject) {
54+
this.subject = subject;
55+
}
56+
public String getContent() {
57+
return content;
58+
}
59+
public void setContent(String content) {
60+
this.content = content;
61+
}
62+
63+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.coderising.ood.answer.entity;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* 产品类
7+
* @author readke
8+
*
9+
*/
10+
public class Product implements Serializable{
11+
private static final long serialVersionUID = 409352331475497580L;
12+
13+
private String pId;
14+
private String pDec;
15+
16+
public Product() {
17+
18+
}
19+
public Product(String pId, String pDec) {
20+
super();
21+
this.pId = pId;
22+
this.pDec = pDec;
23+
}
24+
public String getpId() {
25+
return pId;
26+
}
27+
public void setpId(String pId) {
28+
this.pId = pId;
29+
}
30+
public String getpDec() {
31+
return pDec;
32+
}
33+
public void setpDec(String pDec) {
34+
this.pDec = pDec;
35+
}
36+
37+
38+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.coderising.ood.answer.entity;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* 用户类
7+
* @author readke
8+
*
9+
*/
10+
public class User implements Serializable{
11+
private static final long serialVersionUID = -7916484660512326120L;
12+
13+
private String id;
14+
private String name;
15+
private String email;
16+
public String getId() {
17+
return id;
18+
}
19+
public void setId(String id) {
20+
this.id = id;
21+
}
22+
public String getName() {
23+
return name;
24+
}
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
public String getEmail() {
29+
return email;
30+
}
31+
public void setEmail(String email) {
32+
this.email = email;
33+
}
34+
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.coderising.ood.answer.service;
2+
3+
import java.util.List;
4+
5+
import com.coderising.ood.answer.entity.MailMessage;
6+
import com.coderising.ood.answer.entity.Product;
7+
import com.coderising.ood.answer.entity.User;
8+
import com.coderising.ood.answer.utils.DBUtils;
9+
import com.coderising.ood.answer.utils.MailUtils;
10+
11+
/**
12+
* 邮件发送服务
13+
* @author readke
14+
*
15+
*/
16+
public class MailService {
17+
public void sendMail(List<Product> list){
18+
19+
for(Product p: list){
20+
List<User> uList = DBUtils.queryByProductID(p.getpId());
21+
for(User u : uList){
22+
MailMessage m = MailMessage.getMessage("","", "", p, u);
23+
MailUtils.sendMail(m);
24+
}
25+
}
26+
}
27+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.coderising.ood.answer.utils;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.Properties;
6+
7+
import org.apache.logging.log4j.LogManager;
8+
import org.apache.logging.log4j.Logger;
9+
10+
/**
11+
* 配置文件读取工具
12+
* @author readke
13+
*
14+
*/
15+
public class ConfigUtils {
16+
17+
private static final Logger log = LogManager.getLogger(ConfigUtils.class);
18+
private static Properties prop = null;
19+
20+
static {
21+
InputStream in = ConfigUtils.class.getResourceAsStream("../config/config.properties");
22+
prop = new Properties();
23+
//System.out.println(in);
24+
try {
25+
prop.load(in);
26+
} catch (IOException e) {
27+
// TODO Auto-generated catch block
28+
e.printStackTrace();
29+
}finally {
30+
if(in != null){
31+
try {
32+
in.close();
33+
} catch (IOException e) {
34+
// TODO Auto-generated catch block
35+
e.printStackTrace();
36+
}
37+
}
38+
}
39+
}
40+
41+
public static String getProperty(String key){
42+
return prop.getProperty(key);
43+
}
44+
45+
public static void main(String[] args) {
46+
log.info(ConfigUtils.getProperty("smtp.server"));
47+
}
48+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.coderising.ood.answer.utils;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.coderising.ood.answer.entity.User;
7+
8+
/**
9+
* db工具
10+
* @author readke
11+
*
12+
*/
13+
public class DBUtils {
14+
15+
public static List<User> queryByProductID(String productID){
16+
/**
17+
* sql = "Select name from subscriptions "
18+
+ "where product_id= '" + productID +"' "
19+
+ "and send_mail=1 ";
20+
*/
21+
List<User> userList = new ArrayList<User>();
22+
23+
for (int i = 1; i <= 3; i++) {
24+
User userInfo = new User();
25+
userInfo.setName("User" + i);
26+
userInfo.setEmail("aa@bb.com");
27+
userList.add(userInfo);
28+
}
29+
30+
return userList;
31+
}
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.coderising.ood.answer.utils;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.net.URI;
6+
import java.net.URL;
7+
8+
import org.apache.logging.log4j.LogManager;
9+
import org.apache.logging.log4j.Logger;
10+
11+
/**
12+
* 文件读取工具
13+
* @author readke
14+
*
15+
*/
16+
public class FileUtils {
17+
private static final Logger log = LogManager.getLogger(FileUtils.class);
18+
public static File readFile(){
19+
20+
File file = null;
21+
BufferedReader br = null;
22+
23+
try {
24+
URL url = FileUtils.class.getClassLoader().getResource(ConfigUtils.getProperty("product.txt"));
25+
log.info(url.getPath());
26+
URI uri = url.toURI();
27+
file = new File(uri);
28+
29+
}catch (Exception e) {
30+
// TODO: handle exception
31+
e.printStackTrace();
32+
}
33+
return file;
34+
}
35+
36+
public static void main(String[] args) {
37+
readFile();
38+
}
39+
}

0 commit comments

Comments
 (0)