Skip to content

Commit 1537bae

Browse files
authored
nanusl 第一次作业完成 SRP 课后作业
OOD之SRP课后作业,希望老师能批改一下。
2 parents 62769e5 + fda9200 commit 1537bae

File tree

14 files changed

+621
-0
lines changed

14 files changed

+621
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package srp;
2+
3+
import srp.model.Product;
4+
import srp.model.User;
5+
import srp.service.ProductService;
6+
import srp.service.PromotionService;
7+
import srp.service.UserService;
8+
import srp.util.RandomUtils;
9+
10+
import java.util.List;
11+
12+
/**
13+
* @version V1.0
14+
* @Title: Main
15+
* @Package: srp
16+
* @Description: 主程序
17+
* @author: 南来
18+
* @date: 2017-06-12 9:22
19+
*/
20+
public class Main {
21+
22+
/**
23+
* //TODO 写的crud项目太多,作业越写越懵逼,最后我也不知道写成啥了,也不知道是否符合SRP原则。。。总之……欢迎老师和同学们批评指正!
24+
*/
25+
public static void main(String[] args) throws InterruptedException {
26+
//模拟业务场景
27+
for (; ; ) {
28+
start();
29+
Thread.sleep(Long.parseLong(RandomUtils.randomNumber(3)));
30+
}
31+
}
32+
33+
private static void start() {
34+
//region 模拟自动装配
35+
UserService userService = new UserService();
36+
ProductService productService = new ProductService();
37+
PromotionService promotionService = new PromotionService();
38+
//endregion
39+
40+
// 1 遍历商品 是否降价
41+
List<Product> products = productService.getProduct();
42+
if (null != products) {
43+
for (Product product : products) {
44+
// 2 商品降价
45+
if (product.getDown()) {
46+
// 3 获取所有关注这个产品的用户
47+
List<User> watchProductUsers = userService.getWatchProductUsers(product.getId());
48+
if (null != watchProductUsers && watchProductUsers.size() > 0)
49+
// 4 发送促销邮件
50+
for (User user : watchProductUsers)
51+
promotionService.promotionMail(user.getName(), user.getEmail(), product.getDesc());
52+
}
53+
}
54+
}
55+
56+
//TODO 以上代码有明显线程问题。。。大家无视就好。。。。。¯\_(ツ)_/¯
57+
}
58+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package srp.config;
2+
3+
/**
4+
* @version V1.0
5+
* @Title: Config
6+
* @Package: srp.config
7+
* @Description: 配置对象 模拟读取配置文件 //todo 或者应该封装成对象?
8+
* @author: 南来
9+
* @date: 2017-06-12 9:21
10+
*/
11+
public class Config {
12+
/**
13+
* 主邮件服务器
14+
*/
15+
public static final String smtpHost = "smtp.server";
16+
/**
17+
* 备用邮件服务器
18+
*/
19+
public static final String altSmtpHost = "alt.smtp.server";
20+
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package srp.config;
2+
3+
/**
4+
* @version V1.0
5+
* @Title: Constant
6+
* @Package: srp.config
7+
* @Description: 存放项目常量,避免写死代码,便于后期维护。
8+
* @author: 南来
9+
* @date: 2017-06-12 9:17
10+
*/
11+
public class Constant {
12+
/**
13+
* from address
14+
*/
15+
public static final String EMAIL_ADMIN = "email.admin";
16+
17+
/**
18+
* 促销邮件主题
19+
*/
20+
public static final String SUBJECT = "您关注的产品降价了";
21+
22+
/**
23+
* 促销邮件内容
24+
*/
25+
public static final String MESSAGE = "尊敬的 %s, 您关注的产品 %s 降价了,欢迎购买!";
26+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package srp.dao;
2+
3+
import srp.model.Product;
4+
import srp.model.User;
5+
import srp.util.RandomUtils;
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.List;
13+
14+
/**
15+
* @version V1.0
16+
* @Title: DB
17+
* @Package: srp.dao
18+
* @Description: 伪dao层 持续输出数据 没有细分每个对象单独dao层的部分
19+
* @author: 南来
20+
* @date: 2017-06-12 9:20
21+
*/
22+
public class DB {
23+
24+
/**
25+
* 模拟在数据库中查询用户
26+
*
27+
* @return 所有用户
28+
*/
29+
public List<User> getUsers() {
30+
List<User> users = new ArrayList<>();
31+
for (int i = 0; i < 10; i++) {
32+
User user = new User();
33+
user.setName(RandomUtils.randomName());
34+
user.setEmail(RandomUtils.randomMail());
35+
//region 模拟关注商品
36+
try {
37+
List<Product> products = getProducts();
38+
if (null != products && products.size() > 0)
39+
user.setWatchProductId(RandomUtils.randomOne(products).getId());
40+
} catch (IOException e) {
41+
e.printStackTrace();
42+
}
43+
//endregion
44+
users.add(user);
45+
}
46+
return users;
47+
}
48+
49+
/**
50+
* @param productId 商品id
51+
* @return 所有关注该商品的用户集
52+
*/
53+
public List<User> getWatchProductUsers(String productId) {
54+
if (0 == productId.length()) return null;
55+
List<User> users = getUsers();
56+
List<User> temp = new ArrayList<>();
57+
for (User user : users) {
58+
if (null != user.getWatchProductId() && productId.equals(user.getWatchProductId()))
59+
temp.add(user);
60+
}
61+
return temp;
62+
}
63+
64+
/**
65+
* 模拟在数据库中查询商品
66+
*
67+
* @return 所有商品
68+
*/
69+
public List<Product> getProducts() throws IOException {
70+
List<Product> products = new ArrayList<>();
71+
try (BufferedReader br = new BufferedReader(new FileReader(new File("D:\\product_promotion.txt")))) {
72+
String temp;
73+
while (null != (temp = br.readLine())) {
74+
String[] data = temp.split(" ");
75+
Product product = new Product();
76+
product.setId(data[0]);
77+
product.setDesc(data[1]);
78+
//region 模拟降价
79+
product.setDown(RandomUtils.randomBoolean());
80+
//endregion
81+
products.add(product);
82+
}
83+
}
84+
return products;
85+
}
86+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package srp.model;
2+
3+
/**
4+
* @version V1.0
5+
* @Title: Email
6+
* @Package: srp.model
7+
* @Description: Email 对象
8+
* @author: 南来
9+
* @date: 2017-06-12 10:32
10+
*/
11+
public class Email {
12+
13+
/**
14+
* from address
15+
*/
16+
private String from;
17+
/**
18+
* to address
19+
*/
20+
private String to;
21+
/**
22+
* 主题
23+
*/
24+
private String subject;
25+
/**
26+
* 内容
27+
*/
28+
private String content;
29+
30+
public String getFrom() {
31+
return from;
32+
}
33+
34+
public void setFrom(String from) {
35+
this.from = from;
36+
}
37+
38+
public String getTo() {
39+
return to;
40+
}
41+
42+
public void setTo(String to) {
43+
this.to = to;
44+
}
45+
46+
public String getSubject() {
47+
return subject;
48+
}
49+
50+
public void setSubject(String subject) {
51+
this.subject = subject;
52+
}
53+
54+
public String getContent() {
55+
return content;
56+
}
57+
58+
public void setContent(String content) {
59+
this.content = content;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return "Email{" +
65+
"from='" + from + '\'' +
66+
", to='" + to + '\'' +
67+
", subject='" + subject + '\'' +
68+
", content='" + content + '\'' +
69+
'}';
70+
}
71+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package srp.model;
2+
3+
/**
4+
* @version V1.0
5+
* @Title: Product
6+
* @Package: srp.model
7+
* @Description: 商品对象
8+
* @author: 南来
9+
* @date: 2017-06-12 9:46
10+
*/
11+
public class Product {
12+
13+
/**
14+
* 商品主键
15+
*/
16+
private String Id;
17+
/**
18+
* 商品描述
19+
*/
20+
private String Desc;
21+
/**
22+
* 是否降价
23+
*/
24+
private boolean down;
25+
26+
public String getId() {
27+
return Id;
28+
}
29+
30+
public void setId(String id) {
31+
Id = id;
32+
}
33+
34+
public String getDesc() {
35+
return Desc;
36+
}
37+
38+
public void setDesc(String desc) {
39+
Desc = desc;
40+
}
41+
42+
public boolean getDown() {
43+
return down;
44+
}
45+
46+
public void setDown(boolean down) {
47+
this.down = down;
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return "Product{" +
53+
"Id='" + Id + '\'' +
54+
", Desc='" + Desc + '\'' +
55+
", down='" + down + '\'' +
56+
'}';
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package srp.model;
2+
3+
/**
4+
* @version V1.0
5+
* @Title: User
6+
* @Package: srp.model
7+
* @Description: 用户对象
8+
* @author: 南来
9+
* @date: 2017-06-12 10:07
10+
*/
11+
public class User {
12+
13+
/**
14+
* 用户名
15+
*/
16+
private String name;
17+
/**
18+
* e-mail
19+
*/
20+
private String email;
21+
/**
22+
* 关注的商品Id
23+
*/
24+
private String watchProductId;
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public String getEmail() {
35+
return email;
36+
}
37+
38+
public void setEmail(String email) {
39+
this.email = email;
40+
}
41+
42+
public String getWatchProductId() {
43+
return watchProductId;
44+
}
45+
46+
public void setWatchProductId(String watchProductId) {
47+
this.watchProductId = watchProductId;
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return "User{" +
53+
"name='" + name + '\'' +
54+
", email='" + email + '\'' +
55+
", watchProductId='" + watchProductId + '\'' +
56+
'}';
57+
}
58+
}

0 commit comments

Comments
 (0)